Skip to content

Commit

Permalink
Merge branch 'dev' into refactor-move-function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagadeeshftw authored Feb 7, 2025
2 parents 999fb5d + 6743274 commit 94e5cdb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/test_contracts.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
name: Test Dojo contracts
name: Build and test Dojo contracts

on: [push, pull_request]

jobs:
sozo-test:
runs-on: ubuntu-latest
env:
DOJO_VERSION: v1.1.2
steps:
- uses: actions/checkout@v3
- run: curl -L https://install.dojoengine.org | bash
- run: /home/runner/.config/.dojo/bin/dojoup -v v1.1.2
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.9.2"
- run: |
curl -L https://install.dojoengine.org | bash
/home/runner/.config/.dojo/bin/dojoup -v ${{ env.DOJO_VERSION }}
- run: |
cd onchain
/home/runner/.config/.dojo/bin/sozo build
/home/runner/.config/.dojo/bin/sozo test
if [[ `git status --porcelain` ]]; then
echo The git repo is dirty
echo "Make sure to run \"sozo build\" after changing Scarb.toml"
exit 1
fi
14 changes: 11 additions & 3 deletions onchain/src/models/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub struct Player {
pub owner: ContractAddress, // Account owner of player. An account can own multiple players
pub is_bot: bool,
pub total_games_played: u256, // Count of total games played by this player
pub total_games_won: u256, // Count of total games won by this player
pub total_games_completed: u256, // Count of total games completed by this player
pub total_games_won: u256,
}


Expand All @@ -18,7 +19,7 @@ pub struct Player {
pub struct UsernameToAddress {
#[key]
pub username: felt252,
pub address: ContractAddress
pub address: ContractAddress,
}

#[derive(Drop, Copy, Serde)]
Expand All @@ -39,6 +40,13 @@ pub trait PlayerTrait {

impl PlayerImpl of PlayerTrait {
fn new(username: felt252, owner: ContractAddress, is_bot: bool) -> Player {
Player { username, owner, is_bot, total_games_played: 0, total_games_won: 0 }
Player {
username,
owner,
is_bot,
total_games_played: 0,
total_games_completed: 0,
total_games_won: 0,
}
}
}
6 changes: 6 additions & 0 deletions onchain/src/systems/game_actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ pub mod GameActions {

assert(caller_username != 0, 'PLAYER NOT REGISTERED');

// Verify that player has not already joined the game
assert(game.player_red != caller_username, 'ALREADY SELECTED RED');
assert(game.player_blue != caller_username, 'ALREADY SELECTED BLUE');
assert(game.player_green != caller_username, 'ALREADY SELECTED GREEN');
assert(game.player_yellow != caller_username, 'ALREADY SELECTED YELLOW');

/// Game starts automatically once the last player joins

// Verify that color is available
Expand Down
21 changes: 21 additions & 0 deletions onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ mod tests {

testing::set_contract_address(caller);
// Move piece from initial position with dice throw 6

game_action_system.move('r0');

let game: Game = world.read_model(game_id);
Expand Down Expand Up @@ -469,6 +470,7 @@ mod tests {
testing::set_contract_address(game_action_system.contract_address);
world.write_model(@game);


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

Expand Down Expand Up @@ -511,16 +513,19 @@ mod tests {
game.dice_face = 6;
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.

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.


// Verify the new position
let game: Game = world.read_model(game_id);

Expand Down Expand Up @@ -549,23 +554,28 @@ mod tests {
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('g1'); // 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.


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.


// Verify the new position
let game: Game = world.read_model(game_id);
let game_condition = array![0, 0, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down Expand Up @@ -593,30 +603,38 @@ mod tests {
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('g1'); // 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.


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.


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.


// Verify the new position
let game: Game = world.read_model(game_id);
let game_condition = array![0, 0, 0, 0, 0, 19, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down Expand Up @@ -671,8 +689,10 @@ mod tests {
testing::set_contract_address(caller_red);

// Move red piece to position 15

game_action_system.move('r0');


// Verify the new positions
game = world.read_model(game_id);

Expand Down Expand Up @@ -720,6 +740,7 @@ mod tests {
testing::set_contract_address(caller_red);
game_action_system.move('r0');


// Verify the new positions
game = world.read_model(game_id);

Expand Down

0 comments on commit 94e5cdb

Please sign in to comment.