cryp persistence
This commit is contained in:
+13
-3
@@ -10,7 +10,7 @@ use net::Db;
|
||||
use account::Account;
|
||||
use rpc::{CombatPveParams};
|
||||
|
||||
use cryp::Cryp;
|
||||
use cryp::{Cryp, write_cryp};
|
||||
use battle::Battle;
|
||||
use skill::Skill;
|
||||
|
||||
@@ -113,7 +113,7 @@ where F: Fn(&Battle) -> Result<(), Error>
|
||||
|
||||
// tells from_slice to cast into a cryp
|
||||
let cryp_bytes: Vec<u8> = returned.get("data");
|
||||
let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
|
||||
let mut plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
|
||||
|
||||
let mob = generate_mob(&plr);
|
||||
let mut battle = Battle::new(&plr, &mob);
|
||||
@@ -124,7 +124,17 @@ where F: Fn(&Battle) -> Result<(), Error>
|
||||
send(&battle)?;
|
||||
|
||||
if battle.finished() {
|
||||
break Ok(battle)
|
||||
let success = match battle.winner() {
|
||||
Some(c) => c.id == plr.id,
|
||||
None => false,
|
||||
};
|
||||
|
||||
if success {
|
||||
plr = plr.add_xp();
|
||||
write_cryp(plr, db, account)?;
|
||||
}
|
||||
|
||||
break Ok(battle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,6 +217,31 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result<Cryp,
|
||||
return Ok(cryp);
|
||||
}
|
||||
|
||||
pub fn write_cryp(cryp: Cryp, db: Db, account: Account) -> Result<Cryp, Error> {
|
||||
let cryp_bytes = to_vec(&cryp)?;
|
||||
|
||||
let query = "
|
||||
UPDATE cryps
|
||||
SET data = $1
|
||||
WHERE id = $2
|
||||
AND account = $3
|
||||
RETURNING id, account, data;
|
||||
";
|
||||
|
||||
let tx = db.transaction()?;
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&cryp_bytes, &cryp.id, &account.id])?;
|
||||
|
||||
let _returned = result.iter().next().expect("no row returned");
|
||||
|
||||
println!("{:?} wrote cryp {:}", account.id, cryp.id);
|
||||
|
||||
tx.commit()?;
|
||||
|
||||
return Ok(cryp);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use cryp::*;
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ impl Rpc {
|
||||
"combat_pve" => {
|
||||
let send = |b: &Battle| -> Result<(), Error> {
|
||||
let reply = RpcResponse {
|
||||
method: "combat_pve".to_string(),
|
||||
method: "battle_state".to_string(),
|
||||
params: RpcResult::Pve(b.clone())
|
||||
};
|
||||
let response = to_vec(&reply)?;
|
||||
|
||||
Reference in New Issue
Block a user