Load campaign data lazily.

Error checking comes later, but the new game wizard opens much faster by
not spending time creating theaters it doesn't need.

Fixes https://github.com/Khopa/dcs_liberation/issues/469
This commit is contained in:
Dan Albert 2020-11-26 22:14:23 -08:00
parent 5d4fccd438
commit fd454dce74
5 changed files with 16 additions and 16 deletions

View File

@ -128,7 +128,7 @@ def parse_args() -> argparse.Namespace:
def create_game(campaign_path: Path, blue: str, red: str,
supercarrier: bool) -> Game:
campaign = Campaign.from_json(campaign_path)
generator = GameGenerator(blue, red, campaign.theater,
generator = GameGenerator(blue, red, campaign.load_theater(),
Settings(supercarrier=supercarrier),
start_date=datetime.today(),
starting_budget=650,

View File

@ -4,7 +4,7 @@ import json
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import List
from typing import Any, Dict, List
from PySide2 import QtGui
from PySide2.QtCore import QItemSelectionModel
@ -21,7 +21,8 @@ class Campaign:
icon_name: str
authors: str
description: str
theater: ConflictTheater
data: Dict[str, Any]
path: Path
@classmethod
def from_json(cls, path: Path) -> Campaign:
@ -29,10 +30,17 @@ class Campaign:
data = json.load(campaign_file)
sanitized_theater = data["theater"].replace(" ", "")
return cls(data["name"], f"Terrain_{sanitized_theater}",
data.get("authors", "???"),
data.get("description", ""),
ConflictTheater.from_json(path.parent, data))
return cls(
data["name"],
f"Terrain_{sanitized_theater}",
data.get("authors", "???"),
data.get("description", ""),
data,
path
)
def load_theater(self) -> ConflictTheater:
return ConflictTheater.from_json(self.path.parent, self.data)
def load_campaigns() -> List[Campaign]:

View File

@ -58,7 +58,7 @@ class NewGameWizard(QtWidgets.QWizard):
if selectedCampaign is None:
selectedCampaign = self.campaigns[0]
conflictTheater = selectedCampaign.theater
conflictTheater = selectedCampaign.load_theater()
timePeriod = db.TIME_PERIODS[list(db.TIME_PERIODS.keys())[self.field("timePeriod")]]
midGame = self.field("midGame")

View File

@ -1,7 +1,3 @@
<strong>Author(s): {{ campaign.authors }}</strong>
<br/>
<br/>
<strong>Number of control points:</strong> {{ campaign.theater.controlpoints|length }}
<br/>
{{ campaign.description|safe }}

View File

@ -1,7 +1,3 @@
<strong>Auteur(s) : {{ campaign.authors }}</strong>
<br/>
<br/>
<strong>Nombre de points :</strong> {{ campaign.theater.controlpoints|length }}
<br/>
{{ campaign.description|safe }}