SeaBattle/rust/sea_battle_backend/src/bot_player.rs

97 lines
2.4 KiB
Rust
Raw Normal View History

2022-09-22 15:53:25 +00:00
use actix::Addr;
use uuid::Uuid;
2022-09-24 10:12:40 +00:00
use crate::data::{BoatsLayout, BotType, Coordinates, CurrentGameStatus, FireResult, GameRules};
2022-09-22 15:53:25 +00:00
use crate::game::{Fire, Game, Player, RespondRequestRematch, SetBoatsLayout};
#[derive(Clone)]
2022-09-24 10:12:40 +00:00
pub struct BotPlayer {
2022-09-22 15:53:25 +00:00
game: Addr<Game>,
2022-09-24 10:12:40 +00:00
kind: BotType,
2022-09-22 15:53:25 +00:00
uuid: Uuid,
}
2022-09-24 10:12:40 +00:00
impl BotPlayer {
pub fn new(kind: BotType, game: Addr<Game>) -> Self {
2022-09-22 15:53:25 +00:00
Self {
game,
2022-09-24 10:12:40 +00:00
kind,
2022-09-22 15:53:25 +00:00
uuid: Uuid::new_v4(),
}
}
}
2022-09-24 10:12:40 +00:00
impl Player for BotPlayer {
2022-09-22 15:53:25 +00:00
fn get_name(&self) -> &str {
match self.kind {
BotType::Random => "Random bot",
BotType::Linear => "Linear bot",
BotType::Intermediate => "Intermediate bot",
BotType::Smart => "Smart bot",
}
2022-09-22 15:53:25 +00:00
}
fn get_uid(&self) -> Uuid {
self.uuid
}
fn is_bot(&self) -> bool {
true
}
fn set_other_player_name(&self, _name: &str) {}
fn query_boats_layout(&self, rules: &GameRules) {
match BoatsLayout::gen_random_for_rules(rules) {
Ok(layout) => self.game.do_send(SetBoatsLayout(self.uuid, layout)),
Err(e) => log::error!(
"Failed to use game rules to construct boats layout: {:?}",
e
),
}
}
fn rejected_boats_layout(&self, _errors: Vec<&'static str>) {
unreachable!()
}
fn notify_other_player_ready(&self) {}
fn notify_game_starting(&self) {}
fn request_fire(&self, status: CurrentGameStatus) {
2022-09-24 10:12:40 +00:00
self.game.do_send(Fire(
self.uuid,
status.find_fire_coordinates_for_bot_type(self.kind),
));
2022-09-22 15:53:25 +00:00
}
fn opponent_must_fire(&self, _status: CurrentGameStatus) {}
fn strike_result(&self, _c: Coordinates, _res: FireResult) {}
fn other_player_strike_result(&self, _c: Coordinates, _res: FireResult) {}
fn lost_game(&self, _status: CurrentGameStatus) {}
fn won_game(&self, _status: CurrentGameStatus) {}
fn opponent_requested_rematch(&self) {
self.game.do_send(RespondRequestRematch(self.uuid, true));
}
fn opponent_rejected_rematch(&self) {}
fn opponent_accepted_rematch(&self) {}
fn opponent_left_game(&self) {
// Human are not reliable lol
}
fn opponent_replaced_by_bot(&self) {
// Not such a good idea. will panic, just in case
panic!("Bot shall not play against each other (it is completely useless)");
}
}