Crossfire Server, Trunk
CFBank.py
Go to the documentation of this file.
1 """
2 Created by: Joris Bontje <jbontje@suespammers.org>
3 
4 This module stores bank account information.
5 """
6 
7 import Crossfire
8 import CFSqlDb as cfdb
9 
10 class CFBank:
11  def __init__(self):
12  self.bankdb = cfdb.open()
13 
14  def init_schema(self):
15  self.bankdb.execute("CREATE TABLE IF NOT EXISTS bank_accounts ('name' TEXT PRIMARY KEY, 'balance' INT);")
16 
17  def __enter__(self):
18  return self
19 
20  def __exit__(self, exc_type, exc_value, traceback):
21  self.close()
22 
23  def ensure(self, user):
24  self.bankdb.execute("INSERT OR IGNORE INTO bank_accounts VALUES (?, 0)", (user,))
25 
26  def deposit(self, user, amount):
27  if amount > 0:
28  self.ensure(user)
29  self.bankdb.execute("UPDATE bank_accounts SET balance = balance + ? WHERE name=?", (amount, user))
30 
31  def withdraw(self, user, amount):
32  if self.getbalance(user) - amount < 0:
33  return 0
34  else:
35  self.bankdb.execute("UPDATE bank_accounts SET balance = balance - ? WHERE name=?", (amount, user))
36  return 1
37 
38  def getbalance(self, user):
39  self.convert_legacy_balance(user)
40  c = self.bankdb.cursor()
41  c.execute("SELECT balance FROM bank_accounts WHERE name=?", (user,))
42  result = c.fetchone()
43  if result is not None:
44  return result[0]
45  else:
46  return 0
47 
48  def remove_account(self, user):
49  c.execute("DELETE FROM bank_accounts WHERE name=?", (user,))
50 
51  def close(self):
52  self.bankdb.commit()
53  self.bankdb.close()
54 
55  def convert_legacy_balance(self, name):
56  """Move a player's balance from the player file to the bank."""
57  player = Crossfire.FindPlayer(name)
58  if player is None:
59  return
60  balance_str = player.ReadKey("balance")
61  try:
62  old_balance = int(balance_str)
63  Crossfire.Log(Crossfire.LogInfo, "Converting bank account for %s with %d silver" % (name, old_balance))
64  self.deposit(name, old_balance)
65  except ValueError:
66  pass
67  player.WriteKey("balance", None, 0)
68 
69 def open():
70  return CFBank()
CFBank.CFBank.deposit
def deposit(self, user, amount)
Definition: CFBank.py:26
CFBank.CFBank.bankdb
bankdb
Definition: CFBank.py:12
CFBank.open
def open()
Definition: CFBank.py:69
CFBank.CFBank.convert_legacy_balance
def convert_legacy_balance(self, name)
Definition: CFBank.py:55
CFBank.CFBank.__enter__
def __enter__(self)
Definition: CFBank.py:17
CFBank.CFBank.withdraw
def withdraw(self, user, amount)
Definition: CFBank.py:31
CFBank.CFBank.ensure
def ensure(self, user)
Definition: CFBank.py:23
CFBank.CFBank.__init__
def __init__(self)
Definition: CFBank.py:11
CFBank.CFBank
Definition: CFBank.py:10
CFBank.CFBank.remove_account
def remove_account(self, user)
Definition: CFBank.py:48
CFBank.CFBank.close
def close(self)
Definition: CFBank.py:51
make_face_from_files.int
int
Definition: make_face_from_files.py:32
CFBank.CFBank.__exit__
def __exit__(self, exc_type, exc_value, traceback)
Definition: CFBank.py:20
CFBank.CFBank.getbalance
def getbalance(self, user)
Definition: CFBank.py:38
CFBank.CFBank.init_schema
def init_schema(self)
Definition: CFBank.py:14