Skip to content

Commit

Permalink
Multiplayer now kinda works
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyatin committed Nov 21, 2021
1 parent e68a56d commit 0818bdc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ lto = "fat"
opt-level = 3

[dependencies]
anyhow = "1.0.47"
crossterm = "0.22.1"
fastrand = "1.5.0"
lazy_static = "1.4.0"
21 changes: 11 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ use crossterm::{
cursor::{Hide, MoveTo, Show},
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
execute, queue,
style::{style, Color, Print, PrintStyledContent, SetForegroundColor, Stylize},
style::{style, Print, PrintStyledContent, Stylize},
terminal::{self, Clear, ClearType},
ExecutableCommand, QueueableCommand,
};
use socket::Socket;
use std::{
env,
io::{stdout, Read, Stdout, Write},
net::TcpStream,
thread,
io::{self, stdout, Stdout, Write},
time::{Duration, Instant},
};
use types::{Action, Player, Screen, State, Word};
use types::{Player, Screen, State, Word};

lazy_static! {
static ref DICTIONARY: Vec<Word> = include_str!("../dictionary.txt")
Expand All @@ -40,7 +37,7 @@ fn reset_state(state: &mut State) {
state.players.push(player);
}

fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {
fn main_loop(stdout: &mut Stdout, state: &mut State) -> io::Result<()> {
// Clear the previous frame
queue!(stdout, Clear(ClearType::All))?;

Expand Down Expand Up @@ -94,7 +91,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {

state.socket = Some(Socket::new(&state.sock_addr)?);

let session_token: u16 = player.input.parse()?;
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)?;
Expand Down Expand Up @@ -125,6 +122,9 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {
}) => {
if let Some(player) = state.players.get_mut(state.current_player) {
player.input.pop();
if let Some(socket) = &mut state.socket {
socket.send_input('-')?;
}
}
}
Event::Key(KeyEvent {
Expand All @@ -136,6 +136,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {
terminal::disable_raw_mode()?;
std::process::exit(0);
}
reset_state(state);
state.screen = Screen::Main;
}
Event::Key(KeyEvent {
Expand Down Expand Up @@ -207,7 +208,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {



fn main() -> anyhow::Result<()> {
fn main() -> io::Result<()> {
let sock_addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_owned());

terminal::enable_raw_mode()?;
Expand Down Expand Up @@ -244,7 +245,7 @@ fn main() -> anyhow::Result<()> {

loop {
if let Err(err) = main_loop(&mut stdout, &mut state) {
state.err = Some(err);
state.err = Some(Box::new(err));
}
}
}
26 changes: 13 additions & 13 deletions src/screens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ use super::types::{Action, Player, State};
use crossterm::{
cursor::MoveTo,
queue,
style::{style, Attribute, Color, Print, PrintStyledContent, SetForegroundColor, Stylize},
terminal::{self, Clear, ClearType},
ExecutableCommand, QueueableCommand,
style::{style, Attribute, Color, Print, PrintStyledContent, Stylize},
terminal::{Clear, ClearType},
};
use std::{
io::{self, Read, Stdout, Write},
io::{self, Stdout},
iter,
net::TcpStream,
time::{Duration, Instant},
};

pub fn main(stdout: &mut Stdout, state: &State) -> io::Result<()> {
Expand Down Expand Up @@ -96,6 +93,8 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> {

let socket = state.socket.as_ref().unwrap();

let mut should_go_forward = false;

for action in socket.actions().drain(..) {
match action {
Action::Join(position) => {
Expand Down Expand Up @@ -126,18 +125,19 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> {
}
Action::Input((position, c)) => {
if let Some(player) = state.players.get_mut(position) {
player.input.push(c);
if c == '-' {
player.input.pop();
} else {
player.input.push(c);
}
}
}
Action::Forward => should_go_forward = true,
};
}

let (columns, rows) = (state.columns, state.rows as f32);

let elapsed_millis = state.instant.elapsed().as_millis();

let should_go_forward: bool = elapsed_millis - state.last_instant > 500;

let players_len = state.players.len();

let space_per_player = (rows / players_len as f32) as u16 - 2;
Expand All @@ -150,7 +150,7 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> {
// 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_start = i as u16 * space_per_player;
let y_end = y_start + space_per_player;
let color = match i {
0 => Color::Blue,
Expand All @@ -159,6 +159,7 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> {
3 => Color::Yellow,
_ => Color::White,
};

queue!(
stdout,
MoveTo(0, y_end),
Expand Down Expand Up @@ -206,7 +207,6 @@ pub fn multi_player(stdout: &mut Stdout, state: &mut State) -> io::Result<()> {

if should_go_forward {
word.x += add_x;
state.last_instant = elapsed_millis;
}

add_x -= 1;
Expand Down
12 changes: 8 additions & 4 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ impl Socket {
break;
}

let action = match &buffer[..4] {
b"Join" => Action::Join(buffer[4]),
b"Left" => Action::Left(buffer[4].into()),
_ => Action::Input((buffer[0].into(), buffer[1].into())),
let action = if buffer[0] == b'+' {
Action::Forward
} else {
match &buffer[..4] {
b"Join" => Action::Join(buffer[4]),
b"Left" => Action::Left(buffer[4].into()),
_ => Action::Input((buffer[0].into(), buffer[1].into())),
}
};

if let Ok(mut vec) = actions.try_lock() {
Expand Down
5 changes: 3 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::{net::TcpStream, time::Instant};
use std::{time::Instant};

pub struct State {
pub columns: u16,
Expand All @@ -13,7 +13,7 @@ pub struct State {
pub current_player: usize,
pub session_token: Option<u16>,
pub socket: Option<Socket>,
pub err: Option<anyhow::Error>,
pub err: Option<Box<dyn std::error::Error>>,
}

/// Used in Multiplayer to determine what kind of data is received
Expand All @@ -22,6 +22,7 @@ pub enum Action {
Input((usize, char)),
Join(u8),
Left(usize),
Forward
}

#[derive(Eq, PartialEq)]
Expand Down

0 comments on commit 0818bdc

Please sign in to comment.