Factor out game creation parameters in main.

Want to move this deeper into the launch process so that it can use the
UI, but don't want to pass the loosely typed argparse namespace any
more than we have to.
This commit is contained in:
Dan Albert 2023-06-03 11:53:43 -07:00
parent 5d08990cd0
commit c25e830e6c

View File

@ -1,8 +1,11 @@
from __future__ import annotations
import argparse import argparse
import logging import logging
import ntpath import ntpath
import os import os
import sys import sys
from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@ -261,19 +264,38 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args() return parser.parse_args()
def create_game( @dataclass(frozen=True)
campaign_path: Path, class CreateGameParams:
blue: str, campaign_path: Path
red: str, blue: str
supercarrier: bool, red: str
auto_procurement: bool, supercarrier: bool
inverted: bool, auto_procurement: bool
cheats: bool, inverted: bool
start_date: datetime, cheats: bool
restrict_weapons_by_date: bool, start_date: datetime
advanced_iads: bool, restrict_weapons_by_date: bool
use_new_squadron_rules: bool, advanced_iads: bool
) -> Game: use_new_squadron_rules: bool
@staticmethod
def from_args(args: argparse.Namespace) -> CreateGameParams:
return CreateGameParams(
args.campaign,
args.blue,
args.red,
args.supercarrier,
args.auto_procurement,
args.inverted,
args.cheats,
args.date,
args.restrict_weapons_by_date,
args.advanced_iads,
args.use_new_squadron_rules,
)
def create_game(params: CreateGameParams) -> Game:
first_start = liberation_install.init() first_start = liberation_install.init()
if first_start: if first_start:
sys.exit( sys.exit(
@ -289,32 +311,32 @@ def create_game(
# for loadouts) without saving the generated campaign and reloading it the normal # for loadouts) without saving the generated campaign and reloading it the normal
# way. # way.
inject_custom_payloads(Path(persistence.base_path())) inject_custom_payloads(Path(persistence.base_path()))
campaign = Campaign.from_file(campaign_path) campaign = Campaign.from_file(params.campaign_path)
theater = campaign.load_theater(advanced_iads) theater = campaign.load_theater(params.advanced_iads)
faction_loader = Factions.load() faction_loader = Factions.load()
lua_plugin_manager = LuaPluginManager.load() lua_plugin_manager = LuaPluginManager.load()
lua_plugin_manager.merge_player_settings() lua_plugin_manager.merge_player_settings()
generator = GameGenerator( generator = GameGenerator(
faction_loader.get_by_name(blue), faction_loader.get_by_name(params.blue),
faction_loader.get_by_name(red), faction_loader.get_by_name(params.red),
theater, theater,
campaign.load_air_wing_config(theater), campaign.load_air_wing_config(theater),
Settings( Settings(
supercarrier=supercarrier, supercarrier=params.supercarrier,
automate_runway_repair=auto_procurement, automate_runway_repair=params.auto_procurement,
automate_front_line_reinforcements=auto_procurement, automate_front_line_reinforcements=params.auto_procurement,
automate_aircraft_reinforcements=auto_procurement, automate_aircraft_reinforcements=params.auto_procurement,
enable_frontline_cheats=cheats, enable_frontline_cheats=params.cheats,
enable_base_capture_cheat=cheats, enable_base_capture_cheat=params.cheats,
restrict_weapons_by_date=restrict_weapons_by_date, restrict_weapons_by_date=params.restrict_weapons_by_date,
enable_squadron_aircraft_limits=use_new_squadron_rules, enable_squadron_aircraft_limits=params.use_new_squadron_rules,
), ),
GeneratorSettings( GeneratorSettings(
start_date=start_date, start_date=params.start_date,
start_time=campaign.recommended_start_time, start_time=campaign.recommended_start_time,
player_budget=DEFAULT_BUDGET, player_budget=DEFAULT_BUDGET,
enemy_budget=DEFAULT_BUDGET, enemy_budget=DEFAULT_BUDGET,
inverted=inverted, inverted=params.inverted,
advanced_iads=theater.iads_network.advanced_iads, advanced_iads=theater.iads_network.advanced_iads,
no_carrier=False, no_carrier=False,
no_lha=False, no_lha=False,
@ -334,7 +356,7 @@ def create_game(
lua_plugin_manager, lua_plugin_manager,
) )
game = generator.generate() game = generator.generate()
game.begin_turn_0(squadrons_start_full=use_new_squadron_rules) game.begin_turn_0(squadrons_start_full=params.use_new_squadron_rules)
return game return game
@ -417,19 +439,7 @@ def main():
if args.subcommand == "new-game": if args.subcommand == "new-game":
with logged_duration("New game creation"): with logged_duration("New game creation"):
game = create_game( game = create_game(CreateGameParams.from_args(args))
args.campaign,
args.blue,
args.red,
args.supercarrier,
args.auto_procurement,
args.inverted,
args.cheats,
args.date,
args.restrict_weapons_by_date,
args.advanced_iads,
args.use_new_squadron_rules,
)
if args.subcommand == "lint-weapons": if args.subcommand == "lint-weapons":
lint_weapon_data_for_aircraft(AircraftType.named(args.aircraft)) lint_weapon_data_for_aircraft(AircraftType.named(args.aircraft))
return return