-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
128 lines (108 loc) · 2.85 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pickle
import os
def get_users():
users = []
try:
files = os.listdir('SAVE/')
except:
print("Can´t open SAVE dir")
exit(1)
for i in files:
i = './SAVE/' + i
try:
load_user = pickle.load(open(i,'rb'))
users.append(load_user)
except:
print("Cant load file:", i)
if len(users) == 0:
users.append(User("USER"))
return users
class Skin:
def __init__(self, Id, Type, path, price, name):
self.id = Id
self.type = Type
self.path = path
self.price = price
self.name = name
self.active = False
return
# return path to file with skin
def get_path(self):
return self.path
# change price to new price
def change_price(self, price):
self.price = price
return
# get price of skin
def get_price(self):
return self.price
# set state of skin
def set_active(self, state):
self.active = state
# return True if skin is active
def is_active(self):
return self.active
# return name of skin
def get_name(self):
return self.name
# define user which has all needed data saved internally
class User:
def __init__(self, name):
self.name = name
self.balance = 100
self.skins = []
self.save()
return
# add balance to user
# return new balance
def add_balance(self, add):
self.balance += add
self.save()
return self.balance
# get users balance
# ret actual balance
def get_balance(self):
return self.balance
# subtract from balance
# ret actual balance
def sub_balance(self, sub):
self.balance -= sub
self.save()
return self.balance
# get users name
# return name of user
def get_name(self):
return self.name
# add new skin to user
def add_skin(self, skin):
self.skins.append(skin)
self.save()
return
# return users skins
def get_skins(self):
return self.skins
def save(self):
temp = 'SAVE/' + self.get_name() + '.pkl'
temp_bkp = temp + '.bkp'
try:
os.rename(temp, temp_bkp)
except:
pass
try:
pickle.dump(self, open(temp,'wb'), pickle.HIGHEST_PROTOCOL)
except:
os.rename(temp_bkp, temp)
print("Can´t dump pickle to file:",temp)
return
try:
os.remove(temp_bkp)
except:
pass
return
def delete(self):
temp = 'SAVE/' + self.get_name() + '.pkl'
try:
os.remove(temp)
except:
pass
return