Skip to content

Commit

Permalink
added test on send fleet + collect debris
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Oct 25, 2023
1 parent 1a57cd7 commit 02dd1fc
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 51 deletions.
25 changes: 8 additions & 17 deletions src/game/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/////////////////////////////////////////////////////////////////////
Expand Down
196 changes: 162 additions & 34 deletions tests/write_tests/fleet_write.cairo
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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);

Expand Down Expand Up @@ -72,32 +47,185 @@ 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');
assert(dsp.game.get_ships_levels(2).sparrow == 0, 'wrong ships after');
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');
}

Expand Down

0 comments on commit 02dd1fc

Please sign in to comment.