rejoin games
This commit is contained in:
+29
-15
@@ -98,7 +98,11 @@ impl Game {
|
||||
self
|
||||
}
|
||||
|
||||
fn add_team(&mut self, team: Team) -> Result<&mut Game, Error> {
|
||||
fn already_joined(&self, team_id: Uuid) -> bool {
|
||||
self.teams.iter().any(|t| t.id == team_id)
|
||||
}
|
||||
|
||||
fn team_add(&mut self, team: Team) -> Result<&mut Game, Error> {
|
||||
if self.teams.len() == self.team_num {
|
||||
return Err(err_msg("maximum number of teams"));
|
||||
}
|
||||
@@ -154,6 +158,10 @@ impl Game {
|
||||
self
|
||||
}
|
||||
|
||||
fn joinable(&self) -> bool {
|
||||
self.phase == Phase::Start
|
||||
}
|
||||
|
||||
fn can_start(&self) -> bool {
|
||||
self.teams.len() == self.team_num
|
||||
}
|
||||
@@ -497,13 +505,13 @@ pub fn game_new(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
|
||||
let game_bytes = to_vec(&game)?;
|
||||
|
||||
let query = "
|
||||
INSERT INTO games (id, data)
|
||||
VALUES ($1, $2)
|
||||
INSERT INTO games (id, data, joinable)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&game.id, &game_bytes])?;
|
||||
.query(query, &[&game.id, &game.joinable(), &game_bytes])?;
|
||||
|
||||
result.iter().next().ok_or(format_err!("no game written"))?;
|
||||
|
||||
@@ -568,13 +576,13 @@ pub fn game_update(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
|
||||
|
||||
let query = "
|
||||
UPDATE games
|
||||
SET data = $1
|
||||
WHERE id = $2
|
||||
SET (data = $1, joinable = $2)
|
||||
WHERE id = $3
|
||||
RETURNING id, data;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&game_bytes, &game.id])?;
|
||||
.query(query, &[&game_bytes, &game.joinable(), &game.id])?;
|
||||
|
||||
result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?;
|
||||
|
||||
@@ -634,8 +642,8 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
|
||||
.set_cryps(mobs);
|
||||
|
||||
game
|
||||
.add_team(plr_team)?
|
||||
.add_team(mob_team)?;
|
||||
.team_add(plr_team)?
|
||||
.team_add(mob_team)?;
|
||||
|
||||
game.start();
|
||||
|
||||
@@ -665,7 +673,7 @@ pub fn game_pvp(params: GamePvpParams, tx: &mut Transaction, account: &Account)
|
||||
let mut team = Team::new(account.id);
|
||||
team.set_cryps(cryps);
|
||||
|
||||
game.add_team(team)?;
|
||||
game.team_add(team)?;
|
||||
|
||||
// persist
|
||||
game_new(&game, tx)?;
|
||||
@@ -676,6 +684,13 @@ pub fn game_pvp(params: GamePvpParams, tx: &mut Transaction, account: &Account)
|
||||
|
||||
pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
|
||||
let mut game = game_get(tx, params.game_id)?;
|
||||
|
||||
// rejoining a game from the FE list
|
||||
if game.already_joined(account.id) {
|
||||
return Ok(game);
|
||||
}
|
||||
|
||||
// ok actually adding a new team
|
||||
let game_id = game.id;
|
||||
|
||||
let cryps = params.cryp_ids
|
||||
@@ -689,8 +704,7 @@ pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account
|
||||
|
||||
let mut team = Team::new(account.id);
|
||||
team.set_cryps(cryps);
|
||||
|
||||
game.add_team(team)?;
|
||||
game.team_add(team)?;
|
||||
|
||||
if game.can_start() {
|
||||
game.start();
|
||||
@@ -706,7 +720,7 @@ pub fn game_joinable_list(tx: &mut Transaction, _account: &Account) -> Result<Ve
|
||||
let query = "
|
||||
SELECT games.data
|
||||
FROM games
|
||||
WHERE open;
|
||||
WHERE joinable;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
@@ -771,8 +785,8 @@ mod tests {
|
||||
.set_cryps(vec![y]);
|
||||
|
||||
game
|
||||
.add_team(x_team).unwrap()
|
||||
.add_team(y_team).unwrap();
|
||||
.team_add(x_team).unwrap()
|
||||
.team_add(y_team).unwrap();
|
||||
|
||||
assert!(game.can_start());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user