-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_manager.py
292 lines (261 loc) · 9.81 KB
/
user_manager.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import curses
import glob
import json
import os
from pick import pick
from User import User
def get_user_files():
"""
create a string array with all json file name corresponding to all usernames
:return:array
"""
users = []
for file in glob.glob("*.json"):
users.append(file)
if users.__contains__('users.json'):
users.remove('users.json') # Remove users.json from list
else:
with open("users.json", 'w') as file: # Create the file if not exists
file.write('[]')
file.close()
return users
def merge_json_files(filename: list[str]):
"""
Merge all user's json files in one
:param filename:
:return:
"""
result = list()
for f1 in filename:
with open(f1, 'r') as infile:
result.append(json.load(infile))
with open('users.json', 'w') as output_file:
json.dump(result, output_file, indent=2)
def find_user(username: str):
"""
Return true if user_list contains searched user
:param username:
:return: boolean
"""
if get_user_files().__contains__(username + '.json'):
return True
else:
return False
def save_user(user: User):
"""
Create a json file with user's information
:param user:
:return:
"""
# All default value are 0 without username
users = get_user_files()
f = open("users.json", "w")
f.close()
username = user.username
played_games = user.played_games
nbfail = user.nbfail
nbwin = user.nbwin
greatest_score = user.greatest_score
file_name = user.username + ".json"
users.append(file_name)
json_string = {
'username': username,
'played_games': played_games, # default value to 0
'nbfail': nbfail, # default value to 0
'nbwin': nbwin, # default value to 0
'greatest_score': greatest_score, # default value to 0
}
file = open(file_name, "w")
json.dump(json_string, file, indent=2)
file.close()
def get_all_users(stdscr):
"""
return all user in users.json
:return: users
"""
users_list = get_user_files()
merge_json_files(users_list)
with open('users.json') as users:
tab = json.load(users)
if tab:
for p in tab:
stdscr.addstr(12, 0, f"{p['username']}")
else:
new_user('invite', stdscr)
return users_list
def new_user(username: str, stdscr):
"""
Create a new user with class User
:param stdscr:
:param username:str
:return: user:User
"""
users = get_user_files() # Get all username
if users.__contains__(username + ".json"): # Check if username is free
stdscr.addstr(12, 0, f"User {username} already exists. ")
user = User(username) # Create user
user.username = username
save_user(user) # Save user json file wit info
merge_json_files(users) # Update the users.json file
return user
def get_user_info(username: str, stdscr):
"""
return user.tostring
:param username:
:return:str
"""
users = get_user_files()
file_name = username + '.json'
if users.__contains__(file_name):
with open(file_name, 'r+') as f:
data = json.load(f)
if data['played_games'] > 0:
if data['nbfail'] > 0 or data['nbwin'] > 0:
if data['played_games'] == data['nbfail'] + data['nbwin']: # Check if stats can be coherent
stdscr.addstr(12, 0, f"User {data['username']} has {data['played_games']} played games ")
stdscr.addstr(13, 0,
f"with {data['nbfail']} fails ({int((data['nbfail'] / data['played_games']) * 100)}%) ")
stdscr.addstr(14, 0,
f"and {data['nbwin']} wons ({int((data['nbwin'] / data['played_games']) * 100)}%)")
else:
stdscr.addstr(12, 0, 'There are error in played games count....')
else:
stdscr.addstr(12, 0, 'There are error in win or fail count....')
else:
stdscr.addstr(12, 0, "User {data['username']} have never played")
else:
stdscr.addstr(12, 0, "User chosen doesn't exists")
def delete_user(username: str, stdscr):
users = get_user_files()
filename = username + '.json'
if users.__contains__(filename):
os.remove(filename) # Remove the user's json file
get_all_users(stdscr)
stdscr.addstr(12, 0, f"User {username} has been successfully deleted")
stdscr.refresh()
stdscr.getch()
else:
stdscr.addstr(12, 0, "User doesn't exist")
stdscr.refresh()
stdscr.getch()
def update_username(username: str, new_username: str, stdscr):
get_all_users(stdscr)
if find_user(username):
file_name = username + '.json'
new_file_name = new_username + '.json'
users = get_user_files()
if users.__contains__(new_file_name):
stdscr.addstr(12, 0, f"User {username} already exists.")
else:
with open(file_name, 'r+') as f:
users = get_user_files()
data = json.load(f)
data['username'] = new_username # change `username` value.
f.seek(0) # should reset file position to the beginning.
json.dump(data, f, indent=2)
f.truncate() # remove remaining part
merge_json_files(users)
os.rename(file_name, new_file_name)
get_all_users(stdscr)
stdscr.addstr(12, 0, f" The username {username} has been changed to {new_username}")
else:
stdscr.addstr(12, 0, f'The username {username} you have entered was not found')
def add_win(username: str):
if find_user(username):
file_name = username + '.json'
with open(file_name, 'r+') as f:
data = json.load(f)
data['nbwin'] += 1 # increase win stat value.
f.seek(0) # should reset file position to the beginning.
json.dump(data, f, indent=2)
f.truncate() # remove remaining part
users = get_user_files()
merge_json_files(users)
def add_played_game(username: str):
if find_user(username):
file_name = username + '.json'
with open(file_name, 'r+') as f:
data = json.load(f)
data['played_games'] += 1 # change played game value.
f.seek(0) # should reset file position to the beginning.
json.dump(data, f, indent=2)
f.truncate() # remove remaining part
users = get_user_files()
merge_json_files(users)
def add_fail(username: str):
if find_user(username):
file_name = username + '.json' # Get user's file
with open(file_name, 'r+') as f:
data = json.load(f) # load file
data['nbfail'] += 1 # increase fail value.
f.seek(0) # should reset file position to the beginning.
json.dump(data, f, indent=2)
f.truncate() # remove remaining part
users = get_user_files()
merge_json_files(users) # Update the users.json
def users_menu(stdscr):
"""
Display users' manager menu and execute function with user's choice or return to main menu
:return: int
"""
while True:
title = 'Choose an action to do with users'
options = ['Display users list', 'Search a specific user', 'Create a new user',
'Update user username', 'Delete a user', 'Display user info', 'return to main menu']
_, manage_choice = pick(options, title, screen=stdscr)
if manage_choice == 0:
# stdscr.clear()
stdscr.addstr(9, 0, 'Users list:\n')
stdscr.refresh()
get_all_users(stdscr)
stdscr.addstr(10, 0, 'Press any key to continue')
stdscr.getch()
if manage_choice == 1:
stdscr.addstr(9, 0, 'Type the username you want to search\n')
curses.echo()
username = stdscr.getstr(10, 0).decode('utf-8')
curses.noecho()
if find_user(username):
stdscr.addstr(12, 0, f'{username} is in the list')
stdscr.refresh()
stdscr.getch()
else:
stdscr.addstr(12, 0, f'{username} is not in the list')
stdscr.refresh()
stdscr.getch()
if manage_choice == 2:
get_all_users(stdscr)
stdscr.addstr(9, 0, 'Type the username you want to create\n')
curses.echo()
username = stdscr.getstr(10, 0).decode('utf-8')
curses.noecho()
new_user(username)
stdscr.addstr(10, 0, 'Press any key to continue')
if manage_choice == 3:
get_all_users(stdscr)
stdscr.addstr(9, 0, "Type user's username you want to change\n")
curses.echo()
username = stdscr.getstr(10, 0).decode('utf-8')
curses.noecho()
stdscr.addstr(11, 0, "Type the new username\n")
curses.echo()
new_username = stdscr.getstr(12, 0).decode('utf-8')
curses.noecho()
update_username(username, new_username, stdscr)
if manage_choice == 4:
stdscr.clear()
get_all_users(stdscr)
stdscr.addstr(9, 0, "Type user's username who want to delete\n")
curses.echo()
username = stdscr.getstr(10, 0).decode('utf-8')
curses.noecho()
delete_user(username, stdscr)
if manage_choice == 5:
get_all_users(stdscr)
username = input(
'''
Type user's username you want
''')
get_user_info(username, stdscr)
if manage_choice == 6:
return "main"