135 lines
3.8 KiB
Rust
135 lines
3.8 KiB
Rust
use actix::Addr;
|
|
use uuid::Uuid;
|
|
|
|
use crate::data::*;
|
|
use crate::game::*;
|
|
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
|
|
}
|
|
|
|
fn opponent_connected(&self) {
|
|
self.player.do_send(ServerMessage::OpponentConnected);
|
|
}
|
|
|
|
fn set_other_player_name(&self, name: &str) {
|
|
self.player.do_send(ServerMessage::SetOpponentName {
|
|
name: name.to_string(),
|
|
});
|
|
}
|
|
|
|
fn query_boats_layout(&self, rules: &GameRules) {
|
|
self.player.do_send(ServerMessage::QueryBoatsLayout {
|
|
rules: rules.clone(),
|
|
});
|
|
}
|
|
|
|
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 waiting_for_opponent_boats_layout(&self) {
|
|
self.player
|
|
.do_send(ServerMessage::WaitingForOtherPlayerConfiguration);
|
|
}
|
|
|
|
fn notify_other_player_ready(&self) {
|
|
self.player.do_send(ServerMessage::OpponentReady);
|
|
}
|
|
|
|
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 opponent_must_fire(&self, status: CurrentGameStatus) {
|
|
self.player
|
|
.do_send(ServerMessage::OpponentMustFire { status });
|
|
}
|
|
|
|
fn strike_result(&self, c: Coordinates, res: FireResult) {
|
|
self.player.do_send(ServerMessage::FireResult {
|
|
pos: c,
|
|
result: res,
|
|
});
|
|
}
|
|
|
|
fn other_player_strike_result(&self, c: Coordinates, res: FireResult) {
|
|
self.player.do_send(ServerMessage::OpponentFireResult {
|
|
pos: c,
|
|
result: res,
|
|
});
|
|
}
|
|
|
|
fn lost_game(&self, status: CurrentGameStatus) {
|
|
self.player.do_send(ServerMessage::LostGame { status });
|
|
}
|
|
|
|
fn won_game(&self, status: CurrentGameStatus) {
|
|
self.player.do_send(ServerMessage::WonGame { status });
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
impl HumanPlayer {
|
|
pub fn handle_client_message(&self, msg: ClientMessage) {
|
|
log::debug!("Got message from client: {:?}", msg);
|
|
match msg {
|
|
ClientMessage::StopGame => self.game.do_send(PlayerLeftGame(self.uuid)),
|
|
ClientMessage::BoatsLayout { layout } => {
|
|
self.game.do_send(SetBoatsLayout(self.uuid, layout))
|
|
}
|
|
ClientMessage::Fire { location } => self.game.do_send(Fire(self.uuid, location)),
|
|
|
|
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))
|
|
}
|
|
}
|
|
}
|
|
}
|