From 02dd1fc28cfbecf17dc11f73a8f2c5db2569eee6 Mon Sep 17 00:00:00 2001 From: ametel01 Date: Wed, 25 Oct 2023 21:08:38 +0800 Subject: [PATCH] added test on send fleet + collect debris --- src/game/main.cairo | 25 ++-- tests/write_tests/fleet_write.cairo | 196 +++++++++++++++++++++++----- 2 files changed, 170 insertions(+), 51 deletions(-) diff --git a/src/game/main.cairo b/src/game/main.cairo index 0e6188c..1ddba7d 100644 --- a/src/game/main.cairo +++ b/src/game/main.cairo @@ -668,24 +668,14 @@ mod NoGame { // Write mission let time_now = get_block_timestamp(); let mut mission: Mission = Default::default(); + mission.time_start = time_now; + mission.destination = self.get_position_slot_occupant(destination); + mission.time_arrival = time_now + travel_time; + mission.fleet = f; if is_debris_collection { - mission = - Mission { - time_start: time_now, - destination: self.get_position_slot_occupant(destination), - time_arrival: time_now + travel_time, - fleet: f, - is_debris: true, - }; + mission.is_debris = true; } else { - mission = - Mission { - time_start: time_now, - destination: self.get_position_slot_occupant(destination), - time_arrival: time_now + travel_time, - fleet: f, - is_debris: false, - }; + mission.is_debris = false; self .hostile_missions .write( @@ -696,7 +686,6 @@ mod NoGame { ) ); } - self.active_missions.write((planet_id, active_missions + 1), mission); self.active_missions_len.write(planet_id, active_missions + 1); // Write new fleet levels @@ -781,6 +770,8 @@ mod NoGame { .scraper_available .write(origin, self.scraper_available.read(origin) + mission.fleet.scraper); self.active_missions.write((origin, mission_id), Zeroable::zero()); + let active_missions = self.active_missions_len.read(origin); + self.active_missions_len.write(origin, active_missions - 1); } ///////////////////////////////////////////////////////////////////// diff --git a/tests/write_tests/fleet_write.cairo b/tests/write_tests/fleet_write.cairo index 1b27aa3..cf18648 100644 --- a/tests/write_tests/fleet_write.cairo +++ b/tests/write_tests/fleet_write.cairo @@ -1,4 +1,4 @@ -use starknet::info::get_block_timestamp; +use starknet::info::{get_block_timestamp, get_contract_address}; use snforge_std::{declare, ContractClassTrait, io::PrintTrait, start_prank, start_warp}; @@ -12,32 +12,7 @@ use tests::utils::{ }; #[test] -fn test_send_fleet_success() { // TODO - assert(0 == 0, 'todo'); -} - -#[test] -fn test_send_fleet_fails_no_planet_at_destination() { // TODO - assert(0 == 0, 'todo'); -} - -#[test] -fn test_send_fleet_fails_origin_is_destination() { // TODO - assert(0 == 0, 'todo'); -} - -#[test] -fn test_send_fleet_fails_noob_protection() { // TODO - assert(0 == 0, 'todo'); -} - -#[test] -fn test_send_fleet_fails_not_enough_fleet_slots() { // TODO - assert(0 == 0, 'todo'); -} - -#[test] -fn test_send_fleet() { +fn test_send_fleet_success() { let dsp = set_up(); init_game(dsp); @@ -72,10 +47,7 @@ fn test_send_fleet() { assert(dsp.game.get_ships_levels(1).frigate == 1, 'wrong ships before'); assert(dsp.game.get_ships_levels(1).armade == 1, 'wrong ships before'); - let initial = testing::get_available_gas(); - gas::withdraw_gas().unwrap(); dsp.game.send_fleet(fleet, p2_position, false); - (initial - testing::get_available_gas()).print(); assert(dsp.game.get_ships_levels(2).carrier == 0, 'wrong ships after'); assert(dsp.game.get_ships_levels(2).scraper == 0, 'wrong ships after'); @@ -83,21 +55,177 @@ fn test_send_fleet() { assert(dsp.game.get_ships_levels(2).frigate == 0, 'wrong ships after'); assert(dsp.game.get_ships_levels(2).armade == 0, 'wrong ships after'); - dsp.game.get_mission_details(1, 1).print(); + let mission = dsp.game.get_mission_details(1, 1); + assert(mission.fleet.carrier == 1, 'wrong carrier'); + assert(mission.fleet.scraper == 1, 'wrong scraper'); + assert(mission.fleet.sparrow == 1, 'wrong sparrow'); + assert(mission.fleet.frigate == 1, 'wrong frigate'); + assert(mission.fleet.armade == 1, 'wrong armade'); +} + +#[test] +#[should_panic(expected: ('no planet at destination',))] +fn test_send_fleet_fails_no_planet_at_destination() { + let dsp = set_up(); + init_game(dsp); + + start_prank(dsp.game.contract_address, ACCOUNT1()); + dsp.game.generate_planet(); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + + dsp.game.carrier_build(1); + + let p2_position = dsp.game.get_planet_position(2); + + let mut fleet: Fleet = Default::default(); + fleet.carrier = 1; + + dsp.game.send_fleet(fleet, p2_position, false); +} + +#[test] +#[should_panic(expected: ('cannot send to own planet',))] +fn test_send_fleet_fails_origin_is_destination() { + let dsp = set_up(); + init_game(dsp); + + start_prank(dsp.game.contract_address, ACCOUNT1()); + dsp.game.generate_planet(); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + + dsp.game.carrier_build(1); + + let p1_position = dsp.game.get_planet_position(1); + + let mut fleet: Fleet = Default::default(); + fleet.carrier = 1; + + dsp.game.send_fleet(fleet, p1_position, false); +} + +#[test] +#[should_panic(expected: ('noob protection active',))] +fn test_send_fleet_fails_noob_protection() { + let dsp = set_up(); + init_game(dsp); + + start_prank(dsp.game.contract_address, ACCOUNT2()); + dsp.game.generate_planet(); + start_prank(dsp.game.contract_address, ACCOUNT1()); + dsp.game.generate_planet(); + build_basic_mines(dsp.game); + start_prank(dsp.game.contract_address, ACCOUNT2()); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + + dsp.game.carrier_build(10); + + let p2_position = dsp.game.get_planet_position(2); + + let mut fleet: Fleet = Default::default(); + fleet.carrier = 5; + + dsp.game.send_fleet(fleet, p2_position, false); +} + +#[test] +#[should_panic(expected: ('max active missions',))] +fn test_send_fleet_fails_not_enough_fleet_slots() { + let dsp = set_up(); + init_game(dsp); + + start_prank(dsp.game.contract_address, ACCOUNT2()); + dsp.game.generate_planet(); + start_prank(dsp.game.contract_address, ACCOUNT1()); + dsp.game.generate_planet(); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + start_prank(dsp.game.contract_address, ACCOUNT2()); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + + dsp.game.carrier_build(10); + + let p2_position = dsp.game.get_planet_position(2); + + let mut fleet: Fleet = Default::default(); + fleet.carrier = 5; + + dsp.game.send_fleet(fleet, p2_position, false); + dsp.game.send_fleet(fleet, p2_position, false); +} + +#[test] +fn test_send_fleet_debris_success() { // TODO + let dsp = set_up(); + init_game(dsp); + + start_prank(dsp.game.contract_address, ACCOUNT1()); + dsp.game.generate_planet(); + start_prank(dsp.game.contract_address, ACCOUNT2()); + dsp.game.generate_planet(); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + dsp.game.plasma_projector_build(1); + start_prank(dsp.game.contract_address, ACCOUNT1()); + build_basic_mines(dsp.game); + advance_game_state(dsp.game); + dsp.game.digital_systems_upgrade(); + dsp.game.carrier_build(10); + dsp.game.scraper_build(1); + let p1_position = dsp.game.get_planet_position(2); + + let mut fleet: Fleet = Default::default(); + fleet.carrier = 5; + + dsp.game.send_fleet(fleet, p1_position, false); + let mission = dsp.game.get_mission_details(1, 1); + warp_multiple( + dsp.game.contract_address, + get_contract_address(), + get_block_timestamp() + mission.time_arrival + 1 + ); + dsp.game.attack_planet(1); + + let mut fleet: Fleet = Default::default(); + fleet.scraper = 1; + let debris = dsp.game.get_debris_field(2); + dsp.game.send_fleet(fleet, p1_position, true); + let mission = dsp.game.get_mission_details(1, 2); + warp_multiple( + dsp.game.contract_address, + get_contract_address(), + get_block_timestamp() + mission.time_arrival + 1 + ); + let resources_before = dsp.game.get_spendable_resources(1); + dsp.game.collect_debris(2); + assert(dsp.game.get_ships_levels(1).scraper == 1, 'wrong scraper back'); + let resources_after = dsp.game.get_spendable_resources(1); + assert(resources_after.steel == resources_before.steel + debris.steel, 'wrong steel collected'); + assert( + resources_after.quartz == resources_before.quartz + debris.quartz, 'wrong quartz collected' + ); + let debris_after_collection = dsp.game.get_debris_field(2); + assert( + debris_after_collection.steel.is_zero() && debris_after_collection.quartz.is_zero(), + 'wrong debris after' + ); } #[test] -fn test_dock_fleet() { // TODO +fn test_send_fleet_debris_fails_empty_debris_field() { // TODO assert(0 == 0, 'todo'); } #[test] -fn test_dock_fleet_fails_no_fleet_to_dock() { // TODO +fn test_send_fleet_debris_fails_no_scrapers() { // TODO assert(0 == 0, 'todo'); } #[test] -fn test_dock_fleet_fails_destination_not_reached() { // TODO +fn test_send_fleet_debris_fails_not_only_scrapers() { // TODO assert(0 == 0, 'todo'); }