File: CFBank.py

package info (click to toggle)
crossfire-maps 1.75.0%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 275,656 kB
  • sloc: python: 7,711; sql: 92; sh: 73; makefile: 7
file content (79 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Created by: Joris Bontje <jbontje@suespammers.org>

This module stores bank account information.
"""

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 __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    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:
            balance = self.getbalance(user)
            if balance >= amount:
                self.bankdb[user] = balance - amount
                return 1
        return 0

    def getbalance(self, user):
        self._convert(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()

    def _convert(self, name):
        """Move a player's balance from the player file to the bank."""
        player = Crossfire.FindPlayer(name)
        if player is None:
            return 0
        old_balance = _balance_legacy(player)
        if old_balance > 0:
            Crossfire.Log(Crossfire.LogInfo,
                    "Converting bank account for %s with %d silver" \
                            % (name, old_balance))
            self.deposit(name, old_balance)
            player.WriteKey("balance", "moved-to-bank-file", 1)

def open():
    return CFBank('ImperialBank_DB')

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