From 353b8451020224ea3ddf2d5eed9ea8e588d33b3b Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Sat, 13 Apr 2019 22:05:06 +0300 Subject: [PATCH] Change how changing of projected drone amount is done via commands --- gui/builtinContextMenus/amount.py | 2 +- .../calc/fitChangeFighterAmount.py | 10 +++- .../calc/fitChangeImplantLocation.py | 22 ++++--- .../calc/fitChangeProjectedDroneAmount.py | 59 +++++++++++++++++++ .../calc/fitChangeProjectedDroneQty.py | 27 --------- .../calc/fitRemoveProjectedDrone.py | 4 +- gui/fitCommands/guiChangeProjectedDroneQty.py | 8 +-- 7 files changed, 88 insertions(+), 44 deletions(-) create mode 100644 gui/fitCommands/calc/fitChangeProjectedDroneAmount.py delete mode 100644 gui/fitCommands/calc/fitChangeProjectedDroneQty.py diff --git a/gui/builtinContextMenus/amount.py b/gui/builtinContextMenus/amount.py index 34a608a13..74a9f813f 100644 --- a/gui/builtinContextMenus/amount.py +++ b/gui/builtinContextMenus/amount.py @@ -56,7 +56,7 @@ class ChangeAmount(ContextMenu): if srcContext == "droneItem": self.mainFrame.command.Submit(cmd.GuiChangeDroneQty(fitID, fit.drones.index(thing), int(float(cleanInput)))) else: - self.mainFrame.command.Submit(cmd.GuiChangeProjectedDroneQty(fitID, fit.projectedDrones.index(thing), int(float(cleanInput)))) + self.mainFrame.command.Submit(cmd.GuiChangeProjectedDroneQty(fitID, thing.itemID, int(float(cleanInput)))) elif isinstance(thing, es_Fit): self.mainFrame.command.Submit(cmd.GuiChangeProjectedFitQty(fitID, thing.ID, int(float(cleanInput)))) return diff --git a/gui/fitCommands/calc/fitChangeFighterAmount.py b/gui/fitCommands/calc/fitChangeFighterAmount.py index 2fa21c24b..02d72bc8b 100644 --- a/gui/fitCommands/calc/fitChangeFighterAmount.py +++ b/gui/fitCommands/calc/fitChangeFighterAmount.py @@ -22,9 +22,15 @@ class FitChangeFighterAmount(wx.Command): pyfalog.debug('Doing change of fighter amount to {} at position {} on fit {}'.format(self.amount, self.position, self.fitID)) fit = Fit.getInstance().getFit(self.fitID) fighter = fit.fighters[self.position] + if self.amount == fighter.amount or self.amount == fighter.amountActive: + return False self.savedAmount = fighter.amount - if self.amount > 0 or self.amount == -1: - fighter.amount = min(self.amount, fighter.fighterSquadronMaxSize) + if self.amount == -1: + fighter.amount = self.amount + eos.db.commit() + return True + elif self.amount > 0: + fighter.amount = max(min(self.amount, fighter.fighterSquadronMaxSize), 0) eos.db.commit() return True else: diff --git a/gui/fitCommands/calc/fitChangeImplantLocation.py b/gui/fitCommands/calc/fitChangeImplantLocation.py index fc1dec890..4edc491a1 100644 --- a/gui/fitCommands/calc/fitChangeImplantLocation.py +++ b/gui/fitCommands/calc/fitChangeImplantLocation.py @@ -1,25 +1,31 @@ import wx -import eos.db from logbook import Logger + +import eos.db +from service.fit import Fit + + pyfalog = Logger(__name__) class FitChangeImplantLocation(wx.Command): + def __init__(self, fitID, source): - wx.Command.__init__(self, True, "Drone add") + wx.Command.__init__(self, True, 'Change Implant Location') self.fitID = fitID self.source = source - self.old_source = None + self.savedSource = None def Do(self): - pyfalog.debug("Toggling implant source for fit ID: {0}", self.fitID) - fit = eos.db.getFit(self.fitID) - self.old_source = fit.implantSource + pyfalog.debug('Doing changing of implant source to {} for fit {}'.format(self.fitID, self.source)) + fit = Fit.getInstance().getFit(self.fitID) + if self.source == fit.implantSource: + return False + self.savedSource = fit.implantSource fit.implantSource = self.source eos.db.commit() return True - def Undo(self): - cmd = FitChangeImplantLocation(self.fitID, self.old_source) + cmd = FitChangeImplantLocation(self.fitID, self.savedSource) return cmd.Do() diff --git a/gui/fitCommands/calc/fitChangeProjectedDroneAmount.py b/gui/fitCommands/calc/fitChangeProjectedDroneAmount.py new file mode 100644 index 000000000..5e082aa45 --- /dev/null +++ b/gui/fitCommands/calc/fitChangeProjectedDroneAmount.py @@ -0,0 +1,59 @@ +import math + +import wx +from logbook import Logger + +import eos.db +from gui.fitCommands.helpers import DroneInfo +from service.fit import Fit + + +pyfalog = Logger(__name__) + + +class FitChangeProjectedDroneAmount(wx.Command): + + def __init__(self, fitID, itemID, amount): + wx.Command.__init__(self, True, 'Change Projected Drone Amount') + self.fitID = fitID + self.itemID = itemID + self.amount = amount + self.savedDroneInfo = None + self.removeCommand = None + + def Do(self): + pyfalog.debug('Doing change of projected drone {} amount to {} on fit {}'.format(self.itemID, self.amount, self.fitID)) + fit = Fit.getInstance().getFit(self.fitID) + drone = next((pd for pd in fit.projectedDrones if pd.itemID == self.itemID), None) + if drone is None: + pyfalog.warning('Cannot find projected drone') + return False + self.savedDroneInfo = DroneInfo.fromDrone(drone) + if self.amount > 0: + drone.amount = self.amount + if drone.amountActive > 0: + difference = self.amount - self.savedDroneInfo.amount + drone.amount = self.amount + drone.amountActive = max(min(drone.amountActive + difference, drone.amount), 0) + eos.db.commit() + return True + else: + from .fitRemoveProjectedDrone import FitRemoveProjectedDroneCommand + self.removeCommand = FitRemoveProjectedDroneCommand(fitID=self.fitID, droneInfo=DroneInfo(itemID=self.itemID, amount=math.inf, amountActive=math.inf)) + return self.removeCommand.Do() + + def Undo(self): + pyfalog.debug('Undoing change of projected drone {} amount to {} on fit {}'.format(self.itemID, self.amount, self.fitID)) + if self.removeCommand is not None: + return self.removeCommand.Undo() + if self.savedDroneInfo is not None: + fit = Fit.getInstance().getFit(self.fitID) + drone = next((pd for pd in fit.projectedDrones if pd.itemID == self.savedDroneInfo.itemID), None) + if drone is None: + pyfalog.warning('Cannot find projected drone') + return False + drone.amount = self.savedDroneInfo.amount + drone.amountActive = self.savedDroneInfo.amountActive + eos.db.commit() + return True + return False diff --git a/gui/fitCommands/calc/fitChangeProjectedDroneQty.py b/gui/fitCommands/calc/fitChangeProjectedDroneQty.py deleted file mode 100644 index 3ca926c17..000000000 --- a/gui/fitCommands/calc/fitChangeProjectedDroneQty.py +++ /dev/null @@ -1,27 +0,0 @@ -import wx -import eos.db -from logbook import Logger -pyfalog = Logger(__name__) - - -class FitChangeProjectedDroneQty(wx.Command): - def __init__(self, fitID, position, amount=1): - wx.Command.__init__(self, True, "Drone add") - self.fitID = fitID - self.position = position - self.amount = amount # add x amount. If this goes over amount, removes stack - self.old_amount = None - - def Do(self): - pyfalog.debug("Changing active fighters ({0}) for fit ({1}) to amount: {2}", self.position, self.fitID, self.amount) - fit = eos.db.getFit(self.fitID) - drone = fit.projectedDrones[self.position] - self.old_amount = drone.amount - drone.amount = self.amount - - eos.db.commit() - return True - - def Undo(self): - cmd = FitChangeProjectedDroneQty(self.fitID, self.position, self.old_amount) - return cmd.Do() diff --git a/gui/fitCommands/calc/fitRemoveProjectedDrone.py b/gui/fitCommands/calc/fitRemoveProjectedDrone.py index 39f3d34fe..b4b92caa2 100644 --- a/gui/fitCommands/calc/fitRemoveProjectedDrone.py +++ b/gui/fitCommands/calc/fitRemoveProjectedDrone.py @@ -25,13 +25,13 @@ class FitRemoveProjectedDroneCommand(wx.Command): pyfalog.warning('Unable to find projected drone for removal') return False self.savedDroneInfo = DroneInfo.fromDrone(drone) - drone.amount -= self.droneInfo.amount + drone.amount = max(drone.amount - self.droneInfo.amount, 0) # Remove stack if we have no items remaining if drone.amount == 0: fit.projectedDrones.remove(drone) else: if drone.amountActive > 0: - drone.amountActive -= self.droneInfo.amount + drone.amountActive = min(drone.amountActive, drone.amount) eos.db.commit() return True diff --git a/gui/fitCommands/guiChangeProjectedDroneQty.py b/gui/fitCommands/guiChangeProjectedDroneQty.py index f7ceaa657..7d45324dd 100644 --- a/gui/fitCommands/guiChangeProjectedDroneQty.py +++ b/gui/fitCommands/guiChangeProjectedDroneQty.py @@ -1,24 +1,24 @@ import wx import gui.mainFrame from gui import globalEvents as GE -from .calc.fitChangeProjectedDroneQty import FitChangeProjectedDroneQty +from .calc.fitChangeProjectedDroneAmount import FitChangeProjectedDroneAmount from service.fit import Fit from logbook import Logger pyfalog = Logger(__name__) class GuiChangeProjectedDroneQty(wx.Command): - def __init__(self, fitID, position, amount=1): + def __init__(self, fitID, itemID, amount=1): wx.Command.__init__(self, True, "") self.mainFrame = gui.mainFrame.MainFrame.getInstance() self.sFit = Fit.getInstance() self.fitID = fitID - self.position = position + self.itemID = itemID self.amount = amount self.internal_history = wx.CommandProcessor() def Do(self): - cmd = FitChangeProjectedDroneQty(self.fitID, self.position, self.amount) + cmd = FitChangeProjectedDroneAmount(self.fitID, self.itemID, self.amount) if self.internal_history.Submit(cmd): self.sFit.recalc(self.fitID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))