diff --git a/onchain/src/systems/player_actions.cairo b/onchain/src/systems/player_actions.cairo index 9beae37..a1bc045 100644 --- a/onchain/src/systems/player_actions.cairo +++ b/onchain/src/systems/player_actions.cairo @@ -1,6 +1,14 @@ use starknet::{ContractAddress}; use starkludo::models::{player::{Player, PlayerTrait}}; +pub mod Errors { + pub const ADDRESS_ZERO: felt252 = 'Cannot create from zero address'; + pub const USERNAME_TAKEN: felt252 = 'username already taken'; + pub const USERNAME_NOT_FOUND: felt252 = 'player with username not found'; + pub const USERNAME_EXIST: felt252 = 'username already exist'; + pub const ONLY_OWNER_USERNAME: felt252 = 'only user can udpate username'; +} + #[dojo::interface] trait IPlayerActions { // Create a new player @@ -18,6 +26,7 @@ trait IPlayerActions { #[dojo::contract] mod PlayerActions { + use super::Errors; use super::{IPlayerActions, Player, PlayerTrait}; use starknet::{ContractAddress, get_caller_address, contract_address_const}; @@ -27,14 +36,14 @@ mod PlayerActions { let caller: ContractAddress = get_caller_address(); let zero_address: ContractAddress = contract_address_const::<0x0>(); - assert(caller != zero_address, 'Cannot create from zero address'); + assert(caller != zero_address, Errors::ADDRESS_ZERO); let new_player: Player = PlayerTrait::new(username, caller); // Ensure player username is unique let mut existing_player = get!(world, username, (Player)); - assert(existing_player.owner == 0.try_into().unwrap(), 'username already taken'); + assert(existing_player.owner == 0.try_into().unwrap(), Errors::USERNAME_TAKEN); set!(world, (new_player)); } @@ -45,17 +54,16 @@ mod PlayerActions { let mut player: Player = get!(world, username, (Player)); // Validate username - assert(player.owner != 0.try_into().unwrap(), 'player with username not found'); + assert(player.owner != 0.try_into().unwrap(), Errors::USERNAME_NOT_FOUND); player.owner } - fn get_player_stats( ref world: IWorldDispatcher, username: felt252 ) -> (u256, u256, u256, u256) { let player: Player = get!(world, username, (Player)); - assert(player.owner != 0.try_into().unwrap(), 'player with username not found'); + assert(player.owner != 0.try_into().unwrap(), Errors::USERNAME_NOT_FOUND); let total_points = player.total_games_won; @@ -73,11 +81,11 @@ mod PlayerActions { let mut player: Player = get!(world, old_username, (Player)); // Only allow the player to update their own username - assert(player.owner == caller, 'only user can udpate username'); + assert(player.owner == caller, Errors::ONLY_OWNER_USERNAME); // Check if the new username is already taken let mut existing_player = get!(world, new_username, (Player)); - assert(existing_player.owner == 0.try_into().unwrap(), 'username already exist'); + assert(existing_player.owner == 0.try_into().unwrap(), Errors::USERNAME_EXIST); // Update the player's username player.username = new_username; diff --git a/onchain/src/tests/test_game.cairo b/onchain/src/tests/test_game.cairo index a3fcb6d..96c5425 100644 --- a/onchain/src/tests/test_game.cairo +++ b/onchain/src/tests/test_game.cairo @@ -1,5 +1,11 @@ +pub mod Errors { + pub const WRONG_DICE_VALUE: felt252 = 'Wrong dice value'; + pub const WRONG_DICE_NONCE: felt252 = 'Wrong dice nonce'; +} + #[cfg(test)] mod tests { + use super::Errors; // Import world dispatcher use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; // Import test utils @@ -17,6 +23,7 @@ mod tests { // Import Starknet utils use starknet::{testing, contract_address_const, get_caller_address, ContractAddress}; + fn create_and_setup_game( game_mode: GameMode, number_of_players: u8, @@ -122,12 +129,12 @@ mod tests { #[test] fn test_dice_new_roll() { let mut dice = DiceTrait::new(DICE_FACE_COUNT, DICE_SEED); - assert(dice.roll() == 1, 'Wrong dice value'); - assert(dice.roll() == 6, 'Wrong dice value'); - assert(dice.roll() == 3, 'Wrong dice value'); - assert(dice.roll() == 1, 'Wrong dice value'); - assert(dice.roll() == 4, 'Wrong dice value'); - assert(dice.roll() == 3, 'Wrong dice value'); + assert(dice.roll() == 1, Errors::WRONG_DICE_VALUE); + assert(dice.roll() == 6, Errors::WRONG_DICE_VALUE); + assert(dice.roll() == 3, Errors::WRONG_DICE_VALUE); + assert(dice.roll() == 1, Errors::WRONG_DICE_VALUE); + assert(dice.roll() == 4, Errors::WRONG_DICE_VALUE); + assert(dice.roll() == 3, Errors::WRONG_DICE_VALUE); } #[test] @@ -135,7 +142,7 @@ mod tests { let mut dice = DiceTrait::new(DICE_FACE_COUNT, DICE_SEED); dice.nonce = 0x800000000000011000000000000000000000000000000000000000000000000; dice.roll(); - assert(dice.nonce == 0, 'Wrong dice nonce'); + assert(dice.nonce == 0, Errors::WRONG_DICE_NONCE); } #[test] diff --git a/onchain/src/tests/test_player.cairo b/onchain/src/tests/test_player.cairo index 4f81aa8..848ebf3 100644 --- a/onchain/src/tests/test_player.cairo +++ b/onchain/src/tests/test_player.cairo @@ -1,5 +1,13 @@ +pub mod Errors { + pub const INCORRECT_GAME_PLAYED: felt252 = 'Incorrect games played'; + pub const INCORRECT_GAME_WON: felt252 = 'Incorrect games won'; + pub const INCORRECT_TOTAL_POINTS: felt252 = 'Incorrect total points'; + pub const INCORRECT_LEADBOARD_POSITION: felt252 = 'Incorrect leaderboard position'; +} + #[cfg(test)] mod tests { + use super::Errors; // import world dispatcher use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; // import test utils @@ -13,6 +21,7 @@ mod tests { }; use starknet::{testing, contract_address_const, get_caller_address, ContractAddress}; + fn create_and_setup_player( username: felt252 ) -> (IPlayerActionsDispatcher, IWorldDispatcher, ContractAddress) { @@ -99,11 +108,11 @@ mod tests { // Get the player's stats let (games_played, games_won, total_points, leaderboard_position) = player_actions .get_player_stats(username); - - assert_eq!(games_played, 10, "Incorrect games played"); - assert_eq!(games_won, 5, "Incorrect games won"); - assert_eq!(total_points, 5, "Incorrect total points"); - assert_eq!(leaderboard_position, 0, "Incorrect leaderboard position"); + + assert_eq!(games_played, 10, "{}", Errors::INCORRECT_GAME_PLAYED); + assert_eq!(games_won, 5, "{}", Errors::INCORRECT_GAME_WON); + assert_eq!(total_points, 5, "{}", Errors::INCORRECT_TOTAL_POINTS); + assert_eq!(leaderboard_position, 0, "{}", Errors::INCORRECT_LEADBOARD_POSITION); }