From f0983c14683f1c5268c1307efd9395ed7a1555c4 Mon Sep 17 00:00:00 2001 From: Ryan Holmes Date: Mon, 13 Aug 2018 19:35:32 -0400 Subject: [PATCH] Implement fighter add/remove commands --- gui/builtinAdditionPanes/fighterView.py | 11 ++--- gui/builtinContextMenus/itemRemove.py | 3 +- gui/fitCommands/__init__.py | 4 +- gui/fitCommands/calc/fitAddFighter.py | 53 ++++++++++++++++++++++++ gui/fitCommands/calc/fitRemoveFighter.py | 34 +++++++++++++++ gui/fitCommands/guiAddFighter.py | 30 ++++++++++++++ gui/fitCommands/guiRemoveFighter.py | 32 ++++++++++++++ gui/mainFrame.py | 2 +- service/fit.py | 1 + 9 files changed, 160 insertions(+), 10 deletions(-) create mode 100644 gui/fitCommands/calc/fitAddFighter.py create mode 100644 gui/fitCommands/calc/fitRemoveFighter.py create mode 100644 gui/fitCommands/guiAddFighter.py create mode 100644 gui/fitCommands/guiRemoveFighter.py diff --git a/gui/builtinAdditionPanes/fighterView.py b/gui/builtinAdditionPanes/fighterView.py index 3292583fd..916bcf585 100644 --- a/gui/builtinAdditionPanes/fighterView.py +++ b/gui/builtinAdditionPanes/fighterView.py @@ -30,6 +30,7 @@ from gui.contextMenu import ContextMenu from gui.utils.staticHelpers import DragDropHelper from service.fit import Fit from service.market import Market +import gui.fitCommands as cmd class FighterViewDrop(wx.DropTarget): @@ -269,11 +270,9 @@ class FighterDisplay(d.Display): event.Skip() def addItem(self, event): - sFit = Fit.getInstance() fitID = self.mainFrame.getActiveFit() - trigger = sFit.addFighter(fitID, event.itemID) - if trigger: - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + + if self.mainFrame.command.Submit(cmd.GuiAddFighterCommand(fitID, event.itemID)): self.mainFrame.additionsPane.select("Fighters") event.Skip() @@ -288,9 +287,7 @@ class FighterDisplay(d.Display): def removeFighter(self, fighter): fitID = self.mainFrame.getActiveFit() - sFit = Fit.getInstance() - sFit.removeFighter(fitID, self.original.index(fighter)) - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) + self.mainFrame.command.Submit(cmd.GuiRemoveFighterCommand(fitID, self.original.index(fighter))) def click(self, event): event.Skip() diff --git a/gui/builtinContextMenus/itemRemove.py b/gui/builtinContextMenus/itemRemove.py index 0d464461d..65094c7a4 100644 --- a/gui/builtinContextMenus/itemRemove.py +++ b/gui/builtinContextMenus/itemRemove.py @@ -43,7 +43,8 @@ class ItemRemove(ContextMenu): elif srcContext == "droneItem": sFit.removeDrone(fitID, fit.drones.index(selection[0])) elif srcContext == "fighterItem": - sFit.removeFighter(fitID, fit.fighters.index(selection[0])) + self.mainFrame.command.Submit(cmd.GuiRemoveFighterCommand(fitID, fit.fighters.index(selection[0]))) + return # the command takes care of the PostEvent elif srcContext == "implantItem": self.mainFrame.command.Submit(cmd.GuiRemoveImplantCommand(fitID, fit.implants.index(selection[0]))) return # the command takes care of the PostEvent diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index 966295b7e..5acf62ffd 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -16,4 +16,6 @@ from .guiToggleCommand import GuiToggleCommandCommand from .guiAddProjected import GuiAddProjectedCommand from .guiRemoveProjected import GuiRemoveProjectedCommand from .guiCargoToModule import GuiCargoToModuleCommand -from .guiModuleToCargo import GuiModuleToCargoCommand \ No newline at end of file +from .guiModuleToCargo import GuiModuleToCargoCommand +from .guiAddFighter import GuiAddFighterCommand +from .guiRemoveFighter import GuiRemoveFighterCommand \ No newline at end of file diff --git a/gui/fitCommands/calc/fitAddFighter.py b/gui/fitCommands/calc/fitAddFighter.py new file mode 100644 index 000000000..9ceda53a1 --- /dev/null +++ b/gui/fitCommands/calc/fitAddFighter.py @@ -0,0 +1,53 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +#from .helpers import ModuleInfoCache +from eos.saveddata.module import Module, State +import eos.db +from logbook import Logger +pyfalog = Logger(__name__) +from eos.saveddata.fighter import Fighter + +class FitAddFighterCommand(wx.Command): + """" + from sFit.addFighter + """ + def __init__(self, fitID, itemID, amount=1, replace=False): + wx.Command.__init__(self, True, "Cargo add") + self.fitID = fitID + self.itemID = itemID + self.new_index = None + + def Do(self): + fit = eos.db.getFit(self.fitID) + item = eos.db.getItem(self.itemID, eager=("attributes", "group.category")) + + try: + fighter = Fighter(item) + except ValueError: + pyfalog.warning("Invalid fighter: {}", item) + return False + + if not fighter.fits(fit): + return False + + used = fit.getSlotsUsed(fighter.slot) + total = fit.getNumSlots(fighter.slot) + + if used >= total: + fighter.active = False + + fit.fighters.append(fighter) + self.new_index = fit.fighters.index(fighter) + + eos.db.commit() + + return True + + def Undo(self): + from .fitRemoveFighter import FitRemoveFighterCommand # Avoid circular import + cmd = FitRemoveFighterCommand(self.fitID, self.new_index) + cmd.Do() + return True diff --git a/gui/fitCommands/calc/fitRemoveFighter.py b/gui/fitCommands/calc/fitRemoveFighter.py new file mode 100644 index 000000000..58b1f7c6a --- /dev/null +++ b/gui/fitCommands/calc/fitRemoveFighter.py @@ -0,0 +1,34 @@ +import wx + +from gui.fitCommands.helpers import ModuleInfoCache +import eos.db +from logbook import Logger +pyfalog = Logger(__name__) + + +class FitRemoveFighterCommand(wx.Command): + """" + Fitting command that removes a module at a specified positions + + from sFit.removeFighter + """ + def __init__(self, fitID: int, position: int): + wx.Command.__init__(self, True) + self.fitID = fitID + self.position = position + self.change = None + self.removed_item = None + + def Do(self): + fitID = self.fitID + fit = eos.db.getFit(fitID) + f = fit.fighters[self.position] + fit.fighters.remove(f) + self.removed_item = f.itemID + eos.db.commit() + return True + + def Undo(self): + from gui.fitCommands.calc.fitAddFighter import FitAddFighterCommand # avoids circular import + cmd = FitAddFighterCommand(self.fitID, self.removed_item) + return cmd.Do() diff --git a/gui/fitCommands/guiAddFighter.py b/gui/fitCommands/guiAddFighter.py new file mode 100644 index 000000000..095688142 --- /dev/null +++ b/gui/fitCommands/guiAddFighter.py @@ -0,0 +1,30 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from .calc.fitAddFighter import FitAddFighterCommand + +class GuiAddFighterCommand(wx.Command): + def __init__(self, fitID, itemID): + wx.Command.__init__(self, True, "Cargo Add") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.internal_history = wx.CommandProcessor() + self.fitID = fitID + self.itemID = itemID + + def Do(self): + cmd = FitAddFighterCommand(self.fitID, self.itemID) + if self.internal_history.Submit(cmd): + self.sFit.recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + return False + + def Undo(self): + for _ in self.internal_history.Commands: + self.internal_history.Undo() + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + diff --git a/gui/fitCommands/guiRemoveFighter.py b/gui/fitCommands/guiRemoveFighter.py new file mode 100644 index 000000000..f1b983ec5 --- /dev/null +++ b/gui/fitCommands/guiRemoveFighter.py @@ -0,0 +1,32 @@ +import wx +from service.fit import Fit + +import gui.mainFrame +from gui import globalEvents as GE +from .calc.fitRemoveFighter import FitRemoveFighterCommand + + +class GuiRemoveFighterCommand(wx.Command): + def __init__(self, fitID, position): + wx.Command.__init__(self, True, "Module Remove") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.sFit = Fit.getInstance() + self.fitID = fitID + self.position = position + self.internal_history = wx.CommandProcessor() + + def Do(self): + success = self.internal_history.Submit(FitRemoveFighterCommand(self.fitID, self.position)) + + if success: + self.sFit.recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + return False + + def Undo(self): + for _ in self.internal_history.Commands: + self.internal_history.Undo() + self.sFit.recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True diff --git a/gui/mainFrame.py b/gui/mainFrame.py index 3f35ff7ae..5dd119db0 100644 --- a/gui/mainFrame.py +++ b/gui/mainFrame.py @@ -245,7 +245,7 @@ class MainFrame(wx.Frame): self.Bind(GE.EVT_SSO_LOGGING_IN, self.ShowSsoLogin) @property - def command(self): + def command(self) -> wx.CommandProcessor: return Fit.getCommandProcessor(self.getActiveFit()) def ShowSsoLogin(self, event): diff --git a/service/fit.py b/service/fit.py index 87b8f02a9..c7cb70fd1 100644 --- a/service/fit.py +++ b/service/fit.py @@ -897,6 +897,7 @@ class Fit(object): else: return False + @deprecated def removeFighter(self, fitID, i, recalc=True): pyfalog.debug("Removing fighters from fit ID: {0}", fitID) fit = eos.db.getFit(fitID)