Allow using yaml for campaign definitions.

JSON continues to be supported as well. No additional work needed here,
just needed to allow both.
This commit is contained in:
Dan Albert 2021-08-10 18:21:52 -07:00
parent 6c7b62b8b1
commit 88d52003b3
4 changed files with 20 additions and 17 deletions

View File

@ -244,7 +244,7 @@ class ConflictTheater:
raise KeyError(f"Cannot find ControlPoint with ID {id}") raise KeyError(f"Cannot find ControlPoint with ID {id}")
@staticmethod @staticmethod
def from_json(directory: Path, data: Dict[str, Any]) -> ConflictTheater: def from_file_data(directory: Path, data: Dict[str, Any]) -> ConflictTheater:
theaters = { theaters = {
"Caucasus": CaucasusTheater, "Caucasus": CaucasusTheater,
"Nevada": NevadaTheater, "Nevada": NevadaTheater,

View File

@ -231,7 +231,7 @@ 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(persistency.base_path())) inject_custom_payloads(Path(persistency.base_path()))
campaign = Campaign.from_json(campaign_path) campaign = Campaign.from_file(campaign_path)
generator = GameGenerator( generator = GameGenerator(
FACTIONS[blue], FACTIONS[blue],
FACTIONS[red], FACTIONS[red],

View File

@ -7,6 +7,7 @@ from pathlib import Path
from typing import Any, Dict, List, Union, Tuple from typing import Any, Dict, List, Union, Tuple
import packaging.version import packaging.version
import yaml
from PySide2 import QtGui from PySide2 import QtGui
from PySide2.QtCore import QItemSelectionModel, QModelIndex, Qt from PySide2.QtCore import QItemSelectionModel, QModelIndex, Qt
from PySide2.QtGui import QStandardItem, QStandardItemModel from PySide2.QtGui import QStandardItem, QStandardItemModel
@ -41,8 +42,11 @@ class Campaign:
path: Path path: Path
@classmethod @classmethod
def from_json(cls, path: Path) -> Campaign: def from_file(cls, path: Path) -> Campaign:
with path.open() as campaign_file: with path.open() as campaign_file:
if path.suffix == ".yaml":
data = yaml.safe_load(campaign_file)
else:
data = json.load(campaign_file) data = json.load(campaign_file)
sanitized_theater = data["theater"].replace(" ", "") sanitized_theater = data["theater"].replace(" ", "")
@ -68,7 +72,7 @@ class Campaign:
) )
def load_theater(self) -> ConflictTheater: def load_theater(self) -> ConflictTheater:
return ConflictTheater.from_json(self.path.parent, self.data) return ConflictTheater.from_file_data(self.path.parent, self.data)
@property @property
def is_out_of_date(self) -> bool: def is_out_of_date(self) -> bool:
@ -105,7 +109,7 @@ def load_campaigns() -> List[Campaign]:
for path in campaign_dir.glob("*.json"): for path in campaign_dir.glob("*.json"):
try: try:
logging.debug(f"Loading campaign from {path}...") logging.debug(f"Loading campaign from {path}...")
campaign = Campaign.from_json(path) campaign = Campaign.from_file(path)
campaigns.append(campaign) campaigns.append(campaign)
except RuntimeError: except RuntimeError:
logging.exception(f"Unable to load campaign from {path}") logging.exception(f"Unable to load campaign from {path}")

View File

@ -1,11 +1,10 @@
{ ---
"name": "Persian Gulf - Battle of Abu Dhabi", name: Persian Gulf - Battle of Abu Dhabi
"theater": "Persian Gulf", theater: Persian Gulf
"authors": "Colonel Panic", authors: Colonel Panic
"recommended_player_faction": "Iran 2015", recommended_player_faction: Iran 2015
"recommended_enemy_faction": "United Arab Emirates 2015", recommended_enemy_faction: United Arab Emirates 2015
"description": "<p>You have managed to establish a foothold near Ras Al Khaima. Continue pushing south.</p>", description: <p>You have managed to establish a foothold near Ras Al Khaima. Continue pushing south.</p>
"miz": "battle_of_abu_dhabi.miz", miz: battle_of_abu_dhabi.miz
"performance": 2, performance: 2
"version": "8.0" version": "8.0"
}