Add invite mode

This commit is contained in:
Pierre HUBERT 2022-09-24 11:46:12 +02:00
parent 663a9c2d71
commit 45b6a24eda
9 changed files with 387 additions and 26 deletions

View File

@ -19,3 +19,5 @@ pub const MULTI_PLAYER_PLAYER_CAN_CONTINUE_AFTER_HIT: bool = true;
pub const MULTI_PLAYER_PLAYER_BOATS: [usize; 5] = [2, 3, 3, 4, 5]; pub const MULTI_PLAYER_PLAYER_BOATS: [usize; 5] = [2, 3, 3, 4, 5];
pub const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; pub const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
pub const INVITE_CODE_LENGTH: usize = 5;

View File

@ -0,0 +1,100 @@
//! # Dispatcher actors
//!
//! Allows to establish connections between human players
use std::collections::HashMap;
use std::time::Duration;
use actix::{Actor, Addr, AsyncContext, Context, Handler, Message};
use crate::consts::INVITE_CODE_LENGTH;
use crate::data::GameRules;
use crate::game::Game;
use crate::human_player_ws::{CloseConnection, HumanPlayerWS, ServerMessage, SetGame};
use crate::utils::rand_str;
/// How often garbage collector is run
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(60);
#[derive(Message)]
#[rtype(result = "()")]
pub struct CreateInvite(pub GameRules, pub Addr<HumanPlayerWS>);
#[derive(Message)]
#[rtype(result = "()")]
pub struct AcceptInvite(pub String, pub Addr<HumanPlayerWS>);
struct PendingPlayer {
player: Addr<HumanPlayerWS>,
rules: GameRules,
}
#[derive(Default)]
pub struct DispatcherActor {
with_invite: HashMap<String, PendingPlayer>,
}
impl Actor for DispatcherActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
ctx.run_interval(HEARTBEAT_INTERVAL, |act, _ctx| {
// Garbage collect invites
let ids = act
.with_invite
.iter()
.filter(|p| !p.1.player.connected())
.map(|p| p.0.clone())
.collect::<Vec<_>>();
for id in ids {
log::debug!("Remove dead invite: {}", id);
act.with_invite.remove(&id);
}
});
}
}
impl Handler<CreateInvite> for DispatcherActor {
type Result = ();
fn handle(&mut self, msg: CreateInvite, _ctx: &mut Self::Context) -> Self::Result {
let mut invite_code = rand_str(INVITE_CODE_LENGTH).to_uppercase();
while self.with_invite.contains_key(&invite_code) {
invite_code = rand_str(INVITE_CODE_LENGTH).to_uppercase();
}
log::debug!("Insert new invitation: {}", invite_code);
msg.1.do_send(ServerMessage::SetInviteCode {
code: invite_code.clone(),
});
msg.1.do_send(ServerMessage::WaitingForAnotherPlayer);
self.with_invite.insert(
invite_code,
PendingPlayer {
player: msg.1,
rules: msg.0,
},
);
}
}
impl Handler<AcceptInvite> for DispatcherActor {
type Result = ();
fn handle(&mut self, msg: AcceptInvite, _ctx: &mut Self::Context) -> Self::Result {
let entry = match self.with_invite.remove(&msg.0) {
None => {
msg.1.do_send(ServerMessage::InvalidInviteCode);
msg.1.do_send(CloseConnection);
return;
}
Some(e) => e,
};
let game = Game::new(entry.rules).start();
entry.player.do_send(SetGame(game.clone()));
msg.1.do_send(SetGame(game));
}
}

View File

@ -12,6 +12,7 @@ use crate::bots::linear_bot::LinearBot;
use crate::bots::random_bot::RandomBot; use crate::bots::random_bot::RandomBot;
use crate::bots::smart_bot::SmartBot; use crate::bots::smart_bot::SmartBot;
use crate::data::{BoatsLayout, BotType, Coordinates, CurrentGameStatus, FireResult, GameRules}; use crate::data::{BoatsLayout, BotType, Coordinates, CurrentGameStatus, FireResult, GameRules};
use crate::dispatcher_actor::{AcceptInvite, CreateInvite, DispatcherActor};
use crate::game::{AddPlayer, Game}; use crate::game::{AddPlayer, Game};
use crate::human_player::HumanPlayer; use crate::human_player::HumanPlayer;
@ -20,13 +21,12 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(10);
/// How long before lack of client response causes a timeout /// How long before lack of client response causes a timeout
const CLIENT_TIMEOUT: Duration = Duration::from_secs(120); const CLIENT_TIMEOUT: Duration = Duration::from_secs(120);
#[derive(Default, Debug)] #[derive(Debug)]
pub enum StartMode { pub enum StartMode {
Bot(GameRules), Bot(GameRules),
CreateInvite(GameRules),
#[default] AcceptInvite { code: String },
RandomHuman, // TODO : random human
//TODO : create invite
} }
#[derive(serde::Deserialize, serde::Serialize, Debug)] #[derive(serde::Deserialize, serde::Serialize, Debug)]
@ -45,6 +45,10 @@ pub enum ClientMessage {
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum ServerMessage { pub enum ServerMessage {
SetInviteCode {
code: String,
},
InvalidInviteCode,
WaitingForAnotherPlayer, WaitingForAnotherPlayer,
SetOpponentName { SetOpponentName {
name: String, name: String,
@ -85,23 +89,33 @@ pub enum ServerMessage {
OpponentReplacedByBot, OpponentReplacedByBot,
} }
#[derive(Message)]
#[rtype(result = "()")]
pub struct SetGame(pub Addr<Game>);
#[derive(Message)]
#[rtype(result = "()")]
pub struct CloseConnection;
pub struct HumanPlayerWS { pub struct HumanPlayerWS {
inner: Option<Arc<HumanPlayer>>, inner: Option<Arc<HumanPlayer>>,
pub start_mode: StartMode, pub start_mode: StartMode,
hb: Instant, hb: Instant,
} dispatcher: Addr<DispatcherActor>,
name: String,
impl Default for HumanPlayerWS {
fn default() -> Self {
Self {
inner: None,
start_mode: Default::default(),
hb: Instant::now(),
}
}
} }
impl HumanPlayerWS { impl HumanPlayerWS {
pub fn new(start_mode: StartMode, dispatcher: &Addr<DispatcherActor>, name: String) -> Self {
Self {
inner: None,
start_mode,
hb: Instant::now(),
dispatcher: dispatcher.clone(),
name,
}
}
/// helper method that sends ping to client every second. /// helper method that sends ping to client every second.
/// ///
/// also this method checks heartbeats from client /// also this method checks heartbeats from client
@ -158,7 +172,7 @@ impl Actor for HumanPlayerWS {
}; };
let player = Arc::new(HumanPlayer { let player = Arc::new(HumanPlayer {
name: "Human".to_string(), name: self.name.to_string(),
game: game.clone(), game: game.clone(),
player: ctx.address(), player: ctx.address(),
uuid: Uuid::new_v4(), uuid: Uuid::new_v4(),
@ -167,8 +181,15 @@ impl Actor for HumanPlayerWS {
game.do_send(AddPlayer(player)); game.do_send(AddPlayer(player));
} }
StartMode::RandomHuman => { StartMode::CreateInvite(rules) => {
unimplemented!(); log::info!("Create new play invite");
self.dispatcher
.do_send(CreateInvite(rules.clone(), ctx.address()));
}
StartMode::AcceptInvite { code } => {
log::info!("Accept play invite {}", code);
self.dispatcher
.do_send(AcceptInvite(code.clone(), ctx.address()));
} }
} }
} }
@ -223,3 +244,27 @@ impl Handler<ServerMessage> for HumanPlayerWS {
ctx.text(serde_json::to_string(&msg).unwrap()); ctx.text(serde_json::to_string(&msg).unwrap());
} }
} }
impl Handler<SetGame> for HumanPlayerWS {
type Result = ();
fn handle(&mut self, msg: SetGame, ctx: &mut Self::Context) -> Self::Result {
let game = msg.0;
let player = Arc::new(HumanPlayer {
name: self.name.clone(),
game: game.clone(),
player: ctx.address(),
uuid: Uuid::new_v4(),
});
self.inner = Some(player.clone());
game.do_send(AddPlayer(player));
}
}
impl Handler<CloseConnection> for HumanPlayerWS {
type Result = ();
fn handle(&mut self, _msg: CloseConnection, ctx: &mut Self::Context) -> Self::Result {
ctx.close(None)
}
}

View File

@ -4,9 +4,11 @@ pub mod args;
pub mod bots; pub mod bots;
pub mod consts; pub mod consts;
pub mod data; pub mod data;
pub mod dispatcher_actor;
pub mod game; pub mod game;
pub mod human_player; pub mod human_player;
pub mod human_player_ws; pub mod human_player_ws;
pub mod server; pub mod server;
#[cfg(test)] #[cfg(test)]
mod test; mod test;
pub mod utils;

View File

@ -1,9 +1,11 @@
use actix::{Actor, Addr};
use actix_cors::Cors; use actix_cors::Cors;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder}; use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web_actors::ws; use actix_web_actors::ws;
use crate::args::Args; use crate::args::Args;
use crate::data::{GameRules, PlayConfiguration}; use crate::data::{GameRules, PlayConfiguration};
use crate::dispatcher_actor::DispatcherActor;
use crate::human_player_ws::{HumanPlayerWS, StartMode}; use crate::human_player_ws::{HumanPlayerWS, StartMode};
/// The default '/' route /// The default '/' route
@ -26,23 +28,78 @@ async fn start_bot_play(
req: HttpRequest, req: HttpRequest,
stream: web::Payload, stream: web::Payload,
query: web::Query<GameRules>, query: web::Query<GameRules>,
dispatcher: web::Data<Addr<DispatcherActor>>,
) -> Result<HttpResponse, actix_web::Error> { ) -> Result<HttpResponse, actix_web::Error> {
let errors = query.0.get_errors(); let errors = query.0.get_errors();
if !errors.is_empty() { if !errors.is_empty() {
return Ok(HttpResponse::BadRequest().json(errors)); return Ok(HttpResponse::BadRequest().json(errors));
} }
let mut player_ws = HumanPlayerWS::default(); let player_ws = HumanPlayerWS::new(
player_ws.start_mode = StartMode::Bot(query.0.clone()); StartMode::Bot(query.0.clone()),
&dispatcher,
"Human".to_string(),
);
let resp = ws::start(player_ws, &req, stream); let resp = ws::start(player_ws, &req, stream);
log::info!("New bot play with configuration: {:?}", &query.0); log::info!("New bot play with configuration: {:?}", &query.0);
resp resp
} }
/// Start game by creating invite
async fn start_create_invite(
req: HttpRequest,
stream: web::Payload,
query: web::Query<GameRules>, // TODO : add player name to query
dispatcher: web::Data<Addr<DispatcherActor>>,
) -> Result<HttpResponse, actix_web::Error> {
let errors = query.0.get_errors();
if !errors.is_empty() {
return Ok(HttpResponse::BadRequest().json(errors));
}
let player_ws = HumanPlayerWS::new(
StartMode::CreateInvite(query.0.clone()),
&dispatcher,
"Human".to_string(),
);
let resp = ws::start(player_ws, &req, stream);
log::info!("New create invite play with configuration: {:?}", &query.0);
resp
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct AcceptInviteQuery {
pub code: String,
// TODO : add player name to query
}
/// Start game by creating invite
async fn start_accept_invite(
req: HttpRequest,
stream: web::Payload,
query: web::Query<AcceptInviteQuery>,
dispatcher: web::Data<Addr<DispatcherActor>>,
) -> Result<HttpResponse, actix_web::Error> {
let player_ws = HumanPlayerWS::new(
StartMode::AcceptInvite {
code: query.code.clone(),
},
&dispatcher,
"Human".to_string(),
);
let resp = ws::start(player_ws, &req, stream);
log::info!("New accept invite: {:?}", &query.0.code);
resp
}
pub async fn start_server(args: Args) -> std::io::Result<()> { pub async fn start_server(args: Args) -> std::io::Result<()> {
let args_clone = args.clone(); let args_clone = args.clone();
let dispatcher_actor = DispatcherActor::default().start();
HttpServer::new(move || { HttpServer::new(move || {
let mut cors = Cors::default(); let mut cors = Cors::default();
match args_clone.cors.as_deref() { match args_clone.cors.as_deref() {
@ -52,10 +109,12 @@ pub async fn start_server(args: Args) -> std::io::Result<()> {
} }
App::new() App::new()
.app_data(web::Data::new(dispatcher_actor.clone()))
.wrap(cors) .wrap(cors)
.route("/config", web::get().to(game_configuration)) .route("/config", web::get().to(game_configuration))
.route("/play/bot", web::get().to(start_bot_play)) .route("/play/bot", web::get().to(start_bot_play))
// TODO : create & accept invite .route("/play/create_invite", web::get().to(start_create_invite))
.route("/play/accept_invite", web::get().to(start_accept_invite))
// TODO : join random // TODO : join random
.route("/", web::get().to(index)) .route("/", web::get().to(index))
.route("{tail:.*}", web::get().to(not_found)) .route("{tail:.*}", web::get().to(not_found))

View File

@ -6,6 +6,17 @@ use tokio_tungstenite::tungstenite::Message;
use crate::data::{BoatsLayout, GameRules}; use crate::data::{BoatsLayout, GameRules};
use crate::human_player_ws::{ClientMessage, ServerMessage}; use crate::human_player_ws::{ClientMessage, ServerMessage};
use crate::server::AcceptInviteQuery;
#[derive(Default)]
pub enum RunMode {
#[default]
AgainstBot,
CreateInvite,
AcceptInvite {
code: String,
},
}
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum ClientEndResult { pub enum ClientEndResult {
@ -16,10 +27,12 @@ pub enum ClientEndResult {
InvalidBoatsLayout, InvalidBoatsLayout,
OpponentRejectedRematch, OpponentRejectedRematch,
OpponentLeftGame, OpponentLeftGame,
InvalidInviteCode,
} }
pub struct BotClient { pub struct BotClient {
server: String, server: String,
run_mode: RunMode,
requested_rules: GameRules, requested_rules: GameRules,
layout: Option<BoatsLayout>, layout: Option<BoatsLayout>,
number_plays: usize, number_plays: usize,
@ -33,6 +46,7 @@ impl BotClient {
{ {
Self { Self {
server: server.to_string(), server: server.to_string(),
run_mode: RunMode::default(),
requested_rules: GameRules::random_players_rules(), requested_rules: GameRules::random_players_rules(),
layout: None, layout: None,
number_plays: 1, number_plays: 1,
@ -40,6 +54,11 @@ impl BotClient {
} }
} }
pub fn with_run_mode(mut self, mode: RunMode) -> Self {
self.run_mode = mode;
self
}
pub fn with_rules(mut self, rules: GameRules) -> Self { pub fn with_rules(mut self, rules: GameRules) -> Self {
self.requested_rules = rules; self.requested_rules = rules;
self self
@ -68,11 +87,32 @@ impl BotClient {
let mut number_victories = 0; let mut number_victories = 0;
let mut number_defeats = 0; let mut number_defeats = 0;
let url = format!( let url = match &self.run_mode {
"{}/play/bot?{}", RunMode::AgainstBot => {
self.server.replace("http", "ws"), format!(
serde_urlencoded::to_string(&self.requested_rules).unwrap() "{}/play/bot?{}",
); self.server.replace("http", "ws"),
serde_urlencoded::to_string(&self.requested_rules).unwrap()
)
}
RunMode::CreateInvite => {
format!(
"{}/play/create_invite?{}",
self.server.replace("http", "ws"),
serde_urlencoded::to_string(&self.requested_rules).unwrap()
)
}
RunMode::AcceptInvite { code } => {
format!(
"{}/play/accept_invite?{}",
self.server.replace("http", "ws"),
serde_urlencoded::to_string(&AcceptInviteQuery {
code: code.to_string()
})
.unwrap()
)
}
};
log::debug!("Connecting to {}...", url); log::debug!("Connecting to {}...", url);
let (mut socket, _) = match tokio_tungstenite::connect_async(url).await { let (mut socket, _) = match tokio_tungstenite::connect_async(url).await {
Ok(s) => s, Ok(s) => s,
@ -120,6 +160,13 @@ impl BotClient {
ServerMessage::WaitingForAnotherPlayer => { ServerMessage::WaitingForAnotherPlayer => {
log::debug!("Waiting for other player...") log::debug!("Waiting for other player...")
} }
ServerMessage::SetInviteCode { code } => {
log::debug!("Got invite code: {}", code);
}
ServerMessage::InvalidInviteCode => {
log::debug!("Got invalid invite code!");
return Ok(ClientEndResult::InvalidInviteCode);
}
ServerMessage::QueryBoatsLayout { rules } => { ServerMessage::QueryBoatsLayout { rules } => {
assert_eq!(&rules, &self.requested_rules); assert_eq!(&rules, &self.requested_rules);
log::debug!("Server requested boats layout"); log::debug!("Server requested boats layout");

View File

@ -0,0 +1,84 @@
use std::error::Error;
use tokio::sync::mpsc;
use tokio::sync::mpsc::Sender;
use tokio::task;
use crate::args::Args;
use crate::human_player_ws::ServerMessage;
use crate::server::start_server;
use crate::test::bot_client::{ClientEndResult, RunMode};
use crate::test::network_utils::wait_for_port;
use crate::test::{bot_client, TestPort};
#[tokio::test]
async fn invalid_accept_code() {
let _ = env_logger::builder().is_test(true).try_init();
let local_set = task::LocalSet::new();
local_set
.run_until(async move {
task::spawn_local(start_server(Args::for_test(
TestPort::InviteModeInvalidCode,
)));
wait_for_port(TestPort::InviteModeInvalidCode.port()).await;
let res = bot_client::BotClient::new(TestPort::InviteModeInvalidCode.as_url())
.with_run_mode(RunMode::AcceptInvite {
code: "BadCode".to_string(),
})
.run_client()
.await
.unwrap();
assert_eq!(res, ClientEndResult::InvalidInviteCode)
})
.await;
}
async fn run_other_invite_side(
sender: Sender<Result<ClientEndResult, Box<dyn Error>>>,
port: TestPort,
code: String,
) {
let res = bot_client::BotClient::new(port.as_url())
.with_run_mode(RunMode::AcceptInvite { code })
.run_client()
.await;
sender.send(res).await.unwrap()
}
#[tokio::test]
async fn full_game() {
let _ = env_logger::builder().is_test(true).try_init();
let local_set = task::LocalSet::new();
local_set
.run_until(async move {
task::spawn_local(start_server(Args::for_test(TestPort::InviteModeFullGame)));
wait_for_port(TestPort::InviteModeFullGame.port()).await;
let (sender, mut receiver) = mpsc::channel(1);
let res = bot_client::BotClient::new(TestPort::InviteModeFullGame.as_url())
.with_run_mode(RunMode::CreateInvite)
.with_server_msg_callback(move |msg| {
if let ServerMessage::SetInviteCode { code } = msg {
task::spawn_local(run_other_invite_side(
sender.clone(),
TestPort::InviteModeFullGame,
code.clone(),
));
}
})
.run_client()
.await
.unwrap();
assert!(matches!(res, ClientEndResult::Finished { .. }));
let other_side_res = receiver.recv().await.unwrap().unwrap();
assert!(matches!(other_side_res, ClientEndResult::Finished { .. }));
})
.await;
}

View File

@ -13,6 +13,8 @@ enum TestPort {
LinearBotFullGame, LinearBotFullGame,
LinearBotNoReplayOnHit, LinearBotNoReplayOnHit,
IntermediateBotFullGame, IntermediateBotFullGame,
InviteModeInvalidCode,
InviteModeFullGame,
} }
impl TestPort { impl TestPort {
@ -40,5 +42,6 @@ mod bot_client_bot_intermediate_play;
mod bot_client_bot_linear_play; mod bot_client_bot_linear_play;
mod bot_client_bot_random_play; mod bot_client_bot_random_play;
mod bot_client_bot_smart_play; mod bot_client_bot_smart_play;
mod invite_mode;
mod network_utils; mod network_utils;
mod play_utils; mod play_utils;

View File

@ -0,0 +1,19 @@
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
/// Generate a random string of a given size
///
/// ```
/// use sea_battle_backend::utils::rand_str;
///
/// let size = 10;
/// let rand = rand_str(size);
/// assert_eq!(size, rand.len());
/// ```
pub fn rand_str(len: usize) -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.map(char::from)
.take(len)
.collect()
}