2022-09-11 14:48:20 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
|
|
|
use actix::{Actor, Context, Handler};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-09-14 16:30:33 +00:00
|
|
|
use crate::data::*;
|
2022-09-13 17:02:26 +00:00
|
|
|
|
2022-09-11 14:48:20 +00:00
|
|
|
pub trait Player {
|
|
|
|
fn get_name(&self) -> &str;
|
|
|
|
|
|
|
|
fn get_uid(&self) -> Uuid;
|
|
|
|
|
|
|
|
fn query_boats_layout(&self, rules: &GameRules);
|
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>);
|
|
|
|
|
2022-09-13 17:02:26 +00:00
|
|
|
fn notify_other_player_ready(&self);
|
|
|
|
|
|
|
|
fn notify_game_starting(&self);
|
2022-09-14 16:30:33 +00:00
|
|
|
|
|
|
|
fn request_fire(&self, status: CurrentGameStatus);
|
|
|
|
|
|
|
|
fn other_player_must_fire(&self, status: CurrentGameStatus);
|
2022-09-15 15:37:26 +00:00
|
|
|
|
|
|
|
fn strike_result(&self, c: Coordinates, res: FireResult);
|
|
|
|
|
|
|
|
fn other_player_strike_result(&self, c: Coordinates, res: FireResult);
|
|
|
|
|
|
|
|
fn lost_game(&self, your_map: EndGameMap, opponent_map: EndGameMap);
|
|
|
|
|
|
|
|
fn won_game(&self, your_map: EndGameMap, opponent_map: EndGameMap);
|
2022-09-13 17:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn opponent(index: usize) -> usize {
|
|
|
|
match index {
|
|
|
|
0 => 1,
|
|
|
|
1 => 0,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2022-09-11 14:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Eq, PartialEq, Debug, Copy, Clone)]
|
|
|
|
enum GameStatus {
|
|
|
|
#[default]
|
|
|
|
Created,
|
|
|
|
WaitingForBoatsDisposition,
|
2022-09-13 17:02:26 +00:00
|
|
|
Started,
|
2022-09-15 15:37:26 +00:00
|
|
|
Finished,
|
2022-09-11 14:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Game {
|
|
|
|
rules: GameRules,
|
|
|
|
players: Vec<Arc<dyn Player>>,
|
|
|
|
status: GameStatus,
|
2022-09-13 17:02:26 +00:00
|
|
|
map_0: Option<GameMap>,
|
|
|
|
map_1: Option<GameMap>,
|
2022-09-14 16:30:33 +00:00
|
|
|
turn: usize,
|
2022-09-11 14:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Game {
|
|
|
|
pub fn new(rules: GameRules) -> Self {
|
|
|
|
Self {
|
|
|
|
rules,
|
|
|
|
players: vec![],
|
|
|
|
status: GameStatus::Created,
|
2022-09-13 17:02:26 +00:00
|
|
|
map_0: None,
|
|
|
|
map_1: None,
|
2022-09-14 16:30:33 +00:00
|
|
|
turn: 0,
|
2022-09-11 14:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:02:26 +00:00
|
|
|
/// Find the ID of a player from its UUID
|
|
|
|
fn player_id_by_uuid(&self, uuid: Uuid) -> usize {
|
|
|
|
self.players
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|p| p.1.get_uid() == uuid)
|
|
|
|
.expect("Player is not member of this game!")
|
|
|
|
.0
|
|
|
|
}
|
|
|
|
|
2022-09-11 14:48:20 +00:00
|
|
|
/// Once the two player has been registered, the game may start
|
2022-09-11 14:51:14 +00:00
|
|
|
fn query_boats_disposition(&mut self) {
|
2022-09-13 17:12:16 +00:00
|
|
|
log::debug!("Query boats disposition");
|
2022-09-11 14:48:20 +00:00
|
|
|
assert_eq!(self.status, GameStatus::Created);
|
|
|
|
self.status = GameStatus::WaitingForBoatsDisposition;
|
|
|
|
self.players[0].query_boats_layout(&self.rules);
|
|
|
|
self.players[1].query_boats_layout(&self.rules);
|
|
|
|
}
|
2022-09-13 17:02:26 +00:00
|
|
|
|
|
|
|
/// Start fires exchange
|
|
|
|
fn start_fire_exchanges(&mut self) {
|
|
|
|
self.status = GameStatus::Started;
|
2022-09-14 16:30:33 +00:00
|
|
|
log::debug!(
|
|
|
|
"Start fire exchanges. Player {}#{} goes first",
|
|
|
|
self.players[self.turn].get_name(),
|
|
|
|
self.turn
|
|
|
|
);
|
|
|
|
|
2022-09-15 15:37:26 +00:00
|
|
|
self.request_fire();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_fire(&self) {
|
2022-09-14 16:30:33 +00:00
|
|
|
self.players[self.turn].request_fire(self.get_game_status_for_player(self.turn));
|
|
|
|
self.players[opponent(self.turn)]
|
2022-09-15 15:37:26 +00:00
|
|
|
.other_player_must_fire(self.get_game_status_for_player(opponent(self.turn)));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn player_map(&self, id: usize) -> &GameMap {
|
|
|
|
match id {
|
|
|
|
0 => self.map_0.as_ref(),
|
|
|
|
1 => self.map_1.as_ref(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn player_map_mut(&mut self, id: usize) -> &mut GameMap {
|
|
|
|
match id {
|
|
|
|
0 => self.map_0.as_mut(),
|
|
|
|
1 => self.map_1.as_mut(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle fire attempts
|
|
|
|
fn handle_fire(&mut self, c: Coordinates) {
|
|
|
|
let result = self.player_map_mut(opponent(self.turn)).fire(c);
|
|
|
|
self.players[self.turn].strike_result(c, result);
|
|
|
|
self.players[opponent(self.turn)].strike_result(c, result);
|
|
|
|
|
|
|
|
// Easiest case : player missed his fire
|
|
|
|
if result == FireResult::Missed {
|
|
|
|
self.turn = opponent(self.turn);
|
|
|
|
self.request_fire();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == FireResult::Sunk && self.player_map(opponent(self.turn)).are_all_boat_sunk() {
|
|
|
|
self.status = GameStatus::Finished;
|
|
|
|
|
|
|
|
let winner_map = self.player_map(self.turn).final_map();
|
|
|
|
let looser_map = self.player_map(opponent(self.turn)).final_map();
|
|
|
|
|
|
|
|
self.players[self.turn].won_game(winner_map.clone(), looser_map.clone());
|
|
|
|
self.players[opponent(self.turn)].lost_game(looser_map, winner_map);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == FireResult::Sunk || result == FireResult::Hit)
|
|
|
|
&& !self.rules.player_continue_on_hit
|
|
|
|
{
|
|
|
|
self.turn = opponent(self.turn);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.request_fire();
|
2022-09-14 16:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get current game status for a specific player
|
|
|
|
fn get_game_status_for_player(&self, _id: usize) -> CurrentGameStatus {
|
|
|
|
CurrentGameStatus {
|
|
|
|
rules: self.rules.clone(),
|
|
|
|
}
|
2022-09-13 17:02:26 +00:00
|
|
|
}
|
2022-09-11 14:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Actor for Game {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
2022-09-11 14:58:40 +00:00
|
|
|
pub struct AddPlayer<E>(pub E);
|
2022-09-11 14:48:20 +00:00
|
|
|
|
2022-09-11 14:58:40 +00:00
|
|
|
impl<E> Handler<AddPlayer<Arc<E>>> for Game
|
|
|
|
where
|
|
|
|
E: Player + 'static,
|
|
|
|
{
|
2022-09-11 14:48:20 +00:00
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
/// Add a new player to the game
|
2022-09-11 14:58:40 +00:00
|
|
|
fn handle(&mut self, msg: AddPlayer<Arc<E>>, _ctx: &mut Self::Context) -> Self::Result {
|
2022-09-11 14:48:20 +00:00
|
|
|
assert!(self.players.len() < 2);
|
2022-09-11 14:58:40 +00:00
|
|
|
self.players.push(msg.0);
|
2022-09-11 14:48:20 +00:00
|
|
|
|
|
|
|
if self.players.len() == 2 {
|
2022-09-11 14:51:14 +00:00
|
|
|
self.query_boats_disposition();
|
2022-09-11 14:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 17:02:26 +00:00
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct SetBoatsLayout(pub Uuid, pub BoatsLayout);
|
|
|
|
|
|
|
|
impl Handler<SetBoatsLayout> for Game {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
/// Receive game configuration of a player
|
|
|
|
fn handle(&mut self, msg: SetBoatsLayout, _ctx: &mut Self::Context) -> Self::Result {
|
2022-09-14 16:30:33 +00:00
|
|
|
if self.status != GameStatus::WaitingForBoatsDisposition {
|
|
|
|
log::error!("Player attempted to set boat configuration on invalid step!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:02:26 +00:00
|
|
|
let player_index = self.player_id_by_uuid(msg.0);
|
2022-09-15 16:04:22 +00:00
|
|
|
|
2022-09-15 17:56:11 +00:00
|
|
|
let errors = msg.1.errors(&self.rules);
|
2022-09-15 16:04:22 +00:00
|
|
|
if !errors.is_empty() {
|
2022-09-15 17:56:11 +00:00
|
|
|
log::error!("Got invalid boats layout!");
|
2022-09-15 16:04:22 +00:00
|
|
|
self.players[player_index].rejected_boats_layout(errors);
|
|
|
|
self.players[player_index].query_boats_layout(&self.rules);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:12:16 +00:00
|
|
|
log::debug!("Got boat disposition for player {}", player_index);
|
2022-09-13 17:02:26 +00:00
|
|
|
match player_index {
|
|
|
|
0 => self.map_0 = Some(GameMap::new(self.rules.clone(), msg.1)),
|
|
|
|
1 => self.map_1 = Some(GameMap::new(self.rules.clone(), msg.1)),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.players[opponent(player_index)].notify_other_player_ready();
|
|
|
|
|
|
|
|
if self.map_0.is_some() && self.map_1.is_some() {
|
|
|
|
self.players.iter().for_each(|p| p.notify_game_starting());
|
|
|
|
self.start_fire_exchanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 16:30:33 +00:00
|
|
|
|
|
|
|
#[derive(Message, Debug)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct Fire(pub Uuid, pub Coordinates);
|
|
|
|
|
|
|
|
impl Handler<Fire> for Game {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Fire, _ctx: &mut Self::Context) -> Self::Result {
|
2022-09-15 15:37:26 +00:00
|
|
|
if self.status != GameStatus::Started {
|
|
|
|
log::error!("Player attempted to fire on invalid step!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.0 != self.players[self.turn].get_uid() {
|
|
|
|
log::error!("Player attempted to fire when it was not its turn!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.handle_fire(msg.1)
|
2022-09-14 16:30:33 +00:00
|
|
|
}
|
|
|
|
}
|