Skip to content

Commit

Permalink
chore: update join function (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
muheebyusufbaba1 authored Dec 26, 2024
1 parent 824e8d3 commit 0e8feea
Showing 1 changed file with 69 additions and 38 deletions.
107 changes: 69 additions & 38 deletions onchain/src/systems/game_actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait IGameActions<T> {
) -> usize;
fn start(ref self: T);

fn join(ref self: T);
fn join(ref self: T, username: felt252, selected_color: felt252, game_id: u64);

fn move(ref self: T);

Expand Down Expand Up @@ -47,7 +47,7 @@ pub mod GameActions {

#[derive(Copy, Drop, Serde)]
#[dojo::event]
pub struct GameStarted{
pub struct GameStarted {
#[key]
pub game_id: usize,
pub time_stamp: u64
Expand Down Expand Up @@ -95,44 +95,75 @@ 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) {}
fn join(ref self: ContractState, username: felt252, selected_color: felt252, game_id: u64) {
// Get world state
let mut world = self.world_default();

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

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

game.player_red = match selected_color {
0 => 0,
1 => username,
_ => 0
};
game.player_yellow = match selected_color {
0 => 0,
1 => username,
_ => 0
};
game.player_blue = match selected_color {
0 => 0,
1 => username,
_ => 0
};
game.player_green = match selected_color {
0 => 0,
1 => username,
_ => 0
};
}

fn move(ref self: ContractState) {}

Expand Down

0 comments on commit 0e8feea

Please sign in to comment.