Skip to content

Commit

Permalink
refactor: remove Ship.shipSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Mar 11, 2024
1 parent 33e5d8d commit f693e07
Show file tree
Hide file tree
Showing 23 changed files with 54 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/cli/bin/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool Function(Ship ship)? _shipFilterFromArgs(Agent agent, List<String> only) {
if (onlyShips.isNotEmpty) {
logger.info('Only running ships: $onlyShips');
}
return (Ship ship) => onlyShips.contains(ship.shipSymbol);
return (Ship ship) => onlyShips.contains(ship.symbol);
}

Future<void> cliMain(List<String> args) async {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/bin/fleet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void logShip(
final cargoStatus = ship.cargo.capacity == 0
? ''
: '${ship.cargo.units}/${ship.cargo.capacity}';
logger.info('${ship.shipSymbol.hexNumber} '
logger.info('${ship.symbol.hexNumber} '
'${_behaviorOrTypeString(ship, behavior)} $cargoStatus');
if (ship.cargo.isNotEmpty) {
logger.info(
Expand Down Expand Up @@ -111,7 +111,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final systemsCache = SystemsCache.load(fs)!;
final marketPrices = await MarketPriceSnapshot.load(db);
for (final ship in matchingShips) {
final behaviorState = await db.behaviorStateBySymbol(ship.shipSymbol);
final behaviorState = await db.behaviorStateBySymbol(ship.symbol);
logShip(
systemsCache,
marketPrices,
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/bin/fleet_closest_to.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {

for (final ship in ships.ships) {
final travelTime = travelTimeTo(ship, destination);
travelTimes[ship.shipSymbol] = travelTime;
travelTimes[ship.symbol] = travelTime;
}

final sortedShips =
ships.ships.sortedBy<Duration>((s) => travelTimes[s.shipSymbol]!);
ships.ships.sortedBy<Duration>((s) => travelTimes[s.symbol]!);
for (final ship in sortedShips) {
final travelTime = travelTimes[ship.shipSymbol]!;
logger.info('${ship.shipSymbol.hexNumber.padRight(3)} '
final travelTime = travelTimes[ship.symbol]!;
logger.info('${ship.symbol.hexNumber.padRight(3)} '
'${approximateDuration(travelTime).padLeft(3)}');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/fleet_systems.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Map<SystemSymbol, List<Ship>> _shipsBySystem(List<Ship> ships) {
String _describeBehaviors(BehaviorSnapshot behaviors, List<Ship> ships) {
final behaviorCounts = <Behavior?, int>{};
for (final ship in ships) {
final behavior = behaviors[ship.shipSymbol]?.behavior;
final behavior = behaviors[ship.symbol]?.behavior;
behaviorCounts[behavior] = (behaviorCounts[behavior] ?? 0) + 1;
}
return behaviorCounts.entries.map((e) => '${e.key}: ${e.value}').join(', ');
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
if (argResults.rest.isNotEmpty) {
final onlyShips =
argResults.rest.map((s) => int.parse(s, radix: 16)).toSet();
ships =
ships.where((b) => onlyShips.contains(b.shipSymbol.number)).toList();
ships = ships.where((b) => onlyShips.contains(b.symbol.number)).toList();
}
if (ships.isEmpty) {
logger.info('No ships found.');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/slots_charters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final slotCount = <SystemSymbol, int>{};

SystemSymbol systemForShip(Ship ship) {
final state = behaviors[ship.shipSymbol];
final state = behaviors[ship.symbol];
final endSystem = state?.routePlan?.endSymbol.system;
if (endSystem != null) {
return endSystem;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/squads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final cargoStatus = ship.cargo.capacity == 0
? ''
: '${ship.cargo.units}/${ship.cargo.capacity}';
logger.info(' ${ship.shipSymbol.hexNumber.padLeft(2)} $typeName '
logger.info(' ${ship.symbol.hexNumber.padLeft(2)} $typeName '
'${ship.nav.waypointSymbolObject} $cargoStatus');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/system_watchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void logShip(
) {
const indent = ' ';
final waypoint = systemsCache.waypoint(ship.waypointSymbol);
logger.info(ship.shipSymbol.hexNumber);
logger.info(ship.symbol.hexNumber);

final routePlan = behavior?.routePlan;
if (routePlan != null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/buy_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ShipyardTrip? findBestShipyardToBuy(
// void verifyShipMatchesTemplate(Ship ship, ShipType shipType) {
// final fromTemplate = makeShip(
// type: shipType,
// shipSymbol: ship.shipSymbol,
// shipSymbol: ship.symbol,
// factionSymbol: ship.registration.factionSymbol,
// origin: ship.nav.route.origin,
// now: ship.nav.route.arrival,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/miner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Future<JobResult> extractAndLog(
logEvents(ship, response.events);
await db.insertExtraction(
ExtractionRecord(
shipSymbol: ship.shipSymbol,
shipSymbol: ship.symbol,
waypointSymbol: ship.waypointSymbol,
tradeSymbol: yield_.symbol,
quantity: yield_.units,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/mount_from_buy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Future<MountRequest?> mountRequestForShip(
final hqWaypoints = await caches.waypoints.waypointsInSystem(hqSystem);
final shipyard = hqWaypoints.firstWhere((w) => w.hasShipyard);
return MountRequest(
shipSymbol: ship.shipSymbol,
shipSymbol: ship.symbol,
mountSymbol: mountSymbol,
marketSymbol: buyJob.buyLocation,
shipyardSymbol: shipyard.symbol,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/seeder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Future<JobResult> doSeeder(
// the main jumpgate network and look for the next system to seed?

throw JobException(
'Nothing to do, ${ship.shipSymbol} is already off network.',
'Nothing to do, ${ship.symbol} is already off network.',
const Duration(hours: 1),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/siphoner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Future<JobResult> siphonAndLog(
logEvents(ship, response.events);
await db.insertExtraction(
ExtractionRecord(
shipSymbol: ship.shipSymbol,
shipSymbol: ship.symbol,
waypointSymbol: ship.waypointSymbol,
tradeSymbol: yield_.symbol,
quantity: yield_.units,
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/lib/behavior/system_watcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ Map<ShipSymbol, SystemSymbol> assignProbesToSystems(
continue;
}
if (systemsWithEnoughMarkets.contains(systemSymbol)) {
assignedProbes[probe.shipSymbol] = systemSymbol;
assignedProbes[probe.symbol] = systemSymbol;
systemsNeedingProbes.remove(systemSymbol);
}
}
for (final probeSymbol in assignedProbes.keys) {
availableProbes.removeWhere((s) => s.shipSymbol == probeSymbol);
availableProbes.removeWhere((s) => s.symbol == probeSymbol);
}

// clusterId == null means the system has no jump gate connections, so we
Expand Down Expand Up @@ -62,7 +62,7 @@ Map<ShipSymbol, SystemSymbol> assignProbesToSystems(
continue;
}
final systemSymbol = systemsNeeded.removeLast();
assignedProbes[probe.shipSymbol] = systemSymbol;
assignedProbes[probe.symbol] = systemSymbol;
}
final remainingSystems = systemsNeededByClusterId.values.expand((e) => e);
if (remainingSystems.isNotEmpty) {
Expand Down Expand Up @@ -183,7 +183,7 @@ Future<JobResult> _travelToAssignedSystem(
}) async {
final systemSymbol = assertNotNull(
state.systemWatcherJob?.systemSymbol,
'No assigned system for ${ship.shipSymbol}.',
'No assigned system for ${ship.symbol}.',
const Duration(minutes: 10),
);
if (ship.systemSymbol != systemSymbol) {
Expand Down Expand Up @@ -261,7 +261,7 @@ Future<JobResult> doSystemWatcher(
ships,
behaviors,
systemSymbol,
ship.shipSymbol,
ship.symbol,
);

// Walk our nearby waypoints looking for one that needs refresh.
Expand Down
11 changes: 5 additions & 6 deletions packages/cli/lib/behavior/trader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Future<void> _completeContract(

final contactTransaction = ContractTransaction.fulfillment(
contract: contract,
shipSymbol: ship.shipSymbol,
shipSymbol: ship.symbol,
waypointSymbol: ship.waypointSymbol,
timestamp: DateTime.timestamp(),
);
Expand Down Expand Up @@ -368,7 +368,7 @@ Future<Contract?> _deliverContractGoodsIfPossible(
// Record the delivery transaction.
final contactTransaction = ContractTransaction.delivery(
contract: afterDelivery,
shipSymbol: ship.shipSymbol,
shipSymbol: ship.symbol,
waypointSymbol: ship.waypointSymbol,
unitsDelivered: unitsDelivered,
timestamp: DateTime.timestamp(),
Expand Down Expand Up @@ -489,7 +489,7 @@ Future<SupplyConstruction201ResponseData?>

// Record the delivery transaction.
final delivery = ConstructionDelivery(
shipSymbol: ship.shipSymbol,
shipSymbol: ship.symbol,
waypointSymbol: ship.waypointSymbol,
unitsDelivered: unitsDelivered,
tradeSymbol: tradeSymbol,
Expand Down Expand Up @@ -903,9 +903,8 @@ Future<JobResult> _initDeal(
}

final ships = await ShipSnapshot.load(db);
final avoidSystems = centralCommand
.otherTraderSystems(ships, behaviors, ship.shipSymbol)
.toSet();
final avoidSystems =
centralCommand.otherTraderSystems(ships, behaviors, ship.symbol).toSet();

// If we don't have a deal, move to a better location and try again.
final destinationSymbol = assertNotNull(
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/lib/cache/behavior_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class BehaviorTimeouts {
/// Check if the given behavior is disabled for the given ship.
bool isBehaviorDisabledForShip(Ship ship, Behavior behavior) {
bool matches(_ShipTimeout timeout) {
return timeout.shipSymbol == ship.shipSymbol &&
timeout.behavior == behavior;
return timeout.shipSymbol == ship.symbol && timeout.behavior == behavior;
}

final timeouts = _shipTimeouts.where(matches).toList();
Expand All @@ -46,7 +45,7 @@ class BehaviorTimeouts {
String why,
Duration duration,
) async {
final shipSymbol = ship.shipSymbol;
final shipSymbol = ship.symbol;
final currentState = await db.behaviorStateBySymbol(shipSymbol);
final behavior = currentState?.behavior;
if (behavior == null) {
Expand All @@ -66,7 +65,7 @@ class BehaviorTimeouts {
}

final expiration = DateTime.timestamp().add(duration);
_shipTimeouts.add(_ShipTimeout(ship.shipSymbol, behavior, expiration));
_shipTimeouts.add(_ShipTimeout(ship.symbol, behavior, expiration));
}
}

Expand Down Expand Up @@ -108,7 +107,7 @@ class BehaviorSnapshot {
ShipSnapshot shipCache,
) {
final haulerSymbols =
shipCache.ships.where((s) => s.isHauler).map((s) => s.shipSymbol);
shipCache.ships.where((s) => s.isHauler).map((s) => s.symbol);
final idleBehaviors = [
Behavior.idle,
Behavior.charter,
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/lib/cache/ship_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class ShipSnapshot {
/// Loads the ship cache from the provided [db].
static Future<ShipSnapshot> load(Database db) async {
// It's nicer for callers if ships are in sorted order.
final ships = (await db.allShips())
.sorted((a, b) => a.shipSymbol.compareTo(b.shipSymbol));
final ships = (await db.allShips()).sortedBy((a) => a.symbol);
return ShipSnapshot(ships);
}

Expand Down Expand Up @@ -106,11 +105,11 @@ class ShipSnapshot {
}

/// Returns all ship symbols.
List<ShipSymbol> get shipSymbols => ships.map((s) => s.shipSymbol).toList();
List<ShipSymbol> get shipSymbols => ships.map((s) => s.symbol).toList();

/// Returns the ship for the provided ShipSymbol.
Ship operator [](ShipSymbol symbol) =>
ships.firstWhere((s) => s.shipSymbol == symbol);
ships.firstWhere((s) => s.symbol == symbol);
}

/// Returns a map of ship frame type to count in fleet.
Expand Down
20 changes: 10 additions & 10 deletions packages/cli/lib/central_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class CentralCommand {

/// Returns the system symbol we should assign the given [ship] to.
SystemSymbol? assignedSystemForSatellite(Ship ship) =>
_assignedSystemsForSatellites[ship.shipSymbol];
_assignedSystemsForSatellites[ship.symbol];

/// Returns the mining squad for the given [ship].
ExtractionSquad? squadForShip(Ship ship) {
Expand Down Expand Up @@ -172,7 +172,7 @@ class CentralCommand {
Ship ship,
int credits,
) async {
final shipSymbol = ship.shipSymbol;
final shipSymbol = ship.symbol;
BehaviorState toState(Behavior behavior) {
return BehaviorState(shipSymbol, behavior);
}
Expand Down Expand Up @@ -355,7 +355,7 @@ class CentralCommand {

// A hack to avoid spamming the console until we add a deals cache.
if (deals.isNotEmpty) {
logger.info('Found ${deals.length} deals for ${ship.shipSymbol} from '
logger.info('Found ${deals.length} deals for ${ship.symbol} from '
'$startSymbol');
}
for (final deal in deals) {
Expand All @@ -375,7 +375,7 @@ class CentralCommand {
required int maxJumps,
}) async {
final charterSystems =
otherCharterSystems(ships, behaviors, ship.shipSymbol).toSet();
otherCharterSystems(ships, behaviors, ship.symbol).toSet();

// Only probes should ever chart asteroids.
final chartAsteroids =
Expand Down Expand Up @@ -488,11 +488,11 @@ class CentralCommand {
ShipSnapshot ships,
) async {
for (final ship in ships.ships) {
if (_mountRequests.any((m) => m.shipSymbol == ship.shipSymbol)) {
if (_mountRequests.any((m) => m.shipSymbol == ship.symbol)) {
return;
}
// Don't queue a new mount request if we're currently executing one.
final behaviorState = await db.behaviorStateBySymbol(ship.shipSymbol);
final behaviorState = await db.behaviorStateBySymbol(ship.symbol);
if (behaviorState?.behavior == Behavior.mountFromBuy) {
continue;
}
Expand Down Expand Up @@ -657,7 +657,7 @@ class CentralCommand {
/// Takes and clears the next mount buy job for the given [ship].
MountRequest _takeMountRequest(Ship ship) {
final mountRequest = _mountRequests.firstWhere(
(m) => m.shipSymbol == ship.shipSymbol,
(m) => m.shipSymbol == ship.symbol,
orElse: () => throw ArgumentError('No mount request for $ship'),
);
_mountRequests.remove(mountRequest);
Expand Down Expand Up @@ -830,7 +830,7 @@ class CentralCommand {
}
// Does this ship have a mount it needs?
final mountRequest =
_mountRequests.firstWhereOrNull((m) => m.shipSymbol == ship.shipSymbol);
_mountRequests.firstWhereOrNull((m) => m.shipSymbol == ship.symbol);
if (mountRequest == null) {
return false;
}
Expand Down Expand Up @@ -1100,11 +1100,11 @@ Future<List<ExtractionSquad>> assignShipsToSquads(
// TODO(eseidel): Should use a dynamic count.
final minerHaulerSymbols = ships.ships
.where((s) => s.isHauler)
.map((s) => s.shipSymbol)
.map((s) => s.symbol)
.sorted()
.take(config.minerHaulerCount);
bool useAsMinerHauler(Ship ship) {
return config.enableMining && minerHaulerSymbols.contains(ship.shipSymbol);
return config.enableMining && minerHaulerSymbols.contains(ship.symbol);
}

// Go through and assign all ships to squads.
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/lib/logic/advance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Future<DateTime?> advanceShipBehavior(
Ship ship, {
DateTime Function() getNow = defaultGetNow,
}) async {
final state = await createBehaviorIfAbsent(db, ship.shipSymbol, () async {
final state = await createBehaviorIfAbsent(db, ship.symbol, () async {
final credits = caches.agent.agent.credits;
return centralCommand.getJobForShip(
db,
Expand All @@ -87,7 +87,7 @@ Future<DateTime?> advanceShipBehavior(
);
} on JobException catch (e) {
shipErr(ship, '$e');
await db.deleteBehaviorState(ship.shipSymbol);
await db.deleteBehaviorState(ship.symbol);
return null;
}
if (navResult.shouldReturn()) {
Expand All @@ -108,7 +108,7 @@ Future<DateTime?> advanceShipBehavior(
);
if (state.isComplete) {
// If the behavior is complete, clear it.
await db.deleteBehaviorState(ship.shipSymbol);
await db.deleteBehaviorState(ship.symbol);
} else {
// Otherwise update the behavior state.
await db.setBehaviorState(state);
Expand Down
Loading

0 comments on commit f693e07

Please sign in to comment.