Skip to content

Commit

Permalink
feat: Make it possible to construct ships by type
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Oct 14, 2023
1 parent 475d497 commit b420921
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 3 deletions.
58 changes: 58 additions & 0 deletions packages/cli/bin/ships_validate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'dart:convert';

import 'package:cli/cache/caches.dart';
import 'package:cli/cli.dart';
import 'package:cli/third_party/compare.dart';

bool _jsonCompare(Ship actual, Ship expected) {
final diff = findDifferenceBetweenStrings(
jsonEncode(actual.toJson()),
jsonEncode(expected.toJson()),
);
if (diff != null) {
logger.info('Ship differs from expected: ${diff.which}');
return false;
}
return true;
}

/// Log a warning if the purchased ship does not match the expected template.
// void verifyShipMatchesTemplate(Ship ship, ShipType shipType) {
// final fromTemplate = makeShipForComparison(
// type: shipType,
// shipSymbol: ship.shipSymbol,
// factionSymbol: ship.registration.factionSymbol,
// origin: ship.nav.route.origin,
// now: ship.nav.route.arrival,
// );
// }

Future<void> command(FileSystem fs, ArgResults argResults) async {
final shipCache = ShipCache.loadCached(fs)!;
final ships = shipCache.ships;
for (final ship in ships) {
final frameSymbol = ship.frame.symbol;
final shipType = shipTypeFromFrame(frameSymbol);
if (shipType == null) {
logger.info('Unknown ship type for $frameSymbol');
continue;
}
final exampleShip = makeShipForComparison(
type: shipType,
shipSymbol: ship.shipSymbol,
factionSymbol: FactionSymbols.fromJson(ship.registration.factionSymbol)!,
nav: ship.nav,
fuel: ship.fuel,
cargo: ship.cargo,
);
if (exampleShip == null) {
logger.info('Failed to make example ship for $shipType');
continue;
}
_jsonCompare(ship, exampleShip);
}
}

void main(List<String> args) async {
await runOffline(args, command);
}
12 changes: 12 additions & 0 deletions packages/cli/lib/behavior/buy_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ ShipyardTrip? findBestShipyardToBuy(
return best;
}

/// Log a warning if the purchased ship does not match the expected template.
// void verifyShipMatchesTemplate(Ship ship, ShipType shipType) {
// final fromTemplate = makeShip(
// type: shipType,
// shipSymbol: ship.shipSymbol,
// factionSymbol: ship.registration.factionSymbol,
// origin: ship.nav.route.origin,
// now: ship.nav.route.arrival,
// );
// }

/// Apply the buy ship behavior.
Future<DateTime?> advanceBuyShip(
Api api,
Expand Down Expand Up @@ -129,6 +140,7 @@ Future<DateTime?> advanceBuyShip(
shipyard.waypointSymbol,
shipType,
);
// verifyShipMatchesTemplate(ship, shipType);
} on ApiException catch (e) {
// ApiException 400: {"error":{"message":"Failed to purchase ship.
// Agent has insufficient funds.","code":4216,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/cache/agent_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool _agentsMatch(Agent actual, Agent expected) {
jsonEncode(expected.toJson()),
);
if (diff != null) {
logger.info('Agent differs from expected: $diff');
logger.info('Agent differs from expected: ${diff.which}');
return false;
}
return true;
Expand Down
Loading

0 comments on commit b420921

Please sign in to comment.