From ca34217f3bb09cf0c07f2af94f563cc09a6c50e7 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Wed, 9 Sep 2015 20:33:15 -0400 Subject: [PATCH] Added character saving. Deleting characters is broken for now due to using wrong session, but I believe creating character works. Further tests needed --- eos/config.py | 2 +- eos/db/saveddata/queries.py | 1 - eos/saveddata/character.py | 6 +++- .../changeAffectingSkills.py | 1 + gui/mainFrame.py | 8 +++++ gui/mainMenuBar.py | 3 ++ service/character.py | 36 ++++++++++++++++++- 7 files changed, 53 insertions(+), 4 deletions(-) diff --git a/eos/config.py b/eos/config.py index 35e2fe0dc..042605466 100644 --- a/eos/config.py +++ b/eos/config.py @@ -5,7 +5,7 @@ debug = False gamedataCache = True saveddataCache = True gamedata_connectionstring = 'sqlite:///' + unicode(realpath(join(dirname(abspath(__file__)), "..", "staticdata", "eve.db")), sys.getfilesystemencoding()) -saveddata_connectionstring = 'sqlite:///:memory:' +saveddata_connectionstring = 'sqlite:///' + unicode(realpath(join(dirname(abspath(__file__)), "..", "saveddata", "saveddata.db")), sys.getfilesystemencoding()) #Autodetect path, only change if the autodetection bugs out. path = dirname(unicode(__file__, sys.getfilesystemencoding())) diff --git a/eos/db/saveddata/queries.py b/eos/db/saveddata/queries.py index 5c10b6b9b..c67638c1a 100644 --- a/eos/db/saveddata/queries.py +++ b/eos/db/saveddata/queries.py @@ -148,7 +148,6 @@ def getCharacter(lookfor, eager=None): else: eager = processEager(eager) character = character_session.query(Character).options(*eager).filter(Character.ID == lookfor).first() - print character elif isinstance(lookfor, basestring): eager = processEager(eager) character = character_session.query(Character).options(*eager).filter(Character.name == lookfor).first() diff --git a/eos/saveddata/character.py b/eos/saveddata/character.py index 84e5a6763..3b6bd3643 100644 --- a/eos/saveddata/character.py +++ b/eos/saveddata/character.py @@ -21,6 +21,7 @@ from sqlalchemy.orm import validates, reconstructor from eos.effectHandlerHelpers import HandledItem +import eos.db import eos class Character(object): @@ -33,7 +34,6 @@ class Character(object): @classmethod def getSkillList(cls): if cls.__itemList is None: - import eos.db cls.__itemList = eos.db.getItemsByCategory("Skill") return cls.__itemList @@ -150,6 +150,10 @@ class Character(object): def implants(self): return self.__implants + @property + def isDirty(self): + return getattr(self, "dirty", False) + def iterSkills(self): for item in self.getSkillList(): yield self.getSkill(item) diff --git a/gui/builtinContextMenus/changeAffectingSkills.py b/gui/builtinContextMenus/changeAffectingSkills.py index 32530bdb6..6615d42eb 100644 --- a/gui/builtinContextMenus/changeAffectingSkills.py +++ b/gui/builtinContextMenus/changeAffectingSkills.py @@ -94,6 +94,7 @@ class ChangeAffectingSkills(ContextMenu): fitID = self.mainFrame.getActiveFit() self.sFit.changeChar(fitID, self.charID) + wx.PostEvent(self.mainFrame, GE.CharListUpdated()) wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=fitID)) ChangeAffectingSkills.register() diff --git a/gui/mainFrame.py b/gui/mainFrame.py index 83bff353a..90eebfac5 100644 --- a/gui/mainFrame.py +++ b/gui/mainFrame.py @@ -418,6 +418,8 @@ class MainFrame(wx.Frame): self.Bind(wx.EVT_MENU, self.goWiki, id = menuBar.wikiId) # EVE Forums self.Bind(wx.EVT_MENU, self.goForums, id = menuBar.forumId) + # Save current character + self.Bind(wx.EVT_MENU, self.saveChar, id = menuBar.saveCharId) #Clipboard exports self.Bind(wx.EVT_MENU, self.exportToClipboard, id=wx.ID_COPY) @@ -482,6 +484,12 @@ class MainFrame(wx.Frame): atable = wx.AcceleratorTable(actb) self.SetAcceleratorTable(atable) + def saveChar(self, event): + sChr = service.Character.getInstance() + charID = self.charSelection.getActiveCharacter() + sChr.saveCharacter(charID) + wx.PostEvent(self, GE.CharListUpdated()) + def AdditionsTabSelect(self, event): selTab = self.additionsSelect.index(event.GetId()) diff --git a/gui/mainMenuBar.py b/gui/mainMenuBar.py index 06e1dfabf..756bec594 100644 --- a/gui/mainMenuBar.py +++ b/gui/mainMenuBar.py @@ -36,6 +36,7 @@ class MainMenuBar(wx.MenuBar): self.exportHtmlId = wx.NewId() self.wikiId = wx.NewId() self.forumId = wx.NewId() + self.saveCharId = wx.NewId() self.mainFrame = gui.mainFrame.MainFrame.getInstance() @@ -70,6 +71,8 @@ class MainMenuBar(wx.MenuBar): pasteText = "&From Clipboard" + ("\tCTRL+V" if 'wxMSW' in wx.PlatformInfo else "") editMenu.Append(wx.ID_COPY, copyText, "Export a fit to the clipboard") editMenu.Append(wx.ID_PASTE, pasteText, "Import a fit from the clipboard") + editMenu.AppendSeparator() + editMenu.Append(self.saveCharId, "Save Character") # Character menu windowMenu = wx.Menu() diff --git a/service/character.py b/service/character.py index 1f2c78900..0b4ffdef7 100644 --- a/service/character.py +++ b/service/character.py @@ -191,12 +191,46 @@ class Character(object): # Flush incase all0 & all5 weren't in the db yet eos.db.commit() sFit = service.Fit.getInstance() - return map(lambda c: (c.ID, c.name, c == sFit.character), eos.db.getCharacterList()) + + for thing in eos.db.character_session.dirty: + if isinstance(thing, eos.types.Skill): + if not thing.character.isDirty: + thing.character.dirty = True + + return map(lambda c: (c.ID, c.name if not c.isDirty else "{} *".format(c.name), c == sFit.character), eos.db.getCharacterList()) def getCharacter(self, charID): char = eos.db.getCharacter(charID) return char + def saveCharacter(self, charID): + """ + Saves the edited character. + + I feel like there should be a better way to specifically tell + SQLAlchemy to only flush and commit a specific object. But this magic + function doesn't seem to exist, so instead we basically gather a list + of our characters, expunge the entire session, and then add the + edited character all by themself. After committing, we simply add our + characters back to the session. + + Without doing this, this would save all edited characters, which is + not something that should happen. + """ + + char = eos.db.getCharacter(charID) + if not char.isDirty: + return + charList = eos.db.getCharacterList() + + eos.db.character_session.expunge_all() + eos.db.character_session.add(char) + eos.db.character_session.commit() + char.dirty = False + + for char in charList: + eos.db.character_session.add(char) + def getSkillGroups(self): cat = eos.db.getCategory(16) groups = []