From 65c7d86da2cc36a41e4d1da895a94657ff2aef61 Mon Sep 17 00:00:00 2001 From: ntr Date: Thu, 17 Jan 2019 14:02:12 +1100 Subject: [PATCH] unspec method --- server/WORKLOG.md | 5 +---- server/src/cryp.rs | 32 ++++++++++++++++++++++++++++++-- server/src/item.rs | 8 ++++---- server/src/rpc.rs | 34 +++++++++++++++++++++++++++++++++- server/src/spec.rs | 2 +- 5 files changed, 69 insertions(+), 12 deletions(-) diff --git a/server/WORKLOG.md b/server/WORKLOG.md index 7704e553..fe390aaa 100644 --- a/server/WORKLOG.md +++ b/server/WORKLOG.md @@ -33,12 +33,9 @@ strangle ## NOW * check zone completion -* recalc evasion rating * serialize modified stats - * items add specs - * add effects to spec * remove spec from cryp - * spec limits per category + * dupe rare specs check ## SOON * clean up categories diff --git a/server/src/cryp.rs b/server/src/cryp.rs index 715666bd..fca92035 100644 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -8,7 +8,7 @@ use failure::Error; use failure::err_msg; use account::Account; -use rpc::{CrypSpawnParams, CrypLearnParams, CrypForgetParams}; +use rpc::{CrypSpawnParams, CrypLearnParams, CrypForgetParams, CrypUnspecParams}; use skill::{Skill, Cooldown, Effect, Cast, Category, Immunity, Disable, ResolutionResult}; use spec::{Spec, SpecLevel}; use game::{Log}; @@ -270,7 +270,7 @@ impl Cryp { self } - pub fn spec_apply(&mut self, spec: Spec) -> Result<&mut Cryp, Error> { + pub fn spec_add(&mut self, spec: Spec) -> Result<&mut Cryp, Error> { let max_common = 20; let max_uncommon = 10; let max_rare = 5; @@ -303,6 +303,28 @@ impl Cryp { return Ok(self.recalculate_stats()); } + pub fn spec_remove(mut self, spec: Spec) -> Result { + let find_spec = |spec_v: &Vec| spec_v.iter().position(|s| s.spec == spec.spec); + + match spec.level { + SpecLevel::Common => match find_spec(&self.specs.common) { + Some(p) => self.specs.common.remove(p), + None => return Err(err_msg("spec not found")), + }, + SpecLevel::Uncommon => match find_spec(&self.specs.uncommon) { + Some(p) => self.specs.uncommon.remove(p), + None => return Err(err_msg("spec not found")), + }, + SpecLevel::Rare => match find_spec(&self.specs.rare) { + Some(p) => self.specs.rare.remove(p), + None => return Err(err_msg("spec not found")), + }, + }; + + Ok(self) + } + + fn recalculate_stats(&mut self) -> &mut Cryp { self.stamina.recalculate(&self.specs); self.hp.recalculate(&self.specs); @@ -707,6 +729,12 @@ pub fn cryp_forget(params: CrypForgetParams, tx: &mut Transaction, account: &Acc return cryp_write(cryp, tx); } +pub fn cryp_unspec(params: CrypUnspecParams, tx: &mut Transaction, account: &Account) -> Result { + let mut cryp = cryp_get(tx, params.id, account.id)?; + cryp = cryp.spec_remove(params.spec)?; + return cryp_write(cryp, tx); +} + pub fn cryp_write(cryp: Cryp, tx: &mut Transaction) -> Result { let cryp_bytes = to_vec(&cryp)?; diff --git a/server/src/item.rs b/server/src/item.rs index 1e5765c9..84a720dc 100644 --- a/server/src/item.rs +++ b/server/src/item.rs @@ -56,16 +56,16 @@ impl Item { ItemAction::RerollSpellShield => reroll(self, tx, target, Stat::SpellShield), ItemAction::RerollEvasion => reroll(self, tx, target, Stat::Evasion), - ItemAction::SpecPhysDmg5 => spec_apply(self, tx, target, SpecType::PhysDamage5), - ItemAction::SpecSpellDmg5 => spec_apply(self, tx, target, SpecType::SpellDamage5), + ItemAction::SpecPhysDmg5 => spec_add(self, tx, target, SpecType::PhysDamage5), + ItemAction::SpecSpellDmg5 => spec_add(self, tx, target, SpecType::SpellDamage5), } } } -fn spec_apply(item: &mut Item, tx: &mut Transaction, target: Uuid, spec_type: SpecType) -> Result<(), Error> { +fn spec_add(item: &mut Item, tx: &mut Transaction, target: Uuid, spec_type: SpecType) -> Result<(), Error> { let mut cryp = cryp_get(tx, target, item.account)?; let spec = Spec::new(spec_type); - cryp.spec_apply(spec)?; + cryp.spec_add(spec)?; cryp_write(cryp, tx)?; return Ok(()); } diff --git a/server/src/rpc.rs b/server/src/rpc.rs index f2c30d1f..0348613e 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -15,12 +15,13 @@ use failure::Error; use failure::err_msg; use net::Db; -use cryp::{Cryp, cryp_spawn, cryp_learn, cryp_forget}; +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 item::{Item, ItemAction, items_list, item_use, item_create}; use skill::{Skill}; use zone::{Zone, zone_create, zone_join, zone_close}; +use spec::{Spec}; pub struct Rpc; @@ -63,6 +64,7 @@ impl Rpc { "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), @@ -233,6 +235,23 @@ impl Rpc { 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) { @@ -398,6 +417,7 @@ pub enum RpcResult { CrypSpawn(Cryp), CrypForget(Cryp), CrypLearn(Cryp), + CrypUnspec(Cryp), Account(Account), CrypList(Vec), GameState(Game), @@ -449,6 +469,18 @@ struct CrypForgetMsg { 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, diff --git a/server/src/spec.rs b/server/src/spec.rs index b8d5115e..4fae0229 100644 --- a/server/src/spec.rs +++ b/server/src/spec.rs @@ -31,7 +31,7 @@ impl Spec { } } -#[derive(Debug,Copy,Clone,Serialize,Deserialize)] +#[derive(Debug,Copy,Clone,Serialize,Deserialize,PartialEq)] pub enum SpecType { PhysDamage5, SpellDamage5,