Skip to content

Commit

Permalink
fix: miners getting stuck getting rid of MOUNT_SURVEYOR_II
Browse files Browse the repository at this point in the history
Fixed to be able to jettison it.

Also noticed that sellAllCargo was not updating the shipCache, so fixed
that.
  • Loading branch information
eseidel committed Sep 30, 2023
1 parent e9408ae commit b8b522f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 11 deletions.
7 changes: 7 additions & 0 deletions packages/cli/lib/behavior/jobs/mount_job.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Future<JobResult> doMountJob(
}
}

final needed = mountsToAddToShip(ship, template);
jobAssert(
needed.contains(mountJob.mountSymbol),
'Ship does not need ${mountJob.mountSymbol}?',
const Duration(minutes: 10),
);

// 🛸#6 🔧 MOUNT_MINING_LASER_II on ESEIDEL-6 for 3,600c -> 🏦 89,172c
// Mount the new mount.
await installMountAndLog(
Expand Down
26 changes: 16 additions & 10 deletions packages/cli/lib/behavior/miner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ Future<JobResult> emptyCargoIfNeeded(
caches.marketPrices,
caches.agent,
currentMarket,
caches.ships,
ship,
AccountingType.goods,
);
Expand All @@ -485,17 +486,21 @@ Future<JobResult> emptyCargoIfNeeded(
}

final largestCargo = ship.largestCargo();
final nearestMarket = assertNotNull(
await nearbyMarketWhichTrades(
caches.systems,
caches.waypoints,
caches.markets,
currentWaypoint.waypointSymbol,
largestCargo!.tradeSymbol,
),
'No nearby market which trades ${largestCargo.symbol}.',
const Duration(hours: 1),
final nearestMarket = await nearbyMarketWhichTrades(
caches.systems,
caches.waypoints,
caches.markets,
currentWaypoint.waypointSymbol,
largestCargo!.tradeSymbol,
);
if (nearestMarket == null) {
shipErr(
ship,
'No nearby market to sell ${largestCargo.symbol}, jetisoning cargo!',
);
await jettisonAllCargoAndLog(api, caches.ships, ship);
return JobResult.complete();
}
final waitTime = await beingNewRouteAndLog(
api,
ship,
Expand Down Expand Up @@ -571,6 +576,7 @@ Future<JobResult> sellCargoIfNeeded(
caches.marketPrices,
caches.agent,
currentMarket,
caches.ships,
ship,
// We don't have a good way to know what type of cargo this is.
// Assuming it's goods (rather than captial) is probably fine.
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/lib/behavior/trader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Future<DateTime?> _handleArbitrageDealAtDestination(
caches.marketPrices,
caches.agent,
currentMarket,
caches.ships,
ship,
AccountingType.goods,
);
Expand Down Expand Up @@ -606,6 +607,7 @@ Future<JobResult> handleUnwantedCargoIfNeeded(
caches.marketPrices,
caches.agent,
currentMarket,
caches.ships,
ship,
// We don't have a good way to know what type of cargo this is.
// Assuming it's goods (rather than captial) is probably fine.
Expand Down
28 changes: 27 additions & 1 deletion packages/cli/lib/net/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Stream<SellCargo201ResponseData> sellAllCargo(
Api api,
AgentCache agentCache,
Market market,
ShipCache shipCache,
Ship ship, {
bool Function(TradeSymbol tradeSymbol)? where,
}) async* {
Expand Down Expand Up @@ -82,6 +83,7 @@ Stream<SellCargo201ResponseData> sellAllCargo(
api,
agentCache,
ship,
shipCache,
item.tradeSymbol,
toSell,
);
Expand All @@ -99,6 +101,7 @@ Future<List<Transaction>> sellAllCargoAndLog(
MarketPrices marketPrices,
AgentCache agentCache,
Market market,
ShipCache shipCache,
Ship ship,
AccountingType accounting, {
bool Function(TradeSymbol tradeSymbol)? where,
Expand All @@ -110,7 +113,7 @@ Future<List<Transaction>> sellAllCargoAndLog(

final transactions = <Transaction>[];
await for (final response
in sellAllCargo(api, agentCache, market, ship, where: where)) {
in sellAllCargo(api, agentCache, market, shipCache, ship, where: where)) {
final marketTransaction = response.transaction;
final agent = response.agent;
logMarketTransaction(ship, marketPrices, agent, marketTransaction);
Expand All @@ -125,6 +128,29 @@ Future<List<Transaction>> sellAllCargoAndLog(
return transactions;
}

/// Jettison all cargo.
Future<void> jettisonAllCargoAndLog(
Api api,
ShipCache shipCache,
Ship ship,
) async {
if (ship.cargo.inventory.isEmpty) {
shipInfo(ship, 'No cargo to jettison');
return;
}

for (final item in ship.cargo.inventory) {
shipWarn(ship, 'Jettisoning ${item.units} ${item.symbol}');
final response = await api.fleet.jettison(
ship.symbol,
jettisonRequest:
JettisonRequest(symbol: item.tradeSymbol, units: item.units),
);
ship.cargo = response!.data.cargo;
shipCache.updateShip(ship);
}
}

/// Buy [amountToBuy] units of [tradeSymbol] and log the transaction.
Future<Transaction?> purchaseCargoAndLog(
Api api,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/lib/net/direct.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Future<SellCargo201ResponseData> sellCargo(
Api api,
AgentCache agentCache,
Ship ship,
ShipCache shipCache,
TradeSymbol tradeSymbol,
int units,
) async {
Expand All @@ -135,6 +136,7 @@ Future<SellCargo201ResponseData> sellCargo(
final response =
await api.fleet.sellCargo(ship.symbol, sellCargoRequest: request);
ship.cargo = response!.data.cargo;
shipCache.updateShip(ship);
agentCache.agent = response.data.agent;
return response.data;
}
Expand Down
31 changes: 31 additions & 0 deletions packages/types/test/mount_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@ void main() {
);
});

test('mountsToRemoveFromShip', () {
final ship = _MockShip();
final mountSymbols = [
ShipMountSymbolEnum.MINING_LASER_II,
ShipMountSymbolEnum.SURVEYOR_I,
ShipMountSymbolEnum.MINING_LASER_II,
];
ShipMount mountForSymbol(ShipMountSymbolEnum symbol) {
return ShipMount(
symbol: symbol,
name: '',
description: '',
requirements: ShipRequirements(),
);
}

when(() => ship.mounts)
.thenReturn(mountSymbols.map(mountForSymbol).toList());
final template = ShipTemplate(
frameSymbol: ShipFrameSymbolEnum.MINER,
mounts: MountSymbolSet.from([
ShipMountSymbolEnum.MINING_LASER_II,
ShipMountSymbolEnum.MINING_LASER_II,
ShipMountSymbolEnum.MINING_LASER_I,
]),
);
final toRemove = mountsToRemoveFromShip(ship, template);
expect(toRemove, isNotEmpty);
expect(toRemove, MountSymbolSet.from([ShipMountSymbolEnum.SURVEYOR_I]));
});

test('ShipTemplate.matches', () {
final template = ShipTemplate(
frameSymbol: ShipFrameSymbolEnum.FIGHTER,
Expand Down

0 comments on commit b8b522f

Please sign in to comment.