Skip to content

Commit

Permalink
feat: move Agent into the db
Browse files Browse the repository at this point in the history
Also made a custom Agent class instead of OpenAPI's.
  • Loading branch information
eseidel committed Feb 17, 2024
1 parent d0b1e27 commit efe045d
Show file tree
Hide file tree
Showing 49 changed files with 435 additions and 283 deletions.
11 changes: 3 additions & 8 deletions packages/cli/bin/construction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import 'package:cli/cli.dart';
import 'package:cli/printing.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}

final db = await defaultDatabase();

final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

final systemsCache = SystemsCache.load(fs)!;
final jumpGateSymbol = systemsCache
.waypointsInSystem(startSystemSymbol)
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/bin/deals_nearby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Future<void> cliMain(FileSystem fs, ArgResults argResults) async {

final behaviorCache = BehaviorCache.load(fs);
final shipCache = ShipCache.load(fs)!;
final agentCache = AgentCache.load(fs)!;
final agentCache = await AgentCache.load(db);
final contractCache = ContractCache.load(fs)!;
final centralCommand =
CentralCommand(behaviorCache: behaviorCache, shipCache: shipCache);

final start = startArg == null
? agentCache.headquarters(systemsCache)
? agentCache!.headquarters(systemsCache)
: systemsCache.waypointFromString(startArg)!;

final jumpGate = systemsCache.jumpGateWaypointForSystem(start.system)!;
Expand All @@ -55,7 +55,7 @@ Future<void> cliMain(FileSystem fs, ArgResults argResults) async {
final extraSellOpps = <SellOpp>[];
if (centralCommand.isContractTradingEnabled) {
extraSellOpps
.addAll(centralCommand.contractSellOpps(agentCache, contractCache));
.addAll(centralCommand.contractSellOpps(agentCache!, contractCache));
}
if (centralCommand.isConstructionTradingEnabled) {
extraSellOpps.addAll(centralCommand.constructionSellOpps());
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/bin/exports_supply_chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
final systemsCache = SystemsCache.load(fs)!;
final marketListings = MarketListingCache.load(fs);
final marketPrices = MarketPrices.load(fs);
final agentCache = AgentCache.load(fs)!;
final agent = await myAgent(db);
final constructionCache = ConstructionCache(db);

final jumpgate = systemsCache
.jumpGateWaypointForSystem(agentCache.headquartersSystemSymbol)!;
final jumpgate =
systemsCache.jumpGateWaypointForSystem(agent.headquarters.system)!;
final waypointSymbol = jumpgate.symbol;
final construction = await constructionCache.getConstruction(waypointSymbol);

Expand Down
4 changes: 1 addition & 3 deletions packages/cli/bin/extract_scores.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:cli/cache/agent_cache.dart';
import 'package:cli/cache/charting_cache.dart';
import 'package:cli/cache/market_cache.dart';
import 'package:cli/cache/market_prices.dart';
Expand Down Expand Up @@ -64,8 +63,7 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {

final systems = await SystemsCache.loadOrFetch(fs);
final charting = ChartingCache(db);
final agentCache = AgentCache.load(fs)!;
final hqSystem = agentCache.headquartersSystemSymbol;
final hqSystem = await myHqSystemSymbol(db);
final marketListings = MarketListingCache.load(fs);
final marketPrices = MarketPrices.load(fs);

Expand Down
12 changes: 4 additions & 8 deletions packages/cli/bin/jumpgate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import 'package:cli/net/auth.dart';
import 'package:cli/printing.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}

final db = await defaultDatabase();

final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

final api = defaultApi(fs, db, getPriority: () => networkPriorityLow);

final systemsCache = SystemsCache.load(fs)!;
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/bin/jumpgate_construction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import 'package:cli/cli.dart';
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();

final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}
final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

final systemsCache = SystemsCache.load(fs)!;

Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/jumpgates_to_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
// Start at the agent's headquarters system.
// Walk the web of jump gates to find endpoints we should scan.
final db = await defaultDatabase();
final agentCache = AgentCache.load(fs)!;
final systemSymbol = agentCache.headquartersSystemSymbol;
final systemSymbol = await myHqSystemSymbol(db);
final systemsCache = SystemsCache.load(fs)!;
final jumpGateCache = JumpGateCache.load(fs);
final constructionSnapshot = await ConstructionSnapshot.load(db);
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/market_feeder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
sellsFuel: defaultSellsFuel(marketListings),
);

final agentCache = AgentCache.load(fs)!;
final behaviorCache = BehaviorCache.load(fs);

const shipType = ShipType.LIGHT_HAULER;
final ship = staticCaches.shipyardShips[shipType]!;
final shipSpec = ship.shipSpec;
final credits = agentCache.agent.credits;
final credits = await myCredits(db);

// Given a desired export. Find a market to feed.
final export = TradeSymbol.fromJson(argResults['export'] as String)!;
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/bin/market_listings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ void addSymbols(
}

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();

final systemsCache = SystemsCache.load(fs)!;
final agentCache = AgentCache.load(fs)!;
final hqSystem = agentCache.headquartersSystemSymbol;
final hqSystem = await myHqSystemSymbol(db);
final marketListings = MarketListingCache.load(fs);

final waypoints = systemsCache.waypointsInSystem(hqSystem);
Expand Down Expand Up @@ -98,6 +99,8 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
addSymbols(table, 'exchange', listing.exchange, marketSymbol, marketPrices);
logger.info(table.toString());
}

await db.close();
}

void main(List<String> args) async {
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/bin/market_nearby_sells.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
systemConnectivity,
sellsFuel: (_) => false,
);
final agentCache = AgentCache.load(fs)!;
final shipCache = ShipCache.load(fs)!;

const tradeSymbol = TradeSymbol.DIAMONDS;

final hq = agentCache.headquarters(systemsCache);
final hqSystem = await myHqSystemSymbol(db);
final hqMine = systemsCache
.waypointsInSystem(hq.system)
.waypointsInSystem(hqSystem)
.firstWhere((w) => w.isAsteroid)
.symbol;

Expand Down
7 changes: 5 additions & 2 deletions packages/cli/bin/market_pairs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ int? distanceBetween(
}

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();

final systemsCache = SystemsCache.load(fs)!;
final agentCache = AgentCache.load(fs)!;
final hqSystem = agentCache.headquartersSystemSymbol;
final hqSystem = await myHqSystemSymbol(db);
final marketListings = MarketListingCache.load(fs);
final marketPrices = MarketPrices.load(fs);

Expand Down Expand Up @@ -122,6 +123,8 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
]);
}
logger.info(table.toString());

await db.close();
}

void main(List<String> args) async {
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/bin/simulate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
systemConnectivity,
sellsFuel: (_) => false,
);
final agentCache = AgentCache.load(fs)!;
final shipyardPrices = ShipyardPrices.load(fs);
final shipyardShips = ShipyardShipCache.load(fs);
final shipMounts = ShipMountCache.load(fs);

final hq = agentCache.headquarters(systemsCache);
final hqSystem = await myHqSystemSymbol(db);
final hqMine =
systemsCache.waypointsInSystem(hq.system).firstWhere((w) => w.isAsteroid);
systemsCache.waypointsInSystem(hqSystem).firstWhere((w) => w.isAsteroid);
const tradeSymbol = TradeSymbol.DIAMONDS;
const haulerType = ShipType.LIGHT_HAULER;
final unitsPerHaulerCycle = shipyardShips.capacityForShipType(haulerType)!;
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/system_routing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {

final db = await defaultDatabase();
final systems = SystemsCache.load(fs)!;
final agentCache = AgentCache.load(fs)!;
final hqSystemSymbol = agentCache.headquartersSystemSymbol;
final hqSystemSymbol = await myHqSystemSymbol(db);
final marketListings = MarketListingCache.load(fs);
final shipyardListings = ShipyardListingCache.load(fs);
final jumpGateCache = JumpGateCache.load(fs);
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/bin/system_stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}
final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

logger.info('Starting from $startSystemSymbol, known reachable:');
final systemsCache = SystemsCache.load(fs)!;
Expand Down
11 changes: 2 additions & 9 deletions packages/cli/bin/system_to_chart_next.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@ import 'package:cli/ships.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();

final WaypointSymbol startSymbol;
if (argResults.rest.isNotEmpty) {
startSymbol = WaypointSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSymbol = agentCache.headquartersSymbol;
}

final startSymbol =
await startWaypointFromArg(db, argResults.rest.firstOrNull);
final staticCaches = StaticCaches.load(fs);
final systemsCache = SystemsCache.load(fs)!;
final charting = ChartingCache(db);
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/bin/systems_explored.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
final constructionSnapshot = await ConstructionSnapshot.load(db);
final systemConnectivity =
SystemConnectivity.fromJumpGates(jumpGateCache, constructionSnapshot);
final agentCache = AgentCache.load(fs)!;
final headquartersSystemSymbol = agentCache.headquartersSystemSymbol;
final hqSystemSymbol = await myHqSystemSymbol(db);
final reachableSystems =
systemConnectivity.systemsReachableFrom(headquartersSystemSymbol);
systemConnectivity.systemsReachableFrom(hqSystemSymbol);

final systemsWithCharts = reachableSystems
.where((s) => (chartedWaypointsBySystem[s] ?? []).isNotEmpty);
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/systems_interesting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
final jumpGateCache = JumpGateCache.load(fs);
final systemConnectivity =
SystemConnectivity.fromJumpGates(jumpGateCache, constructionSnapshot);
final agentCache = AgentCache.load(fs)!;
final hqSystemSymbol = agentCache.headquartersSystemSymbol;
final hqSystemSymbol = await myHqSystemSymbol(db);
final reachableSystems =
systemConnectivity.systemsReachableFrom(hqSystemSymbol).toSet();

Expand Down
9 changes: 2 additions & 7 deletions packages/cli/bin/systems_nearby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import 'package:cli/cli.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}
final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

final marketListings = MarketListingCache.load(fs);
final jumpGateCache = JumpGateCache.load(fs);
Expand Down
11 changes: 3 additions & 8 deletions packages/cli/bin/systems_to_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import 'package:cli/cli.dart';
/// Walks our known system graph, starting from HQ and prints systems needing
/// exploration.
Future<void> command(FileSystem fs, ArgResults argResults) async {
final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}

final db = await defaultDatabase();
final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

final jumpGateCache = JumpGateCache.load(fs);
final constructionSnapshot = await ConstructionSnapshot.load(db);
final systemConnectivity =
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/systems_to_warp_to.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final api = defaultApi(fs, db, getPriority: () => networkPriorityLow);

final agentCache = AgentCache.load(fs)!;
final startSystemSymbol = agentCache.headquartersSystemSymbol;
final startSystemSymbol = await myHqSystemSymbol(db);

final staticCaches = StaticCaches.load(fs);
final jumpGateCache = JumpGateCache.load(fs);
Expand Down
11 changes: 3 additions & 8 deletions packages/cli/bin/waypoint_cache_ages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import 'package:cli/printing.dart';
import 'package:cli_table/cli_table.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final SystemSymbol startSystemSymbol;
if (argResults.rest.isNotEmpty) {
startSystemSymbol = SystemSymbol.fromString(argResults.rest.first);
} else {
final agentCache = AgentCache.load(fs)!;
startSystemSymbol = agentCache.headquartersSystemSymbol;
}

final db = await defaultDatabase();
final startSystemSymbol =
await startSystemFromArg(db, argResults.rest.firstOrNull);

final systemsCache = SystemsCache.load(fs)!;
final chartingSnapshot = await ChartingSnapshot.load(db);
final constructionSnapshot = await ConstructionSnapshot.load(db);
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/bin/waypoint_reachability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'package:cli/cli.dart';
import 'package:cli/nav/waypoint_connectivity.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final staticCaches = StaticCaches.load(fs);
final systemsCache = SystemsCache.load(fs)!;
final agentCache = AgentCache.load(fs)!;
final hqSystem = agentCache.headquartersSystemSymbol;
final hqSystem = await myHqSystemSymbol(db);
final fuelCapacity =
staticCaches.shipyardShips[ShipType.COMMAND_FRIGATE]!.frame.fuelCapacity;

Expand All @@ -31,6 +31,8 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
);
}
}

await db.close();
}

void main(List<String> args) async {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/behavior/trader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Future<void> _completeContract(
) async {
final response = await api.contracts.fulfillContract(contract.id);
final data = response!.data;
caches.agent.agent = data.agent;
await caches.agent.updateAgent(Agent.fromOpenApi(data.agent));
caches.contracts.updateContract(data.contract);

final contactTransaction = ContractTransaction.fulfillment(
Expand Down
Loading

0 comments on commit efe045d

Please sign in to comment.