Skip to content

Commit

Permalink
refactor: pass game_id via parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagadeeshftw committed Feb 9, 2025
1 parent 999fb5d commit c224fcb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
23 changes: 10 additions & 13 deletions onchain/src/systems/game_actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait IGameActions<T> {
ref self: T, game_mode: GameMode, player_color: PlayerColor, number_of_players: u8,
) -> u64;
fn join(ref self: T, player_color: PlayerColor, game_id: u64);
fn move(ref self: T, pos: felt252);
fn move(ref self: T, pos: felt252, game_id: u64);
fn roll(ref self: T) -> (u8, u8);

fn get_current_game_id(self: @T) -> u64;
Expand All @@ -20,8 +20,8 @@ trait IGameActions<T> {
fn get_username_from_address(self: @T, address: ContractAddress) -> felt252;
fn get_address_from_username(self: @T, username: felt252) -> ContractAddress;
fn move_deducer(ref self: T, val: u32, dice_throw: u32) -> (u32, bool, bool);
fn get_next_color(ref self: T, current_color: u8, isChance: bool) -> u8;
fn get_active_colors(self: @T) -> Array<u8>;
fn get_next_color(ref self: T, current_color: u8, isChance: bool, game_id: u64) -> u8;
fn get_active_colors(self: @T, game_id: u64) -> Array<u8>;
}

#[dojo::contract]
Expand Down Expand Up @@ -276,13 +276,10 @@ pub mod GameActions {
world.write_model(@game);
}

fn move(ref self: ContractState, pos: felt252) {
fn move(ref self: ContractState, pos: felt252, game_id: u64) {
// Get world state
let mut world = self.world_default();

// Get the current game ID
let game_id = self.get_current_game_id();

// Retrieve the game state
let mut game: Game = world.read_model(game_id);

Expand Down Expand Up @@ -347,7 +344,7 @@ pub mod GameActions {
// Get the safe positions array
let safe_pos = get_safe_positions();

let active_colors = self.get_active_colors();
let active_colors = self.get_active_colors(game_id);

// Check if the new position is not a safe position
if !contains(safe_pos, *val) {
Expand Down Expand Up @@ -403,7 +400,7 @@ pub mod GameActions {
// Update the game condition
game.game_condition = condition.clone();

let active_colors = self.get_active_colors();
let active_colors = self.get_active_colors(game_id);

// Convert the condition back to array positions
let current_condition = condition;
Expand Down Expand Up @@ -480,7 +477,7 @@ pub mod GameActions {
k += 1;
};

let mut new_color = self.get_next_color(color, isChance);
let mut new_color = self.get_next_color(color, isChance, game_id);

// Get the player addresses
let red_address = game.player_red;
Expand Down Expand Up @@ -657,9 +654,9 @@ pub mod GameActions {
username_map.address
}

fn get_next_color(ref self: ContractState, current_color: u8, isChance: bool) -> u8 {
fn get_next_color(ref self: ContractState, current_color: u8, isChance: bool, game_id: u64) -> u8 {
// Gather only active colors
let mut active_colors: Array<u8> = self.get_active_colors();
let mut active_colors: Array<u8> = self.get_active_colors(game_id);

// Find the index of current_color
let mut idx = 0_usize;
Expand All @@ -682,7 +679,7 @@ pub mod GameActions {
new_color
}

fn get_active_colors(self: @ContractState) -> Array<u8> {
fn get_active_colors(self: @ContractState, game_id: u64) -> Array<u8> {
let mut world = self.world_default();
let game_id = self.get_current_game_id();
let game: Game = world.read_model(game_id);
Expand Down
34 changes: 17 additions & 17 deletions onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ mod tests {

testing::set_contract_address(caller);
// Move piece from initial position with dice throw 6
game_action_system.move('r0');
game_action_system.move('r0', game_id);

let game: Game = world.read_model(game_id);

Expand Down Expand Up @@ -470,7 +470,7 @@ mod tests {
world.write_model(@game);

testing::set_contract_address(caller);
game_action_system.move('r0');
game_action_system.move('r0', game_id);

// Verify the new position
let game: Game = world.read_model(game_id);
Expand Down Expand Up @@ -512,14 +512,14 @@ mod tests {
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller_blue);
game_action_system.move('b1'); // move from its initial position to 1.
game_action_system.move('b1', game_id); // move from its initial position to 1.

let mut game: Game = world.read_model(game_id);
game.dice_face = 5;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller_blue);
game_action_system.move('b1'); // move from 1 to 6.
game_action_system.move('b1', game_id); // move from 1 to 6.

// Verify the new position
let game: Game = world.read_model(game_id);
Expand Down Expand Up @@ -550,21 +550,21 @@ mod tests {
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g1'); // move from its initial position to 14.
game_action_system.move('g1', game_id); // move from its initial position to 14.

let mut game: Game = world.read_model(game_id);
game.dice_face = 5;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g1'); // move from 14 to 19.
game_action_system.move('g1', game_id); // move from 14 to 19.

let mut game: Game = world.read_model(game_id);
game.dice_face = 3;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g1'); // move from 19 to 22.
game_action_system.move('g1', game_id); // move from 19 to 22.

// Verify the new position
let game: Game = world.read_model(game_id);
Expand Down Expand Up @@ -594,28 +594,28 @@ mod tests {
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g1'); // move g1 from its initial position to 14.
game_action_system.move('g1', game_id); // move g1 from its initial position to 14.

let mut game: Game = world.read_model(game_id);
game.dice_face = 6;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g2'); // move g2 from its initial position to 14.
game_action_system.move('g2', game_id); // move g2 from its initial position to 14.

let mut game: Game = world.read_model(game_id);
game.dice_face = 5;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g1'); // move from 14 to 19.
game_action_system.move('g1', game_id); // move from 14 to 19.

let mut game: Game = world.read_model(game_id);
game.dice_face = 5;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller);
game_action_system.move('g2'); // move from 14 to 19.
game_action_system.move('g2', game_id); // move from 14 to 19.

// Verify the new position
let game: Game = world.read_model(game_id);
Expand Down Expand Up @@ -671,7 +671,7 @@ mod tests {
testing::set_contract_address(caller_red);

// Move red piece to position 15
game_action_system.move('r0');
game_action_system.move('r0', game_id);

// Verify the new positions
game = world.read_model(game_id);
Expand Down Expand Up @@ -718,7 +718,7 @@ mod tests {
assert(game.game_condition == game_condition, 'Game Condition should match');

testing::set_contract_address(caller_red);
game_action_system.move('r0');
game_action_system.move('r0', game_id);

// Verify the new positions
game = world.read_model(game_id);
Expand Down Expand Up @@ -755,7 +755,7 @@ mod tests {
world.write_model(@game);

testing::set_contract_address(caller_red);
game_action_system.move('r3');
game_action_system.move('r3', game_id);

// Verify the new positions and winning state
game = world.read_model(game_id);
Expand Down Expand Up @@ -813,14 +813,14 @@ mod tests {
// Assume it is blue's turn; blue makes a move.
testing::set_contract_address(caller_blue);
// Using 'b0' token to simulate blue's move.
game_action_system.move('b0');
game_action_system.move('b0', game_id);

let mut game: Game = world.read_model(game_id);
game.dice_face = 5;
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller_blue);
game_action_system.move('b0'); // move from 1 to 6.
game_action_system.move('b0', game_id); // move from 1 to 6.

let mut game: Game = world.read_model(game_id);

Expand Down Expand Up @@ -863,7 +863,7 @@ mod tests {
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);
testing::set_contract_address(caller_red);
game_action_system.move('r0');
game_action_system.move('r0', game_id);

game = world.read_model(game_id);
assert(game.next_player == game.player_red, 'Red should get an extra turn');
Expand Down

0 comments on commit c224fcb

Please sign in to comment.