Skip to content

Commit

Permalink
added more tests for colony
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Jan 18, 2024
1 parent 53f751c commit 4ea2741
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 240 deletions.
78 changes: 50 additions & 28 deletions src/colony/colony.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ use nogame::libraries::types::{

#[starknet::interface]
trait IColonyWrite<TState> {
fn generate_colony(ref self: TState, planet_id: u16) -> (u8, PlanetPosition);
fn generate_colony(ref self: TState, planet_id: u32) -> (u8, PlanetPosition);
fn collect_colony_resources(
ref self: TState, uni_speed: u128, planet_id: u16, colony_id: u8
ref self: TState, uni_speed: u128, planet_id: u32, colony_id: u8
) -> ERC20s;
fn process_colony_compound_upgrade(
ref self: TState, planet_id: u16, colony_id: u8, name: ColonyUpgradeType, quantity: u8
ref self: TState, planet_id: u32, colony_id: u8, name: ColonyUpgradeType, quantity: u8
);
fn process_colony_unit_build(
ref self: TState,
planet_id: u16,
planet_id: u32,
colony_id: u8,
techs: TechLevels,
name: ColonyBuildType,
quantity: u32,
is_testnet: bool
);
fn get_colony_resources(
self: @TState, uni_speed: u128, planet_id: u16, colony_id: u8
self: @TState, uni_speed: u128, planet_id: u32, colony_id: u8
) -> ERC20s;
}

#[starknet::interface]
trait IColonyView<TState> {
fn get_colonies_for_planet(self: @TState, planet_id: u16) -> Array<(u8, PlanetPosition)>;
fn get_colony_coumpounds(self: @TState, planet_id: u16, colony_id: u8) -> CompoundsLevels;
fn get_colony_defences(self: @TState, planet_id: u16, colony_id: u8) -> DefencesLevels;
fn get_colonies_for_planet(self: @TState, planet_id: u32) -> Array<(u8, PlanetPosition)>;
fn get_colony_coumpounds(self: @TState, planet_id: u32, colony_id: u8) -> CompoundsLevels;
fn get_colony_defences(self: @TState, planet_id: u32, colony_id: u8) -> DefencesLevels;
}

mod ResourceName {
Expand All @@ -57,21 +57,21 @@ mod ColonyComponent {
#[storage]
struct Storage {
colony_count: usize,
planet_colonies_count: LegacyMap::<u16, u8>,
colony_position: LegacyMap::<(u16, u8), PlanetPosition>,
position_to_colony: LegacyMap::<PlanetPosition, (u16, u8)>,
colony_resource_timer: LegacyMap<(u16, u8), u64>,
colony_compounds: LegacyMap::<(u16, u8, felt252), u8>,
colony_ships: LegacyMap::<(u16, u8, felt252), u32>,
colony_defences: LegacyMap::<(u16, u8, felt252), u32>,
planet_colonies_count: LegacyMap::<u32, u8>,
colony_position: LegacyMap::<(u32, u8), PlanetPosition>,
position_to_colony: LegacyMap::<PlanetPosition, (u32, u8)>,
colony_resource_timer: LegacyMap<(u32, u8), u64>,
colony_compounds: LegacyMap::<(u32, u8, felt252), u8>,
colony_ships: LegacyMap::<(u32, u8, felt252), u32>,
colony_defences: LegacyMap::<(u32, u8, felt252), u32>,
}

#[embeddable_as(ColonyWrite)]
impl ColonyWriteImpl<
TContractState, +HasComponent<TContractState>
> of super::IColonyWrite<ComponentState<TContractState>> {
fn generate_colony(
ref self: ComponentState<TContractState>, planet_id: u16
ref self: ComponentState<TContractState>, planet_id: u32
) -> (u8, PlanetPosition) {
let current_count = self.colony_count.read();
let position = positions::get_colony_position(current_count);
Expand All @@ -85,7 +85,7 @@ mod ColonyComponent {
}

fn collect_colony_resources(
ref self: ComponentState<TContractState>, uni_speed: u128, planet_id: u16, colony_id: u8
ref self: ComponentState<TContractState>, uni_speed: u128, planet_id: u32, colony_id: u8
) -> ERC20s {
assert!(
!self.colony_position.read((planet_id, colony_id)).is_zero(),
Expand All @@ -101,7 +101,7 @@ mod ColonyComponent {

fn process_colony_compound_upgrade(
ref self: ComponentState<TContractState>,
planet_id: u16,
planet_id: u32,
colony_id: u8,
name: ColonyUpgradeType,
quantity: u8
Expand All @@ -117,7 +117,7 @@ mod ColonyComponent {

fn process_colony_unit_build(
ref self: ComponentState<TContractState>,
planet_id: u16,
planet_id: u32,
colony_id: u8,
techs: TechLevels,
name: ColonyBuildType,
Expand All @@ -134,8 +134,11 @@ mod ColonyComponent {
}

fn get_colony_resources(
self: @ComponentState<TContractState>, uni_speed: u128, planet_id: u16, colony_id: u8
self: @ComponentState<TContractState>, uni_speed: u128, planet_id: u32, colony_id: u8
) -> ERC20s {
if self.colony_position.read((planet_id, colony_id)).is_zero() {
return Zeroable::zero();
}
self.calculate_colony_production(uni_speed, planet_id, colony_id)
}
}
Expand All @@ -145,7 +148,7 @@ mod ColonyComponent {
TContractState, +HasComponent<TContractState>
> of super::IColonyView<ComponentState<TContractState>> {
fn get_colonies_for_planet(
self: @ComponentState<TContractState>, planet_id: u16
self: @ComponentState<TContractState>, planet_id: u32
) -> Array<(u8, PlanetPosition)> {
let mut arr: Array<(u8, PlanetPosition)> = array![];
let mut i = 1;
Expand All @@ -161,7 +164,7 @@ mod ColonyComponent {
}

fn get_colony_coumpounds(
self: @ComponentState<TContractState>, planet_id: u16, colony_id: u8
self: @ComponentState<TContractState>, planet_id: u32, colony_id: u8
) -> CompoundsLevels {
CompoundsLevels {
steel: self.colony_compounds.read((planet_id, colony_id, Names::STEEL)),
Expand All @@ -174,7 +177,7 @@ mod ColonyComponent {
}

fn get_colony_defences(
self: @ComponentState<TContractState>, planet_id: u16, colony_id: u8
self: @ComponentState<TContractState>, planet_id: u32, colony_id: u8
) -> DefencesLevels {
DefencesLevels {
celestia: self.colony_defences.read((planet_id, colony_id, Names::CELESTIA)),
Expand All @@ -190,9 +193,28 @@ mod ColonyComponent {
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn reset_resource_timer(
ref self: ComponentState<TContractState>, planet_id: u32, colony_id: u8
) {
self.colony_resource_timer.write((planet_id, colony_id), get_block_timestamp());
}

fn update_defences_after_attack(
ref self: ComponentState<TContractState>,
planet_id: u32,
colony_id: u8,
d: DefencesLevels
) {
self.colony_defences.write((planet_id, colony_id, Names::CELESTIA), d.celestia);
self.colony_defences.write((planet_id, colony_id, Names::BLASTER), d.blaster);
self.colony_defences.write((planet_id, colony_id, Names::BEAM), d.beam);
self.colony_defences.write((planet_id, colony_id, Names::ASTRAL), d.astral);
self.colony_defences.write((planet_id, colony_id, Names::PLASMA), d.plasma);
}

fn upgrade_component(
ref self: ComponentState<TContractState>,
planet_id: u16,
planet_id: u32,
colony_id: u8,
component: ColonyUpgradeType,
quantity: u8
Expand Down Expand Up @@ -258,7 +280,7 @@ mod ColonyComponent {

fn build_component(
ref self: ComponentState<TContractState>,
planet_id: u16,
planet_id: u32,
colony_id: u8,
techs: TechLevels,
component: ColonyBuildType,
Expand Down Expand Up @@ -324,7 +346,7 @@ mod ColonyComponent {


fn get_coumpounds_levels(
self: @ComponentState<TContractState>, planet_id: u16, colony_id: u8
self: @ComponentState<TContractState>, planet_id: u32, colony_id: u8
) -> CompoundsLevels {
CompoundsLevels {
steel: self.colony_compounds.read((planet_id, colony_id, Names::STEEL)),
Expand All @@ -337,7 +359,7 @@ mod ColonyComponent {
}

fn calculate_colony_production(
self: @ComponentState<TContractState>, uni_speed: u128, planet_id: u16, colony_id: u8
self: @ComponentState<TContractState>, uni_speed: u128, planet_id: u32, colony_id: u8
) -> ERC20s {
let time_now = get_block_timestamp();
let last_collection_time = self.colony_resource_timer.read((planet_id, colony_id));
Expand Down Expand Up @@ -379,7 +401,7 @@ mod ColonyComponent {
ERC20s { steel: steel_available, quartz: quartz_available, tritium: tritium_available, }
}

fn calculate_avg_temperature(self: @ComponentState<TContractState>, orbit: u8) -> u16 {
fn calculate_avg_temperature(self: @ComponentState<TContractState>, orbit: u8) -> u32 {
if orbit == 1 {
return 230;
}
Expand Down
45 changes: 22 additions & 23 deletions src/game/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,27 @@ trait INoGame<TState> {
// View functions
fn get_token_addresses(self: @TState) -> Tokens;
fn get_current_planet_price(self: @TState) -> u128;
fn get_number_of_planets(self: @TState) -> u16;
fn get_generated_planets_positions(self: @TState) -> Array<PlanetPosition>;
fn get_planet_position(self: @TState, planet_id: u16) -> PlanetPosition;
fn get_position_slot_occupant(self: @TState, position: PlanetPosition) -> u16;
fn get_last_active(self: @TState, planet_id: u16) -> u64;
fn get_planet_colonies(self: @TState, planet_id: u16) -> Array<(u8, PlanetPosition)>;
fn get_compounds_levels(self: @TState, planet_id: u16) -> CompoundsLevels;
fn get_colony_compounds(self: @TState, planet_id: u16, colony_id: u8) -> CompoundsLevels;
fn get_tech_levels(self: @TState, planet_id: u16) -> TechLevels;
fn get_debris_field(self: @TState, planet_id: u16) -> Debris;
fn get_spendable_resources(self: @TState, planet_id: u16) -> ERC20s;
fn get_collectible_resources(self: @TState, planet_id: u16) -> ERC20s;
fn get_colony_collectible_resources(self: @TState, planet_id: u16, colony_id: u8) -> ERC20s;
fn get_planet_points(self: @TState, planet_id: u16) -> u128;
fn get_ships_levels(self: @TState, planet_id: u16) -> Fleet;
fn get_celestia_available(self: @TState, planet_id: u16) -> u32;
fn get_celestia_production(self: @TState, planet_id: u16) -> u16;
fn get_defences_levels(self: @TState, planet_id: u16) -> DefencesLevels;
fn get_colony_defences_levels(self: @TState, planet_id: u16, colony_id: u8) -> DefencesLevels;
fn is_noob_protected(self: @TState, planet1_id: u16, planet2_id: u16) -> bool;
fn get_mission_details(self: @TState, planet_id: u16, mission_id: usize) -> Mission;
fn get_hostile_missions(self: @TState, planet_id: u16) -> Array<HostileMission>;
fn get_active_missions(self: @TState, planet_id: u16) -> Array<Mission>;
fn get_number_of_planets(self: @TState) -> u32;
fn get_planet_position(self: @TState, planet_id: u32) -> PlanetPosition;
fn get_position_slot_occupant(self: @TState, position: PlanetPosition) -> u32;
fn get_last_active(self: @TState, planet_id: u32) -> u64;
fn get_planet_colonies(self: @TState, planet_id: u32) -> Array<(u8, PlanetPosition)>;
fn get_compounds_levels(self: @TState, planet_id: u32) -> CompoundsLevels;
fn get_colony_compounds(self: @TState, planet_id: u32, colony_id: u8) -> CompoundsLevels;
fn get_tech_levels(self: @TState, planet_id: u32) -> TechLevels;
fn get_debris_field(self: @TState, planet_id: u32) -> Debris;
fn get_spendable_resources(self: @TState, planet_id: u32) -> ERC20s;
fn get_collectible_resources(self: @TState, planet_id: u32) -> ERC20s;
fn get_colony_collectible_resources(self: @TState, planet_id: u32, colony_id: u8) -> ERC20s;
fn get_planet_points(self: @TState, planet_id: u32) -> u128;
fn get_ships_levels(self: @TState, planet_id: u32) -> Fleet;
fn get_celestia_available(self: @TState, planet_id: u32) -> u32;
fn get_celestia_production(self: @TState, planet_id: u32) -> u32;
fn get_defences_levels(self: @TState, planet_id: u32) -> DefencesLevels;
fn get_colony_defences_levels(self: @TState, planet_id: u32, colony_id: u8) -> DefencesLevels;
fn is_noob_protected(self: @TState, planet1_id: u32, planet2_id: u32) -> bool;
fn get_mission_details(self: @TState, planet_id: u32, mission_id: usize) -> Mission;
fn get_hostile_missions(self: @TState, planet_id: u32) -> Array<HostileMission>;
fn get_active_missions(self: @TState, planet_id: u32) -> Array<Mission>;
}

Loading

0 comments on commit 4ea2741

Please sign in to comment.