SeaBattle/sea_battle_backend/src/human_player.rs

125 lines
3.5 KiB
Rust
Raw Normal View History

2022-09-11 14:48:20 +00:00
use actix::Addr;
use uuid::Uuid;
2022-09-19 17:29:11 +00:00
use crate::data::*;
2022-09-16 14:55:29 +00:00
use crate::game::*;
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 is_bot(&self) -> bool {
false
}
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-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(),
});
}
fn notify_other_player_ready(&self) {
2022-09-16 14:55:29 +00:00
self.player.do_send(ServerMessage::OpponentReady);
}
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 });
}
2022-09-16 14:55:29 +00:00
fn opponent_must_fire(&self, status: CurrentGameStatus) {
2022-09-14 16:30:33 +00:00
self.player
2022-09-16 14:55:29 +00:00
.do_send(ServerMessage::OpponentMustFire { status });
2022-09-14 16:30:33 +00:00
}
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,
});
}
2022-09-19 17:29:11 +00:00
fn lost_game(&self, status: CurrentGameStatus) {
self.player.do_send(ServerMessage::LostGame { status });
2022-09-15 15:37:26 +00:00
}
2022-09-19 17:29:11 +00:00
fn won_game(&self, status: CurrentGameStatus) {
self.player.do_send(ServerMessage::WonGame { status });
2022-09-15 15:37:26 +00:00
}
2022-09-16 14:55:29 +00:00
fn opponent_requested_rematch(&self) {
self.player.do_send(ServerMessage::OpponentRequestedRematch);
}
fn opponent_rejected_rematch(&self) {
self.player.do_send(ServerMessage::OpponentRejectedRematch);
}
fn opponent_accepted_rematch(&self) {
self.player.do_send(ServerMessage::OpponentAcceptedRematch);
}
fn opponent_left_game(&self) {
self.player.do_send(ServerMessage::OpponentLeftGame);
}
fn opponent_replaced_by_bot(&self) {
self.player.do_send(ServerMessage::OpponentReplacedByBot);
}
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 => self.game.do_send(PlayerLeftGame(self.uuid)),
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-16 14:55:29 +00:00
ClientMessage::RequestRematch => self.game.do_send(RequestRematch(self.uuid)),
ClientMessage::AcceptRematch => {
self.game.do_send(RespondRequestRematch(self.uuid, true))
}
ClientMessage::RejectRematch => {
self.game.do_send(RespondRequestRematch(self.uuid, false))
}
2022-09-14 16:02:11 +00:00
}
2022-09-11 14:48:20 +00:00
}
}