Skip to content

Commit

Permalink
Allow canceling private game
Browse files Browse the repository at this point in the history
  • Loading branch information
lindlof committed Dec 5, 2020
1 parent c6187ba commit 78f308c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 23 additions & 16 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ pub fn join_game<S: Storage, A: Api, Q: Querier>(
Some(s) => {
// player2 joins player1 and lobby becomes empty
let p1_locator = Locator::load(&mut deps.storage, s)?;
if p1_locator.canceled {
return Err(StdError::generic_err("forbidden game canceled"));
}
let game_id = p1_locator.game;
let p2_locator = Locator::new(loc_b, game_id, env.message.sender);
p2_locator.save(&mut deps.storage);
Expand Down Expand Up @@ -170,6 +173,9 @@ pub fn private_game<S: Storage, A: Api, Q: Querier>(
}
Some(l) => {
// player2 joins player1
if l.canceled {
return Err(StdError::generic_err("forbidden game canceled"));
}
let game = Game::new(l.game, l.player, env.message.sender);
game.save(&mut deps.storage);
}
Expand All @@ -187,27 +193,28 @@ pub fn claim_inactivity<S: Storage, A: Api, Q: Querier>(
Err(_) => return Err(StdError::generic_err("bad_request invalid_locator")),
Ok(_) => (),
}
let locator = Locator::load(&deps.storage, bytes)?;
let mut locator = Locator::load(&deps.storage, bytes)?;
if locator.canceled {
return Err(StdError::generic_err("forbidden game canceled"));
}
let mut game;

match Game::load(&deps.storage, locator.game) {
Err(_) => {
match lobby_game(&mut deps.storage).load()? {
None => return Err(StdError::generic_err("not_found No game or lobby found")),
Some(s) => {
if s != bytes {
return Err(StdError::generic_err("not_found No game or lobby match"));
}
match Game::may_load(&deps.storage, locator.game)? {
None => {
if let Some(l) = lobby_game(&mut deps.storage).load()? {
if l == bytes {
lobby_game(&mut deps.storage).save(&None)?;
return Ok(payout(
env.contract.address,
env.message.sender,
Uint128(FUNDING_AMOUNT),
));
}
};
}
locator.canceled = true;
locator.save(&mut deps.storage);
return Ok(payout(
env.contract.address,
env.message.sender,
Uint128(FUNDING_AMOUNT),
));
}
Ok(g) => game = g,
Some(g) => game = g,
}

if game.game_over {
Expand Down
2 changes: 2 additions & 0 deletions contract/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Locator {
id: [u8; 32],
pub game: [u8; 32],
pub player: HumanAddr,
pub canceled: bool,
}

impl Locator {
Expand All @@ -20,6 +21,7 @@ impl Locator {
id: id,
game: game,
player: player,
canceled: false,
}
}

Expand Down

0 comments on commit 78f308c

Please sign in to comment.