Ignore keep alive for completed games

This commit is contained in:
Michael Vines
2018-09-29 19:16:57 -07:00
parent aff5649b39
commit 41f6e27bba

View File

@ -12,6 +12,7 @@ pub enum Error {
InvalidArguments, InvalidArguments,
InvalidMove, InvalidMove,
InvalidUserdata, InvalidUserdata,
InvalidTimestamp,
NoGame, NoGame,
NotYourTurn, NotYourTurn,
PlayerNotFound, PlayerNotFound,
@ -74,7 +75,7 @@ impl Game {
#[cfg(test)] #[cfg(test)]
pub fn new(player_x: Pubkey, player_o: Pubkey) -> Game { pub fn new(player_x: Pubkey, player_o: Pubkey) -> Game {
let mut game = Game::create(&player_x); let mut game = Game::create(&player_x);
game.join(player_o, 0).unwrap(); game.join(player_o, 1).unwrap();
game game
} }
@ -82,8 +83,13 @@ impl Game {
if self.state == State::Waiting { if self.state == State::Waiting {
self.player_o = Some(player_o); self.player_o = Some(player_o);
self.state = State::XMove; self.state = State::XMove;
self.keep_alive[1] = timestamp;
Ok(()) if timestamp <= self.keep_alive[1] {
Err(Error::InvalidTimestamp)
} else {
self.keep_alive[1] = timestamp;
Ok(())
}
} else { } else {
Err(Error::GameInProgress) Err(Error::GameInProgress)
} }
@ -143,13 +149,25 @@ impl Game {
} }
pub fn keep_alive(self: &mut Game, player: Pubkey, timestamp: i64) -> Result<()> { pub fn keep_alive(self: &mut Game, player: Pubkey, timestamp: i64) -> Result<()> {
if player == self.player_x { match self.state {
self.keep_alive[0] = timestamp; State::Waiting | State::XMove | State::OMove => {
} else if Some(player) == self.player_o { if player == self.player_x {
self.keep_alive[1] = timestamp; if timestamp <= self.keep_alive[0] {
} else { Err(Error::InvalidTimestamp)?;
Err(Error::PlayerNotFound)?; }
} self.keep_alive[0] = timestamp;
} else if Some(player) == self.player_o {
if timestamp <= self.keep_alive[1] {
Err(Error::InvalidTimestamp)?;
}
self.keep_alive[1] = timestamp;
} else {
Err(Error::PlayerNotFound)?;
}
}
// Ignore keep_alive when game is no longer in progress
State::XWon | State::OWon | State::Draw => {}
};
Ok(()) Ok(())
} }
} }