SeaBattle/sea_battle_backend/src/human_player.rs
2022-09-15 17:37:26 +02:00

90 lines
2.4 KiB
Rust

use actix::Addr;
use uuid::Uuid;
use crate::data::{Coordinates, CurrentGameStatus, EndGameMap, FireResult, GameRules};
use crate::game::{Fire, Game, Player, SetBoatsLayout};
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) {
self.player.do_send(ServerMessage::QueryBoatsLayout {
rules: rules.clone(),
});
}
fn notify_other_player_ready(&self) {
self.player.do_send(ServerMessage::OtherPlayerReady);
}
fn notify_game_starting(&self) {
self.player.do_send(ServerMessage::GameStarting);
}
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 });
}
fn strike_result(&self, c: Coordinates, res: FireResult) {
self.player.do_send(ServerMessage::StrikeResult {
pos: c,
result: res,
});
}
fn other_player_strike_result(&self, c: Coordinates, res: FireResult) {
self.player.do_send(ServerMessage::OpponentStrikeResult {
pos: c,
result: res,
});
}
fn lost_game(&self, your_map: EndGameMap, opponent_map: EndGameMap) {
self.player.do_send(ServerMessage::LostGame {
your_map,
opponent_map,
});
}
fn won_game(&self, your_map: EndGameMap, opponent_map: EndGameMap) {
self.player.do_send(ServerMessage::WonGame {
your_map,
opponent_map,
});
}
}
impl HumanPlayer {
pub fn handle_client_message(&self, msg: ClientMessage) {
match msg {
ClientMessage::StopGame => {
// TODO : do something
}
ClientMessage::BoatsLayout { layout } => {
// TODO : check boat layout validity
self.game.do_send(SetBoatsLayout(self.uuid, layout))
}
ClientMessage::Fire { location } => self.game.do_send(Fire(self.uuid, location)),
}
}
}