diff --git a/server/src/cryp.rs b/server/src/cryp.rs index c5269143..abd97bbc 100644 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -202,6 +202,11 @@ impl Cryp { self } + pub fn learn_mut(&mut self, s: Skill) -> &mut Cryp { + self.skills.push(CrypSkill::new(s)); + self + } + pub fn forget(mut self, skill: Skill) -> Result { match self.skills.iter().position(|s| s.skill == skill) { Some(i) => { diff --git a/server/src/instance.rs b/server/src/instance.rs index cbac4ec7..472d6c57 100644 --- a/server/src/instance.rs +++ b/server/src/instance.rs @@ -70,6 +70,21 @@ impl Instance { self } + pub fn player_update(mut self, player: Player) -> Result { + if self.phase != InstancePhase::Vbox { + return Err(err_msg("instance not in vbox phase")); + } + + let i = self.players + .iter() + .position(|p| p.id == player.id) + .ok_or(err_msg("player_id not found"))?; + + self.players[i] = player; + + Ok(self) + } + fn player_ready(mut self, mut player: Player) -> Result { if self.phase != InstancePhase::Vbox { panic!("instance not in vbox phase"); @@ -278,7 +293,7 @@ impl Instance { } } -pub fn instance_create(instance: Instance, tx: &mut Transaction) -> Result { +pub fn instance_create(tx: &mut Transaction, instance: Instance) -> Result { let instance_bytes = to_vec(&instance)?; let query = " @@ -295,7 +310,7 @@ pub fn instance_create(instance: Instance, tx: &mut Transaction) -> Result Result { +pub fn instance_update(tx: &mut Transaction, instance: Instance) -> Result { let instance_bytes = to_vec(&instance)?; let query = " @@ -360,10 +375,10 @@ pub fn instance_get_open(tx: &mut Transaction) -> Result { pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account: &Account) -> Result { let mut instance = match params.pve { - true => instance_create(Instance::new().add_bots(), tx)?, + true => instance_create(tx, Instance::new().add_bots())?, false => match instance_get_open(tx) { Ok(i) => i, - Err(_) => instance_create(Instance::new(), tx)?, + Err(_) => instance_create(tx, Instance::new())?, }, }; @@ -385,7 +400,7 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account: instance.start(); } - instance_write(instance, tx)?; + instance_update(tx, instance)?; return Ok(player); } @@ -414,7 +429,7 @@ pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account }; player_update(tx, player)?; - instance_write(instance, tx)?; + instance_update(tx, instance)?; return Ok(game); } diff --git a/server/src/player.rs b/server/src/player.rs index a7e87f0a..d02200b6 100644 --- a/server/src/player.rs +++ b/server/src/player.rs @@ -11,6 +11,7 @@ use account::Account; use cryp::{Cryp, cryp_get}; use vbox::{Vbox}; use rpc::{PlayerStateParams, PlayerCrypsSetParams}; +use instance::{Instance, instance_get, instance_update}; #[derive(Debug,Clone,Serialize,Deserialize)] pub struct Score { @@ -64,6 +65,10 @@ impl Player { self.score.losses += 1; self } + + pub fn cryp_get(&mut self, id: Uuid) -> Result<&mut Cryp, Error> { + self.cryps.iter_mut().find(|c| c.id == id).ok_or(err_msg("cryp not found")) + } } pub fn player_get(tx: &mut Transaction, account_id: Uuid, instance_id: Uuid) -> Result { @@ -109,6 +114,14 @@ pub fn player_create(tx: &mut Transaction, player: &Player, account: &Account) - } pub fn player_update(tx: &mut Transaction, player: Player) -> Result { + // update the instance this player is associated with + // if not a global player + if player.instance != Uuid::nil() { + let instance = instance_get(tx, player.instance)? + .player_update(player.clone())?; + instance_update(tx, instance)?; + } + let bytes = to_vec(&player)?; let query = " @@ -123,6 +136,7 @@ pub fn player_update(tx: &mut Transaction, player: Player) -> Result Result { let mut player = player_get(tx, account.id, params.instance_id)?; - let mut cryp = cryp_get(tx, params.cryp_id, account.id)?; - let var = player.vbox.bound.remove(params.index); + let skill = var.skill()?; - // done here because i teach them a tonne of skills for tests - let max_skills = 4; - if cryp.skills.len() >= max_skills { - return Err(format_err!("cryp at max skills ({:?})", max_skills)); + // mess with cryp then release it + { + let cryp = player.cryp_get(params.cryp_id)?; + // done here because i teach them a tonne of skills for tests + let max_skills = 4; + if cryp.skills.len() >= max_skills { + return Err(format_err!("cryp at max skills ({:?})", max_skills)); + } + + cryp.learn_mut(skill); } - let skill = var.skill()?; - cryp = cryp.learn(skill); - cryp_write(cryp, tx)?; return player_update(tx, player); }