From 913cbbff00991c21d246b260de8098d81abe38b6 Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Sun, 20 Aug 2023 22:01:33 +0000 Subject: [PATCH] refactor: move ShipTemplate and ship mount sets to types --- .../cli/lib/behavior/central_command.dart | 34 +++++-------------- packages/cli/lib/behavior/change_mounts.dart | 5 ++- packages/cli/lib/behavior/deliver.dart | 18 ++++------ packages/cli/pubspec.lock | 10 +++--- packages/cli/pubspec.yaml | 1 - .../cli/test/behavior/change_mounts_test.dart | 5 ++- packages/cli/test/behavior/deliver_test.dart | 3 +- packages/types/lib/mount.dart | 20 +++++++++++ packages/types/lib/types.dart | 1 + packages/types/pubspec.yaml | 1 + 10 files changed, 48 insertions(+), 50 deletions(-) create mode 100644 packages/types/lib/mount.dart diff --git a/packages/cli/lib/behavior/central_command.dart b/packages/cli/lib/behavior/central_command.dart index d9a0c8e1..2cad70b0 100644 --- a/packages/cli/lib/behavior/central_command.dart +++ b/packages/cli/lib/behavior/central_command.dart @@ -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. @@ -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 mounts; -} - /// Where are we in the phases of the reset. enum GamePhase with EnumIndexOrdering { /// Early, we have little money, lots of request space. @@ -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, @@ -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, @@ -240,8 +224,8 @@ class CentralCommand { } /// Add up all mounts needed for current ships based on current templating. - Multiset mountsNeededForAllShips() { - final totalNeeded = Multiset(); + MountSymbolSet mountsNeededForAllShips() { + final totalNeeded = MountSymbolSet(); for (final ship in _shipCache.ships) { final template = templateForShip(ship); if (template == null) { @@ -427,8 +411,8 @@ class CentralCommand { } /// Returns the counts of mounts already claimed. - Multiset claimedMounts() { - final claimed = Multiset(); + MountSymbolSet claimedMounts() { + final claimed = MountSymbolSet(); for (final state in _behaviorCache.states) { final behavior = state.behavior; if (behavior != Behavior.changeMounts) { @@ -444,13 +428,13 @@ class CentralCommand { } /// Returns the number of mounts available at the waypoint. - Multiset 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(); + final available = MountSymbolSet(); for (final ship in ships) { final state = _behaviorCache.getBehavior(ship.shipSymbol); if (state == null || state.behavior != Behavior.deliver) { diff --git a/packages/cli/lib/behavior/change_mounts.dart b/packages/cli/lib/behavior/change_mounts.dart index 05b689fc..c635cf50 100644 --- a/packages/cli/lib/behavior/change_mounts.dart +++ b/packages/cli/lib/behavior/change_mounts.dart @@ -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 available, - Multiset needed, + MountSymbolSet available, + MountSymbolSet needed, ) { // We could do something more sophisticated here. return needed.firstWhereOrNull((mount) => available[mount] > 0); diff --git a/packages/cli/lib/behavior/deliver.dart b/packages/cli/lib/behavior/deliver.dart index 60c5cd46..e3ee433b 100644 --- a/packages/cli/lib/behavior/deliver.dart +++ b/packages/cli/lib/behavior/deliver.dart @@ -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 countMountsInInventory(Ship ship) { - final counts = Multiset(); +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. @@ -30,22 +29,19 @@ Multiset countMountsInInventory(Ship ship) { } /// Compute the mounts mounted on the given ship. -Multiset countMountedMounts(Ship ship) { - return Multiset.fromIterable( +MountSymbolSet countMountedMounts(Ship ship) { + return MountSymbolSet.fromIterable( ship.mounts.map((m) => m.symbol), ); } /// Mounts to add to make [ship] match [template]. -Multiset 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 mountsToRemoveFromShip( +MountSymbolSet mountsToRemoveFromShip( Ship ship, ShipTemplate template, ) { @@ -62,7 +58,7 @@ class _BuyRequest { final int units; } -_BuyRequest? _buyRequestFromNeededMounts(Multiset needed) { +_BuyRequest? _buyRequestFromNeededMounts(MountSymbolSet needed) { if (needed.isEmpty) { return null; } diff --git a/packages/cli/pubspec.lock b/packages/cli/pubspec.lock index d9405029..f053cd07 100644 --- a/packages/cli/pubspec.lock +++ b/packages/cli/pubspec.lock @@ -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: @@ -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: diff --git a/packages/cli/pubspec.yaml b/packages/cli/pubspec.yaml index c5fed812..4c132928 100644 --- a/packages/cli/pubspec.yaml +++ b/packages/cli/pubspec.yaml @@ -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 diff --git a/packages/cli/test/behavior/change_mounts_test.dart b/packages/cli/test/behavior/change_mounts_test.dart index 9d90ca1b..b853e44c 100644 --- a/packages/cli/test/behavior/change_mounts_test.dart +++ b/packages/cli/test/behavior/change_mounts_test.dart @@ -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'; @@ -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(); diff --git a/packages/cli/test/behavior/deliver_test.dart b/packages/cli/test/behavior/deliver_test.dart index 92c38e0d..fba631d0 100644 --- a/packages/cli/test/behavior/deliver_test.dart +++ b/packages/cli/test/behavior/deliver_test.dart @@ -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'; @@ -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); diff --git a/packages/types/lib/mount.dart b/packages/types/lib/mount.dart new file mode 100644 index 00000000..f01cb2f3 --- /dev/null +++ b/packages/types/lib/mount.dart @@ -0,0 +1,20 @@ +import 'package:more/collection.dart'; +import 'package:types/api.dart'; + +/// Set of ship mount symbols. +typedef MountSymbolSet = Multiset; + +/// 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; +} diff --git a/packages/types/lib/types.dart b/packages/types/lib/types.dart index 72726153..df80a2bf 100644 --- a/packages/types/lib/types.dart +++ b/packages/types/lib/types.dart @@ -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'; diff --git a/packages/types/pubspec.yaml b/packages/types/pubspec.yaml index 34d27e15..edbfc8e7 100644 --- a/packages/types/pubspec.yaml +++ b/packages/types/pubspec.yaml @@ -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