From 4522edb8143166c3ed63627a494301b143f8bebe Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Sat, 13 Apr 2019 23:08:49 +0300 Subject: [PATCH] Rework module state changing --- gui/fitCommands/calc/fitChangeModuleStates.py | 59 ++++++++++++++ gui/fitCommands/calc/fitChangeState.py | 76 ------------------- gui/fitCommands/guiToggleModuleState.py | 4 +- 3 files changed, 61 insertions(+), 78 deletions(-) create mode 100644 gui/fitCommands/calc/fitChangeModuleStates.py delete mode 100644 gui/fitCommands/calc/fitChangeState.py diff --git a/gui/fitCommands/calc/fitChangeModuleStates.py b/gui/fitCommands/calc/fitChangeModuleStates.py new file mode 100644 index 000000000..6d92ae6c2 --- /dev/null +++ b/gui/fitCommands/calc/fitChangeModuleStates.py @@ -0,0 +1,59 @@ +import wx +from logbook import Logger + +import eos.db +from eos.saveddata.module import Module +from service.fit import Fit + +pyfalog = Logger(__name__) + + +class FitChangeModuleStatesCommand(wx.Command): + + def __init__(self, fitID, mainPosition, positions, click): + wx.Command.__init__(self, True, 'Change Module States') + self.fitID = fitID + self.mainPosition = mainPosition + self.positions = positions + self.click = click + self.savedStates = {} + + def Do(self): + pyfalog.debug('Doing change of module states at position {}/{} to click {} on fit {}'.format(self.mainPosition, self.positions, self.click, self.fitID)) + sFit = Fit.getInstance() + fit = sFit.getFit(self.fitID) + mainMod = fit.modules[self.mainPosition] + if mainMod.isEmpty: + return False + positions = [pos for pos in self.positions if not fit.modules[pos].isEmpty] + self.savedStates = {pos: fit.modules[pos].state for pos in positions} + + changed = False + mainProposedState = Module.getProposedState(mainMod, self.click) + pyfalog.debug('Attempting to change modules to {}'.format(mainProposedState)) + if mainProposedState != mainMod.state: + pyfalog.debug('Toggle {} state: {} for fit ID: {}'.format(mainMod, mainProposedState, self.fitID)) + mainMod.state = mainProposedState + changed = True + for position in [pos for pos in positions if pos != self.mainPosition]: + mod = fit.modules[position] + proposedState = Module.getProposedState(mod, self.click, mainProposedState) + if proposedState != mod.state: + pyfalog.debug('Toggle {} state: {} for fit ID: {}'.format(mod, proposedState, self.fitID)) + mod.state = proposedState + + # if we haven't change the state (eg, overheat -> overheat), simply fail the command + if not changed: + return False + eos.db.commit() + sFit.checkStates(fit, mainMod) + return True + + def Undo(self): + pyfalog.debug('Undoing change of module states at position {}/{} to click {} on fit {}'.format(self.mainPosition, self.positions, self.click, self.fitID)) + fit = Fit.getInstance().getFit(self.fitID) + for position, state in self.savedStates.items(): + mod = fit.modules[position] + pyfalog.debug('Reverting {} to state {} for fit ID {}'.format(mod, state, self.fitID)) + mod.state = state + return True diff --git a/gui/fitCommands/calc/fitChangeState.py b/gui/fitCommands/calc/fitChangeState.py deleted file mode 100644 index 02592271e..000000000 --- a/gui/fitCommands/calc/fitChangeState.py +++ /dev/null @@ -1,76 +0,0 @@ -import wx -from logbook import Logger - -import gui.mainFrame -from eos.saveddata.module import Module -from service.fit import Fit -import eos.db - -pyfalog = Logger(__name__) - - -class FitChangeStatesCommand(wx.Command): - """ - 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. - - - """ - def __init__(self, fitID, baseModPos, positions, click): - # todo: instead of modules, needs to be positions. Dead objects are a thing - wx.Command.__init__(self, True, "Module State Change") - self.mainFrame = gui.mainFrame.MainFrame.getInstance() - self.sFit = Fit.getInstance() - self.fitID = fitID - self.baseModPos = baseModPos - self.positions = positions - self.click = click - self.changed = None - self.old_states = {} - - def Do(self): - fit = eos.db.getFit(self.fitID) - sFit = Fit.getInstance() - baseMod = fit.modules[self.baseModPos] - - # make sure positions only include non-empty positions - self.positions = [x for x in self.positions if not fit.modules[x].isEmpty] - - for x in self.positions: - self.old_states[x] = fit.modules[x].state - - proposedState = Module.getProposedState(baseMod, self.click) - pyfalog.debug("Attempting to change modules to {}", proposedState) - - if proposedState != baseMod.state: - pyfalog.debug("Toggle {} state: {} for fit ID: {}", baseMod, proposedState, self.fitID) - - self.changed = True - baseMod.state = proposedState - for i in [x for x in self.positions if x != self.baseModPos]: # dont consider base module position - mod = fit.modules[i] - p = Module.getProposedState(mod, self.click, proposedState) - mod.state = p - if p != mod.state: - pyfalog.debug("Toggle {} state: {} for fit ID: {}", mod, p, self.fitID) - self.changed = True - - # if we haven't change the state (eg, overheat -> overheat), simply fail the command - if self.changed: - eos.db.commit() - # 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 - sFit.checkStates(fit, baseMod) - # self.checkStates(fit, base) - return True - return False - - def Undo(self): - # todo: some sanity checking to make sure that we are applying state back to the same modules? - fit = self.sFit.getFit(self.fitID) - for k, v in self.old_states.items(): - mod = fit.modules[k] - pyfalog.debug("Reverting {} to state {} for fit ID", mod, v, self.fitID) - mod.state = v - return True diff --git a/gui/fitCommands/guiToggleModuleState.py b/gui/fitCommands/guiToggleModuleState.py index 71e307f93..931262f03 100644 --- a/gui/fitCommands/guiToggleModuleState.py +++ b/gui/fitCommands/guiToggleModuleState.py @@ -3,7 +3,7 @@ from service.fit import Fit import gui.mainFrame from gui import globalEvents as GE -from .calc.fitChangeState import FitChangeStatesCommand +from .calc.fitChangeModuleStates import FitChangeModuleStatesCommand class GuiModuleStateChangeCommand(wx.Command): @@ -19,7 +19,7 @@ class GuiModuleStateChangeCommand(wx.Command): self.internal_history = wx.CommandProcessor() def Do(self): - if self.internal_history.Submit(FitChangeStatesCommand(self.fitID, self.baseMod, self.modules, self.click)): + if self.internal_history.Submit(FitChangeModuleStatesCommand(self.fitID, self.baseMod, self.modules, self.click)): self.sFit.recalc(self.fitID) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID)) return True