Convert CFBank to sqlite3
parent
9838c555ed
commit
376dd8683e
|
@ -4,15 +4,15 @@ Created by: Joris Bontje <jbontje@suespammers.org>
|
||||||
This module stores bank account information.
|
This module stores bank account information.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os.path
|
|
||||||
import shelve
|
|
||||||
|
|
||||||
import Crossfire
|
import Crossfire
|
||||||
|
import CFSqlDb as cfdb
|
||||||
|
|
||||||
class CFBank:
|
class CFBank:
|
||||||
def __init__(self, bankfile):
|
def __init__(self):
|
||||||
self.bankdb_file = os.path.join(Crossfire.LocalDirectory(), bankfile)
|
self.bankdb = cfdb.open()
|
||||||
self.bankdb = shelve.open(self.bankdb_file)
|
|
||||||
|
def init_schema(self):
|
||||||
|
self.bankdb.execute("CREATE TABLE IF NOT EXISTS bank_accounts ('name' TEXT PRIMARY KEY, 'balance' INT);")
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return self
|
return self
|
||||||
|
@ -20,60 +20,51 @@ class CFBank:
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
def ensure(self, user):
|
||||||
|
self.bankdb.execute("INSERT OR IGNORE INTO bank_accounts VALUES (?, 0)", (user,))
|
||||||
|
|
||||||
def deposit(self, user, amount):
|
def deposit(self, user, amount):
|
||||||
if not user in self.bankdb:
|
if amount > 0:
|
||||||
self.bankdb[user] = amount
|
self.ensure(user)
|
||||||
else:
|
self.bankdb.execute("UPDATE bank_accounts SET balance = balance + ? WHERE name=?", (amount, user))
|
||||||
balance = self.bankdb[user]
|
|
||||||
self.bankdb[user] = balance + amount
|
|
||||||
|
|
||||||
def withdraw(self, user, amount):
|
def withdraw(self, user, amount):
|
||||||
if user in self.bankdb:
|
if self.getbalance(user) - amount < 0:
|
||||||
balance = self.getbalance(user)
|
|
||||||
if balance >= amount:
|
|
||||||
self.bankdb[user] = balance - amount
|
|
||||||
return 1
|
|
||||||
return 0
|
return 0
|
||||||
|
else:
|
||||||
|
self.bankdb.execute("UPDATE bank_accounts SET balance = balance - ? WHERE name=?", (amount, user))
|
||||||
|
return 1
|
||||||
|
|
||||||
def getbalance(self, user):
|
def getbalance(self, user):
|
||||||
self._convert(user)
|
self.convert_legacy_balance(user)
|
||||||
if user in self.bankdb:
|
c = self.bankdb.cursor()
|
||||||
return self.bankdb[user]
|
c.execute("SELECT balance FROM bank_accounts WHERE name=?", (user,))
|
||||||
|
result = c.fetchone()
|
||||||
|
if result is not None:
|
||||||
|
return result[0]
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def remove_account(self, user):
|
def remove_account(self, user):
|
||||||
if user in self.bankdb:
|
c.execute("DELETE FROM bank_accounts WHERE name=?", (user,))
|
||||||
del self.bankdb[user]
|
|
||||||
Crossfire.Log(Crossfire.LogDebug,
|
|
||||||
"%s's bank account removed." % user)
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
self.bankdb.commit()
|
||||||
self.bankdb.close()
|
self.bankdb.close()
|
||||||
|
|
||||||
def _convert(self, name):
|
def convert_legacy_balance(self, name):
|
||||||
"""Move a player's balance from the player file to the bank."""
|
"""Move a player's balance from the player file to the bank."""
|
||||||
player = Crossfire.FindPlayer(name)
|
player = Crossfire.FindPlayer(name)
|
||||||
if player is None:
|
if player is None:
|
||||||
return 0
|
return
|
||||||
old_balance = _balance_legacy(player)
|
balance_str = player.ReadKey("balance")
|
||||||
if old_balance > 0:
|
try:
|
||||||
Crossfire.Log(Crossfire.LogInfo,
|
old_balance = int(balance_str)
|
||||||
"Converting bank account for %s with %d silver" \
|
Crossfire.Log(Crossfire.LogInfo, "Converting bank account for %s with %d silver" % (name, old_balance))
|
||||||
% (name, old_balance))
|
|
||||||
self.deposit(name, old_balance)
|
self.deposit(name, old_balance)
|
||||||
player.WriteKey("balance", "moved-to-bank-file", 1)
|
except ValueError:
|
||||||
|
pass
|
||||||
|
player.WriteKey("balance", None, 0)
|
||||||
|
|
||||||
def open():
|
def open():
|
||||||
return CFBank('ImperialBank_DB')
|
return CFBank()
|
||||||
|
|
||||||
def _balance_legacy(player):
|
|
||||||
"""Return the balance of the given player's bank account."""
|
|
||||||
try:
|
|
||||||
balance_str = player.ReadKey("balance")
|
|
||||||
return int(balance_str)
|
|
||||||
except ValueError:
|
|
||||||
return 0
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import os.path
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
import Crossfire
|
||||||
|
|
||||||
|
def open():
|
||||||
|
path = os.path.join(Crossfire.LocalDirectory(), 'crossfire.db')
|
||||||
|
return sqlite3.connect(path)
|
|
@ -0,0 +1,39 @@
|
||||||
|
import os.path
|
||||||
|
|
||||||
|
import Crossfire
|
||||||
|
import CFBank
|
||||||
|
|
||||||
|
def convert_bdb(bank):
|
||||||
|
path = os.path.join(Crossfire.LocalDirectory(), 'ImperialBank_DB.db')
|
||||||
|
if os.path.isfile(path):
|
||||||
|
Crossfire.Log(Crossfire.LogInfo, "Converting ImperialBank_DB.db (BDB)")
|
||||||
|
import berkeleydb.dbshelve as shelve
|
||||||
|
accounts = shelve.open(path, 'r')
|
||||||
|
for account in accounts.keys():
|
||||||
|
name = account.decode('ascii')
|
||||||
|
balance = accounts[account]
|
||||||
|
bank.deposit(name, balance)
|
||||||
|
accounts.close()
|
||||||
|
bak_file = os.path.join(Crossfire.LocalDirectory(), 'ImperialBank_DB.db.bak')
|
||||||
|
os.rename(path, bak_file)
|
||||||
|
|
||||||
|
def convert(bank):
|
||||||
|
path = os.path.join(Crossfire.LocalDirectory(), 'ImperialBank_DB')
|
||||||
|
if os.path.isfile(path):
|
||||||
|
Crossfire.Log(Crossfire.LogInfo, "Converting ImperialBank_DB (DBM)")
|
||||||
|
import shelve
|
||||||
|
s = shelve.open(path, 'r')
|
||||||
|
for name, balance in s.iteritems():
|
||||||
|
bank.deposit(name, balance)
|
||||||
|
s.close()
|
||||||
|
bak_file = os.path.join(Crossfire.LocalDirectory(), 'ImperialBank_DB.bak')
|
||||||
|
os.rename(path, bak_file)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
Crossfire.Log(Crossfire.LogInfo, "Initializing CFBank")
|
||||||
|
with CFBank.CFBank() as bank:
|
||||||
|
bank.init_schema()
|
||||||
|
convert_bdb(bank)
|
||||||
|
convert(bank)
|
||||||
|
|
||||||
|
main()
|
Loading…
Reference in New Issue