From b4742ad54c8c04d455bc581b867507c7718afd91 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Fri, 18 Feb 2022 17:58:34 -0800 Subject: [PATCH] Move faction cache out of db.py. --- game/db.py | 7 ------- game/factions/__init__.py | 4 ++++ gen/coastal/coastal_group_generator.py | 5 +++-- gen/defenses/armor_group_generator.py | 5 +++-- gen/fleet/ship_group_generator.py | 16 ++++++++-------- gen/missiles/missiles_group_generator.py | 5 +++-- qt_ui/main.py | 2 +- qt_ui/windows/newgame/QNewGameWizard.py | 18 +++++++++--------- 8 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 game/factions/__init__.py diff --git a/game/db.py b/game/db.py index a0f5b21e..ce0fee4e 100644 --- a/game/db.py +++ b/game/db.py @@ -27,7 +27,6 @@ from dcs.vehicles import ( ) # PATCH pydcs data with MODS -from game.factions.faction_loader import FactionLoader """ ---------- BEGINNING OF CONFIGURATION SECTION @@ -55,12 +54,6 @@ For example, player accessible Hornet is called `FA_18C_hornet`, and MANPAD Igla # to be cheap enough to repair with a single turn's income. RUNWAY_REPAIR_COST = 100 -""" -Units separated by country. -country : DCS Country name -""" -FACTIONS = FactionLoader() - """ Possible time periods for new games diff --git a/game/factions/__init__.py b/game/factions/__init__.py new file mode 100644 index 00000000..fa5dcc45 --- /dev/null +++ b/game/factions/__init__.py @@ -0,0 +1,4 @@ +from .faction import Faction +from .faction_loader import FactionLoader + +FACTIONS = FactionLoader() diff --git a/gen/coastal/coastal_group_generator.py b/gen/coastal/coastal_group_generator.py index 0d263e3b..1bfdd86b 100644 --- a/gen/coastal/coastal_group_generator.py +++ b/gen/coastal/coastal_group_generator.py @@ -4,7 +4,8 @@ from typing import Optional from dcs.unitgroup import VehicleGroup -from game import db, Game +from game import Game +from game.factions import FACTIONS from game.theater.theatergroundobject import CoastalSiteGroundObject from gen.coastal.silkworm import SilkwormGenerator @@ -21,7 +22,7 @@ def generate_coastal_group( :return: The generated group, or None if this faction does not support coastal defenses. """ - faction = db.FACTIONS[faction_name] + faction = FACTIONS[faction_name] if len(faction.coastal_defenses) > 0: generators = faction.coastal_defenses if len(generators) > 0: diff --git a/gen/defenses/armor_group_generator.py b/gen/defenses/armor_group_generator.py index 1ed04e06..435e76d6 100644 --- a/gen/defenses/armor_group_generator.py +++ b/gen/defenses/armor_group_generator.py @@ -3,9 +3,10 @@ from typing import Optional from dcs.unitgroup import VehicleGroup -from game import db, Game +from game import Game from game.data.groundunitclass import GroundUnitClass from game.dcs.groundunittype import GroundUnitType +from game.factions import FACTIONS from game.theater.theatergroundobject import VehicleGroupGroundObject from gen.defenses.armored_group_generator import ( ArmoredGroupGenerator, @@ -27,7 +28,7 @@ def generate_armor_group( GroundUnitClass.Tank, ) possible_unit = [ - u for u in db.FACTIONS[faction].frontline_units if u.unit_class in armor_types + u for u in FACTIONS[faction].frontline_units if u.unit_class in armor_types ] if len(possible_unit) > 0: unit_type = random.choice(possible_unit) diff --git a/gen/fleet/ship_group_generator.py b/gen/fleet/ship_group_generator.py index 1cd8338c..873dd360 100644 --- a/gen/fleet/ship_group_generator.py +++ b/gen/fleet/ship_group_generator.py @@ -2,14 +2,14 @@ from __future__ import annotations import logging import random -from typing import TYPE_CHECKING, Optional +from typing import Optional, TYPE_CHECKING from dcs.unitgroup import ShipGroup -from game import db +from game.factions import FACTIONS from game.theater.theatergroundobject import ( - LhaGroundObject, CarrierGroundObject, + LhaGroundObject, ShipGroundObject, ) from gen.fleet.carrier_group import CarrierGroupGenerator @@ -21,10 +21,10 @@ from gen.fleet.dd_group import ( from gen.fleet.lacombattanteII import LaCombattanteIIGroupGenerator from gen.fleet.lha_group import LHAGroupGenerator from gen.fleet.ru_dd_group import ( - RussianNavyGroupGenerator, GrishaGroupGenerator, - MolniyaGroupGenerator, KiloSubGroupGenerator, + MolniyaGroupGenerator, + RussianNavyGroupGenerator, TangoSubGroupGenerator, ) from gen.fleet.schnellboot import SchnellbootGroupGenerator @@ -59,7 +59,7 @@ def generate_ship_group( This generate a ship group :return: The generated group, or None if this faction does not support ships. """ - faction = db.FACTIONS[faction_name] + faction = FACTIONS[faction_name] if len(faction.navy_generators) > 0: gen = random.choice(faction.navy_generators) if gen in SHIP_MAP.keys(): @@ -86,7 +86,7 @@ def generate_carrier_group( :param ground_object: The ground object which will own the ship group :return: The generated group. """ - generator = CarrierGroupGenerator(game, ground_object, db.FACTIONS[faction]) + generator = CarrierGroupGenerator(game, ground_object, FACTIONS[faction]) generator.generate() return generator.get_generated_group() @@ -101,6 +101,6 @@ def generate_lha_group( :param ground_object: The ground object which will own the ship group :return: The generated group. """ - generator = LHAGroupGenerator(game, ground_object, db.FACTIONS[faction]) + generator = LHAGroupGenerator(game, ground_object, FACTIONS[faction]) generator.generate() return generator.get_generated_group() diff --git a/gen/missiles/missiles_group_generator.py b/gen/missiles/missiles_group_generator.py index 63f1bb80..15415f48 100644 --- a/gen/missiles/missiles_group_generator.py +++ b/gen/missiles/missiles_group_generator.py @@ -4,7 +4,8 @@ from typing import Optional from dcs.unitgroup import VehicleGroup -from game import db, Game +from game import Game +from game.factions import FACTIONS from game.theater.theatergroundobject import MissileSiteGroundObject from gen.missiles.scud_site import ScudGenerator from gen.missiles.v1_group import V1GroupGenerator @@ -19,7 +20,7 @@ def generate_missile_group( This generate a missiles group :return: Nothing, but put the group reference inside the ground object """ - faction = db.FACTIONS[faction_name] + faction = FACTIONS[faction_name] if len(faction.missiles) > 0: generators = faction.missiles if len(generators) > 0: diff --git a/qt_ui/main.py b/qt_ui/main.py index a769ce9c..0447931e 100644 --- a/qt_ui/main.py +++ b/qt_ui/main.py @@ -15,8 +15,8 @@ from dcs.payloads import PayloadDirectories from game import Game, VERSION, persistency from game.campaignloader.campaign import Campaign from game.data.weapons import Pylon, Weapon, WeaponGroup -from game.db import FACTIONS from game.dcs.aircrafttype import AircraftType +from game.factions import FACTIONS from game.profiling import logged_duration from game.server import EventStream, GameContext, Server from game.settings import Settings diff --git a/qt_ui/windows/newgame/QNewGameWizard.py b/qt_ui/windows/newgame/QNewGameWizard.py index 007a0896..f89f4a17 100644 --- a/qt_ui/windows/newgame/QNewGameWizard.py +++ b/qt_ui/windows/newgame/QNewGameWizard.py @@ -11,7 +11,7 @@ from jinja2 import Environment, FileSystemLoader, select_autoescape from game import db from game.campaignloader.campaign import Campaign -from game.factions.faction import Faction +from game.factions import FACTIONS, Faction from game.settings import Settings from game.theater.start_generator import GameGenerator, GeneratorSettings, ModSettings from qt_ui.widgets.QLiberationCalendar import QLiberationCalendar @@ -179,7 +179,7 @@ class FactionSelection(QtWidgets.QWizardPage): blueFaction = QtWidgets.QLabel("Player Faction :") self.blueFactionSelect = QtWidgets.QComboBox() - for f in db.FACTIONS: + for f in FACTIONS: self.blueFactionSelect.addItem(f) blueFaction.setBuddy(self.blueFactionSelect) @@ -195,7 +195,7 @@ class FactionSelection(QtWidgets.QWizardPage): self.redFactionDescription.setReadOnly(True) # Setup default selected factions - for i, r in enumerate(db.FACTIONS): + for i, r in enumerate(FACTIONS): self.redFactionSelect.addItem(r) if r == "Russia 1990": self.redFactionSelect.setCurrentIndex(i) @@ -241,10 +241,10 @@ class FactionSelection(QtWidgets.QWizardPage): self.blueFactionSelect.clear() self.redFactionSelect.clear() - for f in db.FACTIONS: + for f in FACTIONS: self.blueFactionSelect.addItem(f) - for i, r in enumerate(db.FACTIONS): + for i, r in enumerate(FACTIONS): self.redFactionSelect.addItem(r) if r == campaign.recommended_enemy_faction: self.redFactionSelect.setCurrentIndex(i) @@ -255,8 +255,8 @@ class FactionSelection(QtWidgets.QWizardPage): def updateUnitRecap(self): - red_faction = db.FACTIONS[self.redFactionSelect.currentText()] - blue_faction = db.FACTIONS[self.blueFactionSelect.currentText()] + red_faction = FACTIONS[self.redFactionSelect.currentText()] + blue_faction = FACTIONS[self.blueFactionSelect.currentText()] template = jinja_env.get_template("factiontemplate_EN.j2") @@ -268,11 +268,11 @@ class FactionSelection(QtWidgets.QWizardPage): @property def selected_blue_faction(self) -> Faction: - return db.FACTIONS[self.blueFactionSelect.currentText()] + return FACTIONS[self.blueFactionSelect.currentText()] @property def selected_red_faction(self) -> Faction: - return db.FACTIONS[self.redFactionSelect.currentText()] + return FACTIONS[self.redFactionSelect.currentText()] class TheaterConfiguration(QtWidgets.QWizardPage):