apply var

This commit is contained in:
ntr 2019-02-19 11:34:32 +11:00
parent 3f147b9fee
commit 26950e886a
2 changed files with 73 additions and 6 deletions

View File

@ -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_discard, vbox_combine};
use vbox::{Vbox, vbox_state, vbox_accept, vbox_apply, vbox_discard, vbox_combine};
pub struct Rpc;
@ -80,8 +80,9 @@ 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_discard" => Rpc::vbox_discard(data, &mut tx, account.unwrap(), client),
"vbox_apply" => Rpc::vbox_accept(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),
_ => Err(format_err!("unknown method - {:?}", v.method)),
};
@ -398,6 +399,23 @@ impl Rpc {
return Ok(response);
}
fn vbox_apply(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "vbox_state".to_string(),
params: RpcResult::VboxState(vbox_apply(msg.params, tx, &account)?)
};
Rpc::send_msg(client, RpcResponse {
method: "account_cryps".to_string(),
params: RpcResult::CrypList(account_cryps(tx, &account)?)
})?;
return Ok(response);
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
@ -659,6 +677,19 @@ pub struct VboxCombineParams {
pub indices: Vec<usize>,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct VboxApplyMsg {
method: String,
params: VboxApplyParams,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct VboxApplyParams {
pub game_id: Uuid,
pub cryp_id: Uuid,
pub index: usize,
}
// #[cfg(test)]
// mod tests {
// use super::*;

View File

@ -4,7 +4,7 @@ use uuid::Uuid;
// drops
use rand::prelude::*;
use rand::{thread_rng};
use rand::distributions::{LogNormal,WeightedIndex};
use rand::distributions::{WeightedIndex};
use serde_cbor::{from_slice, to_vec};
@ -14,7 +14,9 @@ use failure::Error;
use failure::err_msg;
use account::Account;
use rpc::{VboxStateParams, VboxAcceptParams, VboxDiscardParams, VboxCombineParams};
use rpc::{VboxStateParams, VboxAcceptParams, VboxDiscardParams, VboxCombineParams, VboxApplyParams};
use skill::{Skill};
use cryp::{cryp_get, cryp_write};
#[derive(Debug,Clone,Copy,PartialEq,Eq,Ord,PartialOrd,Serialize,Deserialize)]
pub enum Var {
@ -46,6 +48,23 @@ impl Var {
_ => false,
}
}
fn skill(&self) -> Result<Skill, Error> {
match self {
Attack => Ok(Skill::Attack),
Block => Ok(Skill::Attack),
Stun => Ok(Skill::Attack),
Buff => Ok(Skill::Attack),
Debuff => Ok(Skill::Attack),
Strike => Ok(Skill::Attack),
Blast => Ok(Skill::Blast),
Heal => Ok(Skill::Heal),
Throw => Ok(Skill::Throw),
Hex => Ok(Skill::Hex),
_ => Err(err_msg("not a usable var"))
}
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
@ -304,6 +323,25 @@ pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &A
return vbox_write(vbox, tx);
}
pub fn vbox_apply(params: VboxApplyParams, tx: &mut Transaction, account: &Account) -> Result<Vbox, Error> {
let mut vbox = vbox_get(tx, params.game_id, account)?;
let mut cryp = cryp_get(tx, params.cryp_id, account.id)?;
let var = vbox.bound.remove(params.index);
// 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)?;
return vbox_write(vbox, tx);
}
#[cfg(test)]
mod tests {
use super::*;
@ -313,8 +351,6 @@ mod tests {
let mut vbox = Vbox::new(Uuid::new_v4(), Uuid::new_v4());
vbox.bound = vec![Var::Attack, Var::Green, Var::Green];
vbox.combine(vec![1,2,0]).unwrap();
println!("{:?}", vbox);
assert_eq!(vbox.bound[0], Var::Heal);
}
}