Skip to content

Commit

Permalink
refactor: remove agentName from Config
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Aug 11, 2024
1 parent 56924f2 commit dcb5c63
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/cli/bin/exports_supply_chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final systems = SystemsCache.load(fs);
final marketListings = await MarketListingSnapshot.load(db);
final charting = await ChartingSnapshot.load(db);
final agent = await myAgent(db);
final agent = await db.getMyAgent();

final jumpgate =
systems.jumpGateWaypointForSystem(agent.headquarters.system)!;
systems.jumpGateWaypointForSystem(agent!.headquarters.system)!;
final waypointSymbol = jumpgate.symbol;
final construction =
(await db.getConstruction(waypointSymbol, defaultMaxAge))!.construction;
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/bin/report_balance_sheet.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:cli/caches.dart';
import 'package:cli/cli.dart';
import 'package:cli/config.dart';
import 'package:cli/logic/printing.dart';
import 'package:cli/plan/accounting.dart';
import 'package:cli/plan/ships.dart';
Expand Down Expand Up @@ -50,7 +49,7 @@ Future<Assets> computeAssets(FileSystem fs, Database db) async {
final shipyardPrices = await ShipyardPriceSnapshot.load(db);
final shipyardShips = ShipyardShipCache.load(fs);

final agent = await db.getAgent(symbol: config.agentSymbol);
final agent = await db.getMyAgent();
final inventory = await computeInventoryValue(ships, marketPrices);
final shipsValue =
await computeShipValue(ships, shipyardShips, shipyardPrices);
Expand Down
7 changes: 0 additions & 7 deletions packages/cli/bin/squads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:cli/cache/static_cache.dart';
import 'package:cli/cache/systems_cache.dart';
import 'package:cli/central_command.dart';
import 'package:cli/cli.dart';
import 'package:cli/config.dart';
import 'package:cli/plan/ships.dart';

String describeJob(ExtractionJob job) {
Expand All @@ -18,12 +17,6 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final charting = ChartingCache(db);
final ships = await ShipSnapshot.load(db);
final shipyardShipCache = ShipyardShipCache.load(fs);
final agentSymbol = await db.getAgentSymbol();
if (agentSymbol == null) {
throw StateError('No agent symbol found in database.');
}
// TODO(eseidel): Compute the current phase or read from db.
config = Config(agentSymbol, GamePhase.construction);

final squads = await assignShipsToSquads(
db,
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/lib/cache/agent_cache.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:cli/api.dart';
import 'package:cli/cache/systems_cache.dart';
import 'package:cli/cli.dart';
import 'package:cli/config.dart';
import 'package:cli/net/queries.dart';

/// Holds the Agent object between requests.
Expand All @@ -19,7 +18,7 @@ class AgentCache {
/// Loads the agent from the cache.
// TODO(eseidel): Do callers need an AgentCache or just an Agent?
static Future<AgentCache?> load(Database db) async {
final agent = await db.getAgent(symbol: config.agentSymbol);
final agent = await db.getMyAgent();
if (agent == null) {
return null;
}
Expand All @@ -28,7 +27,7 @@ class AgentCache {

/// Creates a new AgentCache from the API.
static Future<AgentCache> loadOrFetch(Database db, Api api) async {
final cached = await db.getAgent(symbol: config.agentSymbol);
final cached = await db.getMyAgent();
if (cached != null) {
return AgentCache(cached, db);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/cli/lib/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,10 @@ String argFromShipType(ShipType shipType) {

/// Common lookups which CLIs might need.
/// Get the agent from the database.
Future<Agent> myAgent(Database db) async {
final agent = await db.getAgent(symbol: config.agentSymbol);
return agent!;
}

/// Get the symbol of the agent's headquarters.
Future<WaypointSymbol> myHqSymbol(Database db) async {
final agent = await myAgent(db);
return agent.headquarters;
final agent = await db.getMyAgent();
return agent!.headquarters;
}

/// Get the system symbol of the agent's headquarters.
Expand All @@ -101,8 +95,8 @@ Future<SystemSymbol> myHqSystemSymbol(Database db) async {

/// Get the agent's credits.
Future<int> myCredits(Database db) async {
final agent = await myAgent(db);
return agent.credits;
final agent = await db.getMyAgent();
return agent!.credits;
}

/// Get the start symbol from the command line argument.
Expand Down
11 changes: 2 additions & 9 deletions packages/cli/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ class NetworkConfig {
/// Class for holding our hard-coded configuration values.
class Config {
/// Create a new Config object.
Config(this.agentSymbol, this.gamePhase);
Config(this.gamePhase);

/// Which phase are we in.
final GamePhase gamePhase;

/// The symbol of the agent we are controlling.
final String agentSymbol;

/// Whether or not we should enable mining behaviors.
bool get enableMining => gamePhase < GamePhase.exploration;

Expand Down Expand Up @@ -248,12 +245,8 @@ class Config {
final fuelMaxMarkup = 10.0;

static Future<Config> fromDb(Database db) async {
final agentSymbol = await db.getAgentSymbol();
if (agentSymbol == null) {
throw StateError('No agent symbol found in database.');
}
final gamePhase = await db.getGamePhase() ?? GamePhase.bootstrap;
return Config(agentSymbol, gamePhase);
return Config(gamePhase);
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/db/lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ class Database {
);
}

Future<Agent?> getMyAgent() async {
final symbol = await getAgentSymbol();
if (symbol == null) {
return null;
}
return getAgent(symbol: symbol);
}

/// Get the agent from the database.
Future<Agent?> getAgent({required String symbol}) async {
final query = agentBySymbolQuery(symbol);
Expand Down

0 comments on commit dcb5c63

Please sign in to comment.