diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 79e15c54..dd454058 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -21,7 +21,7 @@ use account::{Account, account_create, account_login, account_from_token, accoun 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}; +use vbox::{Vbox, vbox_state, vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_drop}; pub struct Rpc; @@ -80,7 +80,8 @@ impl Rpc { "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_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), @@ -415,7 +416,16 @@ impl Rpc { 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)] @@ -690,6 +700,18 @@ pub struct VboxApplyParams { 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::*; diff --git a/server/src/vbox.rs b/server/src/vbox.rs index c30c1916..c335d297 100644 --- a/server/src/vbox.rs +++ b/server/src/vbox.rs @@ -14,7 +14,7 @@ use failure::Error; use failure::err_msg; use account::Account; -use rpc::{VboxStateParams, VboxAcceptParams, VboxDiscardParams, VboxCombineParams, VboxApplyParams}; +use rpc::{VboxStateParams, VboxAcceptParams, VboxDiscardParams, VboxCombineParams, VboxApplyParams, VboxDropParams}; use skill::{Skill}; use cryp::{cryp_get, cryp_write}; @@ -51,17 +51,17 @@ impl Var { fn skill(&self) -> Result { match self { - Attack => Ok(Skill::Attack), - Block => Ok(Skill::Attack), - Stun => Ok(Skill::Attack), - Buff => Ok(Skill::Attack), - Debuff => Ok(Skill::Attack), + Var::Attack => Ok(Skill::Attack), + Var::Block => Ok(Skill::Attack), + Var::Stun => Ok(Skill::Attack), + Var::Buff => Ok(Skill::Attack), + Var::Debuff => Ok(Skill::Attack), - Strike => Ok(Skill::Attack), - Blast => Ok(Skill::Blast), - Heal => Ok(Skill::Heal), - Throw => Ok(Skill::Throw), - Hex => Ok(Skill::Hex), + Var::Strike => Ok(Skill::Attack), + Var::Blast => Ok(Skill::Blast), + Var::Heal => Ok(Skill::Heal), + Var::Throw => Ok(Skill::Throw), + Var::Hex => Ok(Skill::Hex), _ => Err(err_msg("not a usable var")) } } @@ -128,6 +128,13 @@ impl Vbox { Ok(self) } + pub fn drop(&mut self, i: usize) -> Result<&mut Vbox, Error> { + self.free.get(i).ok_or(format_err!("no var at index {:?}", i))?; + self.free.remove(i); + // balance update + Ok(self) + } + pub fn combine(&mut self, mut indices: Vec) -> Result<&mut Vbox, Error> { if indices.len() != 3 { return Err(err_msg("exactly 3 indices required")); @@ -323,6 +330,12 @@ pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &A return vbox_write(vbox, tx); } +pub fn vbox_drop(params: VboxDropParams, tx: &mut Transaction, account: &Account) -> Result { + let mut vbox = vbox_get(tx, params.game_id, account)?; + vbox.drop(params.index)?; + return vbox_write(vbox, tx); +} + pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result { let mut vbox = vbox_get(tx, params.game_id, account)?; let mut cryp = cryp_get(tx, params.cryp_id, account.id)?;