Skip to content

Commit

Permalink
Fix Join screen scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyatin committed Nov 23, 2021
1 parent 5753929 commit 3792d06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
18 changes: 10 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[macro_use] extern crate lazy_static;
#[macro_use]
extern crate lazy_static;

mod screens;
mod socket;
Expand Down Expand Up @@ -91,15 +92,18 @@ 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)?;

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());
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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());

Expand Down
48 changes: 23 additions & 25 deletions src/screens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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,
Expand All @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::socket::Socket;
use std::{time::Instant};
use std::time::Instant;

pub struct State {
pub columns: u16,
Expand All @@ -22,7 +22,7 @@ pub enum Action {
Input((usize, char)),
Join(u8),
Left(usize),
Forward
Forward,
}

#[derive(Eq, PartialEq)]
Expand Down

0 comments on commit 3792d06

Please sign in to comment.