Skip to content

Commit

Permalink
introduce formal public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Feb 18, 2024
1 parent d7aeae2 commit 323d6f1
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 144 deletions.
89 changes: 68 additions & 21 deletions src/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
use kingdom_lord::models::resource::{Wood, Brick, Steel, Cereal};
use starknet::ContractAddress;

use kingdom_lord::models::building::{BuildingUpgradeResource};
use kingdom_lord::interface::IKingdomLord;
const MAX_STORAGE: u64 = 100000;
const GROWTH_RATE: u64 = 10;


#[starknet::interface]
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 {
use kingdom_lord::components::barn::{barn_component, Barn};
use kingdom_lord::components::warehouse::{warehouse_component, Warehouse};
use kingdom_lord::components::outer_city::{outer_city_component, OuterCity};
use kingdom_lord::components::outer_city::outer_city_component::OuterCityInternalImpl;
use kingdom_lord::components::city_hall::city_hall_component;
use kingdom_lord::helpers::tuple::{TupleFour, TupleSix};
use kingdom_lord::models::building::Building;
use kingdom_lord::models::resource::{Wood, Brick, Steel, Cereal};
use kingdom_lord::models::building::{
Building, BuildingUpgradeRequirementImpl, BuildingUpgradeTimeRequirementImpl,
BuildingUpgradeResource
};
use kingdom_lord::models::resource::{Wood, Brick, Steel, Cereal, Resource};
use kingdom_lord::models::level::Level;
use kingdom_lord::models::growth::GrowthRate;
use kingdom_lord::models::building::BuildingId;
use kingdom_lord::interface::IKingdomLord;
use starknet::get_caller_address;
use core::starknet::info::get_block_number;
use super::{MAX_STORAGE, GROWTH_RATE, SpawnTrait};
use super::{MAX_STORAGE, GROWTH_RATE};


component!(path: barn_component, storage: barn, event: BarnEvent);
Expand Down Expand Up @@ -58,21 +56,16 @@ mod kingdom_lord_controller {
CityHallEvent: city_hall_component::Event
}

#[abi(embed_v0)]
impl KLBarnImpl = barn_component::BarnImpl<ContractState>;

#[abi(embed_v0)]
impl KLWarehouseImpl = warehouse_component::WarehouseImpl<ContractState>;
impl KLWarehouseImpl = warehouse_component::WarehouseInternalImpl<ContractState>;

#[abi(embed_v0)]
impl KLOuterCityImpl = outer_city_component::OuterCityTraitImpl<ContractState>;

#[abi(embed_v0)]
impl KLCityHallImpl = city_hall_component::CityHallImpl<ContractState>;


#[abi(embed_v0)]
impl GameSpawnImpl of SpawnTrait<ContractState> {
impl GameSpawnImpl of IKingdomLord<ContractState> {
fn spawn(ref self: ContractState) {
let world = self.world_dispatcher.read();

Expand Down Expand Up @@ -132,5 +125,59 @@ mod kingdom_lord_controller {
)
);
}

fn get_minable_resource(
self: @ContractState
) -> (Resource<Wood>, Resource<Brick>, Resource<Steel>, Resource<Cereal>) {
self.outer_city.get_minable_resource()
}

fn get_growth_rate(
self: @ContractState
) -> (GrowthRate<Wood>, GrowthRate<Brick>, GrowthRate<Steel>, GrowthRate<Cereal>) {
self.outer_city.get_growth_rate()
}

fn start_upgrade(ref self: ContractState, building_id: u64) {
panic!("gg")
}

fn finish_upgrade(ref self: ContractState, building_id: u64) {
panic!("gg")
}

fn upgrade_requirement(self: @ContractState, building_id: u64) -> BuildingUpgradeResource {
let building_id: BuildingId = building_id.into();
match building_id {
BuildingId::WoodBuilding(id) => {
let building = self.outer_city.get_wood_building_by_index(id);
building.resource_required()
},
BuildingId::SteelBuilding(id) => {
let building = self.outer_city.get_steel_building_by_index(id);
building.resource_required()
},
BuildingId::BrickBuilding(id) => {
let building = self.outer_city.get_brick_building_by_index(id);
building.resource_required()
},
BuildingId::CerealBuilding(id) => {
let building = self.outer_city.get_cereal_building_by_index(id);
building.resource_required()
},
BuildingId::CityHall => { Default::default() },
BuildingId::Fence => { Default::default() },
BuildingId::GatheringPoint => { Default::default() },
BuildingId::SoldierCamp => { Default::default() },
BuildingId::HouseCamp => { Default::default() },
BuildingId::ResearchCenter => { Default::default() },
BuildingId::HeroCamp => { Default::default() },
BuildingId::Warehouse => { Default::default() },
BuildingId::Barn => { Default::default() },
BuildingId::Market => { Default::default() },
BuildingId::Embassy => { Default::default() },
BuildingId::SkyTower => { Default::default() },
}
}
}
}
51 changes: 0 additions & 51 deletions src/components/building_map.cairo

This file was deleted.

18 changes: 9 additions & 9 deletions src/components/city_hall.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod city_hall_component{

#[storage]
struct Storage {
Construction_Lists: LegacyMap::<u64, UnderConstruction>,
Construction_Lists: LegacyMap::<BuildingId, UnderConstruction>,
max_construction_index: u64,
under_construction_count: u64
}
Expand All @@ -58,7 +58,7 @@ mod city_hall_component{
if under_construction_count == 0{
break;
}
let under_construction: UnderConstruction = self.Construction_Lists.read(index);
let under_construction: UnderConstruction = self.Construction_Lists.read(index.into());
if under_construction.address == caller && under_construction.end_block_number < current_block_number{
constructions.append(under_construction);
under_construction_count -= 1;
Expand All @@ -81,7 +81,7 @@ mod city_hall_component{
if under_construction_count == 0{
break;
}
let under_construction: UnderConstruction = self.Construction_Lists.read(index);
let under_construction: UnderConstruction = self.Construction_Lists.read(index.into());
if under_construction.address == caller && under_construction.end_block_number >= current_block_number{
constructions.append(under_construction);
under_construction_count -= 1;
Expand All @@ -98,29 +98,29 @@ mod city_hall_component{
+HasComponent<TContractState>,
+IWorldProvider<TContractState>
> of InternalTrait<TContractState> {
fn start_upgrade(ref self: ComponentState<TContractState>, building_position: BuildingId, caller_address: ContractAddress) -> Result<u64, Error>{
fn start_upgrade(ref self: ComponentState<TContractState>, building_id: BuildingId, caller_address: ContractAddress) -> Result<u64, Error>{
let current_block_number = get_block_number();
let mut index = self.max_construction_index.read();
let mut under_construction_count = self.under_construction_count.read();
let under_construction = UnderConstruction{
address: caller_address,
target: building_position,
target: building_id,
start_block_number: current_block_number,
end_block_number: current_block_number + 3
};
self.max_construction_index.write(index + 1);
self.under_construction_count.write(under_construction_count + 1);
self.Construction_Lists.write(index + 1, under_construction);
self.Construction_Lists.write((index + 1).into(), under_construction);
Result::Ok(index+1)
}

fn finish_upgrade(ref self: ComponentState<TContractState>, index: u64) -> Result<u64 , Error>{
fn finish_upgrade(ref self: ComponentState<TContractState>, building_id: BuildingId) -> Result<BuildingId , Error>{
let current_block_number = get_block_number();
let mut under_construction: UnderConstruction = self.Construction_Lists.read(index);
let mut under_construction: UnderConstruction = self.Construction_Lists.read(building_id);
if under_construction.end_block_number < current_block_number{
let mut under_construction_count = self.under_construction_count.read();
self.under_construction_count.write(under_construction_count - 1);
Result::Ok(index)
Result::Ok(building_id)
}else{
Result::Err(Error::AlreadyUnderConstruction)
}
Expand Down
Empty file removed src/components/inner_city.cairo
Empty file.
27 changes: 25 additions & 2 deletions src/components/outer_city.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::super::models::building::{WoodBuilding, BrickBuilding, SteelBuilding,
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, GrowthSixImpl, GrowthFourImpl};
use super::super::helpers::tuple::{TupleFour, TupleSix, TupleSize6Drop, TupleSize6Serde, GrowthSixImpl, GrowthFourImpl, TupleSixExtension, TupleFourExtension};
use core::Serde;
use core::fmt::{Debug, Formatter, Error};

Expand Down Expand Up @@ -50,7 +50,7 @@ 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, GrowthRate, GrowthSixImpl, GrowthFourImpl};
use super::{TupleSize6Serde, TupleSize6Drop, GrowthRate, GrowthSixImpl, GrowthFourImpl, TupleSixExtension, TupleFourExtension};
use super::{OuterCityTrait, OuterCity, Building};
use super::{OuterCityLevelMap, GetLevel};

Expand Down Expand Up @@ -99,4 +99,27 @@ mod outer_city_component {
(wood_level, brick_level, steel_level, cereal_level)
}
}

#[generate_trait]
impl OuterCityInternalImpl<
TContractState,
+HasComponent<TContractState>,
+IWorldProvider<TContractState>
> of OuterCityInternalTrait<TContractState>{
fn get_wood_building_by_index(self: @ComponentState<TContractState>, index: u64) -> @WoodBuilding{
get!(self.get_contract().world(), get_caller_address(), (OuterCity)).wood_field.get_by_index(index)
}

fn get_brick_building_by_index(self: @ComponentState<TContractState>, index: u64) -> @BrickBuilding{
get!(self.get_contract().world(), get_caller_address(), (OuterCity)).brick_field.get_by_index(index)
}

fn get_steel_building_by_index(self: @ComponentState<TContractState>, index: u64) -> @SteelBuilding{
get!(self.get_contract().world(), get_caller_address(), (OuterCity)).steel_field.get_by_index(index)
}

fn get_cereal_building_by_index(self: @ComponentState<TContractState>, index: u64) -> @CerealBuilding{
get!(self.get_contract().world(), get_caller_address(), (OuterCity)).cereal_field.get_by_index(index)
}
}
}
34 changes: 9 additions & 25 deletions src/components/warehouse.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ struct Warehouse {
max_storage: u64
}

trait WarehouseExtension {
fn get_resource(self:@Warehouse) -> (Resource<Wood>, Resource<Brick>, Resource<Steel>);
fn add_resource(ref self:Warehouse, wood: Resource<Wood>, bricks: Resource<Brick>, steel: Resource<Steel>);
fn remove_resource(ref self: Warehouse, wood: Resource<Wood>, bricks: Resource<Brick>, steel: Resource<Steel>);
}

#[generate_trait]
impl WarehouseExtensionImpl of WarehouseExtension{
fn get_resource(self:@Warehouse) -> (Resource<Wood>, Resource<Brick>, Resource<Steel>) {
(self.wood.clone(), self.bricks.clone(), self.steel.clone())
Expand Down Expand Up @@ -45,11 +40,6 @@ impl AdminWareHouseImpl of AdminWareHouseTrait{
}
}

#[starknet::interface]
trait WarehouseTrait<TState> {
fn get_resource(self:@TState) -> (Resource<Wood>, Resource<Brick>, Resource<Steel>);
}


#[starknet::component]
mod warehouse_component{
Expand All @@ -58,26 +48,15 @@ mod warehouse_component{
use dojo::world::{
IWorldProvider, IWorldProviderDispatcher, IWorldDispatcher, IWorldDispatcherTrait
};
use super::{Warehouse, WarehouseTrait, Brick, Wood, Steel, Resource, WarehouseExtension};
use super::{Warehouse, Brick, Wood, Steel, Resource, WarehouseExtension};

#[storage]
struct Storage {}

#[embeddable_as(WarehouseImpl)]
impl WarehouseTraitImpl<
TContractState, +HasComponent<TContractState>, +IWorldProvider<TContractState>
> of WarehouseTrait<ComponentState<TContractState>>{
fn get_resource(self:@ComponentState<TContractState>) -> (Resource<Wood>, Resource<Brick>, Resource<Steel>){
let warehouse = get!(self.get_contract().world(), get_caller_address(), (Warehouse));
warehouse.get_resource()
}

}

#[generate_trait]
impl InternalImpl<
impl WarehouseInternalImpl<
TContractState, +HasComponent<TContractState>, +IWorldProvider<TContractState>
> of InternalTrait<TContractState> {
> of WarehouseInternalTrait<TContractState> {
fn add_resource(self:@ComponentState<TContractState>, wood: Resource<Wood>, bricks: Resource<Brick>, steel: Resource<Steel>){
let world = self.get_contract().world();
let mut warehouse: Warehouse = get!(world, get_caller_address(), (Warehouse));
Expand All @@ -91,6 +70,11 @@ mod warehouse_component{
warehouse.remove_resource(wood, bricks, steel);
set!(world, (warehouse))
}

fn get_resource(self:@ComponentState<TContractState>) -> (Resource<Wood>, Resource<Brick>, Resource<Steel>){
let warehouse = get!(self.get_contract().world(), get_caller_address(), (Warehouse));
warehouse.get_resource()
}
}
}

Loading

0 comments on commit 323d6f1

Please sign in to comment.