diff --git a/client/src/socket.js b/client/src/socket.js index 37f18af7..81896bb5 100644 --- a/client/src/socket.js +++ b/client/src/socket.js @@ -141,7 +141,7 @@ function createSocket(events) { } function sendInstanceJoin(cryps) { - send({ method: 'instance_join', params: { cryp_ids: cryps, pve: true } }); + send({ method: 'instance_join', params: { cryp_ids: cryps, pve: false } }); } function sendInstanceReady(instanceId) { @@ -255,7 +255,7 @@ function createSocket(events) { if (!account) events.loginPrompt(); if (process.env.NODE_ENV !== 'production') { - send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } }); + // send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } }); } return true; diff --git a/server/src/game.rs b/server/src/game.rs index eb02fa7f..133dfba8 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -13,12 +13,14 @@ use cryp::{Cryp, cryp_get}; use skill::{Skill, Cast, ResolutionResult}; use mob::{generate_mob_team}; use player::{Player}; +use instance::{instance_game_finished}; pub type Log = Vec; #[derive(Debug,Clone,Serialize,Deserialize)] pub struct Team { pub id: Uuid, + pub player: Option, pub bot: bool, cryps: Vec, } @@ -29,9 +31,15 @@ impl Team { id: account, cryps: vec![], bot: false, + player: None, }; } + pub fn set_player(&mut self, id: Uuid) -> &mut Team { + self.player = Some(id); + self + } + pub fn set_bot(&mut self) -> &mut Team { self.bot = true; self @@ -87,7 +95,7 @@ pub struct Game { pub stack: Vec, pub resolved: Vec, pub log: Vec, - pub zone: Option<(Uuid, u32)>, + pub instance: Option, pub mode: GameMode, } @@ -103,7 +111,7 @@ impl Game { stack: vec![], resolved: vec![], log: vec![], - zone: None, + instance: None, mode: GameMode::Normal, }; } @@ -123,8 +131,8 @@ impl Game { self } - pub fn set_zone(&mut self, id: Uuid, node: u32) -> &mut Game { - self.zone = Some((id, node)); + pub fn set_instance(&mut self, id: Uuid) -> &mut Game { + self.instance = Some(id); self } @@ -605,7 +613,7 @@ pub fn game_skill(params: GameSkillParams, tx: &mut Transaction, account: &Accou // game.resolve_phase_start(); // } -// game_update(&game, tx)?; +// game_update(game, tx)?; // Ok(game) // } @@ -670,13 +678,11 @@ 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() { - // // check for zone update - // if let Some((z, i)) = game.zone { - // node_finish(game, z, i, tx)?; - // } - - // } + if game.finished() { + if let Some(i) = game.instance { + instance_game_finished(tx, &game, i)?; + } + } return Ok(()); } @@ -741,6 +747,7 @@ pub fn game_instance_new(tx: &mut Transaction, player: Player, game_id: Uuid) -> .set_pve(false) .set_team_num(2) .set_team_size(3) + .set_instance(player.instance) .set_mode(GameMode::Pvp); // create the initiators team @@ -758,7 +765,7 @@ pub fn game_instance_new(tx: &mut Transaction, player: Player, game_id: Uuid) -> pub fn game_instance_join(tx: &mut Transaction, player: Player, game_id: Uuid) -> Result { let mut game = game_get(tx, game_id)?; - let mut team = Team::new(player.id); + let mut team = Team::new(player.account); team.set_cryps(player.cryps); game.team_add(team)?; diff --git a/server/src/instance.rs b/server/src/instance.rs index 472d6c57..79d67dc4 100644 --- a/server/src/instance.rs +++ b/server/src/instance.rs @@ -28,7 +28,7 @@ enum InstancePhase { struct Round { player_ids: Vec, game_id: Uuid, - outcome: Option<(Uuid, Uuid)>, + finished: bool, } #[derive(Debug,Clone,Serialize,Deserialize)] @@ -72,7 +72,7 @@ impl Instance { pub fn player_update(mut self, player: Player) -> Result { if self.phase != InstancePhase::Vbox { - return Err(err_msg("instance not in vbox phase")); + return Err(format_err!("instance not in vbox phase {:?}", self.phase)); } let i = self.players @@ -143,28 +143,11 @@ impl Instance { // self.players.sort_unstable_by_key(|p| p.id); self.generate_rounds(); self.open = false; - self.phase = InstancePhase::Vbox; - self.vbox_phase_start() } fn vbox_phase_start(&mut self) -> &mut Instance { - // match self.rounds.last() { - // Some(r) => { - // for round in r { - // { - // let winner = self.players.iter_mut().find(|p| p.account == round.outcome.unwrap().0).unwrap(); - // winner.add_win(); - // } - // { - // let loser = self.players.iter_mut().find(|p| p.account == round.outcome.unwrap().1).unwrap(); - // loser.add_loss(); - // } - // } - // } - // None => (), - // } - + self.phase = InstancePhase::Vbox; self.bot_vbox_phase(); self @@ -189,15 +172,27 @@ impl Instance { self } + fn game_finished(&mut self, game: &Game) -> Result<&mut Instance, Error> { + let round_num = self.rounds.len() - 1; + self.rounds[round_num] + .iter_mut() + .find(|r| r.game_id == game.id) + .ok_or(err_msg("could not find matchup in current round"))? + .finished = true; + + Ok(self) + } + fn games_phase_finished(&self) -> bool { match self.rounds.last() { - Some(r) => r.iter().all(|g| g.outcome.is_some()), + Some(r) => r.iter().all(|g| g.finished), None => true, } } fn bot_vbox_phase(&mut self) -> &mut Instance { for bot in self.players.iter_mut().filter(|p| p.bot) { + bot.vbox.fill(); bot.set_ready(true); } @@ -242,7 +237,7 @@ impl Instance { game.start(); assert!(game.finished()); - round.outcome = Some((game.winner().unwrap().id, Uuid::new_v4())); + round.finished = true; } } } @@ -272,7 +267,7 @@ impl Instance { .map(|(i, id)| Round { player_ids: vec![*id, matched_players[np - (i + 1)]], game_id: Uuid::new_v4(), - outcome: None, + finished: false, }) .collect::>(); @@ -382,6 +377,8 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account: }, }; + println!("{:?}", instance); + let cryps = params.cryp_ids .iter() .map(|id| cryp_get(tx, *id, account.id)) @@ -433,6 +430,47 @@ pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account return Ok(game); } +pub fn instance_game_finished(tx: &mut Transaction, game: &Game, instance_id: Uuid) -> Result<(), Error> { + // update scores + let winner = game.winner().ok_or(err_msg("game not finished"))?; + + for team in game.teams.iter() + .filter(|t| !t.bot) + .filter(|t| t.player.is_some()) { + let mut player = player_get(tx, team.id, instance_id)?; + match team.id == winner.id { + true => player.add_win(), + false => player.add_loss(), + }; + println!("{:?}", player); + player_update(tx, player)?; + } + + // update instance and persist + let mut instance = instance_get(tx, instance_id)?; + instance.game_finished(game)?; + let mut instance = instance_update(tx, instance)?; + + // now modify the players and write them all + // each player update will also update the instance in db + if instance.games_phase_finished() { + instance.vbox_phase_start(); + let instance = instance_update(tx, instance)?; + + for player in instance.players + .iter() + .filter(|p| !p.bot) { + let mut player = player_get(tx, player.account, instance_id)?; + player.vbox.fill(); + player_update(tx, player)?; + } + } + + println!("{:?}", instance_get(tx, instance_id)?); + + Ok(()) +} + #[cfg(test)] mod tests { use super::*;