Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3e79181
feat: remove dependency on systems.json
eseidel May 4, 2025
effe348
chore: remove unnecessary async
eseidel May 4, 2025
82649a3
chore: update todo
eseidel May 4, 2025
3c8b977
test: fix one tests
eseidel May 4, 2025
f997849
test: fix another test
eseidel May 4, 2025
0abc616
chore: fix the last test
eseidel May 4, 2025
0f48267
feat: add system_record and system_waypoint tables
eseidel May 5, 2025
eae328d
chore: move SystemsSnapshot down into types
eseidel May 5, 2025
d21b264
chore: remove dead code
eseidel May 5, 2025
1cefd05
chore: remove empty file
eseidel May 5, 2025
3abfd1f
fix: reduce dependance on System class
eseidel May 6, 2025
b2fab2a
chore: further reduce use of System
eseidel May 6, 2025
536b6b1
feat: add a task to fetch all waypoints
eseidel May 6, 2025
7598d7a
feat: add an index for system on waypoint
eseidel May 6, 2025
f5a2446
chore: remove spurious log file
eseidel May 6, 2025
05342b1
feat: reload SystemsSnapshot if needed
eseidel May 6, 2025
1bb6e04
test: fix failing test
eseidel May 6, 2025
d47b1aa
fix: remove index which was invalid postgresql
eseidel May 6, 2025
ff23c86
test: restore planRoute test
eseidel May 6, 2025
e6fcbd0
fix: add more tests for SystemsStore
eseidel May 6, 2025
422fcc0
chore: move systems methods onto systems.db
eseidel May 6, 2025
c34b444
test: fix tests
eseidel May 6, 2025
ac91ec4
test: add more System tests
eseidel May 6, 2025
330b02a
test: new updateRoutingCaches behavior
eseidel May 6, 2025
77523e2
test: add coverage for allSystems query
eseidel May 6, 2025
92e62d2
chore: rename db.marketListingForSymbol to marketListingAt and add tests
eseidel May 6, 2025
27a8042
test: add test for db get/set auth token
eseidel May 6, 2025
f3eab19
test: add tests for market listing and ship tables
eseidel May 6, 2025
8453e6c
test: more behavior db tests
eseidel May 6, 2025
1994bcf
test: add db migration tests
eseidel May 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -1542,4 +1542,4 @@ cli-1 | [WARN] FleetRole.probe has no specified behaviors, idling.

### fleet_charters will be empty until restart and then full.

Not sure why. Some cache is not getting invalidated.
Not sure why. Some cache is not getting invalidated.
5 changes: 2 additions & 3 deletions packages/cli/bin/asteroids.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'package:cli/caches.dart';
import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
// List all known asteroids that have a market or shipyard.
final marketListings = await MarketListingSnapshot.load(db);
final shipyardListings = await ShipyardListingSnapshot.load(db);
final systemsCache = SystemsCache.load(fs);

for (final marketListing in marketListings.listings) {
final waypointSymbol = marketListing.waypointSymbol;
final waypoint = systemsCache.waypoint(waypointSymbol);
final waypoint = await db.systems.waypoint(waypointSymbol);
if (!waypoint.isAsteroid) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/behavior.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:convert';

import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
var behaviors = await db.allBehaviorStates();
if (argResults.rest.isNotEmpty) {
final onlyShips =
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/bin/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:cli/logic/logic.dart';
import 'package:cli/net/auth.dart';
import 'package:cli/net/register.dart';
import 'package:db/db.dart';
import 'package:file/local.dart';
import 'package:scoped_deps/scoped_deps.dart';
import 'package:types/types.dart';

Expand Down Expand Up @@ -129,8 +128,6 @@ Future<void> cliMain(List<String> args) async {

logger.info('Welcome to Space Traders! 🚀');

// Use package:file to make things mockable.
const fs = LocalFileSystem();
final db = await defaultDatabase();

var agentSymbol = await db.getAgentSymbol();
Expand Down Expand Up @@ -163,7 +160,7 @@ Future<void> cliMain(List<String> args) async {

config = await Config.fromDb(db);

final caches = await Caches.loadOrFetch(fs, api, db);
final caches = await Caches.loadOrFetch(api, db);
final marketPricesCount = await db.marketPricesCount();
final marketWaypointsCount = await db.marketPricesWaypointCount();
final shipyardPricesCount = await db.shipyardPricesCount();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/contracts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Future<void> printContracts(
}
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final printAll = argResults['all'] as bool;
final contractSnapshot = await ContractSnapshot.load(db);
await printContracts(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/db_stats.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
// Get a list of all tables.
final tableNames = await db.allTableNames();
// Get counts for each table.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/deals_in_progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ String annotatedName(CostedDeal deal) {
return deal.tradeSymbol.value;
}

Future<void> cliMain(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> cliMain(Database db, ArgResults argResults) async {
final behaviors = await BehaviorSnapshot.load(db);
final states = behaviors.states.where((state) => state.deal != null).toList();

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/deals_recent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool Function(Transaction t) filterFromArgs(List<String> args) {
return (Transaction t) => t.shipSymbol == shipSymbol;
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final filter = filterFromArgs(argResults.rest);
final deals = <SyntheticDeal>[];
final openDeals = <ShipSymbol, List<Transaction>>{};
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/bin/delete_behavior.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
var behaviors = await db.allBehaviorStates();
final onlyShips = argResults.rest.map((s) => int.parse(s, radix: 16)).toSet();
behaviors =
Expand All @@ -13,7 +13,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
logger.confirm('Deleting behaviors: ${behaviors.map((b) => b.shipSymbol)}');

for (final behavior in behaviors) {
await db.deleteBehaviorState(behavior.shipSymbol);
await db.deleteBehavior(behavior.shipSymbol);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/earning_per_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TransactionSummary {
}
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
// For a given ship, show the credits per minute averaged over the
// last hour.
final lookbackMinutesString = argResults.rest.firstOrNull;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/earning_rate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int hoursAgo(DateTime time) {
return DateTime.timestamp().difference(time).inHours;
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
// Credits per hour.
final oneDayAgoAsHour = snapToHour(
DateTime.timestamp().subtract(const Duration(hours: 24)),
Expand Down
24 changes: 0 additions & 24 deletions packages/cli/bin/exports_load.dart

This file was deleted.

20 changes: 10 additions & 10 deletions packages/cli/bin/exports_supply_chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import 'package:cli/plan/supply_chain.dart';
// Returns the distance between two waypoints, or null if they are in different
// systems.
int? _distanceBetween(
SystemsCache systemsCache,
SystemsSnapshot systems,
WaypointSymbol a,
WaypointSymbol b,
) {
final aWaypoint = systemsCache.waypoint(a);
final bWaypoint = systemsCache.waypoint(b);
final aWaypoint = systems.waypoint(a);
final bWaypoint = systems.waypoint(b);
if (aWaypoint.system != bWaypoint.system) {
return null;
}
Expand All @@ -31,7 +31,7 @@ class DescribingVisitor extends SupplyLinkVisitor {
/// Create a new describing visitor.
DescribingVisitor(this.systems, this.db);

final SystemsCache systems;
final SystemsSnapshot systems;
final Database db;

final _indent = ' ';
Expand Down Expand Up @@ -83,19 +83,19 @@ class DescribingVisitor extends SupplyLinkVisitor {
}
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final exports = await TradeExportCache(db).snapshot();
final systems = SystemsCache.load(fs);
final systems = await db.systems.snapshotAllSystems();
final marketListings = await MarketListingSnapshot.load(db);
final charting = await ChartingSnapshot.load(db);
final agent = await db.getMyAgent();

final jumpgate =
systems.jumpGateWaypointForSystem(agent!.headquarters.system)!;
final waypointSymbol = jumpgate.symbol;
final waypointSymbol = systems.jumpGateSymbolForSystem(
agent!.headquarters.system,
);
final construction =
(await db.getConstructionRecord(
waypointSymbol,
waypointSymbol!,
defaultMaxAge,
))!.construction;

Expand Down
5 changes: 2 additions & 3 deletions packages/cli/bin/extract_scores.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:cli/cache/charting_cache.dart';
import 'package:cli/cache/market_price_snapshot.dart';
import 'package:cli/cache/systems_cache.dart';
import 'package:cli/cli.dart';
import 'package:cli/config.dart';
import 'package:cli/plan/extraction_score.dart';
Expand Down Expand Up @@ -39,7 +38,7 @@ double _scoreMarkets(
return percentiles.average / 100.0;
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final countLimit = int.tryParse(argResults['limit'] as String);
if (countLimit == null) {
throw ArgumentError.value(
Expand All @@ -65,7 +64,7 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
' with matching markets within $maxDistance total round-trip:',
);

final systems = await SystemsCache.loadOrFetch(fs);
final systems = await db.systems.snapshotAllSystems();
final charting = ChartingCache(db);
final hqSystem = await myHqSystemSymbol(db);
final marketPrices = await MarketPriceSnapshot.loadOneSystem(db, hqSystem);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/bin/find_mounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'package:cli/caches.dart';
import 'package:cli/cli.dart';
import 'package:cli/plan/trading.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final marketPrices = await MarketPriceSnapshot.loadAll(db);
final systemsCache = SystemsCache.load(fs);
final systemsCache = await db.systems.snapshotAllSystems();
final systemConnectivity = await loadSystemConnectivity(db);
final routePlanner = RoutePlanner.fromSystemsCache(
final routePlanner = RoutePlanner.fromSystemsSnapshot(
systemsCache,
systemConnectivity,
sellsFuel: (_) => false,
Expand Down
19 changes: 9 additions & 10 deletions packages/cli/bin/fleet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ String _behaviorOrTypeString(Ship ship, BehaviorState? behavior) {
'${ship.registration.role}';
}

void logShip(
SystemsCache systemsCache,
Future<void> logShip(
Database db,
MarketPriceSnapshot marketPrices,
Ship ship,
BehaviorState? behavior,
) {
) async {
const indent = ' ';
final waypoint = systemsCache.waypoint(ship.waypointSymbol);
final waypointType = await db.systems.waypointType(ship.waypointSymbol);
final cargoStatus =
ship.cargo.capacity == 0
? ''
Expand All @@ -74,14 +74,14 @@ void logShip(
if (routePlan != null) {
final timeLeft = ship.timeToArrival(routePlan);
final destination = routePlan.endSymbol.sectorLocalName;
final destinationType = systemsCache.waypoint(routePlan.endSymbol).type;
final destinationType = await db.systems.waypointType(routePlan.endSymbol);
final arrival = approximateDuration(timeLeft);
logger.info(
'${indent}en route to $destination $destinationType '
'in $arrival',
);
} else {
logger.info('$indent${describeShipNav(ship.nav)} ${waypoint.type}');
logger.info('$indent${describeShipNav(ship.nav)} $waypointType');
}
final deal = behavior?.deal;
if (deal != null) {
Expand All @@ -99,7 +99,7 @@ bool Function(Ship) filterFromArgs(List<String> args) {
return (ship) => ship.symbol.symbol == symbol;
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final filter = filterFromArgs(argResults.rest);
final ships = await ShipSnapshot.load(db);

Expand All @@ -113,11 +113,10 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
return;
}

final systemsCache = SystemsCache.load(fs);
final marketPrices = await MarketPriceSnapshot.loadAll(db);
for (final ship in matchingShips) {
final behaviorState = await db.behaviorStateBySymbol(ship.symbol);
logShip(systemsCache, marketPrices, ship, behaviorState);
final behaviorState = await db.getBehavior(ship.symbol);
await logShip(db, marketPrices, ship, behaviorState);
}

final behaviors = await BehaviorSnapshot.load(db);
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/bin/fleet_charters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ String plural(int count, String singular, [String plural = 's']) {
return '$count ${count == 1 ? singular : singular + plural}';
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final charterStates = await db.behaviorStatesWithBehavior(Behavior.charter);
final systemsCache = SystemsCache.load(fs);
Future<void> command(Database db, ArgResults argResults) async {
final charterStates = await db.behaviorsOfType(Behavior.charter);
final systemsCache = db.systems;
final ships = await ShipSnapshot.load(db);

logger.info('${plural(charterStates.length, 'charter')}:');
Expand All @@ -27,7 +27,9 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
if (routePlan != null) {
final timeLeft = ship.timeToArrival(routePlan);
final destination = routePlan.endSymbol.sectorLocalName;
final destinationType = systemsCache.waypoint(routePlan.endSymbol).type;
final destinationType = await systemsCache.waypointType(
routePlan.endSymbol,
);
final arrival = approximateDuration(timeLeft);
logger.info(' en route to $destination $destinationType in $arrival');
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/bin/fleet_closest_to.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'package:cli/cli.dart';
import 'package:cli/nav/navigation.dart';
import 'package:collection/collection.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
// For a given destination, compute the time to travel there for each ship.
final destination = WaypointSymbol.fromString(argResults.rest[0]);
final ships = await ShipSnapshot.load(db);
final systemsCache = SystemsCache.load(fs);
final systemsCache = await db.systems.snapshotAllSystems();
final marketListings = await MarketListingSnapshot.load(db);

final systemConnectivity = await loadSystemConnectivity(db);
final routePlanner = RoutePlanner.fromSystemsCache(
final routePlanner = RoutePlanner.fromSystemsSnapshot(
systemsCache,
systemConnectivity,
sellsFuel: defaultSellsFuel(marketListings),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/fleet_idle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:cli/caches.dart';
import 'package:cli/cli.dart';

// count the number of idle behaviors by FleetRole.
Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final ships = await ShipSnapshot.load(db);
final behaviors = await BehaviorSnapshot.load(db);
final idleByRole = <FleetRole, int>{};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/fleet_needed_mounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:cli/caches.dart';
import 'package:cli/central_command.dart';
import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final ships = await ShipSnapshot.load(db);
final centralCommand = CentralCommand();

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/fleet_ship_to_buy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:cli/central_command.dart';
import 'package:cli/cli.dart';
import 'package:cli/config.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final shipCache = await ShipSnapshot.load(db);
final shipyardListings = await ShipyardListingSnapshot.load(db);
final shipyardShips = await ShipyardShipCache(db).snapshot();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/fleet_systems.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ String _describeBehaviors(BehaviorSnapshot behaviors, List<Ship> ships) {
return behaviorCounts.entries.map((e) => '${e.key}: ${e.value}').join(', ');
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final ships = await ShipSnapshot.load(db);
final behaviors = await BehaviorSnapshot.load(db);

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/fleet_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Map<ShipType, int> _shipTypeCounts(
return typeCounts;
}

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Future<void> command(Database db, ArgResults argResults) async {
final ships = await ShipSnapshot.load(db);
final marketPrices = await MarketPriceSnapshot.loadAll(db);
final shipyardPrices = await ShipyardPriceSnapshot.load(db);
Expand Down
Loading