mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
Refactor the sell unit changes as requested.
It works more simply now, and also doesn't immediately sell the unit. Also adds a matching UI dialog popup for selling too many ground units.
This commit is contained in:
parent
64066bfc90
commit
ab2046a2c2
@ -342,6 +342,8 @@ class UnitsDeliveryEvent:
|
|||||||
def __init__(self, control_point: ControlPoint) -> None:
|
def __init__(self, control_point: ControlPoint) -> None:
|
||||||
self.to_cp = control_point
|
self.to_cp = control_point
|
||||||
self.units: Dict[Type[UnitType], int] = {}
|
self.units: Dict[Type[UnitType], int] = {}
|
||||||
|
self.bought_units = {}
|
||||||
|
self.sold_units = {}
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "Pending delivery to {}".format(self.to_cp)
|
return "Pending delivery to {}".format(self.to_cp)
|
||||||
@ -350,6 +352,10 @@ class UnitsDeliveryEvent:
|
|||||||
for k, v in units.items():
|
for k, v in units.items():
|
||||||
self.units[k] = self.units.get(k, 0) + v
|
self.units[k] = self.units.get(k, 0) + v
|
||||||
|
|
||||||
|
def sell(self, units: Dict[Type[UnitType], int]) -> None:
|
||||||
|
for k, v in units.items():
|
||||||
|
self.units[k] = self.units.get(k, 0) - v
|
||||||
|
|
||||||
def consume_each_order(self) -> Iterator[Tuple[Type[UnitType], int]]:
|
def consume_each_order(self) -> Iterator[Tuple[Type[UnitType], int]]:
|
||||||
while self.units:
|
while self.units:
|
||||||
yield self.units.popitem()
|
yield self.units.popitem()
|
||||||
@ -371,7 +377,16 @@ class UnitsDeliveryEvent:
|
|||||||
coalition = "Ally" if self.to_cp.captured else "Enemy"
|
coalition = "Ally" if self.to_cp.captured else "Enemy"
|
||||||
aircraft = unit_type.id
|
aircraft = unit_type.id
|
||||||
name = self.to_cp.name
|
name = self.to_cp.name
|
||||||
|
if count >= 0:
|
||||||
|
self.bought_units[unit_type] = count
|
||||||
game.message(
|
game.message(
|
||||||
f"{coalition} reinforcements: {aircraft} x {count} at {name}")
|
f"{coalition} reinforcements: {aircraft} x {count} at {name}")
|
||||||
self.to_cp.base.commision_units(self.units)
|
else:
|
||||||
|
self.sold_units[unit_type] = 0 - count
|
||||||
|
game.message(
|
||||||
|
f"{coalition} sold: {aircraft} x {0 - count} at {name}")
|
||||||
|
self.to_cp.base.commision_units(self.bought_units)
|
||||||
|
self.to_cp.base.commit_losses(self.sold_units)
|
||||||
self.units = {}
|
self.units = {}
|
||||||
|
self.bought_units = {}
|
||||||
|
self.sold_units = {}
|
||||||
|
|||||||
@ -98,7 +98,6 @@ class GlobalAircraftInventory:
|
|||||||
inventory = self.inventories[control_point]
|
inventory = self.inventories[control_point]
|
||||||
for aircraft, count in control_point.base.aircraft.items():
|
for aircraft, count in control_point.base.aircraft.items():
|
||||||
inventory.add_aircraft(aircraft, count)
|
inventory.add_aircraft(aircraft, count)
|
||||||
control_point.base.sold_units[aircraft] = 0
|
|
||||||
|
|
||||||
def for_control_point(
|
def for_control_point(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@ -29,7 +29,6 @@ class Base:
|
|||||||
self.aa: Dict[AirDefence, int] = {}
|
self.aa: Dict[AirDefence, int] = {}
|
||||||
self.commision_points: Dict[Type, float] = {}
|
self.commision_points: Dict[Type, float] = {}
|
||||||
self.strength = 1
|
self.strength = 1
|
||||||
self.sold_units = {}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def total_aircraft(self) -> int:
|
def total_aircraft(self) -> int:
|
||||||
|
|||||||
@ -117,7 +117,6 @@ class GroundPlanner:
|
|||||||
print(key)
|
print(key)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.cp.base.sold_units[key] = 0
|
|
||||||
available = self.cp.base.armor[key]
|
available = self.cp.base.armor[key]
|
||||||
while available > 0:
|
while available > 0:
|
||||||
|
|
||||||
|
|||||||
@ -61,12 +61,6 @@ class QRecruitBehaviour:
|
|||||||
unitName = QLabel("<b>" + db.unit_type_name_2(unit_type) + "</b>")
|
unitName = QLabel("<b>" + db.unit_type_name_2(unit_type) + "</b>")
|
||||||
unitName.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
|
unitName.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
|
||||||
|
|
||||||
sold_count = self.cp.base.sold_units.get(unit_type)
|
|
||||||
if sold_count is None:
|
|
||||||
sold_count = 0
|
|
||||||
if sold_count > 0:
|
|
||||||
existing_units = QLabel("<b>{} (-{})</b>".format(existing_units, sold_count))
|
|
||||||
else:
|
|
||||||
existing_units = QLabel(str(existing_units))
|
existing_units = QLabel(str(existing_units))
|
||||||
existing_units.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))
|
existing_units.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))
|
||||||
|
|
||||||
@ -123,14 +117,6 @@ class QRecruitBehaviour:
|
|||||||
unit_type in self.pending_deliveries.units and "{}".format(self.pending_deliveries.units[unit_type]) or "0"
|
unit_type in self.pending_deliveries.units and "{}".format(self.pending_deliveries.units[unit_type]) or "0"
|
||||||
))
|
))
|
||||||
|
|
||||||
sold_count = self.cp.base.sold_units.get(unit_type)
|
|
||||||
if sold_count is None:
|
|
||||||
sold_count = 0
|
|
||||||
if sold_count > 0:
|
|
||||||
self.existing_units_labels[unit_type].setText("<b>{} (-{})</b>".format(
|
|
||||||
self.cp.base.total_units_of_type(unit_type), self.cp.base.sold_units[unit_type]
|
|
||||||
))
|
|
||||||
else:
|
|
||||||
self.existing_units_labels[unit_type].setText("<b>{}</b>".format(
|
self.existing_units_labels[unit_type].setText("<b>{}</b>".format(
|
||||||
self.cp.base.total_units_of_type(unit_type)
|
self.cp.base.total_units_of_type(unit_type)
|
||||||
))
|
))
|
||||||
@ -141,14 +127,6 @@ class QRecruitBehaviour:
|
|||||||
def buy(self, unit_type: Type[UnitType]):
|
def buy(self, unit_type: Type[UnitType]):
|
||||||
price = db.PRICES[unit_type]
|
price = db.PRICES[unit_type]
|
||||||
if self.budget >= price:
|
if self.budget >= price:
|
||||||
sold_count = self.cp.base.sold_units.get(unit_type)
|
|
||||||
if sold_count is None:
|
|
||||||
sold_count = 0
|
|
||||||
if sold_count > 0:
|
|
||||||
self.cp.base.sold_units[unit_type] -= 1
|
|
||||||
self.cp.base.commision_units({unit_type: 1})
|
|
||||||
self.budget -= price
|
|
||||||
else:
|
|
||||||
self.pending_deliveries.order({unit_type: 1})
|
self.pending_deliveries.order({unit_type: 1})
|
||||||
self.budget -= price
|
self.budget -= price
|
||||||
else:
|
else:
|
||||||
@ -158,18 +136,12 @@ class QRecruitBehaviour:
|
|||||||
self.update_available_budget()
|
self.update_available_budget()
|
||||||
|
|
||||||
def sell(self, unit_type):
|
def sell(self, unit_type):
|
||||||
if self.pending_deliveries.units.get(unit_type, 0) > 0:
|
if self.pending_deliveries.units.get(unit_type, 0) > 0 or self.cp.base.total_units_of_type(unit_type) > 0:
|
||||||
price = db.PRICES[unit_type]
|
price = db.PRICES[unit_type]
|
||||||
self.budget += price
|
self.budget += price
|
||||||
self.pending_deliveries.units[unit_type] = self.pending_deliveries.units[unit_type] - 1
|
self.pending_deliveries.sell({unit_type: 1})
|
||||||
if self.pending_deliveries.units[unit_type] == 0:
|
if self.pending_deliveries.units[unit_type] == 0:
|
||||||
del self.pending_deliveries.units[unit_type]
|
del self.pending_deliveries.units[unit_type]
|
||||||
elif self.cp.base.total_units_of_type(unit_type) > 0:
|
|
||||||
price = db.PRICES[unit_type]
|
|
||||||
self.budget += price
|
|
||||||
self.cp.base.commit_losses({unit_type: 1})
|
|
||||||
self.cp.base.sold_units[unit_type] += 1
|
|
||||||
|
|
||||||
self._update_count_label(unit_type)
|
self._update_count_label(unit_type)
|
||||||
self.update_available_budget()
|
self.update_available_budget()
|
||||||
|
|
||||||
|
|||||||
@ -85,8 +85,6 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
|
|||||||
self.setLayout(main_layout)
|
self.setLayout(main_layout)
|
||||||
|
|
||||||
def buy(self, unit_type):
|
def buy(self, unit_type):
|
||||||
global_inventory = self.game_model.game.aircraft_inventory
|
|
||||||
inventory = global_inventory.for_control_point(self.cp)
|
|
||||||
if self.maximum_units > 0:
|
if self.maximum_units > 0:
|
||||||
if self.cp.unclaimed_parking(self.game_model.game) <= 0:
|
if self.cp.unclaimed_parking(self.game_model.game) <= 0:
|
||||||
logging.debug(f"No space for additional aircraft at {self.cp}.")
|
logging.debug(f"No space for additional aircraft at {self.cp}.")
|
||||||
@ -94,23 +92,13 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
|
|||||||
self, "No space for additional aircraft",
|
self, "No space for additional aircraft",
|
||||||
f"There is no parking space left at {self.cp.name} to accommodate another plane.", QMessageBox.Ok)
|
f"There is no parking space left at {self.cp.name} to accommodate another plane.", QMessageBox.Ok)
|
||||||
return
|
return
|
||||||
sold_count = self.cp.base.sold_units.get(unit_type)
|
|
||||||
if sold_count is None:
|
|
||||||
sold_count = 0
|
|
||||||
if sold_count > 0:
|
|
||||||
inventory.add_aircraft(unit_type, 1)
|
|
||||||
super().buy(unit_type)
|
super().buy(unit_type)
|
||||||
self.hangar_status.update_label()
|
self.hangar_status.update_label()
|
||||||
|
|
||||||
def sell(self, unit_type: UnitType):
|
def sell(self, unit_type: UnitType):
|
||||||
# Don't need to remove aircraft from the inventory if we're canceling
|
# Don't need to remove aircraft from the inventory if we're canceling
|
||||||
# orders.
|
# orders.
|
||||||
if self.pending_deliveries.units.get(unit_type, 0) <= 0:
|
if self.pending_deliveries.units.get(unit_type, 0) <= 0 - self.cp.base.total_units_of_type(unit_type):
|
||||||
global_inventory = self.game_model.game.aircraft_inventory
|
|
||||||
inventory = global_inventory.for_control_point(self.cp)
|
|
||||||
try:
|
|
||||||
inventory.remove_aircraft(unit_type, 1)
|
|
||||||
except ValueError:
|
|
||||||
QMessageBox.critical(
|
QMessageBox.critical(
|
||||||
self, "Could not sell aircraft",
|
self, "Could not sell aircraft",
|
||||||
f"Attempted to sell one {unit_type.id} at {self.cp.name} "
|
f"Attempted to sell one {unit_type.id} at {self.cp.name} "
|
||||||
|
|||||||
@ -5,8 +5,10 @@ from PySide2.QtWidgets import (
|
|||||||
QScrollArea,
|
QScrollArea,
|
||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
QWidget,
|
QWidget,
|
||||||
|
QMessageBox,
|
||||||
)
|
)
|
||||||
from dcs.task import PinpointStrike
|
from dcs.task import PinpointStrike
|
||||||
|
from dcs.unittype import FlyingType, UnitType
|
||||||
|
|
||||||
from game import db
|
from game import db
|
||||||
from game.theater import ControlPoint
|
from game.theater import ControlPoint
|
||||||
@ -57,3 +59,15 @@ class QArmorRecruitmentMenu(QFrame, QRecruitBehaviour):
|
|||||||
scroll.setWidget(scroll_content)
|
scroll.setWidget(scroll_content)
|
||||||
main_layout.addWidget(scroll)
|
main_layout.addWidget(scroll)
|
||||||
self.setLayout(main_layout)
|
self.setLayout(main_layout)
|
||||||
|
|
||||||
|
def sell(self, unit_type: UnitType):
|
||||||
|
# Don't need to remove aircraft from the inventory if we're canceling
|
||||||
|
# orders.
|
||||||
|
if self.pending_deliveries.units.get(unit_type, 0) <= 0 - self.cp.base.total_units_of_type(unit_type):
|
||||||
|
QMessageBox.critical(
|
||||||
|
self, "Could not sell ground unit",
|
||||||
|
f"Attempted to sell one {unit_type.id} at {self.cp.name} "
|
||||||
|
"but none are available.", QMessageBox.Ok)
|
||||||
|
return
|
||||||
|
super().sell(unit_type)
|
||||||
|
self.hangar_status.update_label()
|
||||||
Loading…
x
Reference in New Issue
Block a user