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