From 7e41d8e20cf3dff9a1d9a4d513893bb7f1da6cb5 Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Wed, 10 Apr 2019 09:14:22 +0300 Subject: [PATCH] Make conversion to mutaplasmid undoable --- gui/builtinContextMenus/mutaplasmids.py | 16 ++++----- gui/fitCommands/__init__.py | 1 + gui/fitCommands/calc/fitChangeState.py | 2 +- gui/fitCommands/calc/fitReplaceModule.py | 11 +++--- gui/fitCommands/guiMutaConvert.py | 45 ++++++++++++++++++++++++ service/fit.py | 31 ---------------- 6 files changed, 62 insertions(+), 44 deletions(-) create mode 100644 gui/fitCommands/guiMutaConvert.py diff --git a/gui/builtinContextMenus/mutaplasmids.py b/gui/builtinContextMenus/mutaplasmids.py index 98656b840..e4b24a66f 100644 --- a/gui/builtinContextMenus/mutaplasmids.py +++ b/gui/builtinContextMenus/mutaplasmids.py @@ -1,8 +1,10 @@ -from gui.contextMenu import ContextMenu -import gui.mainFrame # noinspection PyPackageRequirements import wx + import gui.globalEvents as GE +import gui.mainFrame +from gui.fitCommands import GuiMutaConvertCommand +from gui.contextMenu import ContextMenu from service.fit import Fit from service.settings import ContextMenuSettings @@ -55,13 +57,11 @@ class MutaplasmidCM(ContextMenu): def handleMenu(self, event): mutaplasmid, mod = self.eventIDs[event.Id] - fit = self.mainFrame.getActiveFit() - sFit = Fit.getInstance() - # todo: dev out function to switch module to an abyssal module. Also, maybe open item stats here automatically - # with the attribute tab set? - sFit.convertMutaplasmid(fit, mod.modPosition, mutaplasmid) - wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fit)) + self.mainFrame.command.Submit(GuiMutaConvertCommand( + fitID=self.mainFrame.getActiveFit(), + position=mod.modPosition, + mutaplasmid=mutaplasmid)) def activate(self, fullContext, selection, i): sFit = Fit.getInstance() diff --git a/gui/fitCommands/__init__.py b/gui/fitCommands/__init__.py index 83b7dbb46..5d8ea66ec 100644 --- a/gui/fitCommands/__init__.py +++ b/gui/fitCommands/__init__.py @@ -37,3 +37,4 @@ from .guiChangeImplantLocation import GuiChangeImplantLocation from .guiImportMutatedModule import GuiImportMutatedModuleCommand from .guiSetSpoolup import GuiSetSpoolup from .guiRebaseItems import GuiRebaseItemsCommand +from .guiMutaConvert import GuiMutaConvertCommand diff --git a/gui/fitCommands/calc/fitChangeState.py b/gui/fitCommands/calc/fitChangeState.py index 0b901e640..02592271e 100644 --- a/gui/fitCommands/calc/fitChangeState.py +++ b/gui/fitCommands/calc/fitChangeState.py @@ -11,7 +11,7 @@ pyfalog = Logger(__name__) class FitChangeStatesCommand(wx.Command): """ - Fitting command that trys to change the state of modules in [positions]. We use the base module to determine the + Fitting command that tries to change the state of modules in [positions]. We use the base module to determine the state that we will try to apply for all modules. diff --git a/gui/fitCommands/calc/fitReplaceModule.py b/gui/fitCommands/calc/fitReplaceModule.py index 02f5f1a33..02772269c 100644 --- a/gui/fitCommands/calc/fitReplaceModule.py +++ b/gui/fitCommands/calc/fitReplaceModule.py @@ -4,6 +4,8 @@ from logbook import Logger import eos.db from eos.saveddata.module import Module from gui.fitCommands.helpers import ModuleInfoCache, stateLimit +from service.fit import Fit +from service.market import Market pyfalog = Logger(__name__) @@ -27,7 +29,7 @@ class FitReplaceModuleCommand(wx.Command): self.oldModuleInfo = None def Do(self): - fit = eos.db.getFit(self.fitID) + fit = Fit.getInstance().getFit(self.fitID) mod = fit.modules[self.position] if not mod.isEmpty: self.oldModuleInfo = ModuleInfoCache( @@ -45,7 +47,7 @@ class FitReplaceModuleCommand(wx.Command): def Undo(self): if self.oldModuleInfo is None: - fit = eos.db.getFit(self.fitID) + fit = Fit.getInstance().getFit(self.fitID) fit.modules.toDummy(self.position) return True self.changeModule( @@ -63,9 +65,10 @@ class FitReplaceModuleCommand(wx.Command): pyfalog.debug("Changing module on position ({0}) for fit ID: {1}", self.position, self.fitID) - item = eos.db.getItem(itemID, eager=("attributes", "group.category")) + sMarket = Market.getInstance() + item = sMarket.getItem(itemID, eager=("attributes", "group.category")) if baseItemID and mutaplasmidID: - baseItem = eos.db.getItem(baseItemID, eager=("attributes", "group.category")) + baseItem = sMarket.getItem(baseItemID, eager=("attributes", "group.category")) mutaplasmid = eos.db.getDynamicItem(mutaplasmidID) else: baseItem = None diff --git a/gui/fitCommands/guiMutaConvert.py b/gui/fitCommands/guiMutaConvert.py new file mode 100644 index 000000000..b56fa2807 --- /dev/null +++ b/gui/fitCommands/guiMutaConvert.py @@ -0,0 +1,45 @@ +import wx + +import eos.db +import gui.mainFrame +from gui import globalEvents as GE +from service.fit import Fit +from .calc.fitReplaceModule import FitReplaceModuleCommand + + +class GuiMutaConvertCommand(wx.Command): + + def __init__(self, fitID, position, mutaplasmid): + wx.Command.__init__(self, True, "Convert Item to Mutated") + self.mainFrame = gui.mainFrame.MainFrame.getInstance() + self.internal_history = wx.CommandProcessor() + self.fitID = fitID + self.position = position + self.mutaplasmid = mutaplasmid + + def Do(self): + fit = eos.db.getFit(self.fitID) + oldMod = fit.modules[self.position] + + success = self.internal_history.Submit(FitReplaceModuleCommand( + fitID=self.fitID, + position=self.position, + newItemID=self.mutaplasmid.resultingItem.ID, + newBaseItemID=oldMod.item.ID, + newMutaplasmidID=self.mutaplasmid.ID, + newMutations={}, + newState=oldMod.state, + newCharge=oldMod.charge)) + if not success: + return False + + Fit.getInstance().recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True + + def Undo(self): + for _ in self.internal_history.Commands: + self.internal_history.Undo() + Fit.getInstance().recalc(self.fitID) + wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) + return True diff --git a/service/fit.py b/service/fit.py index d116cd4f1..342346290 100644 --- a/service/fit.py +++ b/service/fit.py @@ -369,37 +369,6 @@ class Fit(FitDeprecated): eos.db.commit() return mutator.value - def convertMutaplasmid(self, fitID, position, mutaplasmid): - # this is mostly the same thing as the self.changeModule method, however it initializes an abyssal module with - # the old module as it's base, and then replaces it - fit = eos.db.getFit(fitID) - base = fit.modules[position] - fit.modules.toDummy(position) - - try: - m = es_Module(mutaplasmid.resultingItem, base.item, mutaplasmid) - except ValueError: - pyfalog.warning("Invalid item: {0} AHHHH") - return False - - if m.fits(fit): - m.owner = fit - fit.modules.toModule(position, m) - if m.isValidState(FittingModuleState.ACTIVE): - m.state = FittingModuleState.ACTIVE - - # As some items may affect state-limiting attributes of the ship, calculate new attributes first - self.recalc(fit) - # Then, check states of all modules and change where needed. This will recalc if needed - self.checkStates(fit, m) - - fit.fill() - eos.db.commit() - - return True - else: - return None - @deprecated def addDrone(self, fitID, itemID, numDronesToAdd=1, recalc=True): pyfalog.debug("Adding {0} drones ({1}) to fit ID: {2}", numDronesToAdd, itemID, fitID)