1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
# CFBank.py -- CFBank class
# Created by: Joris Bontje <jbontje@suespammers.org>
# CFBank uses the 'shelve' Python library to store persistent data. The
# shelved dictionary cannot be read and written to concurrently (so no '+=' or
# 'append' operations).
#
# The original implementation opened the database with 'writeback' set and
# called 'sync()' after each write operation. The current implementation does
# not set 'writeback' and instead requires 'close()' to be called before data
# is saved. This is the method recommended by the Python reference manual.
import os.path
import shelve
import Crossfire
class CFBank:
def __init__(self, bankfile):
self.bankdb_file = os.path.join(Crossfire.LocalDirectory(),
bankfile)
self.bankdb = shelve.open(self.bankdb_file)
def deposit(self, user, amount):
if not user in self.bankdb:
self.bankdb[user] = amount
else:
balance = self.bankdb[user]
self.bankdb[user] = balance + amount
def withdraw(self, user, amount):
if user in self.bankdb:
if self.bankdb[user] >= amount:
balance = self.bankdb[user]
self.bankdb[user] = balance - amount
return 1
return 0
def getbalance(self, user):
if user in self.bankdb:
return self.bankdb[user]
else:
return 0
def remove_account(self, user):
if user in self.bankdb:
del self.bankdb[user]
Crossfire.Log(Crossfire.LogDebug,
"%s's bank account removed." % user)
return 1
else:
return 0
def close(self):
self.bankdb.close()
|