Skip to content

Commit

Permalink
grow rate
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Feb 17, 2024
1 parent 999d82f commit d7aeae2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ trait SpawnTrait<TState> {
fn spawn(ref self: TState);
}

// #[starket::interface]
// trait UpgradeTrait<TState> {
// fn upgrade(ref self: TState, building_id: u64);
// }

// dojo decorator
#[dojo::contract]
mod kingdom_lord_controller {
Expand Down
22 changes: 11 additions & 11 deletions src/components/outer_city.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use starknet::ContractAddress;
use starknet::get_caller_address;
use super::super::models::building::{WoodBuilding, BrickBuilding, SteelBuilding, CerealBuilding};
use super::super::models::building::{WoodBuilding, BrickBuilding, SteelBuilding, CerealBuilding, Building};
use super::super::models::resource::{Resource, Wood, Brick, Steel, Cereal};
use super::super::models::growth::{GrowthRate};
use super::super::models::level::{Level, GetLevel};
use super::super::helpers::tuple::{TupleFour, TupleSix, TupleSize6Drop, TupleSize6Serde};
use super::super::helpers::tuple::{TupleFour, TupleSix, TupleSize6Drop, TupleSize6Serde, GrowthSixImpl, GrowthFourImpl};
use core::Serde;
use core::fmt::{Debug, Formatter, Error};

Expand Down Expand Up @@ -32,13 +33,12 @@ struct OuterCity {
#[starknet::interface]
trait OuterCityTrait<TState> {
fn get_minable_resource(self: @TState)-> (Resource<Wood>, Resource<Brick>, Resource<Steel>, Resource<Cereal>);
fn get_growth_rate(self: @TState) -> (u64, u64, u64, u64);
fn get_growth_rate(self: @TState)-> (GrowthRate<Wood>, GrowthRate<Brick>, GrowthRate<Steel>, GrowthRate<Cereal>);
fn get_last_mined_block(self: @TState) -> u64;
fn get_outer_city_building_level(self: @TState) -> OuterCityLevelMap;
}



#[starknet::component]
mod outer_city_component {
use starknet::{get_caller_address};
Expand All @@ -50,8 +50,8 @@ mod outer_city_component {
use super::{Resource, Wood, Brick, Steel, Cereal};
use super::super::super::models::building::{Minable};
use super::super::super::models::growth::Growth;
use super::{TupleSize6Serde, TupleSize6Drop};
use super::{OuterCityTrait, OuterCity};
use super::{TupleSize6Serde, TupleSize6Drop, GrowthRate, GrowthSixImpl, GrowthFourImpl};
use super::{OuterCityTrait, OuterCity, Building};
use super::{OuterCityLevelMap, GetLevel};

#[storage]
Expand All @@ -77,12 +77,12 @@ mod outer_city_component {
)
}

fn get_growth_rate(self: @ComponentState<TContractState>) -> (u64, u64, u64, u64){
fn get_growth_rate(self: @ComponentState<TContractState>) -> (GrowthRate<Wood>, GrowthRate<Brick>, GrowthRate<Steel>, GrowthRate<Cereal>){
let outer_city = get!(self.get_contract().world(), get_caller_address(), (OuterCity));
let wood_growth_rate = outer_city.wood_field.get_growth_rate();
let brick_growth_rate = outer_city.brick_field.get_growth_rate();
let steel_growth_rate = outer_city.steel_field.get_growth_rate();
let cereal_growth_rate = outer_city.cereal_field.get_growth_rate();
let wood_growth_rate: GrowthRate<Wood> = outer_city.wood_field.get_growth_rate();
let brick_growth_rate: GrowthRate<Brick> = outer_city.brick_field.get_growth_rate();
let steel_growth_rate: GrowthRate<Steel> = outer_city.steel_field.get_growth_rate();
let cereal_growth_rate: GrowthRate<Cereal> = outer_city.cereal_field.get_growth_rate();
(wood_growth_rate, brick_growth_rate, steel_growth_rate, cereal_growth_rate)
}

Expand Down
10 changes: 5 additions & 5 deletions src/helpers/tuple.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::serde::Serde;
use super::super::models::building::{Minable, Building};
use super::super::models::growth::Growth;
use super::super::models::growth::{Growth, GrowthRate};
use super::super::models::level::{GetLevel, Level};
#[derive(Serde, Drop, Copy, Introspect, Default, Debug)]
struct TupleFour<T> {
Expand All @@ -19,8 +19,8 @@ impl MinableFourImpl<T, +Minable<T>> of Minable<TupleFour<T>> {
}
}

impl GrowthFourImpl<T, +Growth<T>> of Growth<TupleFour<T>> {
fn get_growth_rate(self: @TupleFour<T>) -> u64 {
impl GrowthFourImpl<T, R, +Growth<T, R>, +Drop<T>, +Drop<R>> of Growth<TupleFour<T>, R> {
fn get_growth_rate(self: @TupleFour<T>) -> GrowthRate<R> {
self.first.get_growth_rate()
+ self.second.get_growth_rate()
+ self.third.get_growth_rate()
Expand Down Expand Up @@ -55,8 +55,8 @@ impl MinableSixImpl<T, +Minable<T>> of Minable<TupleSix<T>> {
}
}

impl GrowthSixImpl<T, +Growth<T>> of Growth<TupleSix<T>> {
fn get_growth_rate(self: @TupleSix<T>) -> u64 {
impl GrowthSixImpl<T, R, +Growth<T, R>, +Drop<T>, +Drop<R>> of Growth<TupleSix<T>, R> {
fn get_growth_rate(self: @TupleSix<T>) -> GrowthRate<R> {
self.first.get_growth_rate()
+ self.second.get_growth_rate()
+ self.third.get_growth_rate()
Expand Down
10 changes: 5 additions & 5 deletions src/models/building.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::clone::Clone;
use super::level::{Level, GetLevel};
use super::resource::{Resource, Wood, Brick, Steel, Cereal};
use super::growth::Growth;
use super::growth::{Growth, GrowthRate};

#[derive(Drop, Introspect, Copy, Serde, Default, Debug)]
struct Building<T>{
Expand Down Expand Up @@ -98,9 +98,9 @@ impl GetLevelImpl<T> of GetLevel<Building<T>, Level>{
}
}

impl GrowthRate<T> of Growth<Building<T>>{
fn get_growth_rate(self: @Building<T>) -> u64{
*self.growth_rate * *self.level.level
impl BuildingGrowthRate<T> of Growth<Building<T>, T>{
fn get_growth_rate(self: @Building<T>) -> GrowthRate<T>{
GrowthRate { amount: *self.growth_rate * *self.level.level}
}

}
Expand All @@ -111,7 +111,7 @@ trait Minable<T>{

impl MinableImpl<T, +Serde<T>> of Minable<Building<T>>{
fn get_minable(self: @Building<T>, last_block_number:u64, current_block_number: u64)-> u64{
self.get_growth_rate() * (current_block_number - last_block_number)
self.get_growth_rate().into() * (current_block_number - last_block_number)
}
}

Expand Down
37 changes: 34 additions & 3 deletions src/models/growth.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
trait Growth<T>{
fn get_growth_rate(self: @T) -> u64;
}
use super::resource::{Wood, Brick, Steel, Cereal};
use core::traits::{Add, Into};

trait Growth<T, R>{
fn get_growth_rate(self: @T) -> GrowthRate<R>;
}

#[derive(Copy, Drop, Serde, PartialEq)]
struct GrowthRate<T> {
amount: u64
}

impl LevelInto<T> of Into<u64, GrowthRate<T>>{
fn into(self: u64) -> GrowthRate<T>{
GrowthRate{ amount: self }
}
}

impl GrowthRateIntou64<T> of Into<GrowthRate<T>,u64>{
fn into(self: GrowthRate<T>) -> u64{
self.amount
}
}

impl GrowthRateAdd<T> of Add<GrowthRate<T>>{
fn add(lhs: GrowthRate<T>, rhs: GrowthRate<T>) -> GrowthRate<T>{
GrowthRate{ amount: lhs.amount + rhs.amount }
}
}

type WoodGrowthRate = GrowthRate<Wood>;
type BrickGrowthRate = GrowthRate<Brick>;
type SteelGrowthRate = GrowthRate<Steel>;
type CerealGrowthRate = GrowthRate<Cereal>;
8 changes: 4 additions & 4 deletions src/tests/test_spawn.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ mod tests {
assert(cereal == 0_u64.into(), 'initial cereal should be 0');

let (wood_growth_rate, steel_growth_rate, brick_growth_rate, cereal_growth_rate) = context.outer_city_trait.get_growth_rate();
assert(wood_growth_rate == 10_u64, 'wood growth rate should be 10');
assert(steel_growth_rate == 10_u64, 'steel growth rate should be 10');
assert(brick_growth_rate == 10_u64, 'brick growth rate should be 10');
assert(cereal_growth_rate == 10_u64, 'cereal growth rate should be 10');
assert(wood_growth_rate.into() == 10_u64, 'wood growth rate should be 10');
assert(steel_growth_rate.into() == 10_u64, 'steel growth rate should be 10');
assert(brick_growth_rate.into() == 10_u64, 'brick growth rate should be 10');
assert(cereal_growth_rate.into() == 10_u64, 'cereal growth rate should be 10');

}
}

0 comments on commit d7aeae2

Please sign in to comment.