SeaBattle/sea_battle_backend/src/human_player.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

2022-09-11 14:48:20 +00:00
use actix::Addr;
use uuid::Uuid;
2022-09-14 16:30:33 +00:00
use crate::data::{CurrentGameStatus, GameRules};
use crate::game::{Fire, Game, Player, SetBoatsLayout};
2022-09-11 14:48:20 +00:00
use crate::human_player_ws::{ClientMessage, HumanPlayerWS, ServerMessage};
pub struct HumanPlayer {
pub name: String,
pub game: Addr<Game>,
pub player: Addr<HumanPlayerWS>,
pub uuid: Uuid,
}
impl Player for HumanPlayer {
fn get_name(&self) -> &str {
&self.name
}
fn get_uid(&self) -> Uuid {
self.uuid
}
fn query_boats_layout(&self, rules: &GameRules) {
2022-09-12 14:07:46 +00:00
self.player.do_send(ServerMessage::QueryBoatsLayout {
rules: rules.clone(),
});
2022-09-11 14:48:20 +00:00
}
fn notify_other_player_ready(&self) {
self.player.do_send(ServerMessage::OtherPlayerReady);
}
fn notify_game_starting(&self) {
self.player.do_send(ServerMessage::GameStarting);
}
2022-09-14 16:30:33 +00:00
fn request_fire(&self, status: CurrentGameStatus) {
self.player.do_send(ServerMessage::RequestFire { status });
}
fn other_player_must_fire(&self, status: CurrentGameStatus) {
self.player
.do_send(ServerMessage::OtherPlayerMustFire { status });
}
2022-09-11 14:48:20 +00:00
}
impl HumanPlayer {
pub fn handle_client_message(&self, msg: ClientMessage) {
2022-09-14 16:02:11 +00:00
match msg {
ClientMessage::StopGame => {
// TODO : do something}
}
ClientMessage::BoatsLayout { layout } => {
self.game.do_send(SetBoatsLayout(self.uuid, layout))
}
2022-09-14 16:30:33 +00:00
ClientMessage::Fire { location } => self.game.do_send(Fire(self.uuid, location)),
2022-09-14 16:02:11 +00:00
}
2022-09-11 14:48:20 +00:00
}
}