2022-09-11 14:48:20 +00:00
|
|
|
use actix::Addr;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-09-15 15:37:26 +00:00
|
|
|
use crate::data::{Coordinates, CurrentGameStatus, EndGameMap, FireResult, GameRules};
|
2022-09-14 16:30:33 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:13:06 +00:00
|
|
|
fn set_other_player_name(&self, name: &str) {
|
|
|
|
self.player.do_send(ServerMessage::SetOpponentName {
|
|
|
|
name: name.to_string(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-11 14:48:20 +00:00
|
|
|
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
|
|
|
}
|
2022-09-13 17:02:26 +00:00
|
|
|
|
2022-09-15 16:04:22 +00:00
|
|
|
fn rejected_boats_layout(&self, errors: Vec<&'static str>) {
|
|
|
|
self.player.do_send(ServerMessage::RejectedBoatsLayout {
|
|
|
|
errors: errors.iter().map(|s| s.to_string()).collect(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:02:26 +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-15 15:37:26 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
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 => {
|
2022-09-15 15:37:26 +00:00
|
|
|
// TODO : do something
|
2022-09-14 16:02:11 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|