From 244425381dd3199a13cdc1a3d3bce5fb6f037f9e Mon Sep 17 00:00:00 2001 From: RndName Date: Fri, 6 May 2022 19:31:40 +0200 Subject: [PATCH] Fix incorrect forcegroup loading Forcegroups were not loaded correctly from preset groups during faction initialization. When a user created a new game and directly after that created another game with different factions the Forcegroups for the preset groups were still reused and therefore units which were not accessible by the faction were accidently available to the new faction. closes #2186 --- game/armedforces/armedforces.py | 6 ++---- game/armedforces/forcegroup.py | 12 ++++++++++-- game/factions/faction.py | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/game/armedforces/armedforces.py b/game/armedforces/armedforces.py index c3e9e946..ee19926a 100644 --- a/game/armedforces/armedforces.py +++ b/game/armedforces/armedforces.py @@ -12,12 +12,10 @@ if TYPE_CHECKING: class ArmedForces: - """TODO Description""" - - # All available force groups for a specific Role - forces: list[ForceGroup] + """Represents all ForceGroups which are available to the faction""" def __init__(self, faction: Faction): + self.forces: list[ForceGroup] = [] with logged_duration(f"Loading armed forces for {faction.name}"): self._load_forces(faction) diff --git a/game/armedforces/forcegroup.py b/game/armedforces/forcegroup.py index 221f936a..fd44265d 100644 --- a/game/armedforces/forcegroup.py +++ b/game/armedforces/forcegroup.py @@ -118,10 +118,18 @@ class ForceGroup: return self @classmethod - def named(cls, name: str) -> ForceGroup: + def from_preset_group(cls, name: str) -> ForceGroup: if not cls._loaded: cls._load_all() - return cls._by_name[name] + preset_group = cls._by_name[name] + # Return a copy of the PresetGroup as new ForceGroup + return ForceGroup( + name=str(preset_group.name), + units=list(preset_group.units), + statics=list(preset_group.statics), + tasks=list(preset_group.tasks), + layouts=list(preset_group.layouts), + ) def has_access_to_dcs_type(self, type: Type[DcsUnitType]) -> bool: return ( diff --git a/game/factions/faction.py b/game/factions/faction.py index 70721f8d..0668f8dc 100644 --- a/game/factions/faction.py +++ b/game/factions/faction.py @@ -216,7 +216,7 @@ class Faction: ] faction.preset_groups = [ - ForceGroup.named(n) for n in json.get("preset_groups", []) + ForceGroup.from_preset_group(g) for g in json.get("preset_groups", []) ] faction.requirements = json.get("requirements", {})