Merge pull request #716 from SimonC6R/sold-aircraft-state

Adds a buffer for sold aircraft/vehicles.
This commit is contained in:
Dan Albert 2021-01-05 02:12:49 -08:00 committed by GitHub
commit 8a9177b459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 11 deletions

View File

@ -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 = {}

View File

@ -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()

View File

@ -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()

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,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)