Skip to content

Commit

Permalink
tempfix(contracts): use custom events instead of emit! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Aug 25, 2023
1 parent f94111b commit f4bef39
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
18 changes: 13 additions & 5 deletions src/events.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<felt252>) {
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,
Expand All @@ -18,7 +26,7 @@ struct GameCreated {
max_players: usize,
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, Serde)]
struct PlayerJoined {
game_id: u32,
player_id: ContractAddress,
Expand All @@ -43,15 +51,15 @@ struct Sold {
payout: u128
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, Serde)]
struct Traveled {
game_id: u32,
player_id: ContractAddress,
from_location: felt252,
to_location: felt252,
}

#[derive(Drop, starknet::Event)]
#[derive(Drop, Serde)]
struct RandomEvent {
game_id: u32,
player_id: ContractAddress,
Expand Down
21 changes: 14 additions & 7 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions src/systems/join.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 14 additions & 8 deletions src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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
Expand All @@ -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
}
Expand Down

1 comment on commit f4bef39

@vercel
Copy link

@vercel vercel bot commented on f4bef39 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rollyourown – ./web

rollyourown.preview.cartridge.gg
rollyourown-git-main.preview.cartridge.gg

Please sign in to comment.