Add support for linear bots
This commit is contained in:
87
sea_battle_backend/src/bots/linear_bot.rs
Normal file
87
sea_battle_backend/src/bots/linear_bot.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use actix::Addr;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::data::{BoatsLayout, Coordinates, CurrentGameStatus, EndGameMap, FireResult, GameRules};
|
||||
use crate::game::{Fire, Game, Player, RespondRequestRematch, SetBoatsLayout};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LinearBot {
|
||||
game: Addr<Game>,
|
||||
uuid: Uuid,
|
||||
}
|
||||
|
||||
impl LinearBot {
|
||||
pub fn new(game: Addr<Game>) -> Self {
|
||||
Self {
|
||||
game,
|
||||
uuid: Uuid::new_v4(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Player for LinearBot {
|
||||
fn get_name(&self) -> &str {
|
||||
"Linear Bot"
|
||||
}
|
||||
|
||||
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) {
|
||||
self.game
|
||||
.do_send(Fire(self.uuid, status.find_first_valid_fire_location()));
|
||||
}
|
||||
|
||||
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, _your_map: EndGameMap, _opponent_map: EndGameMap) {}
|
||||
|
||||
fn won_game(&self, _your_map: EndGameMap, _opponent_map: EndGameMap) {}
|
||||
|
||||
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)");
|
||||
}
|
||||
}
|
2
sea_battle_backend/src/bots/mod.rs
Normal file
2
sea_battle_backend/src/bots/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod linear_bot;
|
||||
pub mod random_bot;
|
87
sea_battle_backend/src/bots/random_bot.rs
Normal file
87
sea_battle_backend/src/bots/random_bot.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use actix::Addr;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::data::{BoatsLayout, Coordinates, CurrentGameStatus, EndGameMap, FireResult, GameRules};
|
||||
use crate::game::{Fire, Game, Player, RespondRequestRematch, SetBoatsLayout};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RandomBot {
|
||||
game: Addr<Game>,
|
||||
uuid: Uuid,
|
||||
}
|
||||
|
||||
impl RandomBot {
|
||||
pub fn new(game: Addr<Game>) -> Self {
|
||||
Self {
|
||||
game,
|
||||
uuid: Uuid::new_v4(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Player for RandomBot {
|
||||
fn get_name(&self) -> &str {
|
||||
"Random Bot"
|
||||
}
|
||||
|
||||
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) {
|
||||
self.game
|
||||
.do_send(Fire(self.uuid, status.find_valid_random_fire_location()));
|
||||
}
|
||||
|
||||
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, _your_map: EndGameMap, _opponent_map: EndGameMap) {}
|
||||
|
||||
fn won_game(&self, _your_map: EndGameMap, _opponent_map: EndGameMap) {}
|
||||
|
||||
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)");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user