Skip to content

Commit

Permalink
refactor systems (remove world)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzn committed Dec 22, 2023
1 parent 482e28b commit a87acce
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 210 deletions.
6 changes: 3 additions & 3 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod systems {
mod match_system;
mod map_system;
mod move_system;
mod turn_system;
// mod turn_system;
mod stadistics_system;
mod skill_system;
}
Expand All @@ -31,10 +31,10 @@ mod systems {
mod tests {
mod setup;
mod test_character_system;
mod test_map_system;
// mod test_map_system;
mod test_match_system;
mod test_move_system;
mod test_skill_system;
mod test_turn_system;
// mod test_turn_system;
}

21 changes: 8 additions & 13 deletions src/systems/action_system.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::interface]
trait IActionSystem<TContractState> {
fn action(
self: @TContractState,
world: IWorldDispatcher,
match_id: u32,
player: felt252,
player_character_id: u32,
Expand All @@ -15,7 +12,7 @@ trait IActionSystem<TContractState> {
);
}

#[starknet::contract]
#[dojo::contract]
mod action_system {
use super::IActionSystem;

Expand All @@ -27,11 +24,9 @@ mod action_system {
use starkane::models::states::character_state::{
CharacterState, ActionState, ActionStateTrait
};
use starkane::systems::stadistics_system::stadistics_system::StadisticsSystem;
use starkane::systems::stadistics_system::stadistics_system;
use starkane::store::{Store, StoreTrait};

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use debug::PrintTrait;

#[storage]
Expand All @@ -42,7 +37,6 @@ mod action_system {
// Character -> Character
fn action(
self: @ContractState,
world: IWorldDispatcher,
match_id: u32,
player: felt252,
player_character_id: u32,
Expand All @@ -52,6 +46,7 @@ mod action_system {
receiver_character_id: u32
) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

let match_state = store.get_match_state(match_id);
Expand Down Expand Up @@ -88,7 +83,7 @@ mod action_system {
receiver_character,
receiver_character_state
);
check_and_update_game_state_winner(
check_and_update_game_state_winner(world,
ref store, match_id, player, receiver_character_id, receiver
);
},
Expand All @@ -101,7 +96,7 @@ mod action_system {
receiver_character,
receiver_character_state
);
check_and_update_game_state_winner(
check_and_update_game_state_winner(world,
ref store, match_id, player, receiver_character_id, receiver
);
},
Expand All @@ -115,7 +110,7 @@ mod action_system {
receiver_character_state
);

check_and_update_game_state_winner(
check_and_update_game_state_winner(world,
ref store, match_id, player, receiver_character_id, receiver
);
},
Expand All @@ -130,7 +125,6 @@ mod action_system {
}

// character can do the action, so we have to save that

let action_state = ActionStateTrait::new(
match_id, player_character_id, player, true, last_action_state.movement
);
Expand All @@ -139,6 +133,7 @@ mod action_system {
}

fn check_and_update_game_state_winner(
world: IWorldDispatcher,
ref store: Store,
match_id: u32,
attacker: felt252,
Expand All @@ -158,11 +153,11 @@ mod action_system {
let mut match_state = store.get_match_state(match_id);
match_state.winner = attacker;
store.set_match_state(match_state);
stadistics_system::record_match_stadistics(world, match_id);
} else {
match_player_characters_len.remain_characters -= 1;
store.set_match_player_character_len(match_player_characters_len);
}
// StadisticsSystem::record_match_stadistics(store.world, match_id);
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/systems/character_system.cairo
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starkane::models::entities::character::CharacterType;

#[starknet::interface]
trait ICharacterSystem<TContractState> {
fn init(self: @TContractState, world: IWorldDispatcher);
fn init(self: @TContractState);
fn mint(
self: @TContractState,
world: IWorldDispatcher,
character_type: CharacterType,
owner: felt252,
skin_id: u32
);
}

#[starknet::contract]
#[dojo::contract]
mod character_system {
use super::ICharacterSystem;
use starkane::models::data::starkane::CharacterPlayerProgress;
use starkane::models::entities::character::{Character, CharacterTrait, CharacterType};
use starkane::store::{Store, StoreTrait};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[storage]
struct Storage {}

#[external(v0)]
impl CharacterSystem of ICharacterSystem<ContractState> {
fn init(self: @ContractState, world: IWorldDispatcher) {
fn init(self: @ContractState) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

store.set_character(CharacterTrait::new(CharacterType::Archer));
Expand All @@ -38,12 +36,12 @@ mod character_system {

fn mint(
self: @ContractState,
world: IWorldDispatcher,
character_type: CharacterType,
owner: felt252,
skin_id: u32
) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

assert(owner.is_non_zero(), 'owner cannot be zero');
Expand Down
10 changes: 4 additions & 6 deletions src/systems/map_system.cairo
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::interface]
trait IMapSystem<TContractState> {
fn init(self: @TContractState, world: IWorldDispatcher);
fn init(self: @TContractState);
}

#[starknet::contract]
#[dojo::contract]
mod map_system {
use super::IMapSystem;
use starkane::models::entities::map::{Map, MapTrait};
use starkane::store::{Store, StoreTrait};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[storage]
struct Storage {}

#[external(v0)]
impl MapSystem of IMapSystem<ContractState> {
fn init(self: @ContractState, world: IWorldDispatcher) {
fn init(self: @ContractState) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

let map_id = 1;
Expand Down
9 changes: 3 additions & 6 deletions src/systems/match_system.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use starkane::models::entities::character::Character;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[derive(Copy, Drop, Serde)]
struct PlayerCharacter {
Expand All @@ -10,11 +9,11 @@ struct PlayerCharacter {
#[starknet::interface]
trait IMatchSystem<TContractState> {
fn init(
self: @TContractState, world: IWorldDispatcher, players_characters: Array<PlayerCharacter>
self: @TContractState, players_characters: Array<PlayerCharacter>
);
}

#[starknet::contract]
#[dojo::contract]
mod match_system {
use super::{IMatchSystem, PlayerCharacter};
use starkane::models::data::starkane::{MatchCount, MATCH_COUNT_KEY};
Expand All @@ -29,19 +28,17 @@ mod match_system {
};
use starkane::store::{Store, StoreTrait};

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[storage]
struct Storage {}

#[external(v0)]
impl MatchSystem of IMatchSystem<ContractState> {
fn init(
self: @ContractState,
world: IWorldDispatcher,
players_characters: Array<PlayerCharacter>
) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

assert(players_characters.len() > 0, 'characters cannot be empty');
Expand Down
9 changes: 2 additions & 7 deletions src/systems/move_system.cairo
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::interface]
trait IMoveSystem<TContractState> {
fn move(
self: @TContractState,
world: IWorldDispatcher,
match_id: u32,
player: felt252,
character_id: u32,
position: (u128, u128)
);
}

#[starknet::contract]
#[dojo::contract]
mod move_system {
use super::IMoveSystem;
use starkane::models::states::match_state::MatchState;
Expand All @@ -24,8 +21,6 @@ mod move_system {
use starkane::models::entities::character::Character;
use starkane::store::{Store, StoreTrait};

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use debug::PrintTrait;

#[storage]
Expand All @@ -35,13 +30,13 @@ mod move_system {
impl MoveSystem of IMoveSystem<ContractState> {
fn move(
self: @ContractState,
world: IWorldDispatcher,
match_id: u32,
player: felt252,
character_id: u32,
position: (u128, u128)
) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

let match_state = store.get_match_state(match_id);
Expand Down
14 changes: 4 additions & 10 deletions src/systems/skill_system.cairo
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::interface]
trait ISkillSystem<TContractState> {
fn init(self: @TContractState, world: IWorldDispatcher);
fn init(self: @TContractState);
fn has_skill(
self: @TContractState,
world: IWorldDispatcher,
character_id: u32,
player: felt252,
skill_id: u32
) -> bool;
fn can_use_skill(
self: @TContractState,
world: IWorldDispatcher,
character_id: u32,
player: felt252,
skill_id: u32,
level: u32
) -> bool;
}

#[starknet::contract]
#[dojo::contract]
mod skill_system {
use super::ISkillSystem;
use starkane::models::entities::character::{Character, CharacterType};
use starkane::models::entities::skill::{Skill, SkillTrait, SkillType};
use starkane::store::{Store, StoreTrait};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[storage]
struct Storage {}

#[external(v0)]
impl SkillSystem of ISkillSystem<ContractState> {
fn init(self: @ContractState, world: IWorldDispatcher) {
fn init(self: @ContractState) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

// [Skill] MeeleAttack
Expand Down Expand Up @@ -68,7 +64,6 @@ mod skill_system {

fn has_skill(
self: @ContractState,
world: IWorldDispatcher,
character_id: u32,
player: felt252,
skill_id: u32
Expand All @@ -79,7 +74,6 @@ mod skill_system {

fn can_use_skill(
self: @ContractState,
world: IWorldDispatcher,
character_id: u32,
player: felt252,
skill_id: u32,
Expand Down
14 changes: 7 additions & 7 deletions src/systems/stadistics_system.cairo
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::interface]
trait IStadisticsSystem<TContractState> {
fn record_match_stadistics(self: @TContractState, world: IWorldDispatcher, match_id: u32);
// fn record_match_stadistics(self: @TContractState, match_id: u32);
}
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::contract]
#[dojo::contract]
mod stadistics_system {
use super::IStadisticsSystem;
use starkane::store::{Store, StoreTrait};
use starkane::models::states::match_state::{ MatchState, MatchPlayer };
use starkane::models::states::character_state::CharacterState;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};


#[storage]
struct Storage {}

#[external(v0)]
impl StadisticsSystem of IStadisticsSystem<ContractState> {
fn record_match_stadistics(self: @ContractState, world: IWorldDispatcher, match_id: u32) {
}

fn record_match_stadistics(world: IWorldDispatcher, match_id: u32) {
// [Setup] Datastore
let mut store: Store = StoreTrait::new(world);

Expand All @@ -45,7 +46,6 @@ mod stadistics_system {
i += 1;
}
}
}

fn calculate_score(match_state: MatchState, characters_states: @Array<CharacterState>) -> u128 {
let mut score = 0;
Expand Down
Loading

0 comments on commit a87acce

Please sign in to comment.