Skip to content

Commit

Permalink
feat: prepare next buy-ship job after each purchase
Browse files Browse the repository at this point in the history
Previously we were waiting until the next "top of loop" which could
be minutes away.
  • Loading branch information
eseidel committed Mar 17, 2024
1 parent c3678cb commit fc85b03
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
13 changes: 13 additions & 0 deletions packages/cli/lib/behavior/buy_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ Future<JobResult> _doBuyShipJob(
// Record our success!
// Rate limiting for ship buying is done by CentralCommand.
shipWarn(ship, 'Purchased ${result.ship.symbol} ($shipType)!');

// This is a bit of a hack, but lets us buy ships more often, otherwise
// we won't have a next job once this one is done.
final shipyardListings = await ShipyardListingSnapshot.load(db);
final ships = await ShipSnapshot.load(db);
await centralCommand.updateBuyShipJobIfNeeded(
db,
api,
caches,
shipyardListings,
ships,
);

return JobResult.complete();
}

Expand Down
33 changes: 26 additions & 7 deletions packages/cli/lib/central_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,18 @@ class CentralCommand {
}
}

/// Creates a new buy ship job if needed.
Future<void> updateBuyShipJobIfNeeded(
Database db,
Api api,
Caches caches,
ShipyardListingSnapshot shipyardListings,
ShipSnapshot ships,
) async {
_nextShipBuyJob ??=
await _computeNextShipBuyJob(db, api, caches, shipyardListings, ships);
}

/// Give central planning a chance to advance.
/// Currently only run once every N loops (currently 50).
Future<void> advanceCentralPlanning(
Expand Down Expand Up @@ -665,8 +677,13 @@ class CentralCommand {
miningSquads = [];
}

_nextShipBuyJob ??=
await _computeNextShipBuyJob(db, api, caches, shipyardListings, ships);
await updateBuyShipJobIfNeeded(
db,
api,
caches,
shipyardListings,
ships,
);

// Mounts are currently only used for mining.
if (config.enableMining) {
Expand Down Expand Up @@ -756,12 +773,13 @@ class CentralCommand {
Future<ShipBuyJob?> _unreachableSystemProbe(
Database db,
Api api,
Caches caches,
AgentCache agentCache,
SystemConnectivity systemConnectivity,
ShipyardListingSnapshot shipyardListings,
ShipSnapshot ships,
) async {
// Get our main cluster id.
final hqSystemSymbol = caches.agent.headquartersSystemSymbol;
final hqSystemSymbol = agentCache.headquartersSystemSymbol;
// List all systems with explorers in them.
final systemsWithExplorers = ships.ships
.where((s) => s.fleetRole == FleetRole.explorer)
Expand All @@ -770,8 +788,7 @@ class CentralCommand {
// Any system which is not in our main cluster id.
final unreachableSystems = systemsWithExplorers
.where(
(s) => caches.systemConnectivity
.existsJumpPathBetween(s, hqSystemSymbol),
(s) => systemConnectivity.existsJumpPathBetween(s, hqSystemSymbol),
)
.toSet();
// And does not have a probe in it.
Expand All @@ -787,6 +804,7 @@ class CentralCommand {
}
const shipType = ShipType.PROBE;
final systemSymbol = systemWithoutProbes.first;
// TODO(eseidel): This should be a db query.
final shipyardSymbol = shipyardListings
.listingsInSystem(systemSymbol)
.firstWhereOrNull((s) => s.hasShip(shipType))
Expand Down Expand Up @@ -814,7 +832,8 @@ class CentralCommand {
final unreachableProbeJob = await _unreachableSystemProbe(
db,
api,
caches,
caches.agent,
caches.systemConnectivity,
shipyardListings,
ships,
);
Expand Down

0 comments on commit fc85b03

Please sign in to comment.