Alter the beacon format to be keyed by ID.

ID based lookup will be used for finding ILS and TACAN beacons from the
pydcs data.
This commit is contained in:
Dan Albert
2022-09-08 20:43:59 -07:00
committed by Raffson
parent 2aab7c34be
commit 2c53f7952f
11 changed files with 500 additions and 476 deletions

View File

@@ -1,12 +1,13 @@
from dataclasses import dataclass
from enum import auto, IntEnum
import json
from collections.abc import Iterator
from dataclasses import dataclass
from enum import IntEnum, auto
from pathlib import Path
from typing import Iterable, Optional
from typing import Optional
from game.radio.radios import RadioFrequency
from game.radio.tacan import TacanBand, TacanChannel
from game.theater import ConflictTheater
BEACONS_RESOURCE_PATH = Path("resources/dcs/beacons")
@@ -65,10 +66,32 @@ class Beacon:
return TacanChannel(self.channel, TacanBand.X)
def load_beacons_for_terrain(name: str) -> Iterable[Beacon]:
beacons_file = BEACONS_RESOURCE_PATH / f"{name.lower()}.json"
if not beacons_file.exists():
raise RuntimeError(f"Beacon file {beacons_file.resolve()} is missing")
class Beacons:
_by_terrain: dict[str, dict[str, Beacon]] = {}
for beacon in json.loads(beacons_file.read_text()):
yield Beacon(**beacon)
@classmethod
def _load_for_theater_if_needed(cls, theater: ConflictTheater) -> None:
if theater.terrain.name in cls._by_terrain:
return
beacons_file = BEACONS_RESOURCE_PATH / f"{theater.terrain.name.lower()}.json"
if not beacons_file.exists():
raise RuntimeError(f"Beacon file {beacons_file.resolve()} is missing")
beacons = {}
for bid, beacon in json.loads(beacons_file.read_text()).items():
beacons[bid] = Beacon(**beacon)
cls._by_terrain[theater.terrain.name] = beacons
@classmethod
def _dict_for_theater(cls, theater: ConflictTheater) -> dict[str, Beacon]:
cls._load_for_theater_if_needed(theater)
return cls._by_terrain[theater.terrain.name]
@classmethod
def iter_theater(cls, theater: ConflictTheater) -> Iterator[Beacon]:
yield from cls._dict_for_theater(theater).values()
@classmethod
def with_id(cls, beacon_id: str, theater: ConflictTheater) -> Beacon:
return cls._dict_for_theater(theater)[beacon_id]

View File

@@ -23,7 +23,7 @@ from game.theater.bullseye import Bullseye
from game.unitmap import UnitMap
from .airconflictdescription import AirConflictDescription
from .airsupportgenerator import AirSupportGenerator
from .beacons import load_beacons_for_terrain
from .beacons import Beacons
from .briefinggenerator import BriefingGenerator, MissionInfoGenerator
from .cargoshipgenerator import CargoShipGenerator
from .convoygenerator import ConvoyGenerator
@@ -169,8 +169,7 @@ class MissionGenerator:
Dedup beacon/radio frequencies, since some maps have some frequencies
used multiple times.
"""
beacons = load_beacons_for_terrain(self.game.theater.terrain.name)
for beacon in beacons:
for beacon in Beacons.iter_theater(self.game.theater):
unique_map_frequencies.add(beacon.frequency)
if beacon.is_tacan:
if beacon.channel is None: