diff --git a/python/CFBank.py b/python/CFBank.py index 268f611ed..9d074be30 100644 --- a/python/CFBank.py +++ b/python/CFBank.py @@ -1,7 +1,16 @@ # CFBank.py -- CFBank class # Created by: Joris Bontje + +# 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()