Skip to content

Commit

Permalink
Fix challenge bool (#17)
Browse files Browse the repository at this point in the history
* add bool to challenge

* fix

* remove warning + test

* fix

* fix + add end game events
  • Loading branch information
dubzn authored Nov 1, 2024
1 parent 39305c4 commit 076e4ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/models/status/round/challenge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,19 @@ impl ChallengeImpl of ChallengeTrait {
_resolve_challenges(world, ref challenge, result_hand, ref hit_cards, @cards, hand_score);
ChallengeStore::set(@challenge, world);

if Self::is_completed(@world, game_id) {
if Self::is_completed(world, 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 {
challenge_player.plays -= 1;
if challenge_player.plays.is_zero() {
let play_game_over_event = PlayGameOverEvent { player: get_caller_address(), game_id: game.id };
emit!(world, (play_game_over_event));
game.state = GameState::FINISHED;
GameStore::set(@game, world);
return;
}
emit!(world, (challenge_player));
ChallengePlayerStore::set(@challenge_player, world);
}
Expand Down Expand Up @@ -152,8 +159,19 @@ impl ChallengeImpl of ChallengeTrait {
}
}

fn is_completed(world: @IWorldDispatcher, game_id: u32) -> bool {
ChallengeStore::get(*world, game_id).active_ids.is_empty()
fn is_completed(world: IWorldDispatcher, game_id: u32) -> bool {
let mut active_challenges_ids = ChallengeStore::get(world, game_id).active_ids;
let mut is_completed = true;
loop {
match active_challenges_ids.pop_front() {
Option::Some(challenge) => {
let (_, completed) = *challenge;
is_completed = is_completed && completed;
},
Option::None => { break; }
}
};
is_completed
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/test_challenge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod test_challenge {

let mut challenge = ChallengeStore::get(world, game.id);
assert(challenge.active_ids.len() == 4, 'wrong len');
assert(ChallengeImpl::is_completed(world, game.id), 'challenges should be completed');
loop {
match challenge.active_ids.pop_front() {
Option::Some(challenge) => {
Expand Down

0 comments on commit 076e4ca

Please sign in to comment.