Skip to content

Commit

Permalink
added generate colony test
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Feb 7, 2024
1 parent c9af2bb commit e3bb044
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 11 deletions.
80 changes: 78 additions & 2 deletions src/colony/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod colonyactions {
};
use nogame::colony::models::{
ColonyCompounds, ColonyCount, ColonyResourceTimer, ColonyPosition, ColonyDefences,
PlanetColoniesCount, ColonyResource, ColonyShips,
PlanetColoniesCount, ColonyResource, ColonyShips, ColonyOwner
};
use nogame::colony::positions;
use nogame::compound::library as compound;
Expand Down Expand Up @@ -64,6 +64,7 @@ mod colonyactions {
let position = positions::get_colony_position(current_count.into());
let colony_id = current_planet_colonies + 1;
let id = ((planet_id * 1000) + colony_id.into());
set!(world, ColonyOwner { colony_planet_id: id, planet_id, });
set!(world, ColonyPosition { planet_id, colony_id, position });
set!(
world,
Expand Down Expand Up @@ -100,7 +101,11 @@ mod colonyactions {

fn process_defence_build(
ref self: ContractState, colony_id: u8, name: DefenceBuildType, quantity: u32,
) { // ...
) {
let world = self.world_dispatcher.read();
let caller = get_caller_address();
let planet_id = get!(world, caller, GamePlanet).planet_id;
build_defence(world, planet_id, colony_id, name, quantity);
}
}

Expand Down Expand Up @@ -623,3 +628,74 @@ mod colonyactions {
ERC20s { steel: steel_available, quartz: quartz_available, tritium: tritium_available, }
}
}

#[cfg(test)]
mod test {
use starknet::testing::{set_contract_address, set_block_timestamp};
use dojo::world::{IWorldDispatcherTrait, IWorldDispatcher};
use nogame::colony::actions::{IColonyActionsDispatcher, IColonyActionsDispatcherTrait};
use nogame::colony::models::{
ColonyOwner, ColonyPosition, ColonyCount, ColonyResourceTimer, PlanetColoniesCount,
ColonyResource, ColonyShips, ColonyDefences,
};
use nogame::libraries::{constants};
use nogame::data::types::{Position, ShipBuildType};
use nogame::libraries::names::Names;
use nogame::compound::models::{PlanetCompounds};
use nogame::game::models::{GameSetup, GamePlanetCount};
use nogame::planet::models::{
PlanetPosition, PositionToPlanet, PlanetResource, PlanetResourceTimer
};
use nogame::utils::test_utils::{
setup_world, OWNER, GAME_SPEED, ACCOUNT_1, ACCOUNT_2, ACCOUNT_3, ACCOUNT_4, ACCOUNT_5, DAY
};
use nogame::game::actions::{IGameActionsDispatcher, IGameActionsDispatcherTrait};
use nogame::planet::actions::{IPlanetActionsDispatcher, IPlanetActionsDispatcherTrait};
use nogame::dockyard::actions::{IDockyardActionsDispatcher, IDockyardActionsDispatcherTrait};
use nogame::tech::models::{PlanetTechs};
use nogame::dockyard::models::{PlanetShips};
use debug::PrintTrait;

#[test]
fn test_generate_colony() {
let (world, actions, nft, eth) = setup_world();
actions.game.spawn(OWNER(), nft, eth, constants::MIN_PRICE_UNSCALED, GAME_SPEED,);

set_contract_address(ACCOUNT_1());
actions.planet.generate_planet();
set!(world, PlanetTechs { planet_id: 1, name: Names::Tech::EXOCRAFT, level: 3 });

actions.colony.generate_colony();

let colony_owner = get!(world, 1001, ColonyOwner).planet_id;
assert!(colony_owner == 1, "Colony: owner not set correctly");

let total_colonies_count = get!(world, constants::GAME_ID, ColonyCount).count;
assert!(total_colonies_count == 1, "Colony: count not set correctly");

let planet_colonies_count = get!(world, 1, PlanetColoniesCount).count;
assert!(planet_colonies_count == 1, "Colony: count not set correctly");

let colony_position = get!(world, (1, 1), ColonyPosition).position;
assert!(
colony_position.system == 188 && colony_position.orbit == 10,
"Colony: position not set correctly"
);

actions.colony.generate_colony();
let colony_owner = get!(world, 1002, ColonyOwner).planet_id;
assert!(colony_owner == 1, "Colony: owner not set correctly");

let total_colonies_count = get!(world, constants::GAME_ID, ColonyCount).count;
assert!(total_colonies_count == 2, "Colony: count not set correctly");

let planet_colonies_count = get!(world, 1, PlanetColoniesCount).count;
assert!(planet_colonies_count == 2, "Colony: count not set correctly");

let colony_position = get!(world, (1, 2), ColonyPosition).position;
assert!(
colony_position.system == 182 && colony_position.orbit == 2,
"Colony: position not set correctly"
);
}
}
16 changes: 8 additions & 8 deletions src/colony/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use nogame::data::types::Position;
#[derive(Model, Copy, Drop, Serde)]
struct ColonyOwner {
#[key]
colony_planet_id: u32,
planet_id: u32,
colony_id: u8,
}

#[derive(Model, Copy, Drop, Serde)]
Expand All @@ -30,13 +30,13 @@ struct ColonyPosition {
position: Position,
}

#[derive(Model, Copy, Drop, Serde)]
struct PositionToColony {
#[key]
position: Position,
planet_id: u32,
colony_id: u8,
}
// #[derive(Model, Copy, Drop, Serde)]
// struct PositionToColony {
// #[key]
// position: Position,
// planet_id: u32,
// colony_id: u8,
// }

#[derive(Model, Copy, Drop, Serde)]
struct ColonyResource {
Expand Down
28 changes: 27 additions & 1 deletion src/utils/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ use dojo::test_utils::{spawn_test_world, deploy_contract};
use openzeppelin::token::erc20::interface::{
IERC20Dispatcher, IERC20DispatcherTrait, IERC20CamelDispatcher, IERC20CamelDispatcherTrait
};
use nogame::colony::models::{
ColonyOwner, ColonyCount, PlanetColoniesCount, ColonyPosition, ColonyResource,
ColonyResourceTimer, ColonyCompounds, ColonyShips, ColonyDefences
};
use nogame::colony::models::{
colony_owner, colony_count, planet_colonies_count, colony_position, colony_resource,
colony_resource_timer, colony_compounds, colony_ships, colony_defences
};
use nogame::colony::actions::{
colonyactions, {IColonyActionsDispatcher, IColonyActionsDispatcherTrait}
};
use nogame::compound::models::{planet_compounds};
use nogame::compound::models::{PlanetCompounds};
use nogame::game::models::{game_setup, game_planet, game_planet_owner, game_planet_count};
Expand Down Expand Up @@ -62,6 +73,7 @@ fn ACCOUNT_5() -> ContractAddress {

#[derive(Clone, Copy, Serde)]
struct Actions {
colony: IColonyActionsDispatcher,
compound: ICompoundActionsDispatcher,
game: IGameActionsDispatcher,
planet: IPlanetActionsDispatcher,
Expand All @@ -84,7 +96,16 @@ fn setup_world() -> (IWorldDispatcher, Actions, ContractAddress, ContractAddress
planet_resource_timer::TEST_CLASS_HASH,
planet_techs::TEST_CLASS_HASH,
planet_ships::TEST_CLASS_HASH,
planet_defences::TEST_CLASS_HASH
planet_defences::TEST_CLASS_HASH,
colony_owner::TEST_CLASS_HASH,
colony_count::TEST_CLASS_HASH,
planet_colonies_count::TEST_CLASS_HASH,
colony_position::TEST_CLASS_HASH,
colony_resource::TEST_CLASS_HASH,
colony_resource_timer::TEST_CLASS_HASH,
colony_ships::TEST_CLASS_HASH,
colony_defences::TEST_CLASS_HASH,
colony_compounds::TEST_CLASS_HASH
];

// deploy world with models
Expand Down Expand Up @@ -115,6 +136,10 @@ fn setup_world() -> (IWorldDispatcher, Actions, ContractAddress, ContractAddress
.deploy_contract('salt', defenceactions::TEST_CLASS_HASH.try_into().unwrap());
let defence_actions = IDefenceActionsDispatcher { contract_address };

let contract_address = world
.deploy_contract('salt', colonyactions::TEST_CLASS_HASH.try_into().unwrap());
let colony_actions = IColonyActionsDispatcher { contract_address };

let nft = deploy_nft(array!['NoGame NFT', 'NGPLANET', world.contract_address.into()]);
let eth = deploy_eth(array!['Ether', 'ETH', ETH_SUPPLY, 0, OWNER().into()]);

Expand Down Expand Up @@ -142,6 +167,7 @@ fn setup_world() -> (IWorldDispatcher, Actions, ContractAddress, ContractAddress
eth_contract.approve(planet_actions.contract_address, 10 * E18);

let actions = Actions {
colony: colony_actions,
compound: compound_actions,
game: game_actions,
planet: planet_actions,
Expand Down

0 comments on commit e3bb044

Please sign in to comment.