diff --git a/game/event/event.py b/game/event/event.py index c00ab477..ea7702c9 100644 --- a/game/event/event.py +++ b/game/event/event.py @@ -350,6 +350,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() @@ -366,12 +370,30 @@ class UnitsDeliveryEvent: f"Refunding {count} {unit_type.id} at {self.to_cp.name}") game.adjust_budget(price * count, player=self.to_cp.captured) + def available_next_turn(self, unit_type: Type[UnitType]) -> int: + pending_units = self.units.get(unit_type) + if pending_units is None: + pending_units = 0 + current_units = self.to_cp.base.total_units_of_type(unit_type) + return pending_units + current_units + def process(self, game: Game) -> None: + bought_units: Dict[Type[UnitType], int] = {} + sold_units: Dict[Type[UnitType], int] = {} for unit_type, count in self.units.items(): 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: + bought_units[unit_type] = count + game.message( + f"{coalition} reinforcements: {aircraft} x {count} at {name}") + else: + sold_units[unit_type] = -count + game.message( + f"{coalition} sold: {aircraft} x {-count} at {name}") + self.to_cp.base.commision_units(bought_units) + self.to_cp.base.commit_losses(sold_units) self.units = {} + bought_units = {} + sold_units = {} diff --git a/qt_ui/windows/basemenu/QRecruitBehaviour.py b/qt_ui/windows/basemenu/QRecruitBehaviour.py index 0cccd16d..968d2b2a 100644 --- a/qt_ui/windows/basemenu/QRecruitBehaviour.py +++ b/qt_ui/windows/basemenu/QRecruitBehaviour.py @@ -136,17 +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.available_next_turn(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._update_count_label(unit_type) self.update_available_budget() diff --git a/qt_ui/windows/basemenu/airfield/QAircraftRecruitmentMenu.py b/qt_ui/windows/basemenu/airfield/QAircraftRecruitmentMenu.py index 79ba0d1f..4fa2530e 100644 --- a/qt_ui/windows/basemenu/airfield/QAircraftRecruitmentMenu.py +++ b/qt_ui/windows/basemenu/airfield/QAircraftRecruitmentMenu.py @@ -92,7 +92,13 @@ 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 - + # If we change our mind about selling, we want the aircraft to be put + # back in the inventory immediately. + elif 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) + inventory.add_aircraft(unit_type, 1) + super().buy(unit_type) self.hangar_status.update_label() diff --git a/qt_ui/windows/basemenu/ground_forces/QArmorRecruitmentMenu.py b/qt_ui/windows/basemenu/ground_forces/QArmorRecruitmentMenu.py index c359eaaf..14801ba4 100644 --- a/qt_ui/windows/basemenu/ground_forces/QArmorRecruitmentMenu.py +++ b/qt_ui/windows/basemenu/ground_forces/QArmorRecruitmentMenu.py @@ -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,12 @@ class QArmorRecruitmentMenu(QFrame, QRecruitBehaviour): scroll.setWidget(scroll_content) main_layout.addWidget(scroll) self.setLayout(main_layout) + + def sell(self, unit_type: UnitType): + if self.pending_deliveries.available_next_turn(unit_type) <= 0: + 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) \ No newline at end of file