From bd42296f15437aa53d6141256fcd7725ee64183b Mon Sep 17 00:00:00 2001 From: Martin Barksten Date: Tue, 7 Jun 2016 22:27:26 +0200 Subject: [PATCH] Handle tournament mode correctly And close websocket on game end in training mode as well --- src/main.rs | 21 +++++++++++++++++---- src/messages.rs | 25 ++++++++++++++++++++----- src/snake.rs | 9 +++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index c2031a0..bbfd6ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,6 @@ use std::sync::Arc; const HOST: &'static str = "snake.cygni.se"; const PORT: i32 = 80; -const MODE: &'static str = "training"; const HEART_BEAT_S: u64 = 20; const LOG_TARGET: &'static str = "client"; @@ -48,11 +47,15 @@ quick_error! { } } +pub fn is_training_mode() -> bool{ + snake::TRAINING_VENUE == snake::get_venue() +} + struct Client { out: Arc, snake: Snake, out_sender: mpsc::Sender>, - id_sender: mpsc::Sender, + id_sender: mpsc::Sender } fn route_msg(client: &mut Client, msg: &String) -> Result<(), ClientError> { @@ -61,6 +64,14 @@ fn route_msg(client: &mut Client, msg: &String) -> Result<(), ClientError> { if msg.contains(messages::GAME_ENDED) { let json_msg: messages::GameEnded = try!(serde_json::from_str(msg)); snake.on_game_ended(&json_msg); + + if is_training_mode() { + try!(client.out.close(ws::CloseCode::Normal)); + } + } else if msg.contains(messages::TOURNAMENT_ENDED) { + let json_msg: messages::TournamentEnded = try!(serde_json::from_str(msg)); + snake.on_tournament_ended(&json_msg); + try!(client.out.close(ws::CloseCode::Normal)); } else if msg.contains(messages::MAP_UPDATE) { let json_msg: messages::MapUpdate = try!(serde_json::from_str(msg)); let direction = snake.get_next_move(&json_msg); @@ -145,15 +156,17 @@ impl ws::Handler for Client { fn start_websocket_thread(id_sender: mpsc::Sender, out_sender: mpsc::Sender>) -> thread::JoinHandle<()> { + thread::spawn(move || { - let connection_url = format!("ws://{}:{}/{}", HOST, PORT, MODE); + let connection_url = format!("ws://{}:{}/{}", HOST, PORT, snake::get_venue()); + info!(target: LOG_TARGET, "Connecting to {:?}", connection_url); let result = ws::connect(connection_url, |out| { Client { out: Arc::from(out), snake: snake::Snake, out_sender: out_sender.clone(), - id_sender: id_sender.clone(), + id_sender: id_sender.clone() } }); debug!(target: LOG_TARGET, "Websocket is done, result {:?}", result); diff --git a/src/messages.rs b/src/messages.rs index 2a535fa..08096bb 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -2,6 +2,8 @@ //inbound messages pub const GAME_ENDED: &'static str = "se.cygni.snake.api.event.GameEndedEvent"; +pub const TOURNAMENT_ENDED: &'static str = + "se.cygni.snake.api.event.TournamentEndedEvent"; pub const MAP_UPDATE: &'static str = "se.cygni.snake.api.event.MapUpdateEvent"; pub const SNAKE_DEAD: &'static str = @@ -12,17 +14,18 @@ pub const PLAYER_REGISTERED: &'static str = "se.cygni.snake.api.response.PlayerRegistered"; pub const INVALID_PLAYER_NAME: &'static str = "se.cygni.snake.api.exception.InvalidPlayerName"; -pub const HEART_BEAT_REQUEST: &'static str = - "se.cygni.snake.api.request.HeartBeatRequest"; +pub const HEART_BEAT_RESPONSE: &'static str = + "se.cygni.snake.api.request.HeartBeatResponse"; //outbound messages pub const REGISTER_PLAYER_MESSAGE_TYPE: &'static str = "se.cygni.snake.api.request.RegisterPlayer"; -pub const START_GAME: &'static str = "se.cygni.snake.api.request.StartGame"; +pub const START_GAME: &'static str = + "se.cygni.snake.api.request.StartGame"; pub const REGISTER_MOVE: &'static str = "se.cygni.snake.api.request.RegisterMove"; -pub const HEART_BEAT_RESPONSE: &'static str = - "se.cygni.snake.api.request.HeartBeatResponse"; +pub const HEART_BEAT_REQUEST: &'static str = + "se.cygni.snake.api.request.HeartBeatRequest"; // Outbound messages #[derive(Serialize, Deserialize, Debug)] @@ -153,6 +156,18 @@ pub struct HeartBeatResponse { pub receivingPlayerId: Option } +#[derive(Serialize, Deserialize, Debug)] +pub struct TournamentEnded { + #[serde(rename="type")] + pub type_: String, + pub playerWinnerId: String, + pub gameId: String, + pub gameResult: String, + pub tournamentName: String, + pub tournamentId: String, + pub gameTick: Option +} + #[derive(Serialize, Deserialize, Debug)] pub struct Map { #[serde(rename="type")] diff --git a/src/snake.rs b/src/snake.rs index f3fa150..25eabf7 100644 --- a/src/snake.rs +++ b/src/snake.rs @@ -3,9 +3,14 @@ use maputil::{ Direction }; use util::{ translate_positions }; const LOG_TARGET: &'static str = "snake"; +pub const TRAINING_VENUE: &'static str = "TRAINING"; pub struct Snake; +pub fn get_venue() -> String { + String::from(TRAINING_VENUE) +} + impl Snake { pub fn get_name(&self) -> String { String::from("rusty-snake") @@ -42,6 +47,10 @@ impl Snake { info!(target: LOG_TARGET, "Game ended, the winner is: {:?}", msg.playerWinnerId); } + pub fn on_tournament_ended(&self, msg: &messages::TournamentEnded) { + info!(target: LOG_TARGET, "Game ended, the winner is: {:?}", msg.playerWinnerId); + } + pub fn on_snake_dead(&self, msg: &messages::SnakeDead) { info!(target: LOG_TARGET, "The snake died, reason was: {:?}", msg.deathReason); }