instance game finished

This commit is contained in:
ntr 2019-03-02 20:10:30 +11:00
parent c60f38716f
commit f4e1c368e0
3 changed files with 83 additions and 38 deletions

View File

@ -141,7 +141,7 @@ function createSocket(events) {
} }
function sendInstanceJoin(cryps) { 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) { function sendInstanceReady(instanceId) {
@ -255,7 +255,7 @@ function createSocket(events) {
if (!account) events.loginPrompt(); if (!account) events.loginPrompt();
if (process.env.NODE_ENV !== 'production') { 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; return true;

View File

@ -13,12 +13,14 @@ use cryp::{Cryp, cryp_get};
use skill::{Skill, Cast, ResolutionResult}; use skill::{Skill, Cast, ResolutionResult};
use mob::{generate_mob_team}; use mob::{generate_mob_team};
use player::{Player}; use player::{Player};
use instance::{instance_game_finished};
pub type Log = Vec<String>; pub type Log = Vec<String>;
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Team { pub struct Team {
pub id: Uuid, pub id: Uuid,
pub player: Option<Uuid>,
pub bot: bool, pub bot: bool,
cryps: Vec<Cryp>, cryps: Vec<Cryp>,
} }
@ -29,9 +31,15 @@ impl Team {
id: account, id: account,
cryps: vec![], cryps: vec![],
bot: false, 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 { pub fn set_bot(&mut self) -> &mut Team {
self.bot = true; self.bot = true;
self self
@ -87,7 +95,7 @@ pub struct Game {
pub stack: Vec<Cast>, pub stack: Vec<Cast>,
pub resolved: Vec<Cast>, pub resolved: Vec<Cast>,
pub log: Vec<String>, pub log: Vec<String>,
pub zone: Option<(Uuid, u32)>, pub instance: Option<Uuid>,
pub mode: GameMode, pub mode: GameMode,
} }
@ -103,7 +111,7 @@ impl Game {
stack: vec![], stack: vec![],
resolved: vec![], resolved: vec![],
log: vec![], log: vec![],
zone: None, instance: None,
mode: GameMode::Normal, mode: GameMode::Normal,
}; };
} }
@ -123,8 +131,8 @@ impl Game {
self self
} }
pub fn set_zone(&mut self, id: Uuid, node: u32) -> &mut Game { pub fn set_instance(&mut self, id: Uuid) -> &mut Game {
self.zone = Some((id, node)); self.instance = Some(id);
self self
} }
@ -605,7 +613,7 @@ pub fn game_skill(params: GameSkillParams, tx: &mut Transaction, account: &Accou
// game.resolve_phase_start(); // game.resolve_phase_start();
// } // }
// game_update(&game, tx)?; // game_update(game, tx)?;
// Ok(game) // 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))?; result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?;
// if game.finished() { if game.finished() {
// // check for zone update if let Some(i) = game.instance {
// if let Some((z, i)) = game.zone { instance_game_finished(tx, &game, i)?;
// node_finish(game, z, i, tx)?; }
// } }
// }
return Ok(()); return Ok(());
} }
@ -741,6 +747,7 @@ pub fn game_instance_new(tx: &mut Transaction, player: Player, game_id: Uuid) ->
.set_pve(false) .set_pve(false)
.set_team_num(2) .set_team_num(2)
.set_team_size(3) .set_team_size(3)
.set_instance(player.instance)
.set_mode(GameMode::Pvp); .set_mode(GameMode::Pvp);
// create the initiators team // 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<Game, Error> { pub fn game_instance_join(tx: &mut Transaction, player: Player, game_id: Uuid) -> Result<Game, Error> {
let mut game = game_get(tx, game_id)?; 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); team.set_cryps(player.cryps);
game.team_add(team)?; game.team_add(team)?;

View File

@ -28,7 +28,7 @@ enum InstancePhase {
struct Round { struct Round {
player_ids: Vec<Uuid>, player_ids: Vec<Uuid>,
game_id: Uuid, game_id: Uuid,
outcome: Option<(Uuid, Uuid)>, finished: bool,
} }
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
@ -72,7 +72,7 @@ impl Instance {
pub fn player_update(mut self, player: Player) -> Result<Instance, Error> { pub fn player_update(mut self, player: Player) -> Result<Instance, Error> {
if self.phase != InstancePhase::Vbox { 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 let i = self.players
@ -143,28 +143,11 @@ impl Instance {
// self.players.sort_unstable_by_key(|p| p.id); // self.players.sort_unstable_by_key(|p| p.id);
self.generate_rounds(); self.generate_rounds();
self.open = false; self.open = false;
self.phase = InstancePhase::Vbox;
self.vbox_phase_start() self.vbox_phase_start()
} }
fn vbox_phase_start(&mut self) -> &mut Instance { fn vbox_phase_start(&mut self) -> &mut Instance {
// match self.rounds.last() { self.phase = InstancePhase::Vbox;
// 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.bot_vbox_phase(); self.bot_vbox_phase();
self self
@ -189,15 +172,27 @@ impl Instance {
self 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 { fn games_phase_finished(&self) -> bool {
match self.rounds.last() { match self.rounds.last() {
Some(r) => r.iter().all(|g| g.outcome.is_some()), Some(r) => r.iter().all(|g| g.finished),
None => true, None => true,
} }
} }
fn bot_vbox_phase(&mut self) -> &mut Instance { fn bot_vbox_phase(&mut self) -> &mut Instance {
for bot in self.players.iter_mut().filter(|p| p.bot) { for bot in self.players.iter_mut().filter(|p| p.bot) {
bot.vbox.fill();
bot.set_ready(true); bot.set_ready(true);
} }
@ -242,7 +237,7 @@ impl Instance {
game.start(); game.start();
assert!(game.finished()); 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 { .map(|(i, id)| Round {
player_ids: vec![*id, matched_players[np - (i + 1)]], player_ids: vec![*id, matched_players[np - (i + 1)]],
game_id: Uuid::new_v4(), game_id: Uuid::new_v4(),
outcome: None, finished: false,
}) })
.collect::<Vec<Round>>(); .collect::<Vec<Round>>();
@ -382,6 +377,8 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account:
}, },
}; };
println!("{:?}", instance);
let cryps = params.cryp_ids let cryps = params.cryp_ids
.iter() .iter()
.map(|id| cryp_get(tx, *id, account.id)) .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); 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;