Add laser code config parsing and prop generation.

This commit is contained in:
Dan Albert 2023-07-21 22:46:39 -07:00
parent b10395715d
commit 01e4ebc706
4 changed files with 119 additions and 0 deletions

View File

@ -14,6 +14,7 @@ from dcs.unitpropertydescription import UnitPropertyDescription
from dcs.unittype import FlyingType from dcs.unittype import FlyingType
from game.data.units import UnitClass from game.data.units import UnitClass
from game.dcs.lasercodeconfig import LaserCodeConfig
from game.dcs.unittype import UnitType from game.dcs.unittype import UnitType
from game.radio.channels import ( from game.radio.channels import (
ApacheChannelNamer, ApacheChannelNamer,
@ -205,6 +206,8 @@ class AircraftType(UnitType[Type[FlyingType]]):
# when no TGP is mounted on any station. # when no TGP is mounted on any station.
has_built_in_target_pod: bool has_built_in_target_pod: bool
laser_code_configs: list[LaserCodeConfig]
_by_name: ClassVar[dict[str, AircraftType]] = {} _by_name: ClassVar[dict[str, AircraftType]] = {}
_by_unit_type: ClassVar[dict[type[FlyingType], list[AircraftType]]] = defaultdict( _by_unit_type: ClassVar[dict[type[FlyingType], list[AircraftType]]] = defaultdict(
list list
@ -486,6 +489,9 @@ class AircraftType(UnitType[Type[FlyingType]]):
can_carry_crates=data.get("can_carry_crates", aircraft.helicopter), can_carry_crates=data.get("can_carry_crates", aircraft.helicopter),
task_priorities=task_priorities, task_priorities=task_priorities,
has_built_in_target_pod=data.get("has_built_in_target_pod", False), has_built_in_target_pod=data.get("has_built_in_target_pod", False),
laser_code_configs=[
LaserCodeConfig.from_yaml(d) for d in data.get("laser_codes", [])
],
) )
def __hash__(self) -> int: def __hash__(self) -> int:

View File

@ -0,0 +1,48 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any
class LaserCodeConfig(ABC):
def __init__(self, pylon: int) -> None:
self.pylon = pylon
@staticmethod
def from_yaml(data: dict[str, Any]) -> LaserCodeConfig:
pylon = data["pylon"]
if (property_def := data.get("property")) is not None:
return SinglePropertyLaserCodeConfig(
pylon, property_def["id"], int(property_def["digits"])
)
return MultiplePropertyLaserCodeConfig(
pylon, [(d["id"], d["digit"]) for d in data["properties"]]
)
@abstractmethod
def property_dict_for_code(self, code: int) -> dict[str, int]:
...
class SinglePropertyLaserCodeConfig(LaserCodeConfig):
def __init__(self, pylon: int, property_id: str, digits: int) -> None:
super().__init__(pylon)
self.property_id = property_id
self.digits = digits
def property_dict_for_code(self, code: int) -> dict[str, int]:
return {self.property_id: code % 10**self.digits}
class MultiplePropertyLaserCodeConfig(LaserCodeConfig):
def __init__(
self, pylon: int, property_digit_mappings: list[tuple[str, int]]
) -> None:
super().__init__(pylon)
self.property_digit_mappings = property_digit_mappings
def property_dict_for_code(self, code: int) -> dict[str, int]:
d = {}
for prop_id, idx in self.property_digit_mappings:
d[prop_id] = code // 10**idx % 10
return d

0
tests/dcs/__init__.py Normal file
View File

View File

@ -0,0 +1,65 @@
from game.dcs.lasercodeconfig import (
SinglePropertyLaserCodeConfig,
MultiplePropertyLaserCodeConfig,
LaserCodeConfig,
)
def test_singlepropertylasercodeproperty() -> None:
config = SinglePropertyLaserCodeConfig(0, "code", 3)
assert config.property_dict_for_code(1688) == {"code": 688}
assert config.property_dict_for_code(1000) == {"code": 0}
assert config.property_dict_for_code(1234) == {"code": 234}
assert config.property_dict_for_code(1) == {"code": 1}
def test_multiplepropertylasercodeproperty() -> None:
config = MultiplePropertyLaserCodeConfig(
0,
[
("digit0", 0),
("digit1", 1),
("digit2", 2),
],
)
assert config.property_dict_for_code(1688) == {
"digit0": 8,
"digit1": 8,
"digit2": 6,
}
assert config.property_dict_for_code(1000) == {
"digit0": 0,
"digit1": 0,
"digit2": 0,
}
assert config.property_dict_for_code(1234) == {
"digit0": 4,
"digit1": 3,
"digit2": 2,
}
assert config.property_dict_for_code(1) == {"digit0": 1, "digit1": 0, "digit2": 0}
def test_lasercodeconfig_from_yaml() -> None:
config = LaserCodeConfig.from_yaml(
{"pylon": 0, "property": {"id": "code", "digits": 3}}
)
assert config.pylon == 0
assert config.property_dict_for_code(1688) == {"code": 688}
config = LaserCodeConfig.from_yaml(
{
"pylon": 1,
"properties": [
{"id": "digit0", "digit": 0},
{"id": "digit1", "digit": 1},
{"id": "digit2", "digit": 2},
],
}
)
assert config.pylon == 1
assert config.property_dict_for_code(1688) == {
"digit0": 8,
"digit1": 8,
"digit2": 6,
}