More work on commands, this time focusing on details with module add / remove functionalities.

This commit is contained in:
blitzmann
2018-08-02 02:06:03 -04:00
parent 098f0f92ee
commit fc153915b6
8 changed files with 80 additions and 52 deletions

View File

@@ -16,10 +16,12 @@ class FitAddModuleCommand(wx.Command):
from sFit.appendModule
"""
def __init__(self, fitID, itemID):
def __init__(self, fitID, itemID, mutaplasmidID=None, baseID=None):
wx.Command.__init__(self, True, "Module Add")
self.fitID = fitID
self.itemID = itemID
self.mutaplasmidID = mutaplasmidID
self.baseID = baseID
self.new_position = None
self.change = None
@@ -30,8 +32,11 @@ class FitAddModuleCommand(wx.Command):
fit = eos.db.getFit(fitID)
item = eos.db.getItem(itemID, eager=("attributes", "group.category"))
bItem = eos.db.getItem(self.baseID) if self.baseID else None
mItem = next((x for x in bItem.mutaplasmids if x.ID == self.mutaplasmidID)) if self.mutaplasmidID else None
try:
self.module = Module(item)
self.module = Module(item, bItem, mItem)
except ValueError:
pyfalog.warning("Invalid item: {0}", itemID)
return False
@@ -52,7 +57,7 @@ class FitAddModuleCommand(wx.Command):
# Then, check states of all modules and change where needed. This will recalc if needed
# self.checkStates(fit, m)
fit.fill()
#fit.fill()
eos.db.commit()
self.change = numSlots != len(fit.modules)

View File

@@ -27,10 +27,10 @@ class FitRemoveModuleCommand(wx.Command):
for x in self.positions:
mod = fit.modules[x]
if not mod.isEmpty:
self.modCache.append(ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge))
self.modCache.append(ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge, mod.baseItemID, mod.mutaplasmidID))
fit.modules.toDummy(x)
# if no modules have changes, report back None
# if no modules have changes, skip command
if not len(self.modCache) > 0:
return False
@@ -38,7 +38,7 @@ class FitRemoveModuleCommand(wx.Command):
# todo: determine if we need to do this still
# self.recalc(fit)
# self.checkStates(fit, None)
fit.fill()
# fit.fill()
eos.db.commit()
self.slotsChanged = numSlots != len(fit.modules)
return True
@@ -46,7 +46,8 @@ class FitRemoveModuleCommand(wx.Command):
def Undo(self):
from gui.fitCommands.calc.fitAddModule import FitAddModuleCommand # avoids circular import
for mod in self.modCache:
cmd = FitAddModuleCommand(self.fitID, mod.itemID)
# todo, send the state and charge?
cmd = FitAddModuleCommand(self.fitID, mod.itemID, mod.mutaplasmidID, mod.baseID)
cmd.Do()
cmd.module.state = mod.state
cmd.module.charge = mod.charge

View File

@@ -28,7 +28,12 @@ class FitReplaceModuleCommand(wx.Command):
return self.change_module(self.fitID, self.position, self.itemID)
def Undo(self):
self.change_module(self.fitID, self.position, self.itemID)
if self.old_module is None:
fit = eos.db.getFit(self.fitID)
fit.modules.toDummy(self.position)
return True
self.change_module(self.fitID, self.position, self.old_module.itemID)
self.module.state = self.old_module.state
self.module.charge = self.old_module.charge
return True
@@ -51,10 +56,8 @@ class FitReplaceModuleCommand(wx.Command):
item = eos.db.getItem(itemID, eager=("attributes", "group.category"))
mod = fit.modules[self.position]
self.old_module.append(ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge))
# Dummy it out in case the next bit fails
fit.modules.toDummy(self.position)
if not mod.isEmpty:
self.old_module = ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge, mod.baseItemID, mod.mutaplasmidID)
try:
self.module = Module(item)
@@ -62,6 +65,12 @@ class FitReplaceModuleCommand(wx.Command):
pyfalog.warning("Invalid item: {0}", itemID)
return False
if self.module.slot != mod.slot:
return False
# Dummy it out in case the next bit fails
fit.modules.toDummy(self.position)
if self.module.fits(fit):
self.module.owner = fit
fit.modules.toModule(self.position, self.module)
@@ -71,7 +80,7 @@ class FitReplaceModuleCommand(wx.Command):
# Then, check states of all modules and change where needed. This will recalc if needed
# self.checkStates(fit, m)
fit.fill()
# fit.fill()
eos.db.commit()
return True
return False

View File

@@ -9,34 +9,36 @@ pyfalog = Logger(__name__)
import eos.db
class FitSetChargeCommand(wx.Command):
def __init__(self, fitID, modules, chargeID=None):
def __init__(self, fitID, positions, chargeID=None):
# todo: determine if this command really should be used with a group of modules, or a simple per module basis
wx.Command.__init__(self, True, "Module Charge Add")
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
self.sFit = Fit.getInstance()
self.fitID = fitID
self.chargeID = chargeID
self.modules = modules
self.positions = {mod.modPosition: mod.chargeID for mod in modules}
self.positions = positions
self.cache = None
def Do(self):
pyfalog.debug("Set ammo for fit ID: {0}", self.fitID)
if self.fitID is None:
return False
return self.__setAmmo(self.modules, self.chargeID)
return self.__setAmmo(self.positions, self.chargeID)
def Undo(self):
fit = eos.db.getFit(self.fitID)
for position, chargeID in self.positions.items():
self.__setAmmo([fit.modules[position]], chargeID)
for position, chargeID in self.cache.items():
self.__setAmmo([position], chargeID)
return True
@staticmethod
def __setAmmo(modules, chargeID):
def __setAmmo(self, positions, chargeID):
fit = eos.db.getFit(self.fitID)
self.cache = {fit.modules[i].modPosition: fit.modules[i].chargeID for i in positions}
ammo = eos.db.getItem(chargeID) if chargeID else None
if not ammo.isCharge:
return False
result = False
for mod in modules:
for pos in positions:
mod = fit.modules[pos]
if not mod.isEmpty and mod.isValidCharge(ammo):
result = True
mod.charge = ammo

View File

@@ -13,10 +13,11 @@ class GuiModuleAddChargeCommand(wx.Command):
self.internal_history = wx.CommandProcessor()
self.fitID = fitID
# can set his up no to not have to set variables on our object
self.cmd = FitSetChargeCommand(fitID, modules, itemID)
self.cmd = FitSetChargeCommand(fitID, [mod.modPosition for mod in modules], itemID)
def Do(self):
if self.internal_history.Submit(self.cmd):
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
return True
return False
@@ -24,6 +25,7 @@ class GuiModuleAddChargeCommand(wx.Command):
def Undo(self):
for x in self.internal_history.Commands:
self.internal_history.Undo()
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
return True

View File

@@ -1,36 +1,48 @@
import wx
import eos.db
import gui.mainFrame
from gui import globalEvents as GE
from .calc.fitAddModule import FitAddModuleCommand
from .calc.fitReplaceModule import FitReplaceModuleCommand
from .calc.fitSetCharge import FitSetChargeCommand
from service.fit import Fit
class GuiModuleAddCommand(wx.Command):
def __init__(self, fitID, itemID, position=None):
wx.Command.__init__(self, True, "Module Add")
# todo: evaluate mutaplasmid modules
"""
Handles adding an item, usually a module, to the Fitting Window.
:param fitID: The fit ID that we are modifying
:param itemID: The item that is to be added to the Fitting View. If this turns out to be a charge, we attempt to
set the charge on the underlying module (requires position)
:param position: Optional. The position in fit.modules that we are attempting to set the item to
"""
wx.Command.__init__(self, True, "Module Add: {}".format(itemID))
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
self.sFit = Fit.getInstance()
self.fitID = fitID
self.itemID = itemID
self.internal_history = wx.CommandProcessor()
self.new_position = position
self.position = position
self.old_mod = None
def Do(self):
success = False
# if we have a position set, try to apply the module to that position
# todo: check to see if item is a charge. if it is, dont try to add module, but instead set ammo
if self.new_position:
success = self.internal_history.Submit(FitReplaceModuleCommand(self.fitID, self.new_position, self.itemID))
item = eos.db.getItem(self.itemID)
if item.isCharge and self.position is not None:
success = self.internal_history.Submit(FitSetChargeCommand(self.fitID, [self.position], self.itemID))
if not success:
# something went wrong with trying to fit the module into specific location, attemp to append it
self.new_position = None
return False # if it's a charge item and this failed, nothing more we can try.
# if we have a position set, try to apply the module to that position
elif self.position is not None:
success = self.internal_history.Submit(FitReplaceModuleCommand(self.fitID, self.position, self.itemID))
if not success:
# something went wrong with trying to fit the module into specific location, attempt to append it
self.position = None
# if we're not trying to set module to a position, simply append
if not self.new_position:
if self.position is None:
success = self.internal_history.Submit(FitAddModuleCommand(self.fitID, self.itemID))
if success:
@@ -39,12 +51,6 @@ class GuiModuleAddCommand(wx.Command):
return True
return False
#
# if change is not None:
# print('new position: ',self.new_position )
# # self.slotsChanged() # unsure how to handle this right now? Perhaps move this to the event itself?
# return True
# return False
def Undo(self):
for _ in self.internal_history.Commands:

View File

@@ -10,26 +10,29 @@ from .calc.fitRemoveModule import FitRemoveModuleCommand
class GuiModuleRemoveCommand(wx.Command):
def __init__(self, fitID, modules):
# todo: evaluate mutaplasmid modules
"""
:param fitID: The fit ID that we are modifying
:param modules: A list of Module objects that we are attempting to remove.
"""
wx.Command.__init__(self, True, "Module Remove")
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
self.sFit = Fit.getInstance()
self.fitID = fitID
self.modCache = [ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge) for mod in modules]
self.modCache = [ModuleInfoCache(mod.modPosition, mod.item.ID, mod.state, mod.charge, mod.baseItemID, mod.mutaplasmidID) for mod in modules if not mod.isEmpty]
self.internal_history = wx.CommandProcessor()
def Do(self):
# todo: what happens when one remove in an array of removes fucks up? (it really shouldn't it's easy peasy)
success = self.internal_history.Submit(FitRemoveModuleCommand(self.fitID, [mod.modPosition for mod in self.modCache]))
if success is not None:
# self.slotsChanged() # todo: fix
if success:
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="moddel", typeID=set([mod.itemID for mod in self.modCache])))
return True
return False
def Undo(self):
for x in self.internal_history.Commands:
for _ in self.internal_history.Commands:
self.internal_history.Undo()
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID, action="modadd", typeID=set([mod.itemID for mod in self.modCache])))

View File

@@ -1,3 +1,3 @@
from collections import namedtuple
ModuleInfoCache = namedtuple('ModuleInfoCache', ['modPosition', 'itemID', 'state', 'charge'])
ModuleInfoCache = namedtuple('ModuleInfoCache', ['modPosition', 'itemID', 'state', 'charge', 'baseID', 'mutaplasmidID'])