instance ready rejoins
This commit is contained in:
parent
8abe18b0f1
commit
b1be8aa3bf
@ -732,19 +732,19 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
|
||||
Ok(game)
|
||||
}
|
||||
|
||||
pub fn game_instance_new(tx: &mut Transaction, player: Player, pve: bool) -> Result<Game, Error> {
|
||||
pub fn game_instance_new(tx: &mut Transaction, player: Player, game_id: Uuid) -> Result<Game, Error> {
|
||||
// create the game
|
||||
let mut game = Game::new();
|
||||
let game_id = game.id;
|
||||
game.id = game_id;
|
||||
|
||||
game
|
||||
.set_pve(pve)
|
||||
.set_pve(false)
|
||||
.set_team_num(2)
|
||||
.set_team_size(3)
|
||||
.set_mode(GameMode::Pvp);
|
||||
|
||||
// create the initiators team
|
||||
let mut team = Team::new(player.id);
|
||||
let mut team = Team::new(player.account);
|
||||
team.set_cryps(player.cryps);
|
||||
|
||||
game.team_add(team)?;
|
||||
|
||||
@ -14,7 +14,7 @@ use account::Account;
|
||||
use player::{Player, player_create, player_get, player_update};
|
||||
use cryp::{Cryp, cryp_get};
|
||||
use mob::{instance_mobs};
|
||||
use game::{Game, Team, game_get, game_instance_new, game_instance_join};
|
||||
use game::{Game, Team, game_get, game_write, game_instance_new, game_instance_join};
|
||||
|
||||
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
||||
enum InstancePhase {
|
||||
@ -27,7 +27,7 @@ enum InstancePhase {
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct Round {
|
||||
player_ids: Vec<Uuid>,
|
||||
game_id: Option<Uuid>,
|
||||
game_id: Uuid,
|
||||
outcome: Option<(Uuid, Uuid)>,
|
||||
}
|
||||
|
||||
@ -87,6 +87,36 @@ impl Instance {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn bot_vs_player_game(&self, player: &Player) -> Game {
|
||||
let current_round = self.current_round(player);
|
||||
|
||||
let plr = self.players.clone().into_iter().find(|p| p.id == player.id).unwrap();
|
||||
let bot = self.players.clone().into_iter().find(|p| p.id != player.id).unwrap();
|
||||
|
||||
let mut game = Game::new();
|
||||
game.id = current_round.game_id;
|
||||
game
|
||||
.set_pve(true)
|
||||
.set_team_num(2)
|
||||
.set_team_size(3);
|
||||
|
||||
// add the players
|
||||
let mut plr_team = Team::new(plr.account);
|
||||
plr_team.set_cryps(plr.cryps);
|
||||
|
||||
let mut bot_team = Team::new(bot.account);
|
||||
bot_team.set_cryps(bot.cryps);
|
||||
bot_team.set_bot();
|
||||
|
||||
game
|
||||
.team_add(plr_team).unwrap()
|
||||
.team_add(bot_team).unwrap();
|
||||
|
||||
game.start();
|
||||
|
||||
game
|
||||
}
|
||||
|
||||
fn can_start(&self) -> bool {
|
||||
match self.pve {
|
||||
true => self.players.len() == 16,
|
||||
@ -97,6 +127,7 @@ impl Instance {
|
||||
fn start(&mut self) -> &mut Instance {
|
||||
// self.players.sort_unstable_by_key(|p| p.id);
|
||||
self.generate_rounds();
|
||||
self.open = false;
|
||||
self.phase = InstancePhase::Vbox;
|
||||
|
||||
self.vbox_phase_start()
|
||||
@ -145,7 +176,7 @@ impl Instance {
|
||||
|
||||
fn games_phase_finished(&self) -> bool {
|
||||
match self.rounds.last() {
|
||||
Some(r) => r.iter().all(|g| g.game_id.is_some() && g.outcome.is_some()),
|
||||
Some(r) => r.iter().all(|g| g.outcome.is_some()),
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
@ -196,8 +227,6 @@ impl Instance {
|
||||
game.start();
|
||||
|
||||
assert!(game.finished());
|
||||
|
||||
round.game_id = Some(game.id);
|
||||
round.outcome = Some((game.winner().unwrap().id, Uuid::new_v4()));
|
||||
}
|
||||
}
|
||||
@ -227,7 +256,7 @@ impl Instance {
|
||||
.enumerate()
|
||||
.map(|(i, id)| Round {
|
||||
player_ids: vec![*id, matched_players[np - (i + 1)]],
|
||||
game_id: None,
|
||||
game_id: Uuid::new_v4(),
|
||||
outcome: None,
|
||||
})
|
||||
.collect::<Vec<Round>>();
|
||||
@ -237,22 +266,15 @@ impl Instance {
|
||||
self
|
||||
}
|
||||
|
||||
fn next_game_id(&mut self, player: &Player) -> Uuid {
|
||||
let current_round = self.rounds.len() - 1;
|
||||
fn current_round(&self, player: &Player) -> &Round {
|
||||
let round_num = self.rounds.len() - 1;
|
||||
|
||||
let next_round = self.rounds[current_round]
|
||||
.iter_mut()
|
||||
let current_round = self.rounds[round_num]
|
||||
.iter()
|
||||
.find(|g| g.player_ids.contains(&player.id))
|
||||
.unwrap();
|
||||
|
||||
match next_round.game_id {
|
||||
Some(id) => return id,
|
||||
None => {
|
||||
let id = Uuid::new_v4();
|
||||
next_round.game_id = Some(id);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
current_round
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,13 +300,13 @@ pub fn instance_write(instance: Instance, tx: &mut Transaction) -> Result<Instan
|
||||
|
||||
let query = "
|
||||
UPDATE instances
|
||||
SET data = $1
|
||||
WHERE id = $2
|
||||
SET data = $1, open = $2
|
||||
WHERE id = $3
|
||||
RETURNING id, data;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&instance_bytes, &instance.id])?;
|
||||
.query(query, &[&instance_bytes, &instance.open, &instance.id])?;
|
||||
|
||||
result.iter().next().ok_or(err_msg("no instance row returned"))?;
|
||||
|
||||
@ -371,19 +393,28 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account:
|
||||
pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
|
||||
let player = player_get(tx, account.id, params.instance_id)?;
|
||||
|
||||
let mut instance = instance_get(tx, params.instance_id)?
|
||||
let instance = instance_get(tx, params.instance_id)?
|
||||
.player_ready(player.clone())?;
|
||||
|
||||
let game_id = instance.next_game_id(&player);
|
||||
let game_id = instance.current_round(&player).game_id;
|
||||
|
||||
let game = match game_get(tx, game_id) {
|
||||
let game = match instance.pve {
|
||||
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(), instance.pve)?,
|
||||
Err(_) => game_instance_new(tx, player.clone(), game_id)?,
|
||||
}
|
||||
};
|
||||
|
||||
instance_write(instance, tx)?;
|
||||
player_update(tx, player)?;
|
||||
|
||||
instance_write(instance, tx)?;
|
||||
return Ok(game);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user