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

@@ -1,80 +0,0 @@
import wx
from service.fit import Fit
import gui.mainFrame
from gui import globalEvents as GE
from gui.fitCommands.calc.module.changeCharges import CalcChangeModuleChargesCommand
from gui.fitCommands.calc.module.localReplace import CalcReplaceLocalModuleCommand
from gui.fitCommands.calc.cargo.remove import CalcRemoveCargoCommand
from gui.fitCommands.helpers import ModuleInfo
from gui.fitCommands.calc.cargo.add import CalcAddCargoCommand
from logbook import Logger
pyfalog = Logger(__name__)
class GuiCargoToModuleCommand(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, moduleIdx, cargoIdx, copy=False):
wx.Command.__init__(self, True, "Cargo to Module")
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]
cargo = fit.cargo[self.cargoIdx]
result = False
# We're trying to move a charge from cargo to a slot. Use SetCharge command (don't respect move vs copy)
# todo: replace with item.ischarge, broken for now
if sFit.isAmmo(cargo.itemID):
result = self.internalHistory.Submit(CalcChangeModuleChargesCommand(self.fitID, False, {module.modPosition: cargo.itemID}))
else:
pyfalog.debug("Moving cargo item to module for fit ID: {0}", self.fitID)
self.addCmd = CalcReplaceLocalModuleCommand(
fitID=self.fitID,
position=module.modPosition,
newModInfo=ModuleInfo(itemID=cargo.itemID))
result = self.internalHistory.Submit(self.addCmd)
if not result:
# creating module failed for whatever reason
return False
if self.addCmd.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.addCmd.old_module.itemID)
result = self.internalHistory.Submit(self.addCargoCmd)
elif not self.copy:
# move, not copying, so remove cargo
self.removeCmd = CalcRemoveCargoCommand(self.fitID, cargo.itemID)
result = self.internalHistory.Submit(self.removeCmd)
if result:
sFit.recalc(self.fitID)
wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))
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

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

@@ -3,17 +3,18 @@ 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
from gui.fitCommands.calc.cargo.add import CalcAddCargoCommand
pyfalog = Logger(__name__)
class GuiModuleToCargoCommand(wx.Command):
class GuiLocalModuleToCargoCommand(wx.Command):
def __init__(self, fitID, moduleIdx, cargoIdx, copy=False):
wx.Command.__init__(self, True, "Module to Cargo")