From 5c6e587044eca1c70c1af5f1435d17b4693e85e5 Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 14 Jun 2019 13:05:19 +1000 Subject: [PATCH] simpify rpc responses --- client/src/socket.jsx | 62 ++++----- server/src/rpc.rs | 299 ++++++++++-------------------------------- 2 files changed, 92 insertions(+), 269 deletions(-) diff --git a/client/src/socket.jsx b/client/src/socket.jsx index 427e320c..d3e84d3b 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -143,9 +143,7 @@ function createSocket(events) { // ------------- // Incoming // ------------- - function accountLogin(res) { - const [struct, login] = res; - + function onAccount(login) { account = login; localStorage.setItem('account', JSON.stringify(login)); events.setAccount(login); @@ -153,19 +151,16 @@ function createSocket(events) { sendAccountInstances(); } - function accountInstanceList(res) { - const [struct, playerList] = res; - events.setAccountInstances(playerList); + function onAccountInstances(list) { + events.setAccountInstances(list); setTimeout(sendAccountInstances, 5000); } - function accountConstructs(response) { - const [structName, constructs] = response; + function onAccountConstructs(constructs) { events.setConstructList(constructs); } - function gameState(response) { - const [structName, game] = response; + function onGameState(game) { events.setGame(game); } @@ -180,15 +175,6 @@ function createSocket(events) { clearTimeout(gameStateTimeout); } - function constructSpawn(response) { - const [structName, construct] = response; - } - - function zoneState(response) { - const [structName, zone] = response; - events.setZone(zone); - } - let instanceStateTimeout; function startInstanceStateTimeout(id) { clearTimeout(instanceStateTimeout); @@ -196,13 +182,12 @@ function createSocket(events) { return true; } - function instanceState(response) { - const [structName, i] = response; - events.setInstance(i); + function onInstanceState(instance) { + events.setInstance(instance); return true; } - function instanceList([, list]) { + function onOpenInstances(list) { events.setInstanceList(list); return true; } @@ -211,12 +196,11 @@ function createSocket(events) { clearTimeout(instanceStateTimeout); } - function itemInfo(response) { - const [structName, info] = response; + function onItemInfo(info) { events.setItemInfo(info); } - function pong() { + function onPong() { events.setPing(Date.now() - ping); setTimeout(sendPing, 1000); } @@ -228,16 +212,14 @@ function createSocket(events) { // when the server sends a reply it will have one of these message types // this object wraps the reply types to a function const handlers = { - construct_spawn: constructSpawn, - game_state: gameState, - account_login: accountLogin, - account_create: accountLogin, - account_constructs: accountConstructs, - account_instances: accountInstanceList, - instance_list: instanceList, - instance_state: instanceState, - item_info: itemInfo, - pong, + Account: onAccount, + AccountConstructs: onAccountConstructs, + AccountInstances: onAccountInstances, + GameState: onGameState, + InstanceState: onInstanceState, + ItemInfo: onItemInfo, + OpenInstances: onOpenInstances, + Pong: onPong, }; function logout() { @@ -265,14 +247,14 @@ function createSocket(events) { // decode binary msg from server const blob = new Uint8Array(event.data); const res = cbor.decode(blob); - const { method, params } = res; + const [msgType, params] = res; - if (method !== 'pong' ) console.log(res); + if (msgType !== 'pong') console.log(res); // check for error and split into response type and data if (res.err) return errHandler(res.err); - if (!handlers[method]) return errorToast(`${method} handler missing`); - return handlers[method](params); + if (!handlers[msgType]) return errorToast(`${msgType} handler missing`); + return handlers[msgType](params); } function connect() { diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 1633c5ea..d2271225 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -17,15 +17,14 @@ use instance::{Instance, instance_state, instance_list, instance_new, instance_r use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_reclaim, vbox_unequip}; use item::{Item, ItemInfoCtr, item_info}; - type MnmlWs = ws::WebsocketContext; -pub fn receive(data: Vec, db: &Db, client: &mut MnmlWs, begin: Instant) -> Result { - // cast the data to this type to receive method name +pub fn receive(data: Vec, db: &Db, _client: &mut MnmlWs, begin: Instant) -> Result { + // cast the msg to this type to receive method name match from_slice::(&data) { Ok(v) => { if v.method == "ping" { - return Ok(RpcResponse { method: "pong".to_string(), params: RpcResult::Pong(()) }); + return Ok(RpcResult::Pong(())); } let mut tx = db.transaction()?; @@ -56,33 +55,33 @@ pub fn receive(data: Vec, db: &Db, client: &mut MnmlWs, begin: Instant) -> R // match on that to determine what fn to call let response = match v.method.as_ref() { // NO AUTH - "account_create" => handle_account_create(data, &mut tx, client), - "account_login" => handle_account_login(data, &mut tx, client), - "item_info" => Ok(RpcResponse { method: "item_info".to_string(), params: RpcResult::ItemInfo(item_info()) }), + "account_create" => handle_account_create(data, &mut tx), + "account_login" => handle_account_login(data, &mut tx), + "item_info" => Ok(RpcResult::ItemInfo(item_info())), // AUTH METHODS - "account_constructs" => handle_account_constructs(data, &mut tx, account.unwrap(), client), - "account_instances" => handle_account_instances(data, &mut tx, account.unwrap(), client), + "account_constructs" => handle_account_constructs(data, &mut tx, account.unwrap()), + "account_instances" => handle_account_instances(data, &mut tx, account.unwrap()), - "construct_spawn" => handle_construct_spawn(data, &mut tx, account.unwrap(), client), - "construct_delete" => handle_construct_delete(data, &mut tx, account.unwrap(), client), + "construct_spawn" => handle_construct_spawn(data, &mut tx, account.unwrap()), + "construct_delete" => handle_construct_delete(data, &mut tx, account.unwrap()), - "game_state" => handle_game_state(data, &mut tx, account.unwrap(), client), - "game_skill" => handle_game_skill(data, &mut tx, account.unwrap(), client), - "game_ready" => handle_game_ready(data, &mut tx, account.unwrap(), client), + "game_state" => handle_game_state(data, &mut tx, account.unwrap()), + "game_skill" => handle_game_skill(data, &mut tx, account.unwrap()), + "game_ready" => handle_game_ready(data, &mut tx, account.unwrap()), - "instance_list" => handle_instance_list(data, &mut tx, account.unwrap(), client), - "instance_join" => handle_instance_join(data, &mut tx, account.unwrap(), client), - "instance_ready" => handle_instance_ready(data, &mut tx, account.unwrap(), client), - "instance_new" => handle_instance_new(data, &mut tx, account.unwrap(), client), - "instance_state" => handle_instance_state(data, &mut tx, account.unwrap(), client), + "instance_list" => handle_instance_list(data, &mut tx, account.unwrap()), + "instance_join" => handle_instance_join(data, &mut tx, account.unwrap()), + "instance_ready" => handle_instance_ready(data, &mut tx, account.unwrap()), + "instance_new" => handle_instance_new(data, &mut tx, account.unwrap()), + "instance_state" => handle_instance_state(data, &mut tx, account.unwrap()), - "vbox_accept" => handle_vbox_accept(data, &mut tx, account.unwrap(), client), - "vbox_apply" => handle_vbox_apply(data, &mut tx, account.unwrap(), client), - "vbox_combine" => handle_vbox_combine(data, &mut tx, account.unwrap(), client), - "vbox_discard" => handle_vbox_discard(data, &mut tx, account.unwrap(), client), - "vbox_reclaim" => handle_vbox_reclaim(data, &mut tx, account.unwrap(), client), - "vbox_unequip" => handle_vbox_unequip(data, &mut tx, account.unwrap(), client), + "vbox_accept" => handle_vbox_accept(data, &mut tx, account.unwrap()), + "vbox_apply" => handle_vbox_apply(data, &mut tx, account.unwrap()), + "vbox_combine" => handle_vbox_combine(data, &mut tx, account.unwrap()), + "vbox_discard" => handle_vbox_discard(data, &mut tx, account.unwrap()), + "vbox_reclaim" => handle_vbox_reclaim(data, &mut tx, account.unwrap()), + "vbox_unequip" => handle_vbox_unequip(data, &mut tx, account.unwrap()), _ => Err(format_err!("unknown method - {:?}", v.method)), }; @@ -100,18 +99,12 @@ pub fn receive(data: Vec, db: &Db, client: &mut MnmlWs, begin: Instant) -> R } } -fn handle_game_state(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_game_state(data: Vec, tx: &mut Transaction, account: Account) -> 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); + return Ok(RpcResult::GameState(game_state(msg.params, tx, &account)?)); } -// fn handle_game_pve(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +// fn handle_game_pve(data: Vec, tx: &mut Transaction, account: Account) -> Result { // let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; // let game_response = RpcResponse { @@ -122,250 +115,107 @@ fn handle_game_state(data: Vec, tx: &mut Transaction, account: Account, _cli // return Ok(game_response); // } -fn handle_game_skill(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_game_skill(data: Vec, tx: &mut Transaction, account: Account) -> 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); + Ok(RpcResult::GameState(game_skill(msg.params, tx, &account)?)) } -fn handle_game_ready(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_game_ready(data: Vec, tx: &mut Transaction, account: Account) -> 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_ready(msg.params, tx, &account)?) - }; - - return Ok(game_response); + Ok(RpcResult::GameState(game_ready(msg.params, tx, &account)?)) } -fn handle_construct_spawn(data: Vec, tx: &mut Transaction, account: Account, client: &mut MnmlWs) -> Result { +fn handle_construct_spawn(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - construct_spawn(msg.params, tx, &account)?; - - let construct_list = RpcResponse { - method: "account_constructs".to_string(), - params: RpcResult::ConstructList(account_constructs(tx, &account)?) - }; - - Ok(construct_list) + Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?)) } -fn handle_construct_delete(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_construct_delete(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - construct_delete(tx, msg.params.id, account.id)?; - - let construct_list = RpcResponse { - method: "account_constructs".to_string(), - params: RpcResult::ConstructList(account_constructs(tx, &account)?) - }; - - Ok(construct_list) + Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?)) } -fn handle_account_create(data: Vec, tx: &mut Transaction, _client: &mut MnmlWs) -> Result { +fn handle_account_create(data: Vec, tx: &mut Transaction) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - let account = account_create(msg.params, tx)?; - - Ok(RpcResponse { - method: "account_create".to_string(), - params: RpcResult::Account(account) - }) + Ok(RpcResult::Account(account)) } -fn handle_account_login(data: Vec, tx: &mut Transaction, _client: &mut MnmlWs) -> 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 handle_account_login(data: Vec, tx: &mut Transaction) -> Result { + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + Ok(RpcResult::Account(account_login(msg.params, tx)?)) } -// fn handle_account_demo(_data: Vec, tx: &mut Transaction, _client: &mut MnmlWs) -> 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(); -// construct_spawn(ConstructSpawnParams { name }, tx, &account)?; - -// let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect(); -// construct_spawn(ConstructSpawnParams { name }, tx, &account)?; - -// let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect(); -// construct_spawn(ConstructSpawnParams { name }, tx, &account)?; - -// let res = RpcResponse { -// method: "account_create".to_string(), -// params: RpcResult::Account(account), -// }; - -// return Ok(res); -// } - - -fn handle_account_constructs(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { - Ok(RpcResponse { - method: "account_constructs".to_string(), - params: RpcResult::ConstructList(account_constructs(tx, &account)?) - }) +fn handle_account_constructs(_data: Vec, tx: &mut Transaction, account: Account) -> Result { + Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?)) } -fn handle_account_instances(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { - Ok(RpcResponse { - method: "account_instances".to_string(), - params: RpcResult::InstanceList(account_instances(tx, &account)?) - }) +fn handle_account_instances(_data: Vec, tx: &mut Transaction, account: Account) -> Result { + Ok(RpcResult::AccountInstances(account_instances(tx, &account)?)) } -fn handle_instance_new(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_instance_new(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(instance_new(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(instance_new(msg.params, tx, &account)?)) } -fn handle_instance_ready(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_instance_ready(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(instance_ready(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(instance_ready(msg.params, tx, &account)?)) } -fn handle_instance_join(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_instance_join(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(instance_join(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(instance_join(msg.params, tx, &account)?)) } -fn handle_instance_list(_data: Vec, tx: &mut Transaction, _account: Account, _client: &mut MnmlWs) -> Result { - let response = RpcResponse { - method: "instance_list".to_string(), - params: RpcResult::InstanceList(instance_list(tx)?) - }; - - return Ok(response); +fn handle_instance_list(_data: Vec, tx: &mut Transaction, _account: Account) -> Result { + Ok(RpcResult::OpenInstances(instance_list(tx)?)) } -// fn handle_instance_ready(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { -// let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - -// let response = RpcResponse { -// method: "game_state".to_string(), -// params: RpcResult::GameState(instance_ready(msg.params, tx, &account)?) -// }; - -// return Ok(response); -// } - -fn handle_instance_state(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_instance_state(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; match instance_state(msg.params, tx, &account)? { - RpcResult::GameState(p) => Ok(RpcResponse { - method: "game_state".to_string(), - params: RpcResult::GameState(p), - }), - RpcResult::InstanceState(p) => Ok(RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(p), - }), + RpcResult::GameState(p) => Ok(RpcResult::GameState(p)), + RpcResult::InstanceState(p) => Ok(RpcResult::InstanceState(p)), _ => Err(err_msg("unhandled instance state")) } } -fn handle_vbox_accept(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_vbox_accept(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(vbox_accept(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(vbox_accept(msg.params, tx, &account)?)) } -fn handle_vbox_discard(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_vbox_discard(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(vbox_discard(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(vbox_discard(msg.params, tx, &account)?)) } -fn handle_vbox_combine(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_vbox_combine(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(vbox_combine(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(vbox_combine(msg.params, tx, &account)?)) } -fn handle_vbox_apply(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_vbox_apply(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(vbox_apply(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(vbox_apply(msg.params, tx, &account)?)) } -fn handle_vbox_reclaim(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_vbox_reclaim(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(vbox_reclaim(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(vbox_reclaim(msg.params, tx, &account)?)) } -fn handle_vbox_unequip(data: Vec, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result { +fn handle_vbox_unequip(data: Vec, tx: &mut Transaction, account: Account) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - let response = RpcResponse { - method: "instance_state".to_string(), - params: RpcResult::InstanceState(vbox_unequip(msg.params, tx, &account)?) - }; - - return Ok(response); + Ok(RpcResult::InstanceState(vbox_unequip(msg.params, tx, &account)?)) } #[derive(Debug,Clone,Serialize,Deserialize)] @@ -373,24 +223,15 @@ pub struct RpcErrorResponse { pub err: String } -#[derive(Debug,Clone,Serialize,Deserialize)] -pub struct RpcResponse { - pub method: String, - params: RpcResult, -} - #[derive(Debug,Clone,Serialize,Deserialize)] pub enum RpcResult { - ConstructSpawn(Construct), - ConstructForget(Construct), - ConstructLearn(Construct), - ConstructUnspec(Construct), Account(Account), - ConstructList(Vec), + AccountConstructs(Vec), + AccountInstances(Vec), GameState(Game), ItemInfo(ItemInfoCtr), - InstanceList(Vec), + OpenInstances(Vec), InstanceState(Instance), Pong(()),