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:
SimonC6R 2021-01-05 00:26:40 +00:00
parent 64066bfc90
commit ab2046a2c2
7 changed files with 47 additions and 61 deletions

View File

@ -342,6 +342,8 @@ class UnitsDeliveryEvent:
def __init__(self, control_point: ControlPoint) -> None:
self.to_cp = control_point
self.units: Dict[Type[UnitType], int] = {}
self.bought_units = {}
self.sold_units = {}
def __str__(self) -> str:
return "Pending delivery to {}".format(self.to_cp)
@ -350,6 +352,10 @@ class UnitsDeliveryEvent:
for k, v in units.items():
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]]:
while self.units:
yield self.units.popitem()
@ -371,7 +377,16 @@ class UnitsDeliveryEvent:
coalition = "Ally" if self.to_cp.captured else "Enemy"
aircraft = unit_type.id
name = self.to_cp.name
game.message(
f"{coalition} reinforcements: {aircraft} x {count} at {name}")
self.to_cp.base.commision_units(self.units)
if count >= 0:
self.bought_units[unit_type] = count
game.message(
f"{coalition} reinforcements: {aircraft} x {count} at {name}")
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.bought_units = {}
self.sold_units = {}

View File

@ -98,7 +98,6 @@ class GlobalAircraftInventory:
inventory = self.inventories[control_point]
for aircraft, count in control_point.base.aircraft.items():
inventory.add_aircraft(aircraft, count)
control_point.base.sold_units[aircraft] = 0
def for_control_point(
self,

View File

@ -29,7 +29,6 @@ class Base:
self.aa: Dict[AirDefence, int] = {}
self.commision_points: Dict[Type, float] = {}
self.strength = 1
self.sold_units = {}
@property
def total_aircraft(self) -> int:

View File

@ -117,7 +117,6 @@ class GroundPlanner:
print(key)
continue
self.cp.base.sold_units[key] = 0
available = self.cp.base.armor[key]
while available > 0:

View File

@ -61,13 +61,7 @@ class QRecruitBehaviour:
unitName = QLabel("<b>" + db.unit_type_name_2(unit_type) + "</b>")
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))
amount_bought = QLabel("<b>{}</b>".format(str(scheduled_units)))
@ -123,17 +117,9 @@ class QRecruitBehaviour:
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.cp.base.total_units_of_type(unit_type)
))
self.existing_units_labels[unit_type].setText("<b>{}</b>".format(
self.cp.base.total_units_of_type(unit_type)
))
def update_available_budget(self) -> None:
GameUpdateSignal.get_instance().updateBudget(self.game_model.game)
@ -141,16 +127,8 @@ class QRecruitBehaviour:
def buy(self, unit_type: Type[UnitType]):
price = db.PRICES[unit_type]
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.budget -= price
self.pending_deliveries.order({unit_type: 1})
self.budget -= price
else:
# TODO : display modal warning
logging.info("Not enough money !")
@ -158,18 +136,12 @@ class QRecruitBehaviour:
self.update_available_budget()
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]
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:
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_available_budget()

View File

@ -85,8 +85,6 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
self.setLayout(main_layout)
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.cp.unclaimed_parking(self.game_model.game) <= 0:
logging.debug(f"No space for additional aircraft at {self.cp}.")
@ -94,29 +92,19 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
self, "No space for additional aircraft",
f"There is no parking space left at {self.cp.name} to accommodate another plane.", QMessageBox.Ok)
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)
self.hangar_status.update_label()
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:
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(
self, "Could not sell aircraft",
f"Attempted to sell one {unit_type.id} at {self.cp.name} "
"but none are available. Are all aircraft currently "
"assigned to a mission?", QMessageBox.Ok)
return
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 aircraft",
f"Attempted to sell one {unit_type.id} at {self.cp.name} "
"but none are available. Are all aircraft currently "
"assigned to a mission?", QMessageBox.Ok)
return
super().sell(unit_type)
self.hangar_status.update_label()

View File

@ -5,8 +5,10 @@ from PySide2.QtWidgets import (
QScrollArea,
QVBoxLayout,
QWidget,
QMessageBox,
)
from dcs.task import PinpointStrike
from dcs.unittype import FlyingType, UnitType
from game import db
from game.theater import ControlPoint
@ -57,3 +59,15 @@ class QArmorRecruitmentMenu(QFrame, QRecruitBehaviour):
scroll.setWidget(scroll_content)
main_layout.addWidget(scroll)
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()