Skip to content

Commit

Permalink
feat: add custome errors in test_game
Browse files Browse the repository at this point in the history
  • Loading branch information
od-hunter committed Oct 5, 2024
1 parent c43c6f1 commit b567262
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions onchain/src/systems/player_actions.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use starknet::{ContractAddress};
use starkludo::models::{player::{Player, PlayerTrait}};

mod Errors {
const ADDRESS_ZERO: felt252 = 'Cannot create from zero address';
const USERNAME_TAKEN: felt252 = 'username already taken';
const USERNAME_NOT_FOUND: felt252 = 'player with username not found';
const USERNAME_EXIST: felt252 = 'username already exist';
const ONLY_OWNER_USERNAME: felt252 = 'only user can udpate username';
}

#[dojo::interface]
trait IPlayerActions {
// Create a new player
Expand All @@ -27,14 +35,17 @@ 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, '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(), 'username already taken');
assert(existing_player.owner == 0.try_into().unwrap(), Errors::USERNAME_TAKEN);

set!(world, (new_player));
}
Expand All @@ -45,17 +56,18 @@ 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(), '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(), 'player with username not found');
assert(player.owner != 0.try_into().unwrap(), Errors::USERNAME_NOT_FOUND);

let total_points = player.total_games_won;

Expand All @@ -73,15 +85,17 @@ 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, '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(), 'username already exist');
assert(existing_player.owner == 0.try_into().unwrap(), Errors::USERNAME_EXIST);

// Update the player's username
player.username = new_username;
set!(world, (player));
}
}
}
}

0 comments on commit b567262

Please sign in to comment.