aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: c693804488ed3db2d656b9dfc77cbb38598ce7f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//use std::io::{self, Stdout};
use std::io::{self, Write};
use std::time::{Duration, Instant};
use rand::prelude::*;
//use rand::{seq::SliceRandom, thread_rng};
use rand::seq::SliceRandom;
use rand::thread_rng;
//use termion::event::Key;
//use termion::input::TermRead;
use crossterm::{
    execute, terminal::{self, ClearType, disable_raw_mode, enable_raw_mode}, event::{Event, KeyCode, KeyEvent}, terminal::size,
    cursor::Hide, cursor::Show, style::{Print, ResetColor, SetBackgroundColor, SetForegroundColor}, 
    terminal::{EnterAlternateScreen, LeaveAlternateScreen}
};
use tui::{
    backend::CrosstermBackend, layout::{Alignment, Constraint, Direction, Layout, Margin, Rect}, 
    style::{Modifier, Color, Style}, widgets::{Block, Borders, BorderType, ListState, Paragraph, Widget, Wrap}, Terminal
//    backend::TermionBackend,
//    symbols::Marker,
//    text::{Span, Spans},
//    terminal::{Frame, Terminal},
};

const PARAGRAPHS: [&str; 3] = [
    "The quick brown fox jumps over the lazy dog.",
    "In the beginning God created the heavens and the earth.",
    "To be, or not to be, that is the question:",
];

enum AppState {
    Playing(GameState),
    Stats(f64, u64),
    Quit,
}

struct GameState {
    paragraph: String,
    user_input: String,
    incorrect_chars: usize,
    difficulty: usize,
    start_time: Instant,
    deleted_chars: usize,
    end_time: Option<Instant>,
    current_index: usize,
}

impl GameState {
    fn new() -> GameState {
        let paragraph = PARAGRAPHS.choose(&mut thread_rng()).unwrap().to_string();
        let difficulty = paragraph.len() / 4;
        GameState {
            paragraph,
            user_input: String::new(),
            incorrect_chars: 0,
            difficulty,
            start_time: Instant::now(),
            deleted_chars: 0,
            end_time: None,
            current_index: 0,
        }
    }

    fn input(&mut self, c: char) {
        if self.current_index < self.paragraph.len() {
            self.user_input.push(c);
            self.current_index += 1;
        }
    }

    fn check_end_condition(&mut self) -> bool {
        if self.current_index == self.paragraph.len() {
            self.end_time = Some(Instant::now());
            return true;
        }
        false
    }

    fn reset(&mut self) {
        self.user_input.clear();
        self.incorrect_chars = 0;
        self.deleted_chars = 0;
        self.start_time = Instant::now();
        self.end_time = None;
        self.current_index = 0;
    }

    fn handle_input(&mut self, c: char) {
        if c == '\u{8}' {
            // Backspace
            if self.user_input.is_empty() {
                self.deleted_chars = 0;
            } else {
                self.user_input.pop();
                self.deleted_chars += 1;
            }
        } else if !c.is_control() {
            // Printable character
            self.user_input.push(c);
            if self.user_input.chars().count() > self.paragraph.chars().count() {
                self.incorrect_chars += 1;
            } else if let (Some(prev_char), Some(curr_char)) = (self.user_input.chars().nth(self.user_input.len() - 2), self.user_input.chars().last()) {
                if prev_char != self.paragraph.chars().nth(self.user_input.len() - 2).unwrap() || curr_char != self.paragraph.chars().nth(self.user_input.len() - 1).unwrap() {
                    self.incorrect_chars += 1;
                }
            }
        }
    }

    fn wpm(&self) -> f64 {
        let elapsed_time = self.elapsed_time().as_secs_f64() / 60.0;
        let cpm = (self.user_input.len() - self.deleted_chars) as f64 / elapsed_time;
        cpm / 5.0
    }

    fn accuracy(&self) -> f64 {
        let total_chars = self.user_input.len().max(self.paragraph.len());
        let correct_chars = self.user_input.chars().zip(self.paragraph.chars()).filter(|&(a, b)| a == b).count();
        (correct_chars as f64 / total_chars as f64) * 100.0
    }

    fn elapsed_time(&self) -> Duration {
        self.start_time.elapsed()
    }

    fn render_widgets<W>(&self, terminal: &mut Terminal<CrosstermBackend<W>>, stats: Option<Duration>) -> Result<(), io::Error> where W: Write {
    //fn render_widgets(&self, terminal: &mut Terminal<CrosstermBackend>, stats: Option<Duration>) -> Result<(), io::Error> {
        // Layout
        let size = terminal.size()?;
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .margin(5)
            .constraints(
                [
                    Constraint::Length(3), // Title
                    Constraint::Length(3), // Stats
                    Constraint::Min(0),    // Paragraph
                ]
                .as_ref(),
            )
            .split(size);

        // Title
        let title = "Typing Game";
        let title_widget = Paragraph::new(title)
            .style(Style::default().fg(Color::White))
            .block(Block::default().borders(Borders::ALL));

        // Stats
        let stats_widget = if let Some(duration) = stats {
            let wpm = self.wpm();
            let accuracy = self.accuracy();
            let stats_text = format!("WPM: {:.1} | Accuracy: {:.1}% | Time: {:.0}s", wpm, accuracy, duration.as_secs());
            Paragraph::new(stats_text)
                .style(Style::default().fg(Color::White))
                .block(Block::default().borders(Borders::ALL))
        } else {
            Paragraph::new("")
        };

        // Paragraph
        let ghost_text = self.paragraph.chars().map(|c| if c.is_whitespace() { ' ' } else { '_' }).collect::<String>();
        let user_input = self.user_input.clone();
        let paragraph_text = format!("{}\n{}", ghost_text, user_input);
        let paragraph_widget = Paragraph::new(paragraph_text)
            .style(Style::default().fg(Color::White))
            .block(Block::default().borders(Borders::ALL));

        // Render
        execute!(terminal.backend_mut(), terminal::Clear(ClearType::All))?;
        terminal.draw(|mut f| {
            f.render_widget(title_widget, chunks[0]);
            f.render_widget(stats_widget, chunks[1]);
            f.render_widget(paragraph_widget, chunks[2]);
        })?;
        Ok(())
    }

}

fn main() -> Result<(), io::Error> {
    let stdout = io::stdout();
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    execute!(terminal.backend_mut(), EnterAlternateScreen, Hide)?;

    let mut app_state = AppState::Playing(GameState::new());
    let mut rng = thread_rng();
    loop {
        match app_state {
            AppState::Playing(ref mut state) => {
                let stats = state.elapsed_time();
                state.render_widgets(&mut terminal, Some(stats))?;
                if let Ok(event) = crossterm::event::read() {
                    match event {
                        crossterm::event::Event::Key(KeyEvent { code: KeyCode::Char(c), .. }) => {
                            state.handle_input(c);
                            if state.check_end_condition() {
                                app_state = AppState::Stats(state.wpm(), state.accuracy() as u64);
                            }
                        },
                        crossterm::event::Event::Key(KeyEvent { code: KeyCode::Enter, .. }) => {
                            state.reset();
                        },
                        crossterm::event::Event::Key(KeyEvent { code: KeyCode::Esc, .. }) => {
                            app_state = AppState::Quit;
                        },
                        _ => {},
                    }
                }
            },
            AppState::Stats(wpm, accuracy) => {
                let stats_text = format!("Your WPM is {:.1} with {:.1}% accuracy!", wpm, accuracy);
                let stats_widget = Paragraph::new(stats_text)
                    .style(Style::default().fg(Color::White))
                    .block(Block::default().borders(Borders::ALL));
                terminal.draw(|f| {
                    let size = f.size();
                    f.render_widget(stats_widget, size);
                })?;
                std::thread::sleep(rng.gen_range(Duration::from_secs(2)..Duration::from_secs(4)));
                app_state = AppState::Playing(GameState::new());
            },
            AppState::Quit => {
                break;
            },
        }
    }

    execute!(terminal.backend_mut(), LeaveAlternateScreen, Show)?;

    Ok(())
}