Correctly close bank file in CFBank.py.

Open bank file with writeback set to false (default); setting it to True
was not needed and caused a mysterious bug on my system. The Python
reference manual recommends closing the database after writing data, so
remove all sync() calls and instead force a close() in the end.

git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@18861 282e977c-c81d-0410-88c4-b93c2d0d6712
master
partmedia 2013-08-03 00:26:30 +00:00
parent f37051dcda
commit e1777f2cb3
1 changed files with 14 additions and 7 deletions

View File

@ -1,7 +1,16 @@
# CFBank.py -- CFBank class
# Created by: Joris Bontje <jbontje@suespammers.org>
# CFBank uses the 'shelve' Python library to store persistent data. The shelve
# is opened for R/W operations by default, but cannot be read from and written
# to simultaneously (so no 'append' or '+=' operations).
#
# Updated to use new path functions in CFPython -Todd Mitchell
# In a past implementation, the bank database was opened with 'writeback' set
# to 'True' and called sync() after each write operation, but still suffered
# from strange bank problems.
#
# In its current implementation, close() MUST be called to preserve data
# across runs. This fixed the aforementioned bug for me.
import os.path
import shelve
@ -9,12 +18,10 @@ import shelve
import Crossfire
class CFBank:
bankdb = {}
def __init__(self, bankfile):
self.bankdb_file = os.path.join(Crossfire.LocalDirectory(),
bankfile)
self.bankdb = shelve.open(self.bankdb_file, writeback=True)
self.bankdb = shelve.open(self.bankdb_file)
def deposit(self, user, amount):
if not user in self.bankdb:
@ -22,14 +29,12 @@ class CFBank:
else:
balance = self.bankdb[user]
self.bankdb[user] = balance + amount
self.bankdb.sync()
def withdraw(self, user, amount):
if user in self.bankdb:
if self.bankdb[user] >= amount:
balance = self.bankdb[user]
self.bankdb[user] = balance - amount
self.bankdb.sync()
return 1
return 0
@ -44,7 +49,9 @@ class CFBank:
del self.bankdb[user]
Crossfire.Log(Crossfire.LogDebug,
"%s's bank account removed." % user)
self.bankdb.sync()
return 1
else:
return 0
def close(self):
self.bankdb.close()