Handle tournament mode correctly
And close websocket on game end in training mode as well
This commit is contained in:
21
src/main.rs
21
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<ws::Sender>,
|
||||
snake: Snake,
|
||||
out_sender: mpsc::Sender<Arc<ws::Sender>>,
|
||||
id_sender: mpsc::Sender<String>,
|
||||
id_sender: mpsc::Sender<String>
|
||||
}
|
||||
|
||||
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<String>,
|
||||
out_sender: mpsc::Sender<Arc<ws::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);
|
||||
|
||||
@ -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<String>
|
||||
}
|
||||
|
||||
#[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<i32>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Map {
|
||||
#[serde(rename="type")]
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user