Skip to content

Commit

Permalink
replace enum
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed May 6, 2024
1 parent e67a023 commit de6f1cd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/components/battle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ mod battle_component {
let target_enemy_village = get!(world, (target_x, target_y), GlobeLocation);

match target_enemy_village.kind{
LocationKind::Village(enemy_player) =>{
let mut enemy_troops = get!(world, (enemy_player), Troops);
LocationKind::Village =>{
let mut enemy_troops = get!(world, (target_enemy_village.player), Troops);
ambush_info.army.fight(ref enemy_troops.army);

ambush_info.is_revealed = true;
Expand Down Expand Up @@ -194,8 +194,8 @@ mod battle_component {
let target_enemy_village = get!(world, (target_x, target_y), GlobeLocation);

match target_enemy_village.kind{
LocationKind::Village(robbed_player) =>{
let mut enemy_troops = get!(world, (robbed_player), Troops);
LocationKind::Village =>{
let mut enemy_troops = get!(world, (target_enemy_village.player), Troops);
ambush_info.army.rob(ref enemy_troops.army);

let load_capacity = ambush_info.army.load_capacity();
Expand All @@ -206,7 +206,7 @@ mod battle_component {
set!(world, (enemy_troops));
set!(world, (self_troops));
set!(world, (ambush_info));
return Result::Ok((robbed_player, player, load_capacity));
return Result::Ok((player, player, load_capacity));
},
_ => {return Result::Err(Error::LocationNotVillage);}
}
Expand Down
22 changes: 16 additions & 6 deletions src/components/globe.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ struct GlobeLocation {
#[key]
y: u64,
kind: LocationKind,
// zero if location kind is not village
player: ContractAddress
}

#[derive(Introspect, Copy, Drop, Serde, PartialEq)]
enum LocationKind{
Nothing: ContractAddress,
Village: ContractAddress,
Army: ContractAddress,
Block: ContractAddress,
Nothing,
Village,
Army,
Block,
}

// #[derive(Introspect, Copy, Drop, Serde, PartialEq)]
// enum LocationKind{
// Nothing: ContractAddress,
// Village: ContractAddress,
// Army: ContractAddress,
// Block: ContractAddress,
// }

#[derive(Model, Copy, Drop, Serde)]
struct VillageConfirm {
#[key]
Expand Down Expand Up @@ -116,11 +126,11 @@ mod globe_component {
let (x, y) = get_position_temp(confirm.block);

let village_location = get!(world, (x, y), GlobeLocation);
if village_location.kind != LocationKind::Nothing(ContractAddressZero::zero()) {
if village_location.kind != LocationKind::Nothing{
return Result::Err(Error::VillagePositionAlreadyTaken);
}
let village = PlayerVillage { player, x, y };
let village_location = GlobeLocation { x, y, kind: LocationKind::Village(player) };
let village_location = GlobeLocation { x, y, kind: LocationKind::Village, player };
set!(world, (village_location));
set!(world, (village));
Result::Ok(())
Expand Down
22 changes: 11 additions & 11 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ mod helpers {
}

mod tests {
// mod test_spawn;
// mod test_storage;
// mod test_upgrade;
// mod test_city_hall;
// mod test_city_wall;
// mod test_train;
// mod test_embassy;
// mod test_total_population;
// // mod test_pay_upgrade;
// mod test_growth_rate;
mod test_spawn;
mod test_storage;
mod test_upgrade;
mod test_city_hall;
mod test_city_wall;
mod test_train;
mod test_embassy;
mod test_total_population;
// mod test_pay_upgrade;
mod test_growth_rate;
mod test_create_city;
// mod test_ambush;
mod test_ambush;
mod utils;
mod upgrade_proof;
mod upgrade_func;
Expand Down
21 changes: 11 additions & 10 deletions src/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ struct TestContext {
kingdom_lord: IKingdomLordDispatcher,
kingdom_lord_test: IKingdomLordTestDispatcher,
kingdom_lord_admin: IKingdomLordAdminDispatcher,
erc20_dispatcher: IERC20Dispatcher,
erc20_address: ContractAddress
// erc20_dispatcher: IERC20Dispatcher,
// erc20_address: ContractAddress
}

// fn NAME() -> felt252 {
Expand Down Expand Up @@ -247,18 +247,19 @@ fn setup_world() -> TestContext {
symbol.serialize(ref calldata);
SUPPLY.serialize(ref calldata);
owner.serialize(ref calldata);
let (erc20_contract_address, _) = starknet::deploy_syscall(
ERC20::TEST_CLASS_HASH.try_into().expect('erc20 test classs'), 0, calldata.span(), false
)
.expect('expect deploy erc20');
// let (erc20_contract_address, _) = starknet::deploy_syscall(
// ERC20::TEST_CLASS_HASH.try_into().expect('erc20 test classs'), 0, calldata.span(), false
// )
// .expect('expect deploy erc20');

let admin_dispatcher = IKingdomLordAdminDispatcher { contract_address: admin_contract_address };
let erc20_dispatcher = IERC20Dispatcher { contract_address: erc20_contract_address };
// let erc20_dispatcher = IERC20Dispatcher { contract_address: erc20_contract_address };
set_caller_address(PLAYER());
set_contract_address(PLAYER());
admin_dispatcher
.set_config(
erc20_contract_address,
// erc20_contract_address,
owner,
200_u256,
owner,
0x608f06197fc3aab41e774567c8e4b7e8fa5dae821240eda6b39f22939315f8c
Expand All @@ -269,7 +270,7 @@ fn setup_world() -> TestContext {
kingdom_lord: IKingdomLordDispatcher { contract_address },
kingdom_lord_test: IKingdomLordTestDispatcher { contract_address },
kingdom_lord_admin: admin_dispatcher,
erc20_dispatcher,
erc20_address: erc20_contract_address
// erc20_dispatcher,
// erc20_address: erc20_contract_address
}
}

0 comments on commit de6f1cd

Please sign in to comment.