diff --git a/packages/cli/lib/behavior/buy_ship.dart b/packages/cli/lib/behavior/buy_ship.dart index e15d3604..aa6c5099 100644 --- a/packages/cli/lib/behavior/buy_ship.dart +++ b/packages/cli/lib/behavior/buy_ship.dart @@ -23,6 +23,7 @@ class ShipBuyJob { final WaypointSymbol shipyardSymbol; } +/// CostedTrip for ShipyardPrice. typedef ShipyardTrip = CostedTrip; List _shipyardsSellingByDistance( diff --git a/packages/cli/lib/behavior/deliver.dart b/packages/cli/lib/behavior/deliver.dart index 80e9cf65..192d8ddc 100644 --- a/packages/cli/lib/behavior/deliver.dart +++ b/packages/cli/lib/behavior/deliver.dart @@ -9,6 +9,7 @@ import 'package:cli/net/actions.dart'; import 'package:cli/printing.dart'; import 'package:cli/trading.dart'; import 'package:db/db.dart'; +import 'package:meta/meta.dart'; import 'package:more/collection.dart'; import 'package:types/types.dart'; @@ -414,73 +415,88 @@ Future doInitJob( return JobResult.complete(); } -/// Advance the behavior of the given ship. -Future advanceDeliver( - Api api, - Database db, - CentralCommand centralCommand, - Caches caches, - BehaviorState state, - Ship ship, { - DateTime Function() getNow = defaultGetNow, -}) async { - final jobFunctions = Function( - BehaviorState, - Api, - Database, - CentralCommand, - Caches, - Ship, { - DateTime Function() getNow, - })>[ - doInitJob, - doBuyJob, - doDeliverJob, - ]; - - for (var i = 0; i < 10; i++) { - shipInfo(ship, 'DELIVER ${state.jobIndex}'); - if (state.jobIndex < 0 || state.jobIndex >= jobFunctions.length) { - centralCommand.disableBehaviorForShip( +/// Creates a behavior from jobs. +@immutable +class MultiJob { + /// Create a new multi-job. + const MultiJob(this.name, this.jobFunctions); + + /// The name of this multi-job. + final String name; + + /// The job functions to run. + final List< + Future Function( + BehaviorState, + Api, + Database, + CentralCommand, + Caches, + Ship, { + DateTime Function() getNow, + })> jobFunctions; + + /// Run the multi-job. + Future run( + Api api, + Database db, + CentralCommand centralCommand, + Caches caches, + BehaviorState state, + Ship ship, { + DateTime Function() getNow = defaultGetNow, + }) async { + for (var i = 0; i < 10; i++) { + shipInfo(ship, '$name ${state.jobIndex}'); + if (state.jobIndex < 0 || state.jobIndex >= jobFunctions.length) { + centralCommand.disableBehaviorForShip( + ship, + 'No behavior state.', + const Duration(hours: 1), + ); + return null; + } + + final jobFunction = jobFunctions[state.jobIndex]; + final result = await jobFunction( + state, + api, + db, + centralCommand, + caches, ship, - 'No behavior state.', - const Duration(hours: 1), ); - return null; + shipInfo(ship, '$name ${state.jobIndex} $result'); + if (result.isComplete) { + state.jobIndex++; + if (state.jobIndex < jobFunctions.length) { + centralCommand.setBehavior(ship.shipSymbol, state); + } else { + centralCommand.completeBehavior(ship.shipSymbol); + shipInfo(ship, '$name complete!'); + return null; + } + } + if (result.shouldReturn) { + return result.waitTime; + } } - - final jobFunction = jobFunctions[state.jobIndex]; - final result = await jobFunction( - state, - api, - db, - centralCommand, - caches, + centralCommand.disableBehaviorForAll( ship, + 'Too many $name job iterations', + const Duration(hours: 1), ); - shipInfo(ship, 'DELIVER ${state.jobIndex} $result'); - if (result.isComplete) { - state.jobIndex++; - if (state.jobIndex < jobFunctions.length) { - centralCommand.setBehavior(ship.shipSymbol, state); - } else { - centralCommand.completeBehavior(ship.shipSymbol); - shipInfo(ship, 'Delivery complete!'); - return null; - } - } - if (result.shouldReturn) { - return result.waitTime; - } + return null; } - centralCommand.disableBehaviorForAll( - ship, - 'Too many deliver job iterations', - const Duration(hours: 1), - ); - return null; } +/// Advance the behavior of the given ship. +final advanceDeliver = const MultiJob('Deliver', [ + doInitJob, + doBuyJob, + doDeliverJob, +]).run; + // This seems related to using haulers for delivery of trade goods. // They get loaded by miners. // Then their job is how to figure out where to sell it. diff --git a/packages/cli/lib/behavior/miner.dart b/packages/cli/lib/behavior/miner.dart index e714b596..4919f6d8 100644 --- a/packages/cli/lib/behavior/miner.dart +++ b/packages/cli/lib/behavior/miner.dart @@ -197,10 +197,18 @@ class MineJob { /// The mine to extract from. final WaypointSymbol mine; - /// The market to sell to. + /// The market to value goods against. final WaypointSymbol market; } +// Miner stages +// - Empty cargo if needed +// - Navigate to mine +// - Survey if needed +// - Mine +// - Navigate to market +// - Sell + /// Apply the miner behavior to the ship. Future advanceMiner( Api api,