diff --git a/game/db.py b/game/db.py index 432d23dc..d09ba7eb 100644 --- a/game/db.py +++ b/game/db.py @@ -149,6 +149,7 @@ from dcs.vehicles import ( ) import pydcs_extensions.frenchpack.frenchpack as frenchpack +from game.factions import faction_loader from game.factions.australia_2005 import Australia_2005 from game.factions.bluefor_coldwar import BLUEFOR_COLDWAR from game.factions.bluefor_coldwar_a4 import BLUEFOR_COLDWAR_A4 @@ -156,6 +157,7 @@ from game.factions.bluefor_coldwar_mods import BLUEFOR_COLDWAR_MODS from game.factions.bluefor_modern import BLUEFOR_MODERN from game.factions.canada_2005 import Canada_2005 from game.factions.china_2010 import China_2010 +from game.factions.faction import Faction from game.factions.france_1995 import France_1995 from game.factions.france_2005 import France_2005 from game.factions.france_modded import France_2005_Modded @@ -961,7 +963,9 @@ CARRIER_TAKEOFF_BAN: List[Type[FlyingType]] = [ Units separated by country. country : DCS Country name """ -FACTIONS: Dict[str, Dict[str, Any]] = { +FACTIONS : [Faction] = faction_loader.load_factions() + +FACTIONS3: Dict[str, Dict[str, Any]] = { "Bluefor Modern": BLUEFOR_MODERN, "Bluefor Cold War 1970s": BLUEFOR_COLDWAR, diff --git a/game/factions/faction.py b/game/factions/faction.py index 33794f5e..be135ce1 100644 --- a/game/factions/faction.py +++ b/game/factions/faction.py @@ -2,11 +2,12 @@ import logging from dataclasses import dataclass from typing import Optional +import dcs from dcs.planes import PlaneType, plane_map from dcs.unittype import VehicleType, UnitType from dcs.vehicles import vehicle_map, Armor, Unarmed, Infantry, Fortification, Artillery, AirDefence -from game.data.doctrine import Doctrine +from game.data.doctrine import Doctrine, MODERN_DOCTRINE, COLDWAR_DOCTRINE, WWII_DOCTRINE @dataclass @@ -36,6 +37,9 @@ class Faction: # Infantry units used infantry_units: [VehicleType] + # Logistics units used + logistics_units: [VehicleType] + # List of units that can be deployed as SHORAD shorad_units: [VehicleType] @@ -70,7 +74,26 @@ class Faction: doctrine: Doctrine def __init__(self): - pass + self.country = "" + self.name = "" + self.aircrafts = [] + self.awacs = [] + self.tankers = [] + self.frontline_units = [] + self.artillery_units = [] + self.infantry_units = [] + self.logistics_units = [] + self.shorad_units = [] + self.sams = [] + self.requirements = {} + self.carrier_names = [] + self.lha_names = [] + self.navy_generators = [] + self.destroyers = [] + self.cruisers = [] + self.has_jtac = False + self.jtac_unit = "" + self.doctrine = None @classmethod def from_json(cls, json): @@ -80,42 +103,63 @@ class Faction: faction.country = json.get("country", "USA") faction.name = json.get("name", "???") - faction.aircrafts = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("aircrafts", [])] is not None] - faction.awacs = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("awacs", [])] is not None] - faction.tankers = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("tankers", [])] is not None] + faction.aircrafts = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("aircrafts", [])] if f is not None] + faction.awacs = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("awacs", [])] if f is not None] + faction.tankers = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("tankers", [])] if f is not None] + + faction.frontline_units = [f for f in [vehicle_loader(vehicle) for vehicle in json.get("frontline_units", [])] if f is not None] + faction.artillery_units = [f for f in [vehicle_loader(vehicle) for vehicle in json.get("artillery_units", [])] if f is not None] + faction.infantry_units = [f for f in [vehicle_loader(vehicle) for vehicle in json.get("infantry_units", [])] if f is not None] + faction.logistics_units = [f for f in [vehicle_loader(vehicle) for vehicle in json.get("logistics_units", [])] if f is not None] + faction.shorad_units = [f for f in [vehicle_loader(vehicle) for vehicle in json.get("shorad_units", [])] if f is not None] + + faction.sams = json.get("sams", []) + faction.name = json.get("requirements", {}) + + faction.carrier_names = json.get("carrier_names", []) + faction.lha_names = json.get("lha_names", []) + faction.navy_generators = json.get("navy_generators", []) + faction.destroyers = [f for f in [ship_loader(vehicle) for vehicle in json.get("destroyers", [])] if f is not None] + faction.cruisers = [f for f in [ship_loader(vehicle) for vehicle in json.get("cruisers", [])] if f is not None] + faction.has_jtac = json.get("has_jtac", False) + faction.jtac_unit = json.get("jtac_unit", "") + + # Load doctrine + doctrine = json.get("doctrine", "modern") + if doctrine == "modern": + faction.doctrine = MODERN_DOCTRINE + if doctrine == "coldwar": + faction.doctrine = COLDWAR_DOCTRINE + else: + faction.doctrine = WWII_DOCTRINE + + return faction + + @property + def units(self): + return self.infantry_units + self.aircrafts + self.awacs + self.artillery_units + self.frontline_units + self.tankers + self.logistics_units -def aircraft_loader(aircraft: str) -> Optional[PlaneType]: +def unit_loader(unit: str, class_repository:[]) -> Optional[PlaneType]: """ - Find aircraft by name - :param aircraft: Aircraft name as string - :return: The aircraft as a PyDCS type + Find unit by name + :param unit: Unit name as string + :return: The unit as a PyDCS type """ - if aircraft in plane_map.keys(): - return plane_map[aircraft] + if unit in plane_map.keys(): + return plane_map[unit] else: - for mother_class in [PlaneType, Unarmed, Infantry, Armor, AirDefence, Artillery, Fortification]: - if getattr(mother_class, vehicle) is not None: - return getattr(mother_class, vehicle) - logging.info("FACTION ERROR : Unable to find " + aircraft + " in pydcs") + for mother_class in class_repository: + if getattr(mother_class, unit, None) is not None: + return getattr(mother_class, unit) + logging.info("FACTION ERROR : Unable to find " + unit + " in pydcs") + print("FACTION ERROR : Unable to find " + unit + " in pydcs") return None -def vehicle_loader(vehicle: str) -> Optional[VehicleType]: - """ - Find vehicle by name - :param vehicle: Vehicle name as string - :return: The vehicle as a PyDCS type - """ - if vehicle in plane_map.keys(): - return plane_map[vehicle] - else: - for mother_class in [Armor, Unarmed, Infantry, Armor, AirDefence, Artillery, Fortification]: - if getattr(mother_class, vehicle) is not None: - return getattr(mother_class, vehicle) - logging.info("FACTION ERROR : Unable to find " + vehicle + " in pydcs") - return None -vehicle_map +aircraft_loader = lambda x: unit_loader(x, [dcs.planes, dcs.helicopters]) +vehicle_loader = lambda x: unit_loader(x, [Infantry, Unarmed, Armor, AirDefence, Artillery]) +ship_loader = lambda x: unit_loader(x, [dcs.ships]) diff --git a/game/factions/faction_loader.py b/game/factions/faction_loader.py index a743c177..6b439b9e 100644 --- a/game/factions/faction_loader.py +++ b/game/factions/faction_loader.py @@ -2,6 +2,8 @@ import json import os import logging +from game.factions.faction import Faction + FACTION_DIRECTORY = "./resources/factions/" def load_factions() -> {}: @@ -11,16 +13,19 @@ def load_factions() -> {}: factions = {} for f in files: + print(f) path = os.path.join(FACTION_DIRECTORY, f) logging.info("Loading faction" + path) - try: - with open(path, "r") as fdata: - data = json.load(fdata) - factions[data["name"]] = data - logging.info("Loaded faction : " + path) - except: - logging.error("Unable to load faction : " + path) + #try: + with open(path, "r") as fdata: + data = json.load(fdata) + factions[data["name"]] = Faction.from_json(data) + logging.info("Loaded faction : " + path) + #except Exception as e: + # print(e) + # logging.error("Unable to load faction : " + path) + print(factions) return factions diff --git a/qt_ui/windows/newgame/QNewGameWizard.py b/qt_ui/windows/newgame/QNewGameWizard.py index 9f49e8d1..04535aae 100644 --- a/qt_ui/windows/newgame/QNewGameWizard.py +++ b/qt_ui/windows/newgame/QNewGameWizard.py @@ -200,8 +200,8 @@ class FactionSelection(QtWidgets.QWizardPage): red_faction = db.FACTIONS[self.redFactionSelect.currentText()] blue_faction = db.FACTIONS[self.blueFactionSelect.currentText()] - red_units = red_faction["units"] - blue_units = blue_faction["units"] + red_units = red_faction.aircrafts + blue_units = blue_faction.aircrafts blue_txt = "" for u in blue_units: @@ -218,16 +218,16 @@ class FactionSelection(QtWidgets.QWizardPage): self.redSideRecap.setText(red_txt) has_mod = False - if "requirements" in red_faction.keys(): + if len(red_faction.requirements.keys()) > 0: has_mod = True - for mod in red_faction["requirements"].keys(): - self.requiredMods.setText(self.requiredMods.text() + "\n