From 01468c7081f12748da7458d68294f4d90af1a9e4 Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 16 Feb 2019 18:32:29 +1100 Subject: [PATCH] remove item from server --- server/src/game.rs | 7 --- server/src/main.rs | 2 +- server/src/rpc.rs | 109 ++++++++++++--------------------------------- server/src/vbox.rs | 73 ++++++++++++++++++------------ 4 files changed, 74 insertions(+), 117 deletions(-) diff --git a/server/src/game.rs b/server/src/game.rs index 81663377..8fe41f5b 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -11,7 +11,6 @@ use account::Account; use rpc::{GameStateParams, GameSkillParams, GamePveParams, GamePvpParams, GameJoinParams}; use cryp::{Cryp, cryp_get}; use skill::{Skill, Cast, ResolutionResult}; -use item::{item_drop}; use zone::{node_finish}; use mob::{generate_mob_team}; @@ -685,12 +684,6 @@ pub fn game_update(game: &Game, tx: &mut Transaction) -> Result<(), Error> { result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?; if game.finished() { - if let Some(t) = game.winner() { - if !t.id.is_nil() { - item_drop(tx, t.id, game.mode)?; - } - } - // check for zone update if let Some((z, i)) = game.zone { node_finish(game, z, i, tx)?; diff --git a/server/src/main.rs b/server/src/main.rs index 31cef43c..570cfd98 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -26,7 +26,7 @@ mod spec; // mod passives; mod rpc; mod account; -mod item; +// mod item; mod zone; mod mob; diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 0b6a5774..b09976e6 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -18,11 +18,10 @@ use net::Db; use cryp::{Cryp, cryp_spawn, cryp_learn, cryp_forget, cryp_unspec}; use game::{Game, game_state, game_pve, game_pvp, game_join, game_joinable_list, game_skill}; use account::{Account, account_create, account_login, account_from_token, account_cryps, account_zone}; -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}; +use vbox::{Vbox, vbox_state, vbox_accept, vbox_discard, vbox_combine}; pub struct Rpc; @@ -77,16 +76,13 @@ impl Rpc { "zone_join" => Rpc::zone_join(data, &mut tx, account.unwrap(), client), "zone_close" => Rpc::zone_close(data, &mut tx, account.unwrap(), client), "account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client), - "account_items" => Rpc::account_items(data, &mut tx, account.unwrap(), client), "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_discard" => 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)), }; @@ -317,13 +313,6 @@ impl Rpc { }) } - fn account_items(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { - Ok(RpcResponse { - method: "account_items".to_string(), - params: RpcResult::ItemList(items_list(tx, &account)?) - }) - } - fn account_zone(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { Ok(RpcResponse { method: "zone_state".to_string(), @@ -331,49 +320,6 @@ impl Rpc { }) } - fn press_r(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { - for action in [ - ItemAction::RerollRedDamage, - ItemAction::RerollBlueDamage, - ItemAction::RerollSpeed, - ItemAction::RerollStamina, - ItemAction::RerollRedShield, - ItemAction::RerollBlueShield, - ItemAction::RerollEvasion, - ItemAction::SpecRedDamage5, - ItemAction::SpecBlueDamage5, - ].into_iter() { - let item = Item::new(*action, account.id); - item_create(item, tx, account.id)?; - } - - let res = RpcResponse { - method: "account_items".to_string(), - params: RpcResult::ItemList(items_list(tx, &account)?) - }; - - return Ok(res); - } - - - fn item_use(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { - let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - - item_use(msg.params, tx, &account)?; - - Rpc::send_msg(client, RpcResponse { - method: "account_items".to_string(), - params: RpcResult::ItemList(items_list(tx, &account)?) - })?; - - let cryps_list = RpcResponse { - method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &account)?) - }; - - return Ok(cryps_list); - } - fn zone_create(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { // let _msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; @@ -419,7 +365,7 @@ impl Rpc { } fn vbox_accept(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { - let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let response = RpcResponse { method: "vbox_state".to_string(), @@ -429,6 +375,18 @@ impl Rpc { return Ok(response); } + fn vbox_discard(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + + let response = RpcResponse { + method: "vbox_state".to_string(), + params: RpcResult::VboxState(vbox_discard(msg.params, tx, &account)?) + }; + + return Ok(response); + } + + fn vbox_combine(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; @@ -458,8 +416,6 @@ pub enum RpcResult { CrypList(Vec), GameState(Game), GameJoinableList(Vec), - ItemList(Vec), - ItemUse(()), ZoneState(Zone), ZoneClose(()), @@ -627,24 +583,6 @@ struct AccountCrypsMsg { params: (), } -#[derive(Debug,Clone,Serialize,Deserialize)] -struct ItemListMsg { - method: String, - params: (), -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -struct ItemUseMsg { - method: String, - params: ItemUseParams, -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -pub struct ItemUseParams { - pub item: Uuid, - pub target: Uuid, -} - #[derive(Debug,Clone,Serialize,Deserialize)] struct ZoneCreateMsg { method: String, @@ -687,17 +625,28 @@ pub struct VboxStateParams { } #[derive(Debug,Clone,Serialize,Deserialize)] -struct VboxBuyMsg { +struct VboxAcceptMsg { method: String, - params: VboxBuyParams, + params: VboxAcceptParams, } #[derive(Debug,Clone,Serialize,Deserialize)] -pub struct VboxBuyParams { +pub struct VboxAcceptParams { pub game_id: Uuid, pub index: u8, } +#[derive(Debug,Clone,Serialize,Deserialize)] +struct VboxDiscardMsg { + method: String, + params: VboxDiscardParams, +} + +#[derive(Debug,Clone,Serialize,Deserialize)] +pub struct VboxDiscardParams { + pub game_id: Uuid, +} + #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxCombineMsg { method: String, diff --git a/server/src/vbox.rs b/server/src/vbox.rs index 7cb7a3ff..2c89937c 100644 --- a/server/src/vbox.rs +++ b/server/src/vbox.rs @@ -1,5 +1,11 @@ +use std::iter; use uuid::Uuid; + +// drops use rand::prelude::*; +use rand::{thread_rng}; +use rand::distributions::{LogNormal,WeightedIndex}; + use serde_cbor::{from_slice, to_vec}; use postgres::transaction::Transaction; @@ -8,15 +14,21 @@ use failure::Error; use failure::err_msg; use account::Account; -use rpc::{VboxStateParams, VboxBuyParams, VboxCombineParams}; -use item::{Item}; +use rpc::{VboxStateParams, VboxAcceptParams, VboxDiscardParams, VboxCombineParams}; + +#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] +pub enum Var { + Red, + Green, + Blue, +} #[derive(Debug,Clone,Serialize,Deserialize)] pub struct Vbox { pub id: Uuid, pub balance: u16, - pub free: Vec, - pub bound: Vec, + pub free: Vec, + pub bound: Vec, pub game: Uuid, pub account: Uuid, } @@ -32,6 +44,25 @@ impl Vbox { balance: 0, } } + + pub fn fill(mut self: Vbox) -> Vbox { + let vars = vec![ + (Var::Red, 1), + (Var::Green, 1), + (Var::Blue, 1), + ]; + + self.free = iter:: + repeat_with(|| { + let mut rng = thread_rng(); + let dist = WeightedIndex::new(vars.iter().map(|item| item.1)).unwrap(); + return vars[dist.sample(&mut rng)].0; + }) + .take(8) + .collect::>(); + + self + } } pub fn vbox_write(vbox: Vbox, tx: &mut Transaction, account: &Account) -> Result { @@ -81,37 +112,21 @@ pub fn vbox_state(params: VboxStateParams, tx: &mut Transaction, account: &Accou Ok(v) => Ok(v), Err(e) => { println!("{:?}", e); - vbox_write(Vbox::new(account.id, params.game_id), tx, account) + vbox_write(Vbox::new(account.id, params.game_id).fill(), tx, account) } } } -pub fn vbox_accept(params: VboxBuyParams, tx: &mut Transaction, account: &Account) -> Result { - return vbox_get(tx, params.game_id, account) +pub fn vbox_discard(params: VboxDiscardParams, tx: &mut Transaction, account: &Account) -> Result { + let vbox = vbox_get(tx, params.game_id, account)?; + return vbox_write(vbox.fill(), tx, account); +} + +pub fn vbox_accept(params: VboxAcceptParams, tx: &mut Transaction, account: &Account) -> Result { + let vbox = vbox_get(tx, params.game_id, account)?; + return Ok(vbox); } pub fn vbox_combine(params: VboxCombineParams, tx: &mut Transaction, account: &Account) -> Result { 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(()) -// }