1
0
Fork 0
spacetac/src/core/Fleet.spec.ts

204 lines
7.2 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
2017-10-26 21:47:13 +00:00
testing("Fleet", test => {
test.case("get average level", check => {
var fleet = new Fleet();
2017-10-26 21:47:13 +00:00
check.equals(fleet.getLevel(), 0);
fleet.addShip(new Ship());
fleet.addShip(new Ship());
fleet.addShip(new Ship());
2017-03-17 00:07:00 +00:00
fleet.ships[0].level.forceLevel(2);
fleet.ships[1].level.forceLevel(4);
fleet.ships[2].level.forceLevel(7);
2017-10-26 21:47:13 +00:00
check.equals(fleet.getLevel(), 4);
});
2017-02-09 22:21:39 +00:00
2017-10-26 21:47:13 +00:00
test.case("adds and removes ships", check => {
2017-07-02 18:21:04 +00:00
let fleet1 = new Fleet();
let fleet2 = new Fleet();
let ship1 = fleet1.addShip();
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, [ship1]);
check.equals(fleet2.ships, []);
2017-07-02 18:21:04 +00:00
let ship2 = new Ship();
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, [ship1]);
check.equals(fleet2.ships, []);
2017-07-02 18:21:04 +00:00
fleet2.addShip(ship2);
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, [ship1]);
check.equals(fleet2.ships, [ship2]);
2017-07-02 18:21:04 +00:00
fleet1.addShip(ship2);
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, [ship1, ship2]);
check.equals(fleet2.ships, []);
2017-07-02 18:21:04 +00:00
fleet1.removeShip(ship1, fleet2);
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, [ship2]);
check.equals(fleet2.ships, [ship1]);
2017-07-02 18:21:04 +00:00
fleet1.removeShip(ship1);
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, [ship2]);
check.equals(fleet2.ships, [ship1]);
2017-07-02 18:21:04 +00:00
fleet1.removeShip(ship2);
2017-10-26 21:47:13 +00:00
check.equals(fleet1.ships, []);
check.equals(fleet2.ships, [ship1]);
2017-07-02 18:21:04 +00:00
});
2017-10-26 21:47:13 +00:00
test.case("changes location, only using jumps to travel between systems", check => {
2017-02-09 22:21:39 +00:00
let fleet = new Fleet();
let universe = new Universe();
let system1 = universe.addStar();
let system2 = universe.addStar();
let jump1 = system1.addLocation(StarLocationType.WARP);
let jump2 = system2.addLocation(StarLocationType.WARP);
2017-02-09 22:21:39 +00:00
jump1.setJumpDestination(jump2);
jump2.setJumpDestination(jump1);
let other1 = system1.addLocation(StarLocationType.PLANET);
universe.updateLocations();
let result = fleet.move(other1);
check.in("cannot move from nowhere", check => {
check.equals(result, false);
check.equals(fleet.location, null);
});
fleet.setLocation(other1);
check.in("force set to other1", check => {
check.equals(fleet.location, other1.id);
});
result = fleet.move(jump2);
check.in("other1=>jump2", check => {
check.equals(result, false);
check.equals(fleet.location, other1.id);
});
result = fleet.move(jump1);
check.in("other1=>jump1", check => {
check.equals(result, true);
check.equals(fleet.location, jump1.id);
});
result = fleet.move(jump2);
check.in("jump1=>jump2", check => {
check.equals(result, true);
check.equals(fleet.location, jump2.id);
});
result = fleet.move(other1);
check.in("jump2=>other1", check => {
check.equals(result, false);
check.equals(fleet.location, jump2.id);
});
result = fleet.move(jump1);
check.in("jump2=>jump1", check => {
check.equals(result, true);
check.equals(fleet.location, jump1.id);
});
});
2017-02-09 22:21:39 +00:00
test.case("registers presence in locations, and keeps track of visited locations", check => {
let fleet = new Fleet();
let universe = new Universe();
let star = universe.addStar();
let loc1 = star.addLocation(StarLocationType.PLANET);
let loc2 = star.addLocation(StarLocationType.PLANET);
let loc3 = star.addLocation(StarLocationType.PLANET);
universe.updateLocations();
function checks(desc: string, fleets1: Fleet[], fleets2: Fleet[], fleets3: Fleet[], visited: RObjectId[]) {
check.in(desc, check => {
check.equals(loc1.fleets, fleets1, "loc1 fleets");
check.equals(loc2.fleets, fleets2, "loc2 fleets");
check.equals(loc3.fleets, fleets3, "loc3 fleets");
check.equals(fleet.visited, visited, "visited");
});
}
checks("initial", [], [], [], []);
fleet.setLocation(loc1);
checks("first move to loc1", [fleet], [], [], [loc1.id]);
fleet.setLocation(loc1);
checks("already in loc1", [fleet], [], [], [loc1.id]);
fleet.setLocation(loc2);
checks("first move to loc2", [], [fleet], [], [loc2.id, loc1.id]);
fleet.setLocation(loc3);
checks("first move to loc3", [], [], [fleet], [loc3.id, loc2.id, loc1.id]);
fleet.setLocation(loc2);
checks("go back to loc2", [], [fleet], [], [loc2.id, loc3.id, loc1.id]);
2017-02-09 22:21:39 +00:00
});
2017-07-02 18:21:04 +00:00
2017-10-26 21:47:13 +00:00
test.case("checks if a fleet is alive", check => {
2017-10-25 22:45:53 +00:00
let battle = new Battle();
let fleet = battle.fleets[0];
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), false);
2017-07-02 18:21:04 +00:00
let ship1 = fleet.addShip();
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), true);
2017-07-02 18:21:04 +00:00
let ship2 = fleet.addShip();
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), true);
2017-07-02 18:21:04 +00:00
ship1.setDead();
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), true);
2017-07-02 18:21:04 +00:00
ship2.setDead();
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), false);
2017-07-02 18:21:04 +00:00
let ship3 = fleet.addShip();
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), true);
2017-07-02 18:21:04 +00:00
let ship4 = fleet.addShip();
ship4.critical = true;
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), true);
2017-07-02 18:21:04 +00:00
ship4.setDead();
2017-10-26 21:47:13 +00:00
check.equals(fleet.isAlive(), false);
2017-07-02 18:21:04 +00:00
});
2017-07-10 22:50:38 +00:00
2017-10-26 21:47:13 +00:00
test.case("adds cargo in first empty slot", check => {
2017-07-10 22:50:38 +00:00
let fleet = new Fleet();
let ship1 = fleet.addShip();
ship1.cargo_space = 1;
let ship2 = fleet.addShip();
ship2.cargo_space = 2;
2017-10-26 21:47:13 +00:00
check.equals(ship1.cargo, []);
check.equals(ship2.cargo, []);
2017-07-10 22:50:38 +00:00
let equipment1 = new Equipment();
let result = fleet.addCargo(equipment1);
2017-10-26 21:47:13 +00:00
check.equals(result, true);
check.equals(ship1.cargo, [equipment1]);
2017-10-26 21:47:13 +00:00
check.equals(ship2.cargo, []);
2017-07-10 22:50:38 +00:00
let equipment2 = new Equipment();
result = fleet.addCargo(equipment2);
2017-10-26 21:47:13 +00:00
check.equals(result, true);
check.equals(ship1.cargo, [equipment1]);
check.equals(ship2.cargo, [equipment2]);
2017-07-10 22:50:38 +00:00
let equipment3 = new Equipment();
result = fleet.addCargo(equipment3);
2017-10-26 21:47:13 +00:00
check.equals(result, true);
check.equals(ship1.cargo, [equipment1]);
check.equals(ship2.cargo, [equipment2, equipment3]);
2017-07-10 22:50:38 +00:00
let equipment4 = new Equipment();
result = fleet.addCargo(equipment4);
2017-10-26 21:47:13 +00:00
check.equals(result, false);
check.equals(ship1.cargo, [equipment1]);
check.equals(ship2.cargo, [equipment2, equipment3]);
2017-07-10 22:50:38 +00:00
});
});
}