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}")
@staticmethod
def from_json(directory: Path, data: Dict[str, Any]) -> ConflictTheater:
def from_file_data(directory: Path, data: Dict[str, Any]) -> ConflictTheater:
theaters = {
"Caucasus": CaucasusTheater,
"Nevada": NevadaTheater,

View File

@ -231,7 +231,7 @@ def create_game(
# for loadouts) without saving the generated campaign and reloading it the normal
# way.
inject_custom_payloads(Path(persistency.base_path()))
campaign = Campaign.from_json(campaign_path)
campaign = Campaign.from_file(campaign_path)
generator = GameGenerator(
FACTIONS[blue],
FACTIONS[red],

View File

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

View File

@ -1,11 +1,10 @@
{
"name": "Persian Gulf - Battle of Abu Dhabi",
"theater": "Persian Gulf",
"authors": "Colonel Panic",
"recommended_player_faction": "Iran 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>",
"miz": "battle_of_abu_dhabi.miz",
"performance": 2,
"version": "8.0"
}
---
name: Persian Gulf - Battle of Abu Dhabi
theater: Persian Gulf
authors: Colonel Panic
recommended_player_faction: Iran 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>
miz: battle_of_abu_dhabi.miz
performance: 2
version": "8.0"