update players when vboxing
This commit is contained in:
parent
1afe725295
commit
ec2304adbb
@ -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<Cryp, Error> {
|
||||
match self.skills.iter().position(|s| s.skill == skill) {
|
||||
Some(i) => {
|
||||
|
||||
@ -70,6 +70,21 @@ impl Instance {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn player_update(mut self, player: Player) -> Result<Instance, Error> {
|
||||
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<Instance, Error> {
|
||||
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<Instance, Error> {
|
||||
pub fn instance_create(tx: &mut Transaction, instance: Instance) -> Result<Instance, Error> {
|
||||
let instance_bytes = to_vec(&instance)?;
|
||||
|
||||
let query = "
|
||||
@ -295,7 +310,7 @@ pub fn instance_create(instance: Instance, tx: &mut Transaction) -> Result<Insta
|
||||
return Ok(instance);
|
||||
}
|
||||
|
||||
pub fn instance_write(instance: Instance, tx: &mut Transaction) -> Result<Instance, Error> {
|
||||
pub fn instance_update(tx: &mut Transaction, instance: Instance) -> Result<Instance, Error> {
|
||||
let instance_bytes = to_vec(&instance)?;
|
||||
|
||||
let query = "
|
||||
@ -360,10 +375,10 @@ pub fn instance_get_open(tx: &mut Transaction) -> Result<Instance, Error> {
|
||||
|
||||
pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -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<Player, Error> {
|
||||
@ -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<Player, Error> {
|
||||
// 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<Player, Err
|
||||
|
||||
result.iter().next().ok_or(format_err!("player {:?} could not be written", player))?;
|
||||
|
||||
|
||||
return Ok(player)
|
||||
}
|
||||
|
||||
|
||||
@ -302,19 +302,21 @@ pub fn vbox_drop(params: VboxDropParams, tx: &mut Transaction, account: &Account
|
||||
|
||||
pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
|
||||
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()?;
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
let skill = var.skill()?;
|
||||
cryp = cryp.learn(skill);
|
||||
cryp_write(cryp, tx)?;
|
||||
cryp.learn_mut(skill);
|
||||
}
|
||||
|
||||
return player_update(tx, player);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user