From a726d9f5fb56e1fd7983c5ac806d408ad78daa86 Mon Sep 17 00:00:00 2001 From: srdusr <99972264+srdusr@users.noreply.github.com> Date: Thu, 11 Sep 2025 21:53:00 +0200 Subject: Add multiplayer bots, typing languages, and rework the UI layout Multiplayer - Quick match: POST /api/multiplayer/quickmatch returns whichever room is still filling, or opens one. Players never see a room code; joining by code stays for racing specific people. - Bots fill quick-match rooms after a short wait so a new game is never an empty lobby. They only ever join quick-match rooms, never a room opened by code. One or two per room, drawn from separate ~40 and ~80 WPM tiers so two bots are never near each other's pace, and they stall to correct mistakes rather than typing a clean straight line. - Live player count via GET /api/multiplayer/online, shown on the Multiplayer control and under the main menu's Multiplayer button. - Per-racer colours: you are the theme accent, opponents take distinct hues that stay the same from lobby to race. - The countdown no longer holds the room lock for its full three seconds, which is what reset clients mid-countdown. Typing languages - 16 languages for the generated-word modes, each with its own high-frequency vocabulary rather than a translation of the English list. - Picker in the top-right rail; non-English uses its own list at every difficulty tier instead of falling back to English words. Fix UTF-8 accuracy in the game core - update_game_state mixed byte and character counts: total_characters_typed accumulated byte-length deltas while total_correct_characters compared a char index against that byte count. Equal on ASCII, so it went unnoticed; a correctly typed Spanish passage scored 6%. The old byte slicing would also have panicked if an index landed inside a multi-byte character. Rewritten char-based, with regression tests. Programming mode - Replaced prose about programming with real code: 26 syntax-highlighted snippets across JavaScript, Python, Rust, C/Go/Java and shell. Single-line by necessity, since the typing input is a single-line field. Layout and readability - One icon rail arrangement on every screen: Settings/Store under the wordmark, Language/Theme/Friends/Account top-right, Stats/Leaderboard/ Multiplayer bottom-right. - Main menu: mode picker moved out of the Single Player button, which it was notching a divider through and pushing the label off-centre. - Escape returns to the menu, closing any open popover first, and confirms before abandoning a live race. - Split --text-color and --sub-color per theme; they shared one value that measured 3.65:1 against the background, below the 4.5:1 body-text floor. - Semantic colours used in exactly one place each: gold for a personal best, amber for the race countdown and the mobile-result badge. - Passage now sits in the same place on the typing and end screens, and its column is a whole number of characters wide so wrapping cannot leave a permanent gap on the right. - End screen: keystrokes and a correct/wrong/extra/missed split, attribution carried over from the typing screen, and a graph with a separate error axis, axis titles including seconds, and smoothed lines. --- src/test.rs | 164 ------------------------------------------------------------ 1 file changed, 164 deletions(-) delete mode 100644 src/test.rs (limited to 'src/test.rs') diff --git a/src/test.rs b/src/test.rs deleted file mode 100644 index 7f2728e..0000000 --- a/src/test.rs +++ /dev/null @@ -1,164 +0,0 @@ -// Import necessary items for testing -use crate::{draw_typing_game, input_handler, read_sentences, App, State}; -use crossterm::event::{KeyCode, KeyEvent}; -use std::{ - io, - sync::{Arc, Mutex}, -}; -use tui::layout::Rect; -use tui::{backend::CrosstermBackend, Terminal}; - -#[cfg(test)] -#[allow(unused_imports)] // Ignore unused import warnings within the test module -mod tests { - use super::*; - - #[test] - fn test_new_app() { - // Test that a new App initializes correctly - let app_result = App::new(); - assert!(app_result.is_ok()); - let app = app_result.unwrap(); - assert_eq!(app.input_string, ""); - assert_eq!(app.time_taken, 0); - assert_eq!(app.timer, None); - assert_eq!(app.state, State::MainMenu); - assert_eq!(app.should_exit, false); - assert!(!app.sentences.is_empty()); - assert!(app.current_sentence_index < app.sentences.len()); - } - - #[test] - fn test_reset_app() { - // Test the reset functionality of App - let mut app = App::new().unwrap(); - app.current_sentence_index = 0; // Set initial index to 0 - let old_index = app.current_sentence_index; - println!( - "Before reset: old_index = {}, current_sentence_index = {}", - old_index, app.current_sentence_index - ); - - app.time_taken = 100; - app.input_string = "Some input".to_string(); - app.reset(); - - println!( - "After reset: old_index = {}, current_sentence_index = {}", - old_index, app.current_sentence_index - ); - - // Check that current_sentence_index changes after reset - assert_ne!(app.current_sentence_index, old_index); - assert_eq!(app.time_taken, 0); - assert_eq!(app.input_string, ""); - assert_eq!(app.timer, None); - assert_eq!(app.state, State::TypingGame); - } - - #[test] - fn test_current_sentence() { - // Test the retrieval of the current sentence - let app = App::new().unwrap(); - let current_sentence = app.current_sentence(); - assert!(!current_sentence.is_empty()); - } - - #[test] - fn test_start_timer() { - // Test starting the timer - let mut app = App::new().unwrap(); - app.start_timer(); - assert!(app.timer.is_some()); - } - - #[test] - fn test_update_timer() { - // Test updating the timer - let mut app = App::new().unwrap(); - app.start_timer(); - let initial_time = app.time_taken; - std::thread::sleep(std::time::Duration::from_secs(1)); - app.update_timer(); - assert!(app.time_taken > initial_time); - } - - #[test] - fn test_update_wpm() { - // Test updating words per minute calculation - let mut app = App::new().unwrap(); - app.time_taken = 60; // 1 minute - app.input_string = "This is a test sentence".to_string(); - let wpm = app.update_wpm(); - assert_eq!(wpm, 5.0); // 5 words per minute for this sentence - } - - #[test] - fn test_read_sentences() { - // Test reading sentences from a file - let sentences_result = read_sentences("sentences.txt"); - assert!(sentences_result.is_ok()); - let sentences = sentences_result.unwrap(); - assert!(!sentences.is_empty()); - } - - #[test] - fn test_draw_typing_game() { - // Test drawing the typing game UI - let mut app = App::new().unwrap(); - let stdout = io::stdout(); - let backend = CrosstermBackend::new(stdout); - let mut terminal = Terminal::new(backend).unwrap(); - let size = Rect::default(); - draw_typing_game(&mut terminal.get_frame(), size, &mut app); - // No need to assert anything here since it's a rendering function - } - - #[tokio::test] - async fn test_input_handler() { - // Test the input handler function - let mut app = App::new().unwrap(); - - // Simulate typing 'a' - let event = KeyEvent::from(KeyCode::Char('a')); - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.input_string, "a"); - - // Simulate typing 'b' - let event = KeyEvent::from(KeyCode::Char('b')); - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.input_string, "ab"); - - // Simulate pressing Backspace - let event = KeyEvent::from(KeyCode::Backspace); - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.input_string, "a"); - - // Simulate pressing Enter in MainMenu state - let event = KeyEvent::from(KeyCode::Enter); - app.state = State::MainMenu; - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.state, State::TypingGame); - assert_eq!(app.input_string, ""); - - // Simulate typing 'T' in TypingGame state - let event = KeyEvent::from(KeyCode::Char('T')); - app.state = State::TypingGame; - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.input_string, "T"); - - // Simulate completing sentence and pressing Enter - let sentence = app.current_sentence().to_string(); - app.input_string = sentence.clone(); - let event = KeyEvent::from(KeyCode::Enter); - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.state, State::EndScreen); - assert_eq!(app.input_string.trim(), sentence.trim()); - - // Simulate pressing Esc in EndScreen state - let event = KeyEvent::from(KeyCode::Esc); - app.state = State::EndScreen; - input_handler(event, &mut app, Arc::new(Mutex::new(()))).await; - assert_eq!(app.should_exit, true); - } -} -- cgit v1.2.3