game finished
This commit is contained in:
parent
f37cb4a7e4
commit
b5b2e95f34
@ -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(());
|
||||
}
|
||||
|
||||
@ -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<Game, Error> {
|
||||
// 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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user