From 3792d06e197d27b9567f7adb3878da0802003f47 Mon Sep 17 00:00:00 2001 From: Selyatin Ismet Date: Tue, 23 Nov 2021 12:55:04 +0200 Subject: [PATCH] Fix Join screen scaling --- src/main.rs | 18 ++++++++++-------- src/screens.rs | 48 +++++++++++++++++++++++------------------------- src/types.rs | 4 ++-- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 197bf89..3a3facf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -#[macro_use] extern crate lazy_static; +#[macro_use] +extern crate lazy_static; mod screens; mod socket; @@ -91,7 +92,10 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { state.socket = Some(Socket::new(&state.sock_addr)?); - let session_token: u16 = player.input.parse().map_err(|_| io::Error::new(io::ErrorKind::Other, "Invalid Number."))?; + let session_token: u16 = player + .input + .parse() + .map_err(|_| io::Error::new(io::ErrorKind::Other, "Invalid Number."))?; player.sort_position = state.socket.as_mut().unwrap().join_session(session_token)?; @@ -99,7 +103,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { state.session_token = Some(session_token); state.socket.as_ref().unwrap().init_reader()?; - + state.dictionary = DICTIONARY.clone(); let rng = fastrand::Rng::with_seed(state.session_token.unwrap().into()); @@ -110,7 +114,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { .dictionary .iter_mut() .for_each(|word| word.y = rng.u16(0..state.rows - 1)); - + player.input.clear(); state.screen = Screen::MultiPlayer; @@ -146,7 +150,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { modifiers: KeyModifiers::NONE, }) => { reset_state(state); - + let rng = fastrand::Rng::new(); rng.shuffle(&mut state.dictionary); @@ -171,7 +175,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { state.socket.as_ref().unwrap().init_reader()?; state.dictionary = DICTIONARY.clone(); - + let rng = fastrand::Rng::with_seed(state.session_token.unwrap().into()); rng.shuffle(&mut state.dictionary); @@ -208,8 +212,6 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { Ok(()) } - - fn main() -> io::Result<()> { let sock_addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_owned()); diff --git a/src/screens.rs b/src/screens.rs index d407ff1..4853a67 100644 --- a/src/screens.rs +++ b/src/screens.rs @@ -11,7 +11,10 @@ use std::{ }; pub fn main(stdout: &mut Stdout, state: &State) -> io::Result<()> { - let (x, y) = ((state.columns as f32 * 0.4) as u16, (state.rows as f32 * 0.41) as u16); + let (x, y) = ( + (state.columns as f32 * 0.4) as u16, + (state.rows as f32 * 0.41) as u16, + ); queue!( stdout, @@ -143,13 +146,12 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { let space_per_player = (rows / players_len as f32) as u16 - 2; let line: String = iter::repeat('-').take(columns.into()).collect(); - + // Might use multithreading to calculate each player's section, // but that might be overengineering too, so we'll see. for (i, player) in state.players.iter_mut().enumerate() { - let y_start = i as u16 * space_per_player; - + let y_end = y_start + space_per_player; let color = match i { @@ -176,7 +178,7 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { .enumerate() { let mut correct_chars = 0; - + let word_y = ((word.y as f32 / rows) * (y_end - y_start) as f32) as u16 + y_start + 1; for (n, c) in word.value.chars().enumerate() { @@ -223,9 +225,10 @@ pub fn join(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { let (columns, rows) = (state.columns as f32, state.rows as f32); - let (x_start, x_end) = ((columns * 0.4) as u16, (columns * 0.6) as u16); + let (mut x_start, mut x_end) = ((columns * 0.4) as u16, (columns * 0.6) as u16); - let (y_start, y_end) = ((rows * 0.4) as u16, (rows * 0.45) as u16); + let mut y_start = (rows * 0.4) as u16; + let y_end = y_start + 2; queue!( stdout, @@ -243,34 +246,29 @@ pub fn join(stdout: &mut Stdout, state: &mut State) -> io::Result<()> { )?; } - let x_end = (columns * 0.599) as u16; + x_end -= 1; + y_start += 1; - let (y_start, y_end) = ((rows * 0.42) as u16, (rows * 0.45) as u16); - - for y in y_start..y_end { - queue!( - stdout, - MoveTo(x_start, y), - Print('|'), - MoveTo(x_end, y), - Print('|') - )?; - } + queue!( + stdout, + MoveTo(x_start, y_start), + Print('|'), + MoveTo(x_end, y_start), + Print('|') + )?; let player = state.players.get(state.current_player).unwrap(); - let x_end = x_end - 1; - - let x = x_start + 1; + x_end = x_end - 1; - let y = (rows * 0.435) as u16; + x_start += 1; for (i, c) in player.input.chars().enumerate() { - let x = x + i as u16; + let x = x_start + i as u16; if x > x_end { break; } - queue!(stdout, MoveTo(x, y), Print(c))?; + queue!(stdout, MoveTo(x, y_start), Print(c))?; } Ok(()) diff --git a/src/types.rs b/src/types.rs index 1bbb357..9a5f346 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,5 @@ use super::socket::Socket; -use std::{time::Instant}; +use std::time::Instant; pub struct State { pub columns: u16, @@ -22,7 +22,7 @@ pub enum Action { Input((usize, char)), Join(u8), Left(usize), - Forward + Forward, } #[derive(Eq, PartialEq)]