-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path__init__.py
52 lines (39 loc) · 1.47 KB
/
__init__.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
import os
import dbm
import uuid
from datetime import datetime
current_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(current_dir, ".dat")
u_path = os.path.join(data_path, "u")
def set_user_id():
if not os.path.exists(data_path):
os.makedirs(data_path)
with dbm.open(u_path, 'c') as db:
if b'user_id' not in db:
db[b'user_id'] = str(uuid.uuid4()).encode('utf-8')
db[b'balance'] = str(0).encode('utf-8')
db[b'last_login'] = datetime.now().isoformat().encode('utf-8')
def get_current_user():
user = {}
if not os.path.exists(data_path):
os.makedirs(data_path)
with dbm.open(u_path, 'c') as db:
# Check if user exists
if b'user_id' not in db:
db[b'user_id'] = str(uuid.uuid4()).encode('utf-8')
if b'balance' not in db:
db[b'balance'] = str(0).encode('utf-8')
if b'inventory' not in db:
# Initialize an empty inventory
db[b'inventory'] = str({}).encode('utf-8')
db[b'last_login'] = datetime.now().isoformat().encode('utf-8')
# return data
for key in db.keys():
user[key.decode('utf-8')] = db[key].decode('utf-8')
return user
def update_balance(balance):
with dbm.open(u_path, 'w') as db:
db[b'balance'] = str(balance).encode('utf-8')
def update_inventory(inventory):
with dbm.open(u_path, 'w') as db:
db[b'inventory'] = str(inventory).encode('utf-8')