From e99439002f7416e98a4af05d22fd0749dd395e9d Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 9 Oct 2018 00:09:38 +1100 Subject: [PATCH] rpc rework for cryp status --- client/src/socket.jsx | 1 + server/src/account.rs | 6 ++-- server/src/combat.rs | 6 +--- server/src/cryp.rs | 4 +-- server/src/net.rs | 2 +- server/src/rpc.rs | 66 ++++++++++++++++++++++--------------------- 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/client/src/socket.jsx b/client/src/socket.jsx index 62686db4..c99fe68d 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -57,6 +57,7 @@ function createSocket(store) { account = login; store.dispatch(actions.setAccount(login)); + // send({ method: 'cryp_spawn', params: { name: 'muji' } }); send({ method: 'account_cryps', params: {} }); console.log(account); } diff --git a/server/src/account.rs b/server/src/account.rs index 7d8c47c7..265e9087 100755 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -59,7 +59,7 @@ pub fn from_token(token: String, db: &Db) -> Result { return Ok(entry); } -pub fn create(params: AccountCreateParams, db: Db) -> Result { +pub fn create(params: AccountCreateParams, db: &Db) -> Result { let id = Uuid::new_v4(); if params.password.len() < PASSWORD_MIN_LEN { @@ -106,7 +106,7 @@ pub fn create(params: AccountCreateParams, db: Db) -> Result { return Ok(entry); } -pub fn login(params: AccountLoginParams, db: Db) -> Result { +pub fn login(params: AccountLoginParams, db: &Db) -> Result { let query = " SELECT id, name, token, password FROM accounts @@ -149,7 +149,7 @@ pub fn login(params: AccountLoginParams, db: Db) -> Result { return Ok(account); } -pub fn fetch_cryps(db: Db, account: Account) -> Result, Error> { +pub fn fetch_cryps(db: &Db, account: &Account) -> Result, Error> { let query = " SELECT data FROM cryps diff --git a/server/src/combat.rs b/server/src/combat.rs index 8ce03fb4..5ed23c34 100755 --- a/server/src/combat.rs +++ b/server/src/combat.rs @@ -94,9 +94,7 @@ fn generate_mob(plr: &Cryp) -> Cryp { } -pub fn pve(params: CombatPveParams, db: Db, account: Account, send: F) -> Result -where F: Fn(&Battle) -> Result<(), Error> - { +pub fn pve(params: CombatPveParams, db: &Db, account: &Account) -> Result { let query = " SELECT * FROM cryps @@ -122,8 +120,6 @@ where F: Fn(&Battle) -> Result<(), Error> loop { battle.next(); - send(&battle)?; - if battle.finished() { let success = match battle.winner() { Some(c) => c.id == plr.id, diff --git a/server/src/cryp.rs b/server/src/cryp.rs index bb16c9d3..8ae3f644 100755 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -188,7 +188,7 @@ impl Cryp { } -pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result { +pub fn spawn(params: CrypSpawnParams, db: &Db, account: &Account) -> Result { let cryp = Cryp::new() .named(¶ms.name) .level(1) @@ -217,7 +217,7 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result Result { +pub fn write_cryp(cryp: Cryp, db: &Db, account: &Account) -> Result { let cryp_bytes = to_vec(&cryp)?; let query = " diff --git a/server/src/net.rs b/server/src/net.rs index 7bb9b458..956dfbf2 100755 --- a/server/src/net.rs +++ b/server/src/net.rs @@ -32,7 +32,7 @@ impl Handler for Server { fn on_message(&mut self, msg: Message) -> Result<()> { let db = self.db.get().expect("unable to get db connection"); - match self.rpc.receive(msg, db, &self.out) { + match self.rpc.receive(msg, &db, &self.out) { Ok(reply) => { let response = to_vec(&reply) .expect("failed to serialize response"); diff --git a/server/src/rpc.rs b/server/src/rpc.rs index cad78566..82c367d3 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -13,7 +13,7 @@ use account::{Account, create, login, from_token, fetch_cryps}; pub struct Rpc; impl Rpc { - pub fn receive(&self, msg: Message, db: Db, out: &Sender) -> Result { + pub fn receive(&self, msg: Message, db: &Db, socket: &Sender) -> Result { // consume the ws data into bytes let data = msg.into_data(); @@ -32,7 +32,7 @@ impl Rpc { // match on that to determine what fn to call match v.method.as_ref() { "cryp_spawn" => Rpc::cryp_spawn(data, db, account), - "combat_pve" => Rpc::combat_pve(data, db, account, out), + "combat_pve" => Rpc::combat_pve(data, db, account, socket), "account_create" => Rpc::account_create(data, db), "account_login" => Rpc::account_login(data, db), "account_cryps" => Rpc::account_cryps(db, account), @@ -44,40 +44,42 @@ impl Rpc { } } - fn combat_pve(data: Vec, db: Db, account: Option, out: &Sender) -> Result { - let send = |b: &Battle| -> Result<(), Error> { - let reply = RpcResponse { - method: "battle_state".to_string(), - params: RpcResult::Pve(b.clone()) - }; - let response = to_vec(&reply)?; - match out.send(response) { - Ok(()) => Ok(()), - Err(e) => Err(err_msg(e)) - } - }; - - match from_slice::(&data) { - Ok(v) => { - match account { - Some(u) => Ok(RpcResponse { - method: v.method, - params: RpcResult::Pve(pve(v.params, db, u, send)?) - }), - None => Err(err_msg("auth required")), - } - } - Err(_e) => Err(err_msg("invalid params")), + fn send_msg(socket: &Sender, msg: RpcResponse) -> Result<(), Error> { + let bytes = to_vec(&msg)?; + match socket.send(bytes) { + Ok(()) => Ok(()), + Err(e) => Err(err_msg(e)) } } - fn cryp_spawn(data: Vec, db: Db, account: Option) -> Result { + fn combat_pve(data: Vec, db: &Db, account: Option, socket: &Sender) -> Result { + let u = match account { + Some(u) => u, + None => return Err(err_msg("auth required")), + }; + + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + + let battle_response = RpcResponse { + method: "battle_state".to_string(), + params: RpcResult::Pve(pve(msg.params, db, &u)?) + }; + + Rpc::send_msg(socket, RpcResponse { + method: "account_cryps".to_string(), + params: RpcResult::CrypList(fetch_cryps(db, &u)?) + })?; + + return Ok(battle_response); + } + + fn cryp_spawn(data: Vec, db: &Db, account: Option) -> Result { match from_slice::(&data) { Ok(v) => { match account { Some(u) => Ok(RpcResponse { method: v.method, - params: RpcResult::SpawnCryp(spawn(v.params, db, u)?) + params: RpcResult::SpawnCryp(spawn(v.params, db, &u)?) }), None => Err(err_msg("auth required")), } @@ -86,7 +88,7 @@ impl Rpc { } } - fn account_create(data: Vec, db: Db) -> Result { + fn account_create(data: Vec, db: &Db) -> Result { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, @@ -96,7 +98,7 @@ impl Rpc { } } - fn account_login(data: Vec, db: Db) -> Result { + fn account_login(data: Vec, db: &Db) -> Result { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, @@ -106,11 +108,11 @@ impl Rpc { } } - fn account_cryps(db: Db, account: Option) -> Result { + fn account_cryps(db: &Db, account: Option) -> Result { match account { Some(u) => Ok(RpcResponse { method: "account_cryps".to_string(), - params: RpcResult::CrypList(fetch_cryps(db, u)?) + params: RpcResult::CrypList(fetch_cryps(db, &u)?) }), None => Err(err_msg("auth required")), }