diff --git a/src/events.cairo b/src/events.cairo index eee598a06..b83834b88 100644 --- a/src/events.cairo +++ b/src/events.cairo @@ -3,13 +3,21 @@ use serde::Serde; use array::{ArrayTrait, SpanTrait}; use starknet::ContractAddress; -#[derive(Drop, starknet::Event)] +// helper function to emit events, eventually dojo will +// have framework level event/logging +fn emit(ctx: Context, name: felt252, values: Span) { + let mut keys = array::ArrayTrait::new(); + keys.append(name); + ctx.world.emit(keys, values); +} + +#[derive(Drop, Serde)] struct LocationCreated { game_id: u32, location_id: u32, } -#[derive(Drop, starknet::Event)] +#[derive(Drop, Serde)] struct GameCreated { game_id: u32, creator: ContractAddress, @@ -18,7 +26,7 @@ struct GameCreated { max_players: usize, } -#[derive(Drop, starknet::Event)] +#[derive(Drop, Serde)] struct PlayerJoined { game_id: u32, player_id: ContractAddress, @@ -43,7 +51,7 @@ struct Sold { payout: u128 } -#[derive(Drop, starknet::Event)] +#[derive(Drop, Serde)] struct Traveled { game_id: u32, player_id: ContractAddress, @@ -51,7 +59,7 @@ struct Traveled { to_location: felt252, } -#[derive(Drop, starknet::Event)] +#[derive(Drop, Serde)] struct RandomEvent { game_id: u32, player_id: ContractAddress, diff --git a/src/systems/create.cairo b/src/systems/create.cairo index 0a4d89974..926ca366f 100644 --- a/src/systems/create.cairo +++ b/src/systems/create.cairo @@ -9,7 +9,7 @@ mod create_game { use dojo::world::Context; - use rollyourown::events::{GameCreated, PlayerJoined}; + use rollyourown::events::{emit, GameCreated, PlayerJoined}; use rollyourown::components::name::Name; use rollyourown::components::game::Game; use rollyourown::components::player::Player; @@ -115,13 +115,20 @@ mod create_game { }; }; - // emit game created and player joined - emit!( - ctx.world, GameCreated { - game_id, creator: ctx.origin, start_time, max_turns, max_players - } + // emit player joined + let mut values = array::ArrayTrait::new(); + serde::Serde::serialize( + @PlayerJoined { game_id, player_id: ctx.origin, location_id: location_id }, ref values ); - emit!(ctx.world, PlayerJoined { game_id, player_id: ctx.origin, location_id }); + emit(ctx, 'PlayerJoined', values.span()); + + // emit game created + let mut values = array::ArrayTrait::new(); + serde::Serde::serialize( + @GameCreated { game_id, creator: ctx.origin, start_time, max_players, max_turns }, + ref values + ); + emit(ctx, 'GameCreated', values.span()); (game_id, ctx.origin) } diff --git a/src/systems/join.cairo b/src/systems/join.cairo index 78cd5420c..0cb34a5a9 100644 --- a/src/systems/join.cairo +++ b/src/systems/join.cairo @@ -7,7 +7,7 @@ mod join_game { use dojo::world::Context; - use rollyourown::events::PlayerJoined; + use rollyourown::events::{emit, PlayerJoined}; use rollyourown::components::game::Game; use rollyourown::components::player::Player; use rollyourown::components::location::{Location, LocationTrait}; @@ -51,7 +51,11 @@ mod join_game { ); // emit player joined - emit!(ctx.world, PlayerJoined { game_id, player_id: ctx.origin, location_id }); + let mut values = array::ArrayTrait::new(); + serde::Serde::serialize( + @PlayerJoined { game_id, player_id: ctx.origin, location_id: location_id }, ref values + ); + emit(ctx, 'PlayerJoined', values.span()); ctx.origin } diff --git a/src/systems/travel.cairo b/src/systems/travel.cairo index 51b273539..13786fd56 100644 --- a/src/systems/travel.cairo +++ b/src/systems/travel.cairo @@ -6,7 +6,7 @@ mod travel { use dojo::world::Context; - use rollyourown::events::{Traveled, RandomEvent}; + use rollyourown::events::{emit, Traveled, RandomEvent}; use rollyourown::components::{game::{Game, GameTrait}, location::Location}; use rollyourown::components::player::{Player, PlayerTrait}; use rollyourown::components::risks::{Risks, RisksTrait, TravelResult}; @@ -29,15 +29,18 @@ mod travel { let (event_occured, result) = risks.travel(seed); if event_occured { - emit!( - ctx.world, RandomEvent { + let mut values = array::ArrayTrait::new(); + serde::Serde::serialize( + @RandomEvent { game_id, player_id, health_loss: result.health_loss, mugged: result.mugged, - arrested: result.arrested - } + arrested: result.arrested, + }, + ref values ); + emit(ctx, 'RandomEvent', values.span()); } // If arrested, player loses a turn and stays at same location @@ -56,11 +59,14 @@ mod travel { player.location_id = next_location_id; set!(ctx.world, (player)); - emit!( - ctx.world, Traveled { + let mut values = array::ArrayTrait::new(); + serde::Serde::serialize( + @Traveled { game_id, player_id, from_location: player.location_id, to_location: next_location_id - } + }, + ref values ); + emit(ctx, 'Traveled', values.span()); event_occured }