Skip to content

Commit

Permalink
refactor: Make deliver's multi-job behavior generic
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Aug 20, 2023
1 parent f384253 commit defed08
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 60 deletions.
1 change: 1 addition & 0 deletions packages/cli/lib/behavior/buy_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ShipBuyJob {
final WaypointSymbol shipyardSymbol;
}

/// CostedTrip for ShipyardPrice.
typedef ShipyardTrip = CostedTrip<ShipyardPrice>;

List<ShipyardTrip> _shipyardsSellingByDistance(
Expand Down
134 changes: 75 additions & 59 deletions packages/cli/lib/behavior/deliver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -414,73 +415,88 @@ Future<JobResult> doInitJob(
return JobResult.complete();
}

/// Advance the behavior of the given ship.
Future<DateTime?> advanceDeliver(
Api api,
Database db,
CentralCommand centralCommand,
Caches caches,
BehaviorState state,
Ship ship, {
DateTime Function() getNow = defaultGetNow,
}) async {
final jobFunctions = <Future<JobResult> 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<JobResult> Function(
BehaviorState,
Api,
Database,
CentralCommand,
Caches,
Ship, {
DateTime Function() getNow,
})> jobFunctions;

/// Run the multi-job.
Future<DateTime?> 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.
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/lib/behavior/miner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateTime?> advanceMiner(
Api api,
Expand Down

0 comments on commit defed08

Please sign in to comment.