Skip to content

Commit

Permalink
fix: set model when update
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Feb 1, 2024
1 parent ebdf9ad commit a7ef4c3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/models/resource/brick.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Drop, Serde, Copy, Introspect)]
#[derive(Drop, Serde, Copy, Introspect, PartialEq)]
struct Brick {
amount: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/resource/cereal.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Drop, Serde, Copy, Introspect)]
#[derive(Drop, Serde, Copy, Introspect, PartialEq)]
struct Cereal {
amount: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/resource/steel.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Drop, Serde, Copy, Introspect)]
#[derive(Drop, Serde, Copy, Introspect, PartialEq)]
struct Steel {
amount: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/resource/wood.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Drop, Serde, Copy, Introspect)]
#[derive(Drop, Serde, Copy, Introspect, PartialEq)]
struct Wood {
amount: u64,
}
Expand Down
39 changes: 37 additions & 2 deletions src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ mod actions {
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
NewUserCreatedEvent: NewUserCreatedEvent,
MineResourceEvent: MineResourceEvent,
SetMinableResourceEvent: SetMinableResourceEvent,
SetGrowthRateEvent: SetGrowthRateEvent,
}


#[derive(Drop, starknet::Event)]
struct NewUserCreatedEvent {
player: ContractAddress,
}

#[derive(Drop, starknet::Event)]
struct MineResourceEvent {
player: ContractAddress,
Expand All @@ -30,11 +38,26 @@ mod actions {
bricks: u64
}

#[derive(Drop, starknet::Event)]
struct SetMinableResourceEvent {
player: ContractAddress,
origin_value: u64,
new_value: u64
}

#[derive(Drop, starknet::Event)]
struct SetGrowthRateEvent {
player: ContractAddress,
origin_value: u64,
new_value: u64
}

#[starknet::interface]
trait ResourceActions<TContractState> {
fn spawn(self: @TContractState);
fn mine(self: @TContractState);
fn get_minable_resource(self: @TContractState)-> (Wood, Brick, Steel, Cereal);
fn get_growth_rate(self: @TContractState) -> u64;
}

#[starknet::interface]
Expand Down Expand Up @@ -74,6 +97,7 @@ mod actions {
ResourceField { player, last_mined_block: block, growth_rate: GROWTH_RATE }
)
);
emit!(world, NewUserCreatedEvent{player});
}

fn mine(self: @ContractState) {
Expand All @@ -93,6 +117,9 @@ mod actions {

warehouse.add_resource(wood, brick, steel);
barn.add_cereal(cereal);
set!(world, (warehouse));
set!(world, (barn));
emit!(world, MineResourceEvent{player, wood: wood.into(), steel: steel.into(), cereals: cereal.into(), bricks: brick.into()});
}

fn get_minable_resource(self: @ContractState) -> (Wood, Brick, Steel, Cereal){
Expand All @@ -103,6 +130,13 @@ mod actions {
let (wood, brick, steel, cereal) = resource_field.get_minable(block);
(wood.clone(), brick.clone(), steel.clone(), cereal.clone())
}

fn get_growth_rate(self: @ContractState) -> u64 {
let world = self.world_dispatcher.read();
let player = get_caller_address();
let resource_field = get!(world, player, (ResourceField));
resource_field.get_growth_rate()
}
}


Expand All @@ -113,13 +147,14 @@ mod actions {
let player = get_caller_address();
let mut resource_field = get!(world, player, (ResourceField));
resource_field.set_growth_rate(growth_rate);

set!(world, (resource_field));
}
fn set_warehouse_size(ref self: ContractState, player: ContractAddress, size: u64){
let world = self.world_dispatcher.read();
let player = get_caller_address();
let mut warehouse = get!(world, player, (Warehouse));
warehouse.set_max_storage(size);
set!(world, (warehouse));
}
}
}
17 changes: 17 additions & 0 deletions src/tests/test_world.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(test)]
mod tests {
use kingdom_lord::systems::actions::actions::AdminActionsDispatcherTrait;
use starknet::class_hash::Felt252TryIntoClassHash;
use starknet::get_caller_address;

// import world dispatcher
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
Expand Down Expand Up @@ -31,9 +33,24 @@ mod tests {
let contract_address = world
.deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap());
let actions_system = ResourceActionsDispatcher { contract_address };
let admin_system = AdminActionsDispatcher{ contract_address };

// call spawn()
actions_system.spawn();

let (wood,brick,steel,cereal) = actions_system.get_minable_resource();

assert(wood == 0_u64.into(), 'wood');
assert(brick == 0_u64.into(), 'brick');
assert(steel == 0_u64.into(), 'steel');
assert(cereal == 0_u64.into(), 'cereal');

let player = get_caller_address();

let growth_rate = actions_system.get_growth_rate();
assert(growth_rate == 10_u64, 'growth rate should be 10');
admin_system.set_minable_growth_rate(player, 100);
let growth_rate = actions_system.get_growth_rate();
assert(growth_rate == 100_u64, 'growth rate should be 100');
}
}

0 comments on commit a7ef4c3

Please sign in to comment.