mnml/server/src/item.rs
2018-10-20 00:36:47 +11:00

157 lines
3.6 KiB
Rust

use serde_cbor::{from_slice, to_vec};
use uuid::Uuid;
use postgres::transaction::Transaction;
use failure::Error;
use failure::err_msg;
use account::Account;
use rpc::{ItemUseParams};
use cryp::{cryp_get, cryp_write};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum ItemAction {
Revive,
}
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct Item {
// mods: Vec<Mod>,
id: Uuid,
account: Uuid,
action: ItemAction,
}
impl Item {
pub fn new(action: ItemAction, account: &Account) -> Item {
let id = Uuid::new_v4();
return Item {
id,
account: account.id,
action,
};
}
fn apply(&mut self, tx: &mut Transaction, target: Uuid) -> Result<(), Error> {
match self.action {
ItemAction::Revive => revive(self, tx, target),
}
}
}
fn revive(item: &mut Item, tx: &mut Transaction, target: Uuid) -> Result<(), Error> {
let mut cryp = cryp_get(tx, target, item.account)?;
cryp.rez();
cryp_write(cryp, tx)?;
return Ok(());
}
pub fn item_create(item: Item, tx: &mut Transaction, account: &Account) -> Result<Item, Error> {
let item_bytes = to_vec(&item)?;
let query = "
INSERT INTO items (id, account, data)
VALUES ($1, $2, $3)
RETURNING id, account, data;
";
let result = tx
.query(query, &[&item.id, &account.id, &item_bytes])?;
let _returned = result.iter().next().expect("no row returned");
println!("{:?} wrote item {:}", account.id, item.id);
return Ok(item);
}
pub fn item_use(params: ItemUseParams, tx: &mut Transaction, account: &Account) -> Result<(), Error> {
let query = "
SELECT data
FROM items
WHERE id = $1
AND account = $2
FOR UPDATE;
";
let result = tx
.query(query, &[&params.item, &account.id])?;
let returned = result.iter().next().expect("no row returned");
let item_bytes: Vec<u8> = returned.get(0);
let mut item = from_slice::<Item>(&item_bytes)?;
item.apply(tx, params.target)?;
return Ok(());
}
pub fn items_list(tx: &mut Transaction, account: &Account) -> Result<Vec<Item>, Error> {
let query = "
SELECT data
FROM items
WHERE account = $1;
";
let result = tx
.query(query, &[&account.id])?;
let items: Result<Vec<Item>, _> = result.iter().map(|row| {
let item_bytes: Vec<u8> = row.get(0);
from_slice::<Item>(&item_bytes)
}).collect();
// catch any errors
if items.is_err() {
return Err(err_msg("could not deserialize an item"));
}
// now unwrap is safe
return Ok(items.unwrap());
}
// # max damage potion
// name
// "MapMonstersCurseEffectOnSelfFinal3": {
// "adds_tags": [],
// "domain": "area",
// "generation_type": "prefix",
// "generation_weights": [],
// "grants_buff": {},
// "grants_effect": {},
// "group": "MapHexproof",
// "is_essence_only": false,
// "name": "Hexwarded",
// "required_level": 1,
// "spawn_weights": [
// {
// "tag": "top_tier_map",
// "weight": 0
// },
// {
// "tag": "default",
// "weight": 0
// }
// ],
// "stats": [
// {
// "id": "map_item_drop_quantity_+%",
// "max": 15,
// "min": 15
// },
// {
// "id": "map_item_drop_rarity_+%",
// "max": 8,
// "min": 8
// },
// {
// "id": "map_monsters_curse_effect_on_self_+%_final",
// "max": -60,
// "min": -60
// }
// ]
// },