Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_get_address_from_username #178

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions onchain/src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ pub enum GameStatus {
#[derive(Serde, Copy, Drop, Introspect, PartialEq)]
pub enum GameMode {
SinglePlayer, // Play with computer
MultiPlayer, // Play online with friends
MultiPlayer // Play online with friends
}

#[derive(Serde, Copy, Drop, Introspect, PartialEq)]
pub enum PiecePosition {
SinglePlayer, // Play with computer
MultiPlayer, // Play online with friends
MultiPlayer // Play online with friends
}

// Game model
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Game {
pub y0: felt252, // yellow piece position on board
pub y1: felt252, // yellow piece position on board
pub y2: felt252, // yellow piece position on board
pub y3: felt252, // yellow piece position on board
pub y3: felt252 // yellow piece position on board
}

pub trait GameTrait {
Expand All @@ -77,7 +77,7 @@ pub trait GameTrait {
player_blue: felt252,
player_yellow: felt252,
player_green: felt252,
number_of_players: u8
number_of_players: u8,
) -> Game;
fn restart(ref self: Game);
fn terminate_game(ref self: Game);
Expand All @@ -92,7 +92,7 @@ impl GameImpl of GameTrait {
player_blue: felt252,
player_yellow: felt252,
player_green: felt252,
number_of_players: u8
number_of_players: u8,
) -> Game {
let zero_address = contract_address_const::<0x0>();
Game {
Expand Down Expand Up @@ -120,127 +120,127 @@ impl GameImpl of GameTrait {
2 => 'B01',
3 => 'B01',
4 => 'B01',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
b1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'B02',
3 => 'B02',
4 => 'B02',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
b2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'B03',
3 => 'B03',
4 => 'B03',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
b3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'B04',
3 => 'B04',
4 => 'B04',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
g0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'G01',
3 => 'G01',
4 => 'G01',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
g1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'G02',
3 => 'G02',
4 => 'G02',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
g2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'G03',
3 => 'G03',
4 => 'G03',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
g3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'GO4',
3 => 'GO4',
4 => 'GO4',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
r0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'R01',
4 => 'R01',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
r1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'R02',
4 => 'R02',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
r2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'R03',
4 => 'R03',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
r3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'R04',
4 => 'R04',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
y0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'Y01',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
y1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'Y02',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
y2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'Y03',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
y3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'Y04',
_ => panic!("invalid number of players")
_ => panic!("invalid number of players"),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions onchain/src/models/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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_won: u256 // Count of total games won by this player
}

#[derive(Drop, Copy, Serde)]
#[dojo::model]
pub struct UsernameToAddress {
#[key]
pub username: felt252,
pub address: ContractAddress
pub address: ContractAddress,
}

#[derive(Drop, Copy, Serde)]
Expand Down
82 changes: 41 additions & 41 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> {
player_yellow: felt252,
player_blue: felt252,
player_red: felt252,
number_of_players: u8
number_of_players: u8,
) -> usize;
fn start(ref self: T);

Expand Down Expand Up @@ -42,15 +42,15 @@ pub mod GameActions {
pub struct GameCreated {
#[key]
pub game_id: usize,
pub timestamp: u64
pub timestamp: u64,
}

#[derive(Copy, Drop, Serde)]
#[dojo::event]
pub struct GameStarted{
pub struct GameStarted {
#[key]
pub game_id: usize,
pub time_stamp: u64
pub time_stamp: u64,
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -84,7 +84,7 @@ pub mod GameActions {
player_blue,
player_yellow,
player_green,
number_of_players
number_of_players,
);

world.write_model(@new_game);
Expand All @@ -95,41 +95,41 @@ pub mod GameActions {
}

fn start(ref self: ContractState) {
// Get world state
let mut world = self.world_default();

// Get caller address
let caller: ContractAddress = get_caller_address();
// Assign a game id
let mut game_id: usize = 999;
//get the game state
let mut game: Game = world.read_model(game_id);
// get the caller's user name
let caller_username: felt252 = self.get_username_from_address(caller);
//assert that caller with the user_name is game creator
assert(game.created_by == caller_username, Errors::ONLY_GAME_CREATOR);
//ensure that game status is pending
assert(game.game_status == GameStatus::Pending, Errors::GAME_NOT_PENDING);
//change game status to Ongoing
game.game_status = GameStatus::Ongoing;
//make player_green the first player by making player green the nest player
game.player_green = game.next_player;
//update the game model
world.write_model(@game);
//get the current block timestamp
let time_stamp: u64 = get_block_timestamp();
//emit event
world.emit_event(@GameStarted { game_id, time_stamp });
// Get world state
let mut world = self.world_default();

// Get caller address
let caller: ContractAddress = get_caller_address();

// Assign a game id
let mut game_id: usize = 999;

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

// get the caller's user name
let caller_username: felt252 = self.get_username_from_address(caller);

//assert that caller with the user_name is game creator
assert(game.created_by == caller_username, Errors::ONLY_GAME_CREATOR);

//ensure that game status is pending
assert(game.game_status == GameStatus::Pending, Errors::GAME_NOT_PENDING);

//change game status to Ongoing
game.game_status = GameStatus::Ongoing;

//make player_green the first player by making player green the nest player
game.player_green = game.next_player;

//update the game model
world.write_model(@game);

//get the current block timestamp
let time_stamp: u64 = get_block_timestamp();

//emit event
world.emit_event(@GameStarted { game_id, time_stamp });
}

fn join(ref self: ContractState) {}
Expand Down Expand Up @@ -159,7 +159,7 @@ pub mod GameActions {
}

fn get_address_from_username(
ref self: ContractState, username: felt252
ref self: ContractState, username: felt252,
) -> ContractAddress {
let mut world = self.world_default();

Expand Down
Loading
Loading