From b5b2e95f34f1e3706cd2cf315f2536cbabc34dcb Mon Sep 17 00:00:00 2001 From: ntr Date: Thu, 25 Apr 2019 19:49:25 +1000 Subject: [PATCH] game finished --- server/src/game.rs | 18 +++--- server/src/instance.rs | 135 +++++++++++------------------------------ 2 files changed, 43 insertions(+), 110 deletions(-) diff --git a/server/src/game.rs b/server/src/game.rs index e64e6e80..eaa9ff53 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -12,7 +12,7 @@ use rpc::{GameStateParams, GameSkillParams}; use cryp::{Cryp}; use skill::{Skill, Effect, Cast, Resolution, Event, resolve}; use player::{Player}; -// use instance::{instance_game_finished, global_game_finished}; +use instance::{instance_game_finished, global_game_finished}; #[derive(Debug,Clone,Serialize,Deserialize)] pub struct Team { @@ -798,14 +798,14 @@ 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(i) = game.instance { - // match i == Uuid::nil() { - // true => global_game_finished(tx, &game)?, - // false => instance_game_finished(tx, &game, i)?, - // } - // } - // } + if game.finished() { + if let Some(i) = game.instance { + match i == Uuid::nil() { + true => global_game_finished(tx, &game)?, + false => instance_game_finished(tx, &game, i)?, + } + } + } return Ok(()); } diff --git a/server/src/instance.rs b/server/src/instance.rs index d4c1e13b..763cc1ef 100644 --- a/server/src/instance.rs +++ b/server/src/instance.rs @@ -228,6 +228,20 @@ impl Instance { .ok_or(err_msg("could not find matchup in current round"))? .finished = true; + let winner = game.winner().ok_or(err_msg("game not finished"))?; + + for team in game.teams.iter() { + let mut player = self.account_player(team.id)?; + match team.id == winner.id { + true => player.add_win(), + false => player.add_loss(), + }; + } + + if self.all_games_finished() { + self.next_round(); + } + Ok(self) } @@ -627,112 +641,31 @@ pub fn instance_state(params: InstanceStateParams, tx: &mut Transaction, account Ok(RpcResult::InstanceState(instance)) } -// pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account: &Account) -> Result { -// let mut player = player_get(tx, account.id, params.instance_id)?; +pub fn global_game_finished(tx: &mut Transaction, game: &Game) -> Result<(), Error> { + let winner = game.winner().ok_or(err_msg("game not finished"))?; -// if params.instance_id == Uuid::nil() { -// return instance_ready_global(tx, account, player); -// } + for team in game.teams.iter() { + let mut player = player_get(tx, team.id, Uuid::nil())?; + match team.id == winner.id { + true => player.add_win(), + false => player.add_loss(), + }; + player.vbox.fill(); + player_update(tx, player, true)?; + } -// let mut instance = instance_get(tx, params.instance_id)?; + Ok(()) +} -// // attempting to re-ready -// // send game state -// match instance.player_ready(player.id) { -// Ok(_) => (), -// Err(_) => return game_get(tx, instance.current_round(player.id).game_id), -// }; +pub fn instance_game_finished(tx: &mut Transaction, game: &Game, instance_id: Uuid) -> Result<(), Error> { + let mut instance = instance_get(tx, instance_id)?; + instance.game_finished(game)?; + // println!("{:?}", instance_get(tx, instance_id)?); -// let game_id = instance.current_round(player.id).game_id; + instance_update(tx, instance)?; -// let game = match instance.player_has_pve_game(&player) { -// true => match game_get(tx, game_id) { -// Ok(g) => g, -// Err(_) => { -// let game = instance.bot_vs_player_game(&player)?; -// game_write(&game, tx)?; -// game -// }, -// }, -// false => match game_get(tx, game_id) { -// Ok(_g) => game_instance_join(tx, player.clone(), game_id)?, -// Err(_) => game_instance_new(tx, player.clone(), game_id)?, -// } -// }; - -// player_update(tx, player, false)?; -// instance_update(tx, instance)?; -// return Ok(game); -// } - -// pub fn global_game_finished(tx: &mut Transaction, game: &Game) -> Result<(), Error> { -// let winner = game.winner().ok_or(err_msg("game not finished"))?; - -// for team in game.teams.iter() { -// let mut player = player_get(tx, team.id, Uuid::nil())?; -// match team.id == winner.id { -// true => player.add_win(), -// false => player.add_loss(), -// }; -// player.vbox.fill(); -// player_update(tx, player, true)?; -// } - -// Ok(()) -// } - -// 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() { -// match team.bot { -// true => { -// let mut instance = instance_get(tx, instance_id)?; -// { -// let mut player = instance.players.iter_mut().find(|p| p.account == team.id).unwrap(); -// match team.id == winner.id { -// true => player.add_win(), -// false => player.add_loss(), -// }; -// } -// instance_update(tx, instance)?; -// }, -// false => { -// let mut player = player_get(tx, team.id, instance_id)?; -// match team.id == winner.id { -// true => player.add_win(), -// false => player.add_loss(), -// }; -// player_update(tx, player, true)?; -// }, -// } -// } - -// // 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.all_games_finished() { -// instance.next_round(); -// 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, false)?; -// } -// } - -// // println!("{:?}", instance_get(tx, instance_id)?); - -// Ok(()) -// } + Ok(()) +} #[cfg(test)] mod tests {