From 152e7926ee8c398536541f40758f7dd831c926d9 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 12 Oct 2022 16:57:48 -0500 Subject: [PATCH] Move game logic to game file --- src/client.rs | 25 ++++++++----------------- src/game.rs | 34 ++++++++++++++++++++++++++++++++++ src/lib.rs | 28 ++++++++-------------------- src/main.rs | 5 ++--- 4 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 src/game.rs diff --git a/src/client.rs b/src/client.rs index f18ef3f..7739f0b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,13 +1,10 @@ use std::sync::Arc; use futures::{select, FutureExt}; -use tokio::sync::Mutex; -use hexland_server::{ - channel_pair, - message::{Message, MessageWebSocket}, - msg, Channel, GameController, GlobalState, -}; +use crate::{msg, GlobalState}; +use crate::game::{channel_pair, Channel, GameController}; +use crate::message::{Message, MessageWebSocket}; pub struct Client { global_state: Arc, @@ -47,19 +44,18 @@ impl Client { match msg.command.as_str() { "HOST" => { let room_code = self.global_state.code_generator.lock().await.generate(); - let mut game_controller = GameController::default(); + let game_controller = GameController::default(); let (client_channel, server_channel) = channel_pair(); self.channel = Some(client_channel); - game_controller.channels.push(server_channel); + game_controller.channels.lock().await.push(server_channel); - let game_controller = Arc::new(Mutex::new(game_controller)); self.global_state .rooms .lock() .await - .insert(room_code.clone(), Arc::clone(&game_controller)); - tokio::spawn(async move { game_loop(game_controller) }); + .insert(room_code.clone(), game_controller.clone()); + tokio::spawn(async move { game_controller.run_loop().await }); self.ws.send(msg!(ROOM_CODE, room_code)).await.unwrap(); } "JOIN" => { @@ -69,10 +65,9 @@ impl Client { match room { Some(room) => { - let mut room = room.lock().await; let (client_channel, server_channel) = channel_pair(); self.channel = Some(client_channel); - room.channels.push(server_channel); + room.channels.lock().await.push(server_channel); self.ws.send(msg!(JOIN_OK)).await.unwrap(); } None => { @@ -93,7 +88,3 @@ impl Client { self.ws.send(msg).await.unwrap(); } } - -fn game_loop(_gc: Arc>) { - todo!(); -} diff --git a/src/game.rs b/src/game.rs new file mode 100644 index 0000000..22fac56 --- /dev/null +++ b/src/game.rs @@ -0,0 +1,34 @@ +use std::sync::Arc; + +use tokio::sync::{ mpsc, Mutex}; + +use crate::message::Message; + +#[derive(Clone)] +pub struct GameController { + pub channels: Arc>>, +} + +impl std::default::Default for GameController { + fn default() -> Self { + let channels = Arc::new(Mutex::new(Vec::new())); + GameController { channels } + } +} + +pub struct Channel { + pub tx: mpsc::Sender, + pub rx: mpsc::Receiver, +} + +pub fn channel_pair() -> (Channel, Channel) { + let (atx, brx) = mpsc::channel(32); + let (btx, arx) = mpsc::channel(32); + (Channel { tx: atx, rx: arx }, Channel { tx: btx, rx: brx }) +} + +impl GameController { + pub async fn run_loop(&self) { + todo!(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 05a2b79..9d6f7b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,30 +1,18 @@ -use std::{collections::HashMap, sync::Arc}; +use std::collections::HashMap; +use std::sync::Arc; -use tokio::sync::{mpsc, Mutex}; +use tokio::sync::Mutex; +pub mod game; pub mod message; -use message::Message; +pub mod client; + +use game::GameController; mod code_generator; use code_generator::CodeGenerator; #[derive(Default)] pub struct GlobalState { pub code_generator: Arc>, - pub rooms: Arc>>>>, -} - -pub struct Channel { - pub tx: mpsc::Sender, - pub rx: mpsc::Receiver, -} - -pub fn channel_pair() -> (Channel, Channel) { - let (atx, brx) = mpsc::channel(32); - let (btx, arx) = mpsc::channel(32); - (Channel { tx: atx, rx: arx }, Channel { tx: btx, rx: brx }) -} - -#[derive(Default)] -pub struct GameController { - pub channels: Vec, + pub rooms: Arc>>, } diff --git a/src/main.rs b/src/main.rs index a5d3a7e..d210565 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,10 @@ use std::{io::Error as IoError, sync::Arc}; use tokio::net::TcpListener; -use hexland_server::message::MessageWebSocket; use hexland_server::GlobalState; +use hexland_server::client::Client; +use hexland_server::message::MessageWebSocket; -mod client; -use client::Client; #[tokio::main] async fn main() -> Result<(), IoError> {