From 6cae6057b93c78da0311263134115ec22609a661 Mon Sep 17 00:00:00 2001 From: ntr Date: Thu, 6 Jun 2019 11:43:04 +1000 Subject: [PATCH] remove some rpc structs and fix warden --- WORKLOG.md | 1 - client/src/components/team.footer.jsx | 2 +- server/src/construct.rs | 26 +++++++++-- server/src/instance.rs | 35 ++++++++++++-- server/src/rpc.rs | 67 +++++++-------------------- 5 files changed, 72 insertions(+), 59 deletions(-) diff --git a/WORKLOG.md b/WORKLOG.md index a77f2303..85c3a5bd 100644 --- a/WORKLOG.md +++ b/WORKLOG.md @@ -40,7 +40,6 @@ hatred maybe reconnect based on time delta -remove rounds consolidate game and instance do not allow vbox actions for finished instances diff --git a/client/src/components/team.footer.jsx b/client/src/components/team.footer.jsx index 3fce4752..7b28dd9e 100644 --- a/client/src/components/team.footer.jsx +++ b/client/src/components/team.footer.jsx @@ -46,7 +46,7 @@ function TeamFooter(args) { ); diff --git a/server/src/construct.rs b/server/src/construct.rs index 155a9a58..e4126e58 100644 --- a/server/src/construct.rs +++ b/server/src/construct.rs @@ -204,7 +204,7 @@ pub struct Construct { pub green_power: ConstructStat, pub speed: ConstructStat, pub green_life: ConstructStat, - pub evasion: ConstructStat, + // pub evasion: ConstructStat, pub skills: Vec, pub effects: Vec, pub specs: Vec, @@ -225,7 +225,7 @@ impl Construct { green_power: ConstructStat { base: 256, value: 256, max: 256, stat: Stat::GreenPower }, green_life: ConstructStat { base: 1024, value: 1024, max: 1024, stat: Stat::GreenLife }, speed: ConstructStat { base: 128, value: 128, max: 128, stat: Stat::Speed }, - evasion: ConstructStat { base: 0, value: 0, max: 0, stat: Stat::Evasion }, + // evasion: ConstructStat { base: 0, value: 0, max: 0, stat: Stat::Evasion }, skills: vec![], effects: vec![], specs: vec![], @@ -295,7 +295,7 @@ impl Construct { self.red_life.recalculate(&self.specs, &self.colours, player_colours); self.blue_power.recalculate(&self.specs, &self.colours, player_colours); self.blue_life.recalculate(&self.specs, &self.colours, player_colours); - self.evasion.recalculate(&self.specs, &self.colours, player_colours); + // self.evasion.recalculate(&self.specs, &self.colours, player_colours); self.speed.recalculate(&self.specs, &self.colours, player_colours); self.green_power.recalculate(&self.specs, &self.colours, player_colours); self.green_life.recalculate(&self.specs, &self.colours, player_colours); @@ -799,6 +799,26 @@ impl Construct { // } } +pub fn construct_delete(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<(), Error> { + let query = " + DELETE + FROM constructs + WHERE id = $1; + and account = $2; + "; + + let result = tx + .execute(query, &[&id, &account_id])?; + + if result != 1 { + return Err(format_err!("unable to delete construct {:?}", id)); + } + + info!("construct deleted {:?}", id); + + return Ok(()); +} + pub fn construct_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result { let query = " SELECT data diff --git a/server/src/instance.rs b/server/src/instance.rs index be42eca7..b6a0bdb9 100644 --- a/server/src/instance.rs +++ b/server/src/instance.rs @@ -80,9 +80,10 @@ impl Instance { max_rounds: 5, name: String::new(), password: None, - phase_end: Utc::now(), phase_start: Utc::now(), - + phase_end: Utc::now() + .checked_add_signed(Duration::minutes(5)) + .expect("could not set phase end"), format: Format::Standard, } } @@ -118,6 +119,12 @@ impl Instance { } pub fn upkeep(mut self) -> (Instance, Vec) { + // time out lobbies that have been open too long + if self.phase == InstancePhase::Lobby && self.phase_timed_out() { + self.finish(); + return (self, vec![]); + } + if self.phase != InstancePhase::InProgress { return (self, vec![]); } @@ -562,7 +569,8 @@ pub fn instance_list(tx: &mut Transaction) -> Result, Error> { let query = " SELECT data, id FROM instances - WHERE open = true; + WHERE open = true + AND finished = false; "; let result = tx @@ -590,7 +598,6 @@ pub fn instances_need_upkeep(tx: &mut Transaction) -> Result, Erro SELECT data, id FROM instances WHERE finished = false - AND open = false AND upkeep < now() FOR UPDATE; "; @@ -664,6 +671,8 @@ pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account pub fn instance_state(params: InstanceStateParams, tx: &mut Transaction, _account: &Account) -> Result { let instance = instance_get(tx, params.instance_id)?; + println!("{:#?}", instance); + if let Some(game_id) = instance.current_game_id() { let game = game_get(tx, game_id)?; @@ -793,4 +802,22 @@ mod tests { // info!("{:#?}", instance); } + + #[test] + fn instance_upkeep_idle_lobby_test() { + let mut instance = Instance::new(); + + let player_account = Uuid::new_v4(); + let constructs = instance_mobs(player_account); + let player = Player::new(player_account, &"a".to_string(), constructs); + let _a_id = player.id; + + instance.add_player(player).expect("could not add player"); + assert!(!instance.can_start()); + + instance.phase_end = Utc::now().checked_sub_signed(Duration::minutes(61)).unwrap(); + let (instance, _new_games) = instance.upkeep(); + + assert!(instance.finished()); + } } diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 5b68311d..4dae9901 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -175,6 +175,20 @@ impl Rpc { Ok(construct_list) } + fn construct_delete(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { + let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; + + construct_delete(msg.params.id, tx, &account)?; + + let construct_list = RpcResponse { + method: "account_constructs".to_string(), + params: RpcResult::ConstructList(account_constructs(tx, &account)?) + }; + + Ok(construct_list) + } + + fn account_create(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; @@ -413,39 +427,14 @@ pub struct ConstructSpawnParams { } #[derive(Debug,Clone,Serialize,Deserialize)] -struct ConstructLearnMsg { +struct ConstructDeleteMsg { method: String, - params: ConstructLearnParams, + params: ConstructDeleteParams, } #[derive(Debug,Clone,Serialize,Deserialize)] -pub struct ConstructLearnParams { +pub struct ConstructDeleteParams { pub id: Uuid, - pub skill: Skill, -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -pub struct ConstructForgetParams { - pub id: Uuid, - pub skill: Skill, -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -struct ConstructForgetMsg { - method: String, - params: ConstructForgetParams, -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -pub struct ConstructUnspecParams { - pub id: Uuid, - pub spec: Spec, -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -struct ConstructUnspecMsg { - method: String, - params: ConstructUnspecParams, } #[derive(Debug,Clone,Serialize,Deserialize)] @@ -459,17 +448,6 @@ pub struct GameStateParams { pub id: Uuid, } -// #[derive(Debug,Clone,Serialize,Deserialize)] -// struct GamePveMsg { -// method: String, -// params: GamePveParams, -// } - -// #[derive(Debug,Clone,Serialize,Deserialize)] -// pub struct GamePveParams { -// pub construct_ids: Vec, -// } - #[derive(Debug,Clone,Serialize,Deserialize)] struct GameSkillMsg { method: String, @@ -562,17 +540,6 @@ pub struct InstanceStateParams { pub instance_id: Uuid, } -#[derive(Debug,Clone,Serialize,Deserialize)] -struct PlayerConstructsSetMsg { - method: String, - params: PlayerConstructsSetParams, -} - -#[derive(Debug,Clone,Serialize,Deserialize)] -pub struct PlayerConstructsSetParams { - pub construct_ids: Vec, -} - #[derive(Debug,Clone,Serialize,Deserialize)] struct VboxAcceptMsg { method: String,