Skip to content

Commit

Permalink
refactor ship to different module
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekkumarweb committed Apr 11, 2024
1 parent 16f9aad commit 3b2cf24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
16 changes: 13 additions & 3 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use serde::{Deserialize, Serialize};
use tokio::{sync::mpsc, task::JoinHandle};

use crate::{
message::{PlayerMsg, Ship, SystemMsg},
message::{PlayerMsg, SystemMsg},
player::Player,
};

#[derive(Debug)]
pub struct Game {
_task: JoinHandle<()>,
#[allow(dead_code)]
task: JoinHandle<()>,
}

impl Game {
Expand All @@ -22,10 +24,18 @@ impl Game {
player_state: [PlayerGameState::default(), PlayerGameState::default()],
};
let task = actor.run();
Self { _task: task }
Self { task }
}
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy)]
pub struct Ship {
pub x: u8,
pub y: u8,
pub size: u8,
pub vertical: bool,
pub sunk: bool,
}
struct GameActor {
players: [Player; 2],
rxs: (mpsc::Receiver<PlayerMsg>, mpsc::Receiver<PlayerMsg>),
Expand Down
20 changes: 1 addition & 19 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
use serde::{Deserialize, Serialize};

// #[derive(Debug, Serialize, Deserialize)]
// #[serde(tag = "type", content = "data")]
// pub enum Msg {
// Connect,
// Id(String),
// OpponentId(String),
// Log(String),
// Chat(String),
// StartGame(Vec<Ship>),
// }

#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy)]
pub struct Ship {
pub x: u8,
pub y: u8,
pub size: u8,
pub vertical: bool,
pub sunk: bool,
}
use crate::game::Ship;

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
Expand Down
5 changes: 3 additions & 2 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum PlayerState {
pub struct Player {
id: String,
state: Arc<Mutex<PlayerState>>,
_task: JoinHandle<()>,
#[allow(dead_code)]
task: JoinHandle<()>,
sender: mpsc::Sender<SystemMsg>,
close_tx: oneshot::Sender<()>,
is_closed: Arc<RwLock<bool>>,
Expand All @@ -53,7 +54,7 @@ impl Player {
Self {
id,
state,
_task: task,
task,
sender,
close_tx,
is_closed,
Expand Down

0 comments on commit 3b2cf24

Please sign in to comment.