vbox init

This commit is contained in:
ntr
2019-02-16 17:45:28 +11:00
parent 8f12075d52
commit d0525a56bd
11 changed files with 241 additions and 40 deletions
+3 -3
View File
@@ -44,9 +44,9 @@ impl Item {
pub fn new(action: ItemAction, account_id: Uuid) -> Item {
let id = Uuid::new_v4();
return Item {
id,
account: account_id,
action,
id,
account: account_id,
action,
};
}
+2
View File
@@ -30,6 +30,8 @@ mod item;
mod zone;
mod mob;
mod vbox;
use dotenv::dotenv;
use net::{start};
+74 -2
View File
@@ -22,6 +22,7 @@ use item::{Item, ItemAction, items_list, item_use, item_create};
use skill::{Skill};
use zone::{Zone, zone_create, zone_join, zone_close};
use spec::{Spec};
use vbox::{Vbox, vbox_state, vbox_accept, vbox_combine};
pub struct Rpc;
@@ -80,6 +81,10 @@ impl Rpc {
"account_zone" => Rpc::account_zone(data, &mut tx, account.unwrap(), client),
"item_use" => Rpc::item_use(data, &mut tx, account.unwrap(), client),
"vbox_state" => Rpc::vbox_state(data, &mut tx, account.unwrap(), client),
"vbox_accept" => Rpc::vbox_accept(data, &mut tx, account.unwrap(), client),
"vbox_combine" => Rpc::vbox_combine(data, &mut tx, account.unwrap(), client),
"press_r" => Rpc::press_r(data, &mut tx, account.unwrap(), client),
_ => Err(format_err!("unknown method - {:?}", v.method)),
@@ -251,8 +256,6 @@ impl Rpc {
Ok(cryp_list)
}
fn account_create(data: Vec<u8>, tx: &mut Transaction, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
match from_slice::<AccountCreateMsg>(&data) {
Ok(v) => Ok(RpcResponse {
@@ -404,6 +407,39 @@ impl Rpc {
return Ok(response);
}
fn vbox_state(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxStateMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "vbox_state".to_string(),
params: RpcResult::VboxState(vbox_state(msg.params, tx, &account)?)
};
return Ok(response);
}
fn vbox_accept(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxBuyMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "vbox_state".to_string(),
params: RpcResult::VboxState(vbox_accept(msg.params, tx, &account)?)
};
return Ok(response);
}
fn vbox_combine(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<VboxCombineMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "vbox_state".to_string(),
params: RpcResult::VboxState(vbox_combine(msg.params, tx, &account)?)
};
return Ok(response);
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
@@ -426,6 +462,8 @@ pub enum RpcResult {
ItemUse(()),
ZoneState(Zone),
ZoneClose(()),
VboxState(Vbox),
}
#[derive(Debug,Clone,Serialize,Deserialize)]
@@ -637,6 +675,40 @@ pub struct ZoneCloseParams {
pub zone_id: Uuid,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct VboxStateMsg {
method: String,
params: VboxStateParams,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct VboxStateParams {
pub game_id: Uuid,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct VboxBuyMsg {
method: String,
params: VboxBuyParams,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct VboxBuyParams {
pub game_id: Uuid,
pub index: u8,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct VboxCombineMsg {
method: String,
params: VboxCombineParams,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct VboxCombineParams {
pub game_id: Uuid,
pub indices: Vec<u8>,
}
// #[cfg(test)]
// mod tests {
+117
View File
@@ -0,0 +1,117 @@
use uuid::Uuid;
use rand::prelude::*;
use serde_cbor::{from_slice, to_vec};
use postgres::transaction::Transaction;
use failure::Error;
use failure::err_msg;
use account::Account;
use rpc::{VboxStateParams, VboxBuyParams, VboxCombineParams};
use item::{Item};
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Vbox {
pub id: Uuid,
pub balance: u16,
pub free: Vec<Item>,
pub bound: Vec<Item>,
pub game: Uuid,
pub account: Uuid,
}
impl Vbox {
pub fn new(account_id: Uuid, game_id: Uuid) -> Vbox {
Vbox {
id: Uuid::new_v4(),
account: account_id,
game: game_id,
free: vec![],
bound: vec![],
balance: 0,
}
}
}
pub fn vbox_write(vbox: Vbox, tx: &mut Transaction, account: &Account) -> Result<Vbox, Error> {
let vbox_bytes = to_vec(&vbox)?;
let query = "
INSERT INTO vbox (id, account, game, data)
VALUES ($1, $2, $3, $4)
RETURNING id;
";
let result = tx
.query(query, &[&vbox.id, &account.id, &vbox.game, &vbox_bytes])?;
result.iter().next().ok_or(format_err!("no vbox written"))?;
// println!("{:} wrote vbox", vbox.id);
return Ok(vbox);
}
pub fn vbox_get(tx: &mut Transaction, game_id: Uuid, account: &Account) -> Result<Vbox, Error> {
let query = "
SELECT *
FROM vbox
WHERE account = $1
AND game = $2;
";
let result = tx
.query(query, &[&account.id, &game_id])?;
let returned = match result.iter().next() {
Some(row) => row,
None => return Err(err_msg("vbox not found")),
};
// tells from_slice to cast into a cryp
let vbox_bytes: Vec<u8> = returned.get("data");
let vbox = from_slice::<Vbox>(&vbox_bytes)?;
return Ok(vbox);
}
pub fn vbox_state(params: VboxStateParams, tx: &mut Transaction, account: &Account) -> Result<Vbox, Error> {
match vbox_get(tx, params.game_id, account) {
Ok(v) => Ok(v),
Err(e) => {
println!("{:?}", e);
vbox_write(Vbox::new(account.id, params.game_id), tx, account)
}
}
}
pub fn vbox_accept(params: VboxBuyParams, tx: &mut Transaction, account: &Account) -> Result<Vbox, Error> {
return vbox_get(tx, params.game_id, account)
}
pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &Account) -> Result<Vbox, Error> {
return vbox_get(tx, params.game_id, account)
}
// pub fn item_drop(tx: &mut Transaction, account_id: Uuid, mode: GameMode) -> Result<(), Error> {
// let mut rng = thread_rng();
// let log_normal = LogNormal::new(1.0, 1.0);
// let num_drops = log_normal.sample(&mut rng).floor() as u16;
// let actions = mode_drops(mode);
// println!("{:?} drops", num_drops);
// for _i in 0..num_drops {
// let dist = WeightedIndex::new(actions.iter().map(|item| item.1)).unwrap();
// let kind = actions[dist.sample(&mut rng)].0;
// let item = Item::new(kind, account_id);
// println!("{:?} dropped {:?}", account_id, item);
// item_create(item, tx, account_id)?;
// }
// Ok(())
// }