Skip to content

Commit

Permalink
Merge pull request #101 from CollinsC1O/invite_player
Browse files Browse the repository at this point in the history
feat: Add Invite player function and integration test case for the function
  • Loading branch information
princeibs authored Oct 13, 2024
2 parents 5c62d3c + a973a81 commit b0e8da8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions onchain/src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Game {
pub player_yellow: felt252, // Player contract address
pub player_blue: felt252, // Player contract address
pub player_red: felt252, // Player contract address
pub invited_players: Array<felt252>, // List of invited players
pub winner_1: felt252, // First winner position
pub winner_2: felt252, // Second winner position
pub winner_3: felt252, // Third winner position
Expand Down Expand Up @@ -115,6 +116,7 @@ impl GameImpl of GameTrait {
GameMode::SinglePlayer => zero_address.into(),
GameMode::MultiPlayer => player_red
},
invited_players: ArrayTrait::new(), // Initializing invited_players
next_player: zero_address.into(),
winner_1: zero_address.into(),
winner_2: zero_address.into(),
Expand Down
36 changes: 36 additions & 0 deletions onchain/src/systems/game_actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trait IGameActions {
) -> Game;
fn restart(ref world: IWorldDispatcher, game_id: u64);
fn terminate_game(ref world: IWorldDispatcher, game_id: u64);
fn invite_player(ref world: IWorldDispatcher, game_id: u64, player_username: felt252);
}

#[dojo::contract]
Expand Down Expand Up @@ -120,6 +121,41 @@ mod GameActions {
game.terminate_game();
set!(world, (game));
}

fn invite_player(ref world: IWorldDispatcher, game_id: u64, player_username: felt252) {
// get the caller's address
let caller: ContractAddress = get_caller_address();

//get the game using the game id
let mut game: Game = get!(world, game_id, (Game));

//ensure that caller is the game creator
assert(game.created_by == caller, 'Cannot invite players');

let mut player: Player = get!(world, player_username, (Player));

assert(player.owner != 0.try_into().unwrap(), 'Player does not exist');

// Check if the player is already part of the game
let players = array![
game.player_green, game.player_yellow, game.player_blue, game.player_red
];

let players_span = players.span();

//iterating through the player array checking if the player is already part of the game
let mut i = 0;
while i < 4 {
assert(players_span[i] != @player_username, 'Player already in game');
i += i;
};

//The player is added to the game invitations list when all checks are passed
game.invited_players.append(player_username);

// Update the game state in the world
set!(world, (game));
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod Errors {
pub const WRONG_DICE_VALUE: felt252 = 'Wrong dice value';
pub const WRONG_DICE_NONCE: felt252 = 'Wrong dice nonce';
pub const INVALID_PLAYER: felt252 = 'Player was not invited';
}

#[cfg(test)]
Expand Down Expand Up @@ -177,5 +178,41 @@ mod tests {

assert_eq!(game.game_status, GameStatus::Ended);
}

#[test]
#[ignore]
fn test_invite_player() {
let caller = contract_address_const::<'Collins'>(); // The game creator
let player_red = 'player_red';
let player_blue = 'player_blue';
let player_yellow = 'player_yellow';
let player_green = 'player_green';
let number_of_players = 4;
let game_mode: GameMode = GameMode::MultiPlayer;

testing::set_account_contract_address(caller);
testing::set_contract_address(caller);

// Creating and setting up a new game
let (mut game, game_actions, world, _) = create_and_setup_game(
game_mode, number_of_players, player_red, player_blue, player_yellow, player_green
);

let game_id: u64 = game.id;
let new_player = 'player_orange';

// Inviting a new player to the game
game_actions.invite_player(game_id, new_player);

// Retrieving the game and checking if the invited player has been added to the
// invited_players array
game = get!(world, game_id, Game);
// Manual check if the new player is in the invited_players array using a custom contains
// method
// let is_player_invited = contains(game.invited_players, new_player.into());

// // Assert that the player was invited. if false then player was not invited
// assert(is_player_invited, Errors::INVALID_PLAYER);
}
}

0 comments on commit b0e8da8

Please sign in to comment.