-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUser.py
113 lines (94 loc) · 4.23 KB
/
User.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from DBSingleton import DBSingleton
class User:
def __init__(self, user_id):
# Default Values
self.user_id = user_id
self._balance = 0
self._referral_users = 0
self._fee_credit = 0
self._language = "en"
self._withdrawal_address = None
self._withdrawal_coin = None
self._amount_to_withdraw = None
self._events = []
self.db = DBSingleton.getInstance().get_db()
self.process_new_user()
def process_new_user(self):
user_data = self.db.read_data([f'Users/{self.user_id}'])
if user_data is None:
# User doesn't exist, add them to the database
new_user_data = {
'balance': self._balance,
'language': self._language,
'withdrawal_address': self._withdrawal_address,
'withdrawal_coin': self._withdrawal_coin,
'amount_to_withdraw': self._amount_to_withdraw,
'events': self._events,
'fee_credit': self._fee_credit
}
self.db.write_data([f'Users/{self.user_id}', new_user_data])
else:
# User already exists, load user data from the database
self._balance = user_data.get('balance', self._balance)
self._language = user_data.get('language', self._language)
self._withdrawal_address = user_data.get('withdrawal_address', self._withdrawal_address)
self._withdrawal_coin = user_data.get('withdrawal_coin', self._withdrawal_coin)
self._amount_to_withdraw = user_data.get('amount_to_withdraw', self._amount_to_withdraw)
self._events = user_data.get('events', self._events)
self._fee_credit = user_data.get('fee_credit', self._fee_credit)
def get_language(self):
return self._language
def get_balance(self):
return round(self._balance, 2)
def set_balance(self, amount):
self._balance = amount
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['balance'] = amount
self.db.write_data([f'Users/{self.user_id}', user_data])
def pay(self, user, amount):
user_balance = user.get_balance()
user.set_balance(user_balance + amount)
self_balance = self.get_balance()
self.set_balance(self_balance - amount)
def get_withdrawal_address(self):
return self._withdrawal_address
def set_withdrawal_address(self, address):
self._withdrawal_address = address
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['withdrawal_address'] = address
self.db.write_data([f'Users/{self.user_id}', user_data])
def set_withdrawal_coin(self, coin):
self._withdrawal_coin = coin
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['withdrawal_coin'] = coin
self.db.write_data([f'Users/{self.user_id}', user_data])
def get_withdrawal_coin(self):
return self._withdrawal_coin
def set_amount_to_withdraw(self, amount):
self._amount_to_withdraw = amount
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['amount_to_withdraw'] = amount
self.db.write_data([f'Users/{self.user_id}', user_data])
def get_amount_to_withdraw(self):
return self._amount_to_withdraw
def get_events(self):
return self._events
def add_new_event(self, event_id):
self._events.append(event_id)
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['events'] = self._events
self.db.write_data([f'Users/{self.user_id}', user_data])
def remove_event(self, event_id):
if event_id not in self._events:
return False
self._events.remove(event_id)
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['events'] = self._events
self.db.write_data([f'Users/{self.user_id}', user_data])
def set_fee_credit(self, credit):
self._fee_credit = credit
user_data = self.db.read_data([f'Users/{self.user_id}'])
user_data['fee_credit'] = credit
self.db.write_data([f'Users/{self.user_id}', user_data])
def get_fee_credit(self):
return self._fee_credit