Send ClientInfo to the server
This commit is contained in:
10
Cargo.toml
10
Cargo.toml
@ -1,13 +1,15 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "snakebot_rust"
|
name = "snakebot_rust"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
authors = ["Martin Barksten <martin.barksten@gmail.com>"]
|
authors = ["Martin Barksten <martin.barksten@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ws = "0.5.2"
|
ws = "0.5.2"
|
||||||
serde = "0.8"
|
serde = "= 0.8.8"
|
||||||
serde_json = "0.8"
|
serde_json = "= 0.8.0"
|
||||||
serde_macros = "0.8"
|
serde_macros = "= 0.8.2"
|
||||||
quick-error = "1.1.0"
|
quick-error = "1.1.0"
|
||||||
log = "0.3.6"
|
log = "0.3.6"
|
||||||
log4rs = { version = "0.4.6", features = ["toml"] }
|
log4rs = { version = "0.4.6", features = ["toml"] }
|
||||||
|
target_info = "0.1.0"
|
||||||
|
rustc_version = "0.1.0"
|
||||||
|
|||||||
@ -20,3 +20,8 @@ appenders = [ "console" ]
|
|||||||
level = "debug"
|
level = "debug"
|
||||||
appenders = [ "client" ]
|
appenders = [ "client" ]
|
||||||
additive = false
|
additive = false
|
||||||
|
|
||||||
|
[loggers.snake]
|
||||||
|
level = "debug"
|
||||||
|
appenders = [ "snake" ]
|
||||||
|
additive = false
|
||||||
|
|||||||
12
src/main.rs
12
src/main.rs
@ -7,6 +7,8 @@ extern crate serde;
|
|||||||
#[macro_use] extern crate quick_error;
|
#[macro_use] extern crate quick_error;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate log4rs;
|
extern crate log4rs;
|
||||||
|
extern crate target_info;
|
||||||
|
extern crate rustc_version;
|
||||||
|
|
||||||
mod structs;
|
mod structs;
|
||||||
mod messages;
|
mod messages;
|
||||||
@ -123,8 +125,16 @@ impl ws::Handler for Client {
|
|||||||
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
|
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
|
||||||
debug!(target: LOG_TARGET, "Connection to Websocket opened");
|
debug!(target: LOG_TARGET, "Connection to Websocket opened");
|
||||||
|
|
||||||
let parse_msg = messages::create_play_registration_msg(self.snake.get_name());
|
let client_info = messages::create_client_info_msg();
|
||||||
|
if let Ok(message) = client_info {
|
||||||
|
info!(target: LOG_TARGET, "Sending client info to server: {:?}", message);
|
||||||
|
try!(self.out.send(message));
|
||||||
|
} else {
|
||||||
|
error!(target: LOG_TARGET, "Unable to create client info message {:?}", client_info);
|
||||||
|
try!(self.out.close(ws::CloseCode::Error));
|
||||||
|
}
|
||||||
|
|
||||||
|
let parse_msg = messages::create_play_registration_msg(self.snake.get_name());
|
||||||
if let Ok(response) = parse_msg {
|
if let Ok(response) = parse_msg {
|
||||||
info!(target: LOG_TARGET, "Registering player with message: {:?}", response);
|
info!(target: LOG_TARGET, "Registering player with message: {:?}", response);
|
||||||
self.out.send(response)
|
self.out.send(response)
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
use structs;
|
use structs;
|
||||||
use serde_json::{ from_str, to_string, Error };
|
use serde_json::{ from_str, to_string, Error };
|
||||||
|
use target_info::Target;
|
||||||
|
use rustc_version;
|
||||||
|
|
||||||
// Inbound
|
// Inbound
|
||||||
pub const GAME_ENDED: &'static str =
|
pub const GAME_ENDED: &'static str =
|
||||||
@ -30,6 +32,8 @@ const REGISTER_MOVE: &'static str =
|
|||||||
"se.cygni.snake.api.request.RegisterMove";
|
"se.cygni.snake.api.request.RegisterMove";
|
||||||
const HEART_BEAT_REQUEST: &'static str =
|
const HEART_BEAT_REQUEST: &'static str =
|
||||||
"se.cygni.snake.api.request.HeartBeatRequest";
|
"se.cygni.snake.api.request.HeartBeatRequest";
|
||||||
|
const CLIENT_INFO: &'static str =
|
||||||
|
"se.cygni.snake.api.request.ClientInfo";
|
||||||
|
|
||||||
pub enum Inbound {
|
pub enum Inbound {
|
||||||
GameEnded(structs::GameEnded),
|
GameEnded(structs::GameEnded),
|
||||||
@ -103,6 +107,17 @@ pub fn create_heart_beat_msg(id: String) -> Result<String, Error> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_client_info_msg() -> Result<String, Error> {
|
||||||
|
to_string(&structs::ClientInfo {
|
||||||
|
type_: String::from(CLIENT_INFO),
|
||||||
|
language: String::from("rust"),
|
||||||
|
languageVersion: format!("{}", rustc_version::version()),
|
||||||
|
operatingSystem: String::from(Target::os()),
|
||||||
|
operatingSystemVersion: String::from(""),
|
||||||
|
clientVersion: String::from(option_env!("CARGO_PKG_VERSION").unwrap_or(""))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn default_gamesettings() -> structs::GameSettings {
|
pub fn default_gamesettings() -> structs::GameSettings {
|
||||||
structs::GameSettings {
|
structs::GameSettings {
|
||||||
maxNoofPlayers: 5,
|
maxNoofPlayers: 5,
|
||||||
|
|||||||
@ -3,7 +3,7 @@ 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 = "tournament";
|
pub const TRAINING_VENUE: &'static str = "training";
|
||||||
|
|
||||||
pub struct Snake;
|
pub struct Snake;
|
||||||
|
|
||||||
|
|||||||
@ -28,6 +28,17 @@ pub struct PlayRegistration {
|
|||||||
pub gameSettings: GameSettings,
|
pub gameSettings: GameSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct ClientInfo {
|
||||||
|
#[serde(rename="type")]
|
||||||
|
pub type_: String,
|
||||||
|
pub language: String,
|
||||||
|
pub languageVersion: String,
|
||||||
|
pub operatingSystem: String,
|
||||||
|
pub operatingSystemVersion: String,
|
||||||
|
pub clientVersion: String
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct RegisterMove {
|
pub struct RegisterMove {
|
||||||
#[serde(rename="type")]
|
#[serde(rename="type")]
|
||||||
|
|||||||
Reference in New Issue
Block a user