use tungstenite::Message; use tungstenite::protocol::WebSocket; use tungstenite::Message::Binary; use postgres::transaction::Transaction; use std::net::{TcpStream}; // demo use std::iter; use rand::{thread_rng, Rng}; use rand::distributions::{Alphanumeric}; use serde_cbor::{from_slice, to_vec}; use uuid::Uuid; use failure::Error; use failure::err_msg; use net::Db; use cryp::{Cryp, cryp_spawn, cryp_learn, cryp_forget, cryp_unspec}; use game::{Game, game_state, game_pve, game_pvp, game_join, game_joinable_list, game_skill}; use account::{Account, account_create, account_login, account_from_token, account_cryps, account_zone}; use skill::{Skill}; use zone::{Zone, zone_create, zone_join, zone_close}; use spec::{Spec}; use vbox::{Vbox, vbox_state, vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_drop}; pub struct Rpc; impl Rpc { pub fn receive(&self, msg: Message, db: &Db, client: &mut WebSocket) -> Result { // consume the ws data into bytes let data = msg.into_data(); // cast the msg to this type to receive method name match from_slice::(&data) { Ok(v) => { let mut tx = db.transaction()?; let account: Option = match v.token { Some(t) => Some(account_from_token(t, &mut tx)?), None => None, }; // check the method // if no auth required match v.method.as_ref() { "account_create" => (), "account_login" => (), "account_demo" => (), _ => match account { Some(_) => (), None => return Err(err_msg("auth required")), }, }; // now we have the method name // match on that to determine what fn to call let response = match v.method.as_ref() { // no auth methods "account_create" => Rpc::account_create(data, &mut tx, client), "account_login" => Rpc::account_login(data, &mut tx, client), "account_demo" => Rpc::account_demo(data, &mut tx, client), // auth methods "cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account.unwrap(), client), "cryp_learn" => Rpc::cryp_learn(data, &mut tx, account.unwrap(), client), "cryp_forget" => Rpc::cryp_forget(data, &mut tx, account.unwrap(), client), "cryp_unspec" => Rpc::cryp_unspec(data, &mut tx, account.unwrap(), client), "game_state" => Rpc::game_state(data, &mut tx, account.unwrap(), client), "game_pve" => Rpc::game_pve(data, &mut tx, account.unwrap(), client), "game_pvp" => Rpc::game_pvp(data, &mut tx, account.unwrap(), client), "game_join" => Rpc::game_join(data, &mut tx, account.unwrap(), client), "game_joinable_list" => Rpc::game_joinable_list(data, &mut tx, account.unwrap(), client), "game_skill" => Rpc::game_skill(data, &mut tx, account.unwrap(), client), // "game_target" => Rpc::game_target(data, &mut tx, account.unwrap(), client), "zone_create" => Rpc::zone_create(data, &mut tx, account.unwrap(), client), "zone_join" => Rpc::zone_join(data, &mut tx, account.unwrap(), client), "zone_close" => Rpc::zone_close(data, &mut tx, account.unwrap(), client), "account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client), "account_zone" => Rpc::account_zone(data, &mut tx, account.unwrap(), client), "vbox_state" => Rpc::vbox_state(data, &mut tx, account.unwrap(), client), "vbox_accept" => Rpc::vbox_accept(data, &mut tx, account.unwrap(), client), "vbox_apply" => Rpc::vbox_apply(data, &mut tx, account.unwrap(), client), "vbox_drop" => Rpc::vbox_drop(data, &mut tx, account.unwrap(), client), "vbox_combine" => Rpc::vbox_combine(data, &mut tx, account.unwrap(), client), "vbox_discard" => Rpc::vbox_discard(data, &mut tx, account.unwrap(), client), _ => Err(format_err!("unknown method - {:?}", v.method)), }; tx.commit()?; return response; }, Err(_e) => Err(err_msg("invalid message")), } } fn send_msg(client: &mut WebSocket, msg: RpcResponse) -> Result<(), Error> { let bytes = to_vec(&msg)?; match client.write_message(Binary(bytes)) { Ok(()) => Ok(()), Err(e) => Err(err_msg(e)) } } fn game_state(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), params: RpcResult::GameState(game_state(msg.params, tx, &account)?) }; return Ok(game_response); } fn game_pve(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), params: RpcResult::GameState(game_pve(msg.params, tx, &account)?) }; Rpc::send_msg(client, RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) })?; return Ok(game_response); } fn game_pvp(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), params: RpcResult::GameState(game_pvp(msg.params, tx, &account)?) }; return Ok(game_response); } fn game_join(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), params: RpcResult::GameState(game_join(msg.params, tx, &account)?) }; return Ok(game_response); } fn game_joinable_list(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { // let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_list = RpcResponse { method: "game_joinable_list".to_string(), params: RpcResult::GameJoinableList(game_joinable_list(tx, &account)?) }; return Ok(game_list); } fn game_skill(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), params: RpcResult::GameState(game_skill(msg.params, tx, &account)?) }; return Ok(game_response); } // fn game_target(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { // let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; // let game_response = RpcResponse { // method: "game_state".to_string(), // params: RpcResult::GameState(game_target(msg.params, tx, &account)?) // }; // return Ok(game_response); // } fn cryp_spawn(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_spawn".to_string(), params: RpcResult::CrypSpawn(cryp_spawn(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } fn cryp_learn(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_learn".to_string(), params: RpcResult::CrypLearn(cryp_learn(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } fn cryp_forget(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_forget".to_string(), params: RpcResult::CrypForget(cryp_forget(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } fn cryp_unspec(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_unspec".to_string(), params: RpcResult::CrypUnspec(cryp_unspec(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } fn account_create(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, params: RpcResult::Account(account_create(v.params, tx)?) }), Err(_e) => Err(err_msg("invalid params")), } } fn account_login(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, params: RpcResult::Account(account_login(v.params, tx)?) }), Err(_e) => Err(err_msg("invalid params")), } } fn account_demo(_data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { let mut rng = thread_rng(); let acc_name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect(); let account = account_create(AccountCreateParams { name: acc_name, password: "grepgrepgrep".to_string() }, tx)?; let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect(); let cryp = cryp_spawn(CrypSpawnParams { name }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Block }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Stun }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Throw }, tx, &account)?; let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect(); let cryp = cryp_spawn(CrypSpawnParams { name }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Decay }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Blast }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Drain }, tx, &account)?; let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect(); let cryp = cryp_spawn(CrypSpawnParams { name }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Triage }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Heal }, tx, &account)?; cryp_learn(CrypLearnParams { id: cryp.id, skill: Skill::Purify }, tx, &account)?; let res = RpcResponse { method: "account_create".to_string(), params: RpcResult::Account(account), }; return Ok(res); } fn account_cryps(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { Ok(RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) }) } fn account_zone(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { Ok(RpcResponse { method: "zone_state".to_string(), params: RpcResult::ZoneState(account_zone(tx, &account)?) }) } fn zone_create(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { // let _msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "zone_state".to_string(), params: RpcResult::ZoneState(zone_create(tx, &account)?) }; return Ok(response); } fn zone_join(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "game_state".to_string(), params: RpcResult::GameState(zone_join(msg.params, tx, &account)?) }; return Ok(response); } fn zone_close(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "zone_close".to_string(), params: RpcResult::ZoneClose(zone_close(msg.params, tx, &account)?) }; return Ok(response); } fn vbox_state(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), params: RpcResult::VboxState(vbox_state(msg.params, tx, &account)?) }; return Ok(response); } fn vbox_accept(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), params: RpcResult::VboxState(vbox_accept(msg.params, tx, &account)?) }; return Ok(response); } fn vbox_discard(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), params: RpcResult::VboxState(vbox_discard(msg.params, tx, &account)?) }; return Ok(response); } fn vbox_combine(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), params: RpcResult::VboxState(vbox_combine(msg.params, tx, &account)?) }; return Ok(response); } fn vbox_apply(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), params: RpcResult::VboxState(vbox_apply(msg.params, tx, &account)?) }; Rpc::send_msg(client, RpcResponse { method: "account_cryps".to_string(), params: RpcResult::CrypList(account_cryps(tx, &account)?) })?; return Ok(response); } fn vbox_drop(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), params: RpcResult::VboxState(vbox_drop(msg.params, tx, &account)?) }; return Ok(response); } } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct RpcResponse { method: String, params: RpcResult, } #[derive(Debug,Clone,Serialize,Deserialize)] pub enum RpcResult { CrypSpawn(Cryp), CrypForget(Cryp), CrypLearn(Cryp), CrypUnspec(Cryp), Account(Account), CrypList(Vec), GameState(Game), GameJoinableList(Vec), ZoneState(Zone), ZoneClose(()), VboxState(Vbox), } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct RpcMessage { method: String, token: Option, } #[derive(Debug,Clone,Serialize,Deserialize)] struct CrypSpawnMsg { method: String, params: CrypSpawnParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct CrypSpawnParams { pub name: String, } #[derive(Debug,Clone,Serialize,Deserialize)] struct CrypLearnMsg { method: String, params: CrypLearnParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct CrypLearnParams { pub id: Uuid, pub skill: Skill, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct CrypForgetParams { pub id: Uuid, pub skill: Skill, } #[derive(Debug,Clone,Serialize,Deserialize)] struct CrypForgetMsg { method: String, params: CrypForgetParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct CrypUnspecParams { pub id: Uuid, pub spec: Spec, } #[derive(Debug,Clone,Serialize,Deserialize)] struct CrypUnspecMsg { method: String, params: CrypUnspecParams, } #[derive(Debug,Clone,Serialize,Deserialize)] struct GameStateMsg { method: String, params: GameStateParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct GameStateParams { pub id: Uuid, } #[derive(Debug,Clone,Serialize,Deserialize)] struct GamePveMsg { method: String, params: GamePveParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct GamePveParams { pub cryp_ids: Vec, } #[derive(Debug,Clone,Serialize,Deserialize)] struct GamePvpMsg { method: String, params: GamePvpParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct GamePvpParams { pub cryp_ids: Vec, } #[derive(Debug,Clone,Serialize,Deserialize)] struct GameJoinMsg { method: String, params: GameJoinParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct GameJoinParams { pub game_id: Uuid, pub cryp_ids: Vec, } #[derive(Debug,Clone,Serialize,Deserialize)] struct GameJoinableListMsg { method: String, params: (), } // #[derive(Debug,Clone,Serialize,Deserialize)] // struct GameTargetMsg { // method: String, // params: GameTargetParams, // } // #[derive(Debug,Clone,Serialize,Deserialize)] // pub struct GameTargetParams { // pub game_id: Uuid, // pub cryp_id: Uuid, // pub skill_id: Uuid, // } #[derive(Debug,Clone,Serialize,Deserialize)] struct GameSkillMsg { method: String, params: GameSkillParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct GameSkillParams { pub game_id: Uuid, pub cryp_id: Uuid, pub target_cryp_id: Option, pub skill: Skill, } #[derive(Debug,Clone,Serialize,Deserialize)] struct AccountCreateMsg { method: String, params: AccountCreateParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct AccountCreateParams { pub name: String, pub password: String, } #[derive(Debug,Clone,Serialize,Deserialize)] struct AccountLoginMsg { method: String, params: AccountLoginParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct AccountLoginParams { pub name: String, pub password: String, } #[derive(Debug,Clone,Serialize,Deserialize)] struct AccountCrypsMsg { method: String, params: (), } #[derive(Debug,Clone,Serialize,Deserialize)] struct ZoneCreateMsg { method: String, params: (), } #[derive(Debug,Clone,Serialize,Deserialize)] struct ZoneJoinMsg { method: String, params: ZoneJoinParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct ZoneJoinParams { pub zone_id: Uuid, pub node_id: u32, pub cryp_ids: Vec, } #[derive(Debug,Clone,Serialize,Deserialize)] struct ZoneCloseMsg { method: String, params: ZoneCloseParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct ZoneCloseParams { pub zone_id: Uuid, } #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxStateMsg { method: String, params: VboxStateParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct VboxStateParams { pub game_id: Uuid, } #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxAcceptMsg { method: String, params: VboxAcceptParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct VboxAcceptParams { pub game_id: Uuid, pub index: usize, } #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxDiscardMsg { method: String, params: VboxDiscardParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct VboxDiscardParams { pub game_id: Uuid, } #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxCombineMsg { method: String, params: VboxCombineParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct VboxCombineParams { pub game_id: Uuid, pub indices: Vec, } #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxApplyMsg { method: String, params: VboxApplyParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct VboxApplyParams { pub game_id: Uuid, pub cryp_id: Uuid, pub index: usize, } #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxDropMsg { method: String, params: VboxDropParams, } #[derive(Debug,Clone,Serialize,Deserialize)] pub struct VboxDropParams { pub game_id: Uuid, pub index: usize, } // #[cfg(test)] // mod tests { // use super::*; // use serde_cbor::to_vec; // #[test] // fn rpc_parse() { // let rpc = Rpc {}; // let msg = GenerateMsg { method: "cryp_generate".to_string(), params: GenerateParams { level: 64 } }; // let v = to_vec(&msg).unwrap(); // let received = rpc.receive(Message::Binary(v)); // } // }