Implement fighter add/remove commands

This commit is contained in:
Ryan Holmes
2018-08-13 19:35:32 -04:00
parent 513e9d14d8
commit f0983c1468
9 changed files with 160 additions and 10 deletions

View File

@@ -30,6 +30,7 @@ from gui.contextMenu import ContextMenu
from gui.utils.staticHelpers import DragDropHelper
from service.fit import Fit
from service.market import Market
import gui.fitCommands as cmd
class FighterViewDrop(wx.DropTarget):
@@ -269,11 +270,9 @@ class FighterDisplay(d.Display):
event.Skip()
def addItem(self, event):
sFit = Fit.getInstance()
fitID = self.mainFrame.getActiveFit()
trigger = sFit.addFighter(fitID, event.itemID)
if trigger:
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID))
if self.mainFrame.command.Submit(cmd.GuiAddFighterCommand(fitID, event.itemID)):
self.mainFrame.additionsPane.select("Fighters")
event.Skip()
@@ -288,9 +287,7 @@ class FighterDisplay(d.Display):
def removeFighter(self, fighter):
fitID = self.mainFrame.getActiveFit()
sFit = Fit.getInstance()
sFit.removeFighter(fitID, self.original.index(fighter))
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID))
self.mainFrame.command.Submit(cmd.GuiRemoveFighterCommand(fitID, self.original.index(fighter)))
def click(self, event):
event.Skip()

View File

@@ -43,7 +43,8 @@ class ItemRemove(ContextMenu):
elif srcContext == "droneItem":
sFit.removeDrone(fitID, fit.drones.index(selection[0]))
elif srcContext == "fighterItem":
sFit.removeFighter(fitID, fit.fighters.index(selection[0]))
self.mainFrame.command.Submit(cmd.GuiRemoveFighterCommand(fitID, fit.fighters.index(selection[0])))
return # the command takes care of the PostEvent
elif srcContext == "implantItem":
self.mainFrame.command.Submit(cmd.GuiRemoveImplantCommand(fitID, fit.implants.index(selection[0])))
return # the command takes care of the PostEvent

View File

@@ -16,4 +16,6 @@ from .guiToggleCommand import GuiToggleCommandCommand
from .guiAddProjected import GuiAddProjectedCommand
from .guiRemoveProjected import GuiRemoveProjectedCommand
from .guiCargoToModule import GuiCargoToModuleCommand
from .guiModuleToCargo import GuiModuleToCargoCommand
from .guiModuleToCargo import GuiModuleToCargoCommand
from .guiAddFighter import GuiAddFighterCommand
from .guiRemoveFighter import GuiRemoveFighterCommand

View File

@@ -0,0 +1,53 @@
import wx
from service.fit import Fit
import gui.mainFrame
from gui import globalEvents as GE
#from .helpers import ModuleInfoCache
from eos.saveddata.module import Module, State
import eos.db
from logbook import Logger
pyfalog = Logger(__name__)
from eos.saveddata.fighter import Fighter
class FitAddFighterCommand(wx.Command):
""""
from sFit.addFighter
"""
def __init__(self, fitID, itemID, amount=1, replace=False):
wx.Command.__init__(self, True, "Cargo add")
self.fitID = fitID
self.itemID = itemID
self.new_index = None
def Do(self):
fit = eos.db.getFit(self.fitID)
item = eos.db.getItem(self.itemID, eager=("attributes", "group.category"))
try:
fighter = Fighter(item)
except ValueError:
pyfalog.warning("Invalid fighter: {}", item)
return False
if not fighter.fits(fit):
return False
used = fit.getSlotsUsed(fighter.slot)
total = fit.getNumSlots(fighter.slot)
if used >= total:
fighter.active = False
fit.fighters.append(fighter)
self.new_index = fit.fighters.index(fighter)
eos.db.commit()
return True
def Undo(self):
from .fitRemoveFighter import FitRemoveFighterCommand # Avoid circular import
cmd = FitRemoveFighterCommand(self.fitID, self.new_index)
cmd.Do()
return True

View File

@@ -0,0 +1,34 @@
import wx
from gui.fitCommands.helpers import ModuleInfoCache
import eos.db
from logbook import Logger
pyfalog = Logger(__name__)
class FitRemoveFighterCommand(wx.Command):
""""
Fitting command that removes a module at a specified positions
from sFit.removeFighter
"""
def __init__(self, fitID: int, position: int):
wx.Command.__init__(self, True)
self.fitID = fitID
self.position = position
self.change = None
self.removed_item = None
def Do(self):
fitID = self.fitID
fit = eos.db.getFit(fitID)
f = fit.fighters[self.position]
fit.fighters.remove(f)
self.removed_item = f.itemID
eos.db.commit()
return True
def Undo(self):
from gui.fitCommands.calc.fitAddFighter import FitAddFighterCommand # avoids circular import
cmd = FitAddFighterCommand(self.fitID, self.removed_item)
return cmd.Do()

View File

@@ -0,0 +1,30 @@
import wx
from service.fit import Fit
import gui.mainFrame
from gui import globalEvents as GE
from .calc.fitAddFighter import FitAddFighterCommand
class GuiAddFighterCommand(wx.Command):
def __init__(self, fitID, itemID):
wx.Command.__init__(self, True, "Cargo Add")
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
self.sFit = Fit.getInstance()
self.internal_history = wx.CommandProcessor()
self.fitID = fitID
self.itemID = itemID
def Do(self):
cmd = FitAddFighterCommand(self.fitID, self.itemID)
if self.internal_history.Submit(cmd):
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
return True
return False
def Undo(self):
for _ in self.internal_history.Commands:
self.internal_history.Undo()
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
return True

View File

@@ -0,0 +1,32 @@
import wx
from service.fit import Fit
import gui.mainFrame
from gui import globalEvents as GE
from .calc.fitRemoveFighter import FitRemoveFighterCommand
class GuiRemoveFighterCommand(wx.Command):
def __init__(self, fitID, position):
wx.Command.__init__(self, True, "Module Remove")
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
self.sFit = Fit.getInstance()
self.fitID = fitID
self.position = position
self.internal_history = wx.CommandProcessor()
def Do(self):
success = self.internal_history.Submit(FitRemoveFighterCommand(self.fitID, self.position))
if success:
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
return True
return False
def Undo(self):
for _ in self.internal_history.Commands:
self.internal_history.Undo()
self.sFit.recalc(self.fitID)
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
return True

View File

@@ -245,7 +245,7 @@ class MainFrame(wx.Frame):
self.Bind(GE.EVT_SSO_LOGGING_IN, self.ShowSsoLogin)
@property
def command(self):
def command(self) -> wx.CommandProcessor:
return Fit.getCommandProcessor(self.getActiveFit())
def ShowSsoLogin(self, event):

View File

@@ -897,6 +897,7 @@ class Fit(object):
else:
return False
@deprecated
def removeFighter(self, fitID, i, recalc=True):
pyfalog.debug("Removing fighters from fit ID: {0}", fitID)
fit = eos.db.getFit(fitID)