Skip to content

Commit

Permalink
refactor: move ShipTemplate and ship mount sets to types
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Aug 20, 2023
1 parent 295bec0 commit 913cbbf
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 50 deletions.
34 changes: 9 additions & 25 deletions packages/cli/lib/behavior/central_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:cli/trading.dart';
import 'package:collection/collection.dart';
import 'package:db/db.dart';
import 'package:meta/meta.dart';
import 'package:more/collection.dart';
import 'package:types/types.dart';

// Central command sets behavior for all ships.
Expand Down Expand Up @@ -65,21 +64,6 @@ class _ShipTimeout {
final DateTime timeout;
}

/// Mounts template for a ship.
class ShipTemplate {
/// Create a new ship template.
const ShipTemplate({
required this.frameSymbol,
required this.mounts,
});

/// Frame type that this template is for.
final ShipFrameSymbolEnum frameSymbol;

/// Mounts that this template has.
final Multiset<ShipMountSymbolEnum> mounts;
}

/// Where are we in the phases of the reset.
enum GamePhase with EnumIndexOrdering<GamePhase> {
/// Early, we have little money, lots of request space.
Expand Down Expand Up @@ -199,7 +183,7 @@ class CentralCommand {
ShipTemplate? templateForShip(Ship ship) {
final genericMiner = ShipTemplate(
frameSymbol: ShipFrameSymbolEnum.MINER,
mounts: Multiset.from([
mounts: MountSymbolSet.from([
ShipMountSymbolEnum.MINING_LASER_II,
ShipMountSymbolEnum.MINING_LASER_II,
ShipMountSymbolEnum.SURVEYOR_I,
Expand All @@ -209,14 +193,14 @@ class CentralCommand {
// According to SAF: Surveyor = 2x mk2s, miner = 2x mk2 + 1x mk1
final surveyOnly = ShipTemplate(
frameSymbol: ShipFrameSymbolEnum.MINER,
mounts: Multiset.from([
mounts: MountSymbolSet.from([
ShipMountSymbolEnum.SURVEYOR_II,
ShipMountSymbolEnum.SURVEYOR_II,
]),
);
// final mineOnly = ShipTemplate(
// frameSymbol: ShipFrameSymbolEnum.MINER,
// mounts: Multiset.from([
// mounts: MountSymbolSet.from([
// ShipMountSymbolEnum.MINING_LASER_II,
// ShipMountSymbolEnum.MINING_LASER_II,
// ShipMountSymbolEnum.MINING_LASER_I,
Expand All @@ -240,8 +224,8 @@ class CentralCommand {
}

/// Add up all mounts needed for current ships based on current templating.
Multiset<ShipMountSymbolEnum> mountsNeededForAllShips() {
final totalNeeded = Multiset<ShipMountSymbolEnum>();
MountSymbolSet mountsNeededForAllShips() {
final totalNeeded = MountSymbolSet();
for (final ship in _shipCache.ships) {
final template = templateForShip(ship);
if (template == null) {
Expand Down Expand Up @@ -427,8 +411,8 @@ class CentralCommand {
}

/// Returns the counts of mounts already claimed.
Multiset<ShipMountSymbolEnum> claimedMounts() {
final claimed = Multiset<ShipMountSymbolEnum>();
MountSymbolSet claimedMounts() {
final claimed = MountSymbolSet();
for (final state in _behaviorCache.states) {
final behavior = state.behavior;
if (behavior != Behavior.changeMounts) {
Expand All @@ -444,13 +428,13 @@ class CentralCommand {
}

/// Returns the number of mounts available at the waypoint.
Multiset<ShipMountSymbolEnum> unclaimedMountsAt(WaypointSymbol waypoint) {
MountSymbolSet unclaimedMountsAt(WaypointSymbol waypoint) {
// Get all the ships at that symbol
final ships = _shipCache.ships
.where((s) => s.waypointSymbol == waypoint && !s.isInTransit);

// That have behavior delivery.
final available = Multiset<ShipMountSymbolEnum>();
final available = MountSymbolSet();
for (final ship in ships) {
final state = _behaviorCache.getBehavior(ship.shipSymbol);
if (state == null || state.behavior != Behavior.deliver) {
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/lib/behavior/change_mounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import 'package:cli/nav/navigation.dart';
import 'package:cli/net/actions.dart';
import 'package:collection/collection.dart';
import 'package:db/db.dart';
import 'package:more/collection.dart';
import 'package:types/types.dart';

ShipMountSymbolEnum? _pickMountFromAvailable(
Multiset<ShipMountSymbolEnum> available,
Multiset<ShipMountSymbolEnum> needed,
MountSymbolSet available,
MountSymbolSet needed,
) {
// We could do something more sophisticated here.
return needed.firstWhereOrNull((mount) => available[mount] > 0);
Expand Down
18 changes: 7 additions & 11 deletions packages/cli/lib/behavior/deliver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import 'package:cli/net/actions.dart';
import 'package:cli/printing.dart';
import 'package:cli/trading.dart';
import 'package:db/db.dart';
import 'package:more/collection.dart';
import 'package:types/types.dart';

// Go buy and deliver.
// Used for modules.

/// Compute the mounts in the given ship's inventory.
Multiset<ShipMountSymbolEnum> countMountsInInventory(Ship ship) {
final counts = Multiset<ShipMountSymbolEnum>();
MountSymbolSet countMountsInInventory(Ship ship) {
final counts = MountSymbolSet();
for (final item in ship.cargo.inventory) {
final mountSymbol = mountSymbolForTradeSymbol(item.tradeSymbol);
// Will be null if the item isn't a mount.
Expand All @@ -30,22 +29,19 @@ Multiset<ShipMountSymbolEnum> countMountsInInventory(Ship ship) {
}

/// Compute the mounts mounted on the given ship.
Multiset<ShipMountSymbolEnum> countMountedMounts(Ship ship) {
return Multiset<ShipMountSymbolEnum>.fromIterable(
MountSymbolSet countMountedMounts(Ship ship) {
return MountSymbolSet.fromIterable(
ship.mounts.map((m) => m.symbol),
);
}

/// Mounts to add to make [ship] match [template].
Multiset<ShipMountSymbolEnum> mountsToAddToShip(
Ship ship,
ShipTemplate template,
) {
MountSymbolSet mountsToAddToShip(Ship ship, ShipTemplate template) {
return template.mounts.difference(countMountedMounts(ship));
}

/// Mounts to remove to make [ship] match [template].
Multiset<ShipMountSymbolEnum> mountsToRemoveFromShip(
MountSymbolSet mountsToRemoveFromShip(
Ship ship,
ShipTemplate template,
) {
Expand All @@ -62,7 +58,7 @@ class _BuyRequest {
final int units;
}

_BuyRequest? _buyRequestFromNeededMounts(Multiset<ShipMountSymbolEnum> needed) {
_BuyRequest? _buyRequestFromNeededMounts(MountSymbolSet needed) {
if (needed.isEmpty) {
return null;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ packages:
dependency: "direct main"
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.17.2"
version: "1.18.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -249,13 +249,13 @@ packages:
source: hosted
version: "0.3.0"
more:
dependency: "direct main"
dependency: transitive
description:
name: more
sha256: "74d7379587be84284e9e1a15b4c296351bfdac0370bf2e55de996f13c93a2dc3"
sha256: a06e9e78bd1446612c9fbb033612176f252bcb9ab905a778202d9f8212000c5a
url: "https://pub.dev"
source: hosted
version: "4.0.0"
version: "4.0.1"
node_preamble:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion packages/cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies:
intl: ^0.17.0
mason_logger: ^0.2.5
meta: ^1.9.1
more: ^4.0.0
openapi:
path: ../openapi
postgres: ^2.6.2
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/test/behavior/change_mounts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:cli/logger.dart';
import 'package:cli/nav/route.dart';
import 'package:db/db.dart';
import 'package:mocktail/mocktail.dart';
import 'package:more/collection.dart';
import 'package:test/test.dart';
import 'package:types/types.dart';

Expand Down Expand Up @@ -109,14 +108,14 @@ void main() {
when(() => centralCommand.templateForShip(ship)).thenReturn(
ShipTemplate(
frameSymbol: ShipFrameSymbolEnum.CARRIER,
mounts: Multiset.from([
mounts: MountSymbolSet.from([
ShipMountSymbolEnum.SURVEYOR_I,
ShipMountSymbolEnum.SURVEYOR_II,
]),
),
);
when(() => centralCommand.unclaimedMountsAt(symbol))
.thenReturn(Multiset.from([ShipMountSymbolEnum.SURVEYOR_II]));
.thenReturn(MountSymbolSet.from([ShipMountSymbolEnum.SURVEYOR_II]));

final state = _MockBehaviorState();

Expand Down
3 changes: 1 addition & 2 deletions packages/cli/test/behavior/deliver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:cli/logger.dart';
import 'package:cli/nav/route.dart';
import 'package:db/db.dart';
import 'package:mocktail/mocktail.dart';
import 'package:more/collection.dart';
import 'package:test/test.dart';
import 'package:types/types.dart';

Expand Down Expand Up @@ -119,7 +118,7 @@ void main() {
);

when(centralCommand.mountsNeededForAllShips)
.thenReturn(Multiset.from([ShipMountSymbolEnum.GAS_SIPHON_I]));
.thenReturn(MountSymbolSet.from([ShipMountSymbolEnum.GAS_SIPHON_I]));
final routePlanner = _MockRoutePlanner();
when(() => caches.routePlanner).thenReturn(routePlanner);
when(() => centralCommand.expectedCreditsPerSecond(ship)).thenReturn(10);
Expand Down
20 changes: 20 additions & 0 deletions packages/types/lib/mount.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:more/collection.dart';
import 'package:types/api.dart';

/// Set of ship mount symbols.
typedef MountSymbolSet = Multiset<ShipMountSymbolEnum>;

/// Mounts template for a ship.
class ShipTemplate {
/// Create a new ship template.
const ShipTemplate({
required this.frameSymbol,
required this.mounts,
});

/// Frame type that this template is for.
final ShipFrameSymbolEnum frameSymbol;

/// Mounts that this template has.
final MountSymbolSet mounts;
}
1 change: 1 addition & 0 deletions packages/types/lib/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export 'enum.dart';
export 'extraction.dart';
export 'jobs.dart';
export 'market_price.dart';
export 'mount.dart';
export 'route.dart';
export 'shipyard_price.dart';
export 'survey.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/types/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
intl: ^0.17.0
# We only want the model types.
meta: ^1.9.1
more: ^4.0.1
openapi:
path: ../openapi

Expand Down

0 comments on commit 913cbbf

Please sign in to comment.