Skip to content

Commit 2333d41

Browse files
committed
fix: improve Ship->ShipType lookup
1 parent 88449fe commit 2333d41

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

packages/cli/bin/report_balance_sheet.dart

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ Future<int> computeShipValue(
2424
ShipyardShipCache shipyardShips,
2525
ShipyardPriceSnapshot shipyardPrices,
2626
) async {
27+
ShipType shipTypeForShip(Ship ship) {
28+
final type = guessShipType(shipyardShips, ship);
29+
if (type == null) {
30+
throw StateError('Unknown ship type for frame: ${ship.frame.symbol}');
31+
}
32+
return type;
33+
}
34+
35+
// Ignoring the first two ships, since they come for free.
2736
final purchasedShips = ships.ships.skip(2).toList();
28-
final purchaseShipTypes = purchasedShips
29-
.map((s) => shipyardShips.shipTypeFromFrame(s.frame.symbol)!)
30-
.toList();
37+
final purchaseShipTypes = purchasedShips.map(shipTypeForShip).toList();
3138
final totalShipCost =
3239
purchaseShipTypes.map((s) => shipyardPrices.medianPurchasePrice(s)!).sum;
3340

packages/cli/bin/squads.dart

+1-18
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@ String describeJob(ExtractionJob job) {
1212
return '$action @ $sourceName';
1313
}
1414

15-
ShipType? guessType(ShipyardShipCache shipyardShipCache, Ship ship) {
16-
final frame = ship.frame;
17-
final type = shipyardShipCache.shipTypeFromFrame(frame.symbol);
18-
if (type != null) {
19-
return type;
20-
}
21-
if (frame.symbol == ShipFrameSymbolEnum.DRONE) {
22-
if (ship.hasMiningLaser) {
23-
return ShipType.MINING_DRONE;
24-
}
25-
if (ship.hasSurveyor) {
26-
return ShipType.SURVEYOR;
27-
}
28-
}
29-
return null;
30-
}
31-
3215
Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
3316
final systems = await SystemsCache.loadOrFetch(fs);
3417
final charting = ChartingCache(db);
@@ -48,7 +31,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
4831
final squad = squads[i];
4932
logger.info('Squad $i: ${describeJob(squad.job)}');
5033
for (final ship in squad.ships) {
51-
final type = guessType(shipyardShipCache, ship)!;
34+
final type = guessShipType(shipyardShipCache, ship)!;
5235
final typeName = type.value.substring('SHIP_'.length);
5336
final cargoStatus = ship.cargo.capacity == 0
5437
? ''

packages/cli/lib/ships.dart

+27
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ extension ShipTypeToFrame on ShipyardShipCache {
8989
}
9090
}
9191

92+
/// Attempt to determine ShipType from a Ship. Since Ships can be modified
93+
/// after purchase there is not always a 1:1 mapping from Ship back to ShipType.
94+
ShipType? guessShipType(ShipyardShipCache shipyardShipCache, Ship ship) {
95+
final frame = ship.frame;
96+
final type = shipyardShipCache.shipTypeFromFrame(frame.symbol);
97+
if (type != null) {
98+
return type;
99+
}
100+
if (frame.symbol == ShipFrameSymbolEnum.DRONE) {
101+
if (ship.hasMiningLaser) {
102+
return ShipType.MINING_DRONE;
103+
}
104+
if (ship.hasSiphon) {
105+
return ShipType.SIPHON_DRONE;
106+
}
107+
if (ship.hasSurveyor) {
108+
return ShipType.SURVEYOR;
109+
}
110+
}
111+
if (frame.symbol == ShipFrameSymbolEnum.HEAVY_FREIGHTER) {
112+
if (ship.hasOreRefinery) {
113+
return ShipType.REFINING_FREIGHTER;
114+
}
115+
}
116+
return null;
117+
}
118+
92119
/// Provides Ship data that ShipyardShip does not.
93120
/// Right now that's only Role, but it could be more in the future.
94121
@immutable

packages/types/lib/api.dart

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ extension ShipUtils on Ship {
183183
bool get hasSiphon =>
184184
mounts.any((m) => kSiphonMountSymbols.contains(m.symbol));
185185

186+
/// Returns true if the ship has a refinery mount.
187+
bool get hasOreRefinery =>
188+
modules.any((m) => m.symbol == ShipModuleSymbolEnum.ORE_REFINERY_I);
189+
186190
/// Returns true if the ship is in transit.
187191
bool get isInTransit => nav.status == ShipNavStatus.IN_TRANSIT;
188192

0 commit comments

Comments
 (0)