Skip to content
Merged
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
4 changes: 2 additions & 2 deletions backend/dojo_examples/combat_game/src/models/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct Player {
pub current_beast_id: u16,
pub battles_won: u16,
pub battles_lost: u16,
pub last_active_day: u32,
pub creation_day: u32,
pub last_active_day: u64,
pub creation_day: u64,
}

#[generate_trait]
Expand Down
58 changes: 58 additions & 0 deletions backend/dojo_examples/combat_game/src/systems/player.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
#[starknet::interface]
pub trait IPlayer<T> {
fn spawn_player(ref self: T, initial_beast_id: u16);
fn update_profile(ref self: T, won: bool, current_beast_id: u16);
}

#[dojo::contract]
pub mod player_system {
use super::IPlayer;

use combat_game::models::player::{Player, PlayerAssert};
use combat_game::store::{StoreTrait};

use starknet::{get_caller_address, get_block_timestamp};

#[storage]
struct Storage {
player_counter: u256,
}

// Constructor
fn dojo_init(ref self: ContractState) {
self.player_counter.write(1);
}

#[abi(embed_v0)]
impl PlayerImpl of IPlayer<ContractState> {
fn spawn_player(ref self: ContractState, initial_beast_id: u16) {
let mut world = self.world(@"combat_game");
let mut store = StoreTrait::new(world);

let found = store.read_player();
found.assert_not_exists();

let player = Player {
address: get_caller_address(),
current_beast_id: initial_beast_id,
battles_won: 0,
battles_lost: 0,
last_active_day: get_block_timestamp(),
creation_day: get_block_timestamp(),
};

store.write_player(player);
}

fn update_profile(ref self: ContractState, won: bool, current_beast_id: u16) {
let mut world = self.world(@"combat_game");
let mut store = StoreTrait::new(world);

let mut player = store.read_player();
player.assert_exists();

store.update_player_battle_result(won);
player.current_beast_id = current_beast_id;
store.write_player(player);
}
}
}