Skip to content

Commit

Permalink
🐛 Fix nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Feb 26, 2024
1 parent 2c9da73 commit a04d977
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/events.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Battle {
#[key]
game_id: u32,
#[key]
nonce: felt252,
nonce: u32,
#[key]
battle_id: u32,
duel_id: u32,
Expand Down
24 changes: 12 additions & 12 deletions src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zconqueror::models::player::{Player, PlayerTrait};

const MINIMUM_PLAYER_COUNT: u8 = 2;
const MAXIMUM_PLAYER_COUNT: u8 = 6;
const TURN_COUNT: u8 = 3;
const TURN_COUNT: u32 = 3;

#[derive(Model, Copy, Drop, Serde)]
struct Game {
Expand All @@ -22,7 +22,7 @@ struct Game {
over: bool,
seed: felt252,
player_count: u8,
nonce: u8,
nonce: u32,
price: u256,
}

Expand Down Expand Up @@ -74,7 +74,7 @@ impl GameImpl of GameTrait {

#[inline(always)]
fn player(self: Game) -> u32 {
let index = self.nonce / TURN_COUNT % self.player_count;
let index = self.nonce / TURN_COUNT % self.player_count.into();
index.into()
}

Expand All @@ -86,7 +86,7 @@ impl GameImpl of GameTrait {

#[inline(always)]
fn next_player(self: Game) -> u32 {
let index = (self.nonce / TURN_COUNT + 1) % self.player_count;
let index = (self.nonce / TURN_COUNT + 1) % self.player_count.into();
index.into()
}

Expand Down Expand Up @@ -199,9 +199,9 @@ impl GameImpl of GameTrait {
}
}

impl U8IntoTurn of Into<u8, Turn> {
impl U32IntoTurn of Into<u32, Turn> {
#[inline(always)]
fn into(self: u8) -> Turn {
fn into(self: u32) -> Turn {
assert(self < 3, 'U8IntoTurn: invalid turn');
if self == 0 {
Turn::Supply
Expand All @@ -213,9 +213,9 @@ impl U8IntoTurn of Into<u8, Turn> {
}
}

impl TurnIntoU8 of Into<Turn, u8> {
impl TurnIntoU32 of Into<Turn, u32> {
#[inline(always)]
fn into(self: Turn) -> u8 {
fn into(self: Turn) -> u32 {
match self {
Turn::Supply => 0,
Turn::Attack => 1,
Expand Down Expand Up @@ -547,13 +547,13 @@ mod tests {
#[available_gas(100_000)]
fn test_game_get_turn_index() {
let mut game = GameTrait::new(ID, HOST, PRICE);
assert(game.turn().into() == 0_u8, 'Game: wrong turn index 0');
assert(game.turn().into() == 0_u32, 'Game: wrong turn index 0');
game.nonce += 1;
assert(game.turn().into() == 1_u8, 'Game: wrong turn index 1');
assert(game.turn().into() == 1_u32, 'Game: wrong turn index 1');
game.nonce += 1;
assert(game.turn().into() == 2_u8, 'Game: wrong turn index 2');
assert(game.turn().into() == 2_u32, 'Game: wrong turn index 2');
game.nonce += 1;
assert(game.turn().into() == 0_u8, 'Game: wrong turn index 3');
assert(game.turn().into() == 0_u32, 'Game: wrong turn index 3');
game.nonce += 1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/systems/play.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ mod play {
) {
// [Setup] Deck
let mut deck = DeckTrait::new(*game.seed, TILE_NUMBER.into());
deck.nonce = *game.nonce;
let nonce: u32 = (*game.nonce) % integer::BoundedU32::max();
deck.nonce = nonce.try_into().unwrap();
loop {
match players.pop_front() {
Option::Some(player) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/host.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_host_create_and_join() {
assert(game.over == false, 'Game: wrong status');
assert(game.player_count == PLAYER_COUNT, 'Game: wrong player count');
assert(game.player() >= 0, 'Game: wrong player index');
assert(game.turn().into() == 0_u8, 'Game: wrong player index');
assert(game.turn().into() == 0_u32, 'Game: wrong player index');

// [Assert] Players
let mut player_index: u8 = 0;
Expand Down

0 comments on commit a04d977

Please sign in to comment.