Skip to content

Commit

Permalink
Troop attack tests (#2704)
Browse files Browse the repository at this point in the history
* contracts: guard_add entrypoint tests

* contracts: add guard_delete test

* minor variable name update

* minor variable name update

* contracts: update troop battle impl

* contracts: update troop battle impl

* contracts: allow explorer to guard troop swaps

* contracts: allow explorer to guard troop swaps

* contracts: test explorer to guard troop swaps

* contracts: test explorer to guard troop swaps

* contracts: test explorer to guard troop swaps

* contracts: improve and test troop movement

* contracts: improve and test troop movement

* misc

* misc

* update ci

* update ci

* update ci

* update ci

* contracts: update troop attack

* contracts: add explorer vs explorer tests

* update ci

* contracts: add explorer vs guards tests

* contracts: add guards vs explorers tests
  • Loading branch information
credence0x authored Feb 24, 2025
1 parent 22a355f commit 5518214
Show file tree
Hide file tree
Showing 7 changed files with 2,636 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
models,
test_troop_management,
test_troop_movement,
test_troop_battle,
]
fail-fast: false
steps:
Expand Down
5 changes: 5 additions & 0 deletions contracts/game/src/models/owner.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub impl OwnerAddressImpl of OwnerAddressTrait {
fn assert_caller_owner(self: ContractAddress) {
assert(self == starknet::get_caller_address(), ErrorMessages::NOT_OWNER);
}
fn assert_caller_not_owner(self: ContractAddress) {
assert!(self != Zero::zero(), "owner is zero");
assert(self != starknet::get_caller_address(), 'caller is owner');
}

fn assert_non_zero(self: ContractAddress) {
assert!(self.is_non_zero(), "owner is zero");
}
Expand Down
7 changes: 6 additions & 1 deletion contracts/game/src/models/structure.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub impl StructureOwnerStoreImpl of StructureOwnerStoreTrait {
return owner;
}

fn store(ref owner: ContractAddress, ref world: WorldStorage, structure_id: ID) {
fn store(owner: ContractAddress, ref world: WorldStorage, structure_id: ID) {
world.write_member(Model::<Structure>::ptr_from_keys(structure_id), selector!("owner"), owner);
}
}
Expand Down Expand Up @@ -72,6 +72,11 @@ pub impl StructureBaseImpl of StructureBaseTrait {
self.category != StructureCategory::None.into()
}

fn is_not_cloaked(self: StructureBase, battle_config: BattleConfig, tick_config: TickConfig) -> bool {
let (is_cloaked, _) = self.is_cloaked(battle_config, tick_config);
return !is_cloaked;
}

fn is_cloaked(self: StructureBase, battle_config: BattleConfig, tick_config: TickConfig) -> (bool, ByteArray) {
// Fragment mines have no immunity
if self.category == StructureCategory::FragmentMine.into() {
Expand Down
29 changes: 24 additions & 5 deletions contracts/game/src/models/troop.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,22 @@ pub enum GuardSlot {

#[generate_trait]
pub impl GuardImpl of GuardTrait {
fn next_attack_slot(ref self: GuardTroops, max_guards: felt252) -> Option<GuardSlot> {
let relevant_slots = match max_guards {
// todo: test
fn assert_functional_slot(ref self: GuardTroops, slot: GuardSlot, max_guards: felt252) {
let functional_slots = self.functional_slots(max_guards);
let mut is_functional_slot: bool = false;
for functional_slot in functional_slots {
if functional_slot == slot {
is_functional_slot = true;
break;
}
};
assert!(is_functional_slot, "slot can't be selected");
}

// todo: test
fn functional_slots(ref self: GuardTroops, max_guards: felt252) -> Array<GuardSlot> {
match max_guards {
0 => panic!("max guards is 0"),
1 => { array![GuardSlot::Delta] },
2 => { array![GuardSlot::Delta, GuardSlot::Charlie] },
Expand All @@ -91,16 +105,21 @@ pub impl GuardImpl of GuardTrait {
panic!("max guards is greater than 4");
array![]
},
};
}
}

// todo: test: critical
fn next_attack_slot(ref self: GuardTroops, max_guards: felt252) -> Option<GuardSlot> {
let functional_slots = self.functional_slots(max_guards);

// Iterate through relevant slots only
let mut i: usize = 0;
loop {
if i == relevant_slots.len() {
if i == functional_slots.len() {
break Option::None;
}

let slot = *relevant_slots.at(i);
let slot = *functional_slots.at(i);
let has_troops = match slot {
GuardSlot::Delta => self.delta.count.is_non_zero(),
GuardSlot::Charlie => self.charlie.count.is_non_zero(),
Expand Down
1 change: 1 addition & 0 deletions contracts/game/src/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod dev {
pub mod combat {
#[cfg(test)]
mod tests {
mod test_troop_battle;
mod test_troop_management;
mod test_troop_movement;
}
Expand Down
Loading

0 comments on commit 5518214

Please sign in to comment.