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 HOST: &'static str = "snake.cygni.se";
|
||||||
const PORT: i32 = 80;
|
const PORT: i32 = 80;
|
||||||
const MODE: &'static str = "training";
|
|
||||||
|
|
||||||
const HEART_BEAT_S: u64 = 20;
|
const HEART_BEAT_S: u64 = 20;
|
||||||
const LOG_TARGET: &'static str = "client";
|
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 {
|
struct Client {
|
||||||
out: Arc<ws::Sender>,
|
out: Arc<ws::Sender>,
|
||||||
snake: Snake,
|
snake: Snake,
|
||||||
out_sender: mpsc::Sender<Arc<ws::Sender>>,
|
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> {
|
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) {
|
if msg.contains(messages::GAME_ENDED) {
|
||||||
let json_msg: messages::GameEnded = try!(serde_json::from_str(msg));
|
let json_msg: messages::GameEnded = try!(serde_json::from_str(msg));
|
||||||
snake.on_game_ended(&json_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) {
|
} else if msg.contains(messages::MAP_UPDATE) {
|
||||||
let json_msg: messages::MapUpdate = try!(serde_json::from_str(msg));
|
let json_msg: messages::MapUpdate = try!(serde_json::from_str(msg));
|
||||||
let direction = snake.get_next_move(&json_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>,
|
fn start_websocket_thread(id_sender: mpsc::Sender<String>,
|
||||||
out_sender: mpsc::Sender<Arc<ws::Sender>>) -> thread::JoinHandle<()> {
|
out_sender: mpsc::Sender<Arc<ws::Sender>>) -> thread::JoinHandle<()> {
|
||||||
|
|
||||||
thread::spawn(move || {
|
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);
|
info!(target: LOG_TARGET, "Connecting to {:?}", connection_url);
|
||||||
let result = ws::connect(connection_url, |out| {
|
let result = ws::connect(connection_url, |out| {
|
||||||
Client {
|
Client {
|
||||||
out: Arc::from(out),
|
out: Arc::from(out),
|
||||||
snake: snake::Snake,
|
snake: snake::Snake,
|
||||||
out_sender: out_sender.clone(),
|
out_sender: out_sender.clone(),
|
||||||
id_sender: id_sender.clone(),
|
id_sender: id_sender.clone()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
debug!(target: LOG_TARGET, "Websocket is done, result {:?}", result);
|
debug!(target: LOG_TARGET, "Websocket is done, result {:?}", result);
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
//inbound messages
|
//inbound messages
|
||||||
pub const GAME_ENDED: &'static str =
|
pub const GAME_ENDED: &'static str =
|
||||||
"se.cygni.snake.api.event.GameEndedEvent";
|
"se.cygni.snake.api.event.GameEndedEvent";
|
||||||
|
pub const TOURNAMENT_ENDED: &'static str =
|
||||||
|
"se.cygni.snake.api.event.TournamentEndedEvent";
|
||||||
pub const MAP_UPDATE: &'static str =
|
pub const MAP_UPDATE: &'static str =
|
||||||
"se.cygni.snake.api.event.MapUpdateEvent";
|
"se.cygni.snake.api.event.MapUpdateEvent";
|
||||||
pub const SNAKE_DEAD: &'static str =
|
pub const SNAKE_DEAD: &'static str =
|
||||||
@ -12,17 +14,18 @@ pub const PLAYER_REGISTERED: &'static str =
|
|||||||
"se.cygni.snake.api.response.PlayerRegistered";
|
"se.cygni.snake.api.response.PlayerRegistered";
|
||||||
pub const INVALID_PLAYER_NAME: &'static str =
|
pub const INVALID_PLAYER_NAME: &'static str =
|
||||||
"se.cygni.snake.api.exception.InvalidPlayerName";
|
"se.cygni.snake.api.exception.InvalidPlayerName";
|
||||||
pub const HEART_BEAT_REQUEST: &'static str =
|
pub const HEART_BEAT_RESPONSE: &'static str =
|
||||||
"se.cygni.snake.api.request.HeartBeatRequest";
|
"se.cygni.snake.api.request.HeartBeatResponse";
|
||||||
|
|
||||||
//outbound messages
|
//outbound messages
|
||||||
pub const REGISTER_PLAYER_MESSAGE_TYPE: &'static str =
|
pub const REGISTER_PLAYER_MESSAGE_TYPE: &'static str =
|
||||||
"se.cygni.snake.api.request.RegisterPlayer";
|
"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 =
|
pub const REGISTER_MOVE: &'static str =
|
||||||
"se.cygni.snake.api.request.RegisterMove";
|
"se.cygni.snake.api.request.RegisterMove";
|
||||||
pub const HEART_BEAT_RESPONSE: &'static str =
|
pub const HEART_BEAT_REQUEST: &'static str =
|
||||||
"se.cygni.snake.api.request.HeartBeatResponse";
|
"se.cygni.snake.api.request.HeartBeatRequest";
|
||||||
|
|
||||||
// Outbound messages
|
// Outbound messages
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@ -153,6 +156,18 @@ pub struct HeartBeatResponse {
|
|||||||
pub receivingPlayerId: Option<String>
|
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)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
#[serde(rename="type")]
|
#[serde(rename="type")]
|
||||||
|
|||||||
@ -3,9 +3,14 @@ use maputil::{ Direction };
|
|||||||
use util::{ translate_positions };
|
use util::{ translate_positions };
|
||||||
|
|
||||||
const LOG_TARGET: &'static str = "snake";
|
const LOG_TARGET: &'static str = "snake";
|
||||||
|
pub const TRAINING_VENUE: &'static str = "TRAINING";
|
||||||
|
|
||||||
pub struct Snake;
|
pub struct Snake;
|
||||||
|
|
||||||
|
pub fn get_venue() -> String {
|
||||||
|
String::from(TRAINING_VENUE)
|
||||||
|
}
|
||||||
|
|
||||||
impl Snake {
|
impl Snake {
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
String::from("rusty-snake")
|
String::from("rusty-snake")
|
||||||
@ -42,6 +47,10 @@ impl Snake {
|
|||||||
info!(target: LOG_TARGET, "Game ended, the winner is: {:?}", msg.playerWinnerId);
|
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) {
|
pub fn on_snake_dead(&self, msg: &messages::SnakeDead) {
|
||||||
info!(target: LOG_TARGET, "The snake died, reason was: {:?}", msg.deathReason);
|
info!(target: LOG_TARGET, "The snake died, reason was: {:?}", msg.deathReason);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user