cryp recover

This commit is contained in:
ntr 2018-12-18 15:17:44 +11:00
parent 950cc6fbad
commit 7424472ddb
2 changed files with 37 additions and 5 deletions

View File

@ -10,7 +10,7 @@ use postgres::transaction::Transaction;
use rpc::{AccountCreateParams, AccountLoginParams}; use rpc::{AccountCreateParams, AccountLoginParams};
use item::{Item, ItemAction, item_create}; use item::{Item, ItemAction, item_create};
use cryp::Cryp; use cryp::{Cryp, CrypRecover, cryp_write};
use game::Game; use game::Game;
use failure::Error; use failure::Error;
@ -153,6 +153,22 @@ pub fn account_login(params: AccountLoginParams, tx: &mut Transaction) -> Result
return Ok(account); return Ok(account);
} }
fn recover_cryp(cryp_bytes: Vec<u8>, tx: &mut Transaction) -> Result<Cryp, Error> {
let c = from_slice::<CrypRecover>(&cryp_bytes)?;
let mut cryp = Cryp::new()
.named(&c.name)
.level(c.lvl)
.set_account(c.account)
.create();
cryp.id = c.id;
println!("recovered cryp {:?}", c.name);
return cryp_write(cryp, tx);
}
pub fn account_cryps(tx: &mut Transaction, account: &Account) -> Result<Vec<Cryp>, Error> { pub fn account_cryps(tx: &mut Transaction, account: &Account) -> Result<Vec<Cryp>, Error> {
let query = " let query = "
SELECT data SELECT data
@ -163,10 +179,15 @@ pub fn account_cryps(tx: &mut Transaction, account: &Account) -> Result<Vec<Cryp
let result = tx let result = tx
.query(query, &[&account.id])?; .query(query, &[&account.id])?;
let cryps: Result<Vec<Cryp>, _> = result.iter().map(|row| { let cryps: Result<Vec<Cryp>, _> = result.iter()
.map(|row| {
let cryp_bytes: Vec<u8> = row.get(0); let cryp_bytes: Vec<u8> = row.get(0);
from_slice::<Cryp>(&cryp_bytes) match from_slice::<Cryp>(&cryp_bytes) {
}).collect(); Ok(c) => Ok(c),
Err(_e) => recover_cryp(cryp_bytes, tx),
}
})
.collect();
// catch any errors // catch any errors
if cryps.is_err() { if cryps.is_err() {

View File

@ -75,6 +75,15 @@ impl CrypStat {
} }
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct CrypRecover {
pub id: Uuid,
pub account: Uuid,
pub xp: u64,
pub lvl: u8,
pub name: String,
}
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Cryp { pub struct Cryp {
pub id: Uuid, pub id: Uuid,
@ -89,6 +98,7 @@ pub struct Cryp {
pub effects: Vec<CrypEffect>, pub effects: Vec<CrypEffect>,
pub name: String, pub name: String,
pub ko_logged: bool, pub ko_logged: bool,
pub bless: bool,
} }
fn check_lvl(lvl: u8) -> u8 { fn check_lvl(lvl: u8) -> u8 {
@ -112,6 +122,7 @@ impl Cryp {
effects: vec![], effects: vec![],
name: String::new(), name: String::new(),
ko_logged: false, ko_logged: false,
bless: true,
}; };
} }