Add a preferred_start_date for campaigns.

Fixes https://github.com/dcs-liberation/dcs_liberation/issues/1079
This commit is contained in:
Dan Albert
2021-10-17 17:36:22 -07:00
parent b174e668f4
commit d5eaa4d091
5 changed files with 41 additions and 4 deletions

View File

@@ -1,11 +1,12 @@
from __future__ import annotations
import datetime
import json
import logging
from collections import Iterator
from dataclasses import dataclass
from pathlib import Path
from typing import Tuple, Dict, Any
from typing import Optional, Tuple, Dict, Any
from packaging.version import Version
import yaml
@@ -46,6 +47,7 @@ class Campaign:
recommended_player_faction: str
recommended_enemy_faction: str
recommended_start_date: Optional[datetime.date]
performance: int
data: Dict[str, Any]
path: Path
@@ -67,6 +69,23 @@ class Campaign:
f"Non-string campaign version in {path}. Parse may be incorrect."
)
version = Version(str(version_field))
start_date_raw = data.get("recommended_start_date")
# YAML automatically parses dates, but while we still support JSON campaigns we
# need to be able to handle parsing dates from strings ourselves as well.
start_date: Optional[datetime.date]
if isinstance(start_date_raw, str):
start_date = datetime.date.fromisoformat(start_date_raw)
elif isinstance(start_date_raw, datetime.date):
start_date = start_date_raw
elif start_date_raw is None:
start_date = None
else:
raise RuntimeError(
f"Invalid value for recommended_start_date in {path}: {start_date_raw}"
)
return cls(
data["name"],
f"Terrain_{sanitized_theater}",
@@ -75,6 +94,7 @@ class Campaign:
(version.major, version.minor),
data.get("recommended_player_faction", "USA 2005"),
data.get("recommended_enemy_faction", "Russia 1990"),
start_date,
data.get("performance", 0),
data,
path,

View File

@@ -115,7 +115,12 @@ VERSION = _build_version_string()
#: * You can now add "Invisible FARP" static to FOB to add helicopter slots
#:
#: Version 9.0
#: * Campaign files now define the initial squadron layouts. See TODO.
#: * Campaign files now define the initial squadron layouts. See
#: https://github.com/dcs-liberation/dcs_liberation/wiki/Custom-Campaigns.
#: * CV and LHA control points now get their names from the group name in the campaign
#: miz.
CAMPAIGN_FORMAT_VERSION = (9, 0)
#:
#: Version 9.1
#: * Campaign files can optionally define a start date with
#: `recommended_start_date: YYYY-MM-DD`.
CAMPAIGN_FORMAT_VERSION = (9, 1)