Skip to content

Commit

Permalink
add substate transition (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzn authored Nov 1, 2024
1 parent a552112 commit b05fb52
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/models/status/round/challenge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ mod errors {
const SUBSTATE_NOT_OBSTACLE: felt252 = 'Substate not OBSTACLE';
const USE_INVALID_CARD: felt252 = 'Use an invalid card';
const ARRAY_REPEATED_ELEMENTS: felt252 = 'Array has repeated elements';
const OUT_OF_PLAYS: felt252 = 'Out of plays';
const OUT_OF_DISCARDS: felt252 = 'Out of discards';
}

#[generate_trait]
Expand All @@ -62,6 +64,9 @@ impl ChallengeImpl of ChallengeTrait {
assert(game.state == GameState::IN_GAME, errors::STATE_NOT_IN_GAME);
assert(game.substate == GameSubState::OBSTACLE, errors::SUBSTATE_NOT_OBSTACLE);

let mut challenge_player = ChallengePlayerStore::get(world, game_id);
assert(challenge_player.plays > 0, errors::OUT_OF_PLAYS);

let mut store = StoreTrait::new(world);
let mut current_special_cards_index = _current_special_cards(ref store, @game);
let (mut cards, _, _) = _get_cards(
Expand All @@ -75,9 +80,10 @@ impl ChallengeImpl of ChallengeTrait {
ChallengeStore::set(@challenge, world);

if Self::is_completed(@world, game_id) {
emit!(world, ChallengeCompleted { player: game.owner, player_name: game.player_name, game_id })
emit!(world, ChallengeCompleted { player: game.owner, player_name: game.player_name, game_id });
game.substate = GameSubState::CREATE_LEVEL;
GameStore::set(@game, world);
} else {
let mut challenge_player = ChallengePlayerStore::get(world, game_id);
challenge_player.plays -= 1;
emit!(world, (challenge_player));
ChallengePlayerStore::set(@challenge_player, world);
Expand All @@ -99,6 +105,9 @@ impl ChallengeImpl of ChallengeTrait {
assert(game.state == GameState::IN_GAME, errors::STATE_NOT_IN_GAME);
assert(game.substate == GameSubState::OBSTACLE, errors::SUBSTATE_NOT_OBSTACLE);

let mut challenge_player = ChallengePlayerStore::get(world, game_id);
assert(challenge_player.discards > 0, errors::OUT_OF_DISCARDS);

let mut store = StoreTrait::new(world);
let mut cards = array![];
let mut idx = 0;
Expand Down Expand Up @@ -126,7 +135,6 @@ impl ChallengeImpl of ChallengeTrait {
};
CurrentHandCardTrait::refresh(world, game_id, cards);

let mut challenge_player = ChallengePlayerStore::get(world, game_id);
challenge_player.discards -= 1;
emit!(world, (challenge_player));
ChallengePlayerStore::set(@challenge_player, world);
Expand Down

0 comments on commit b05fb52

Please sign in to comment.