Rework cargo to module command

This commit is contained in:
DarkPhoenix
2019-04-16 01:41:19 +03:00
parent a270dc44d2
commit 64bba0cfdb
13 changed files with 174 additions and 116 deletions

View File

@@ -0,0 +1,119 @@
import wx
import eos.db
import gui.mainFrame
from gui import globalEvents as GE
from gui.fitCommands.calc.cargo.add import CalcAddCargoCommand
from gui.fitCommands.calc.cargo.remove import CalcRemoveCargoCommand
from gui.fitCommands.calc.module.changeCharges import CalcChangeModuleChargesCommand
from gui.fitCommands.calc.module.localReplace import CalcReplaceLocalModuleCommand
from gui.fitCommands.helpers import CargoInfo, InternalCommandHistory, ModuleInfo
from service.fit import Fit
class GuiCargoToLocalModuleCommand(wx.Command):
"""
Moves cargo to fitting window. Can either do a copy, move, or swap with current module
If we try to copy/move into a spot with a non-empty module, we swap instead.
To avoid redundancy in converting Cargo item, this function does the
sanity checks as opposed to the GUI View. This is different than how the
normal .swapModules() does things, which is mostly a blind swap.
"""
def __init__(self, fitID, cargoItemID, modPosition, copy):
wx.Command.__init__(self, True, 'Cargo to Local Module')
self.internalHistory = InternalCommandHistory()
self.fitID = fitID
self.srcCargoItemID = cargoItemID
self.dstModPosition = modPosition
self.copy = copy
self.addedModItemID = None
self.removedModItemID = None
def Do(self):
sFit = Fit.getInstance()
fit = sFit.getFit(self.fitID)
srcCargo = next((c for c in fit.cargo if c.itemID == self.srcCargoItemID), None)
if srcCargo is None:
return
dstMod = fit.modules[self.dstModPosition]
# Moving charge from cargo to fit - just attempt to load charge into destination module
if srcCargo.item.isCharge and not dstMod.isEmpty:
cmd = CalcChangeModuleChargesCommand(
fitID=self.fitID,
projected=False,
chargeMap={dstMod.modPosition: self.srcCargoItemID},
commit=False)
success = self.internalHistory.submit(cmd)
# Copying item to empty slot
elif srcCargo.item.isModule and self.copy and dstMod.isEmpty:
cmd = CalcReplaceLocalModuleCommand(
fitID=self.fitID,
position=self.dstModPosition,
newModInfo=ModuleInfo(itemID=self.srcCargoItemID),
commit=False)
success = self.internalHistory.submit(cmd)
if success:
self.addedModItemID = self.srcCargoItemID
# Swapping with target module, or moving there if there's no module
elif srcCargo.item.isModule and not self.copy:
dstModItemID = dstMod.itemID
if self.srcCargoItemID == dstModItemID:
return False
newModInfo = ModuleInfo.fromModule(dstMod)
newModInfo.itemID = self.srcCargoItemID
if dstMod.isEmpty:
newCargoItemID = None
elif dstMod.isMutated:
newCargoItemID = dstMod.baseItemID
else:
newCargoItemID = dstMod.itemID
commands = []
commands.append(CalcRemoveCargoCommand(
fitID=self.fitID,
cargoInfo=CargoInfo(itemID=self.srcCargoItemID, amount=1),
commit=False))
if newCargoItemID is not None:
commands.append(CalcAddCargoCommand(
fitID=self.fitID,
cargoInfo=CargoInfo(itemID=newCargoItemID, amount=1),
commit=False))
commands.append(CalcReplaceLocalModuleCommand(
fitID=self.fitID,
position=self.dstModPosition,
newModInfo=newModInfo,
unloadInvalidCharges=True,
commit=False))
success = self.internalHistory.submitBatch(*commands)
if success:
self.addedModItemID = self.srcCargoItemID
self.removedModItemID = dstModItemID
else:
return False
eos.db.commit()
sFit.recalc(self.fitID)
events = []
if self.removedModItemID is not None:
events.append(GE.FitChanged(fitID=self.fitID, action='moddel', typeID=self.removedModItemID))
if self.addedModItemID is not None:
events.append(GE.FitChanged(fitID=self.fitID, action='modadd', typeID=self.addedModItemID))
if not events:
events.append(GE.FitChanged(fitID=self.fitID))
for event in events:
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
return success
def Undo(self):
success = self.internalHistory.undoAll()
eos.db.commit()
Fit.getInstance().recalc(self.fitID)
events = []
if self.addedModItemID is not None:
events.append(GE.FitChanged(fitID=self.fitID, action='moddel', typeID=self.addedModItemID))
if self.removedModItemID is not None:
events.append(GE.FitChanged(fitID=self.fitID, action='modadd', typeID=self.removedModItemID))
if not events:
events.append(GE.FitChanged(fitID=self.fitID))
for event in events:
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
return success

View File

@@ -0,0 +1,77 @@
import wx
from logbook import Logger
import gui.mainFrame
from gui import globalEvents as GE
from gui.fitCommands.calc.cargo.add import CalcAddCargoCommand
from gui.fitCommands.calc.cargo.remove import CalcRemoveCargoCommand
from gui.fitCommands.calc.module.localRemove import CalcRemoveLocalModuleCommand
from gui.fitCommands.calc.module.localReplace import CalcReplaceLocalModuleCommand
from gui.fitCommands.helpers import ModuleInfo
from service.fit import Fit
pyfalog = Logger(__name__)
class GuiLocalModuleToCargoCommand(wx.Command):
def __init__(self, fitID, moduleIdx, cargoIdx, copy=False):
wx.Command.__init__(self, True, "Module to Cargo")
self.fitID = fitID
self.moduleIdx = moduleIdx
self.cargoIdx = cargoIdx
self.copy = copy
self.internalHistory = wx.CommandProcessor()
def Do(self):
sFit = Fit.getInstance()
fit = sFit.getFit(self.fitID)
module = fit.modules[self.moduleIdx]
result = False
if self.cargoIdx: # we're swapping with cargo
if self.copy: # if copying, simply add item to cargo
result = self.internalHistory.Submit(CalcAddCargoCommand(
gui.mainFrame.MainFrame.getInstance().getActiveFit(), module.item.ID if not module.item.isAbyssal else module.baseItemID))
else: # otherwise, try to swap by replacing module with cargo item. If successful, remove old cargo and add new cargo
cargo = fit.cargo[self.cargoIdx]
self.modReplaceCmd = CalcReplaceLocalModuleCommand(
fitID=self.fitID,
position=module.modPosition,
newModInfo=ModuleInfo(itemID=cargo.itemID))
result = self.internalHistory.Submit(self.modReplaceCmd)
if not result:
# creating module failed for whatever reason
return False
if self.modReplaceCmd.old_module is not None:
# we're swapping with an existing module, so remove cargo and add module
self.removeCmd = CalcRemoveCargoCommand(self.fitID, cargo.itemID)
result = self.internalHistory.Submit(self.removeCmd)
self.addCargoCmd = CalcAddCargoCommand(self.fitID, self.modReplaceCmd.old_module.itemID)
result = self.internalHistory.Submit(self.addCargoCmd)
else: # dragging to blank spot, append
result = self.internalHistory.Submit(CalcAddCargoCommand(gui.mainFrame.MainFrame.getInstance().getActiveFit(),
module.item.ID if not module.item.isAbyssal else module.baseItemID))
if not self.copy: # if not copying, remove module
self.internalHistory.Submit(CalcRemoveLocalModuleCommand(gui.mainFrame.MainFrame.getInstance().getActiveFit(), [self.moduleIdx]))
if result:
sFit.recalc(self.fitID)
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID, action="moddel", typeID=module.item.ID))
return result
def Undo(self):
for _ in self.internalHistory.Commands:
self.internalHistory.Undo()
Fit.getInstance().recalc(self.fitID)
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))
return True