-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.py
414 lines (298 loc) · 10.3 KB
/
DB.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import sqlite3 as sql
from datetime import datetime
def create_table() -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS users(
user_id INT PRIMARY KEY,
user_name TEXT,
FIO TEXT,
branch TEXT,
score INT,
time BIGINT);
""")
connection.commit()
cursor.execute("""CREATE TABLE IF NOT EXISTS users_stations(
user_id INT,
question_id INT,
question_group INT,
time DATETIME,
question_opened bit,
CONSTRAINT [users_stations_PK] PRIMARY KEY (user_id, question_id, question_group),
CONSTRAINT [FK_stations_users] FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE);
""")
connection.commit()
cursor.execute("""CREATE TABLE IF NOT EXISTS flags(
finished bit,
closed bit);
""")
cursor.execute("insert into flags values(0, 1)")
connection.commit()
connection.close()
def drop_table():
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute("""drop TABLE IF EXISTS users
""")
cursor.execute("""drop TABLE IF EXISTS users_stations
""")
cursor.execute("""drop TABLE IF EXISTS flags
""")
connection.commit()
connection.close()
def get_finished_flag():
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
select finished from flags;
""")
result = cursor.fetchall()
connection.close()
if result[0][0] == 0:
return False
else:
return True
def set_finished_flag(flag: bool):
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
update flags set finished = '{int(flag)}';
""")
connection.commit()
connection.close()
def get_closed_flag():
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
select closed from flags;
""")
result = cursor.fetchall()
connection.close()
if result[0][0] == 0:
return False
else:
return True
def set_closed_flag(flag: bool):
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
update flags set closed = '{int(flag)}';
""")
connection.commit()
connection.close()
def add_user(user_id: int, user_name: str) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""insert into users
values('{user_id}', '{user_name}', '', '', 0, 0)""")
connection.commit()
connection.close()
def set_user_fio(user_id: int, fio: str) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""Update users
set FIO = '{fio}'
where user_id = '{user_id}';""")
connection.commit()
connection.close()
def set_user_branch(user_id: int, branch: str) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""Update users
set branch = '{branch}'
where user_id = '{user_id}';""")
connection.commit()
connection.close()
def get_auth(user_id: int) -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
select FIO, branch from users
where user_id = {user_id}
""")
result = cursor.fetchall()
connection.close()
if len(result) > 0:
return result[0]
else:
return []
def get_users_list() -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM users;")
result = cursor.fetchall()
connection.close()
return result
def get_stations_list() -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM users_stations;")
result = cursor.fetchall()
connection.close()
return result
def drop_user(user_id: int) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"delete from users"
f" where user_id = '{user_id}';")
cursor.execute(f"delete from users_stations"
f" where user_id = '{user_id}';")
connection.commit()
connection.close()
def get_first_group_stations(user_id: int) -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
SELECT question_id, question_opened FROM users_stations where user_id = '{user_id}' and question_group = 0
order by question_opened;""")
string_result = cursor.fetchall()
connection.close()
try:
return string_result
except:
return []
def get_second_group_stations(user_id: int) -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
SELECT question_id, question_opened FROM users_stations where user_id = '{user_id}' and question_group = 1
order by question_opened;
""")
string_result = cursor.fetchall()
connection.close()
try:
return string_result
except:
return []
def get_third_group_stations(user_id: int) -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
SELECT question_id, question_opened FROM users_stations where user_id = '{user_id}' and question_group = 2
order by question_opened;""")
string_result = cursor.fetchall()
connection.close()
try:
return string_result
except:
return []
def open_station(user_id: int, question_id: int, group: int) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""insert into users_stations
values('{user_id}', '{question_id}', '{group}', '{datetime.now()}', 1);""")
connection.commit()
connection.close()
def get_time_delt(user_id: int, question_id: int, group: int) -> int:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""
SELECT time FROM users_stations
where user_id = '{user_id}' and question_group = '{group}' and question_id = '{question_id}'
""")
result = cursor.fetchall()
connection.close()
try:
return (datetime.now() - datetime.fromisoformat(result[0][0])).seconds
except:
return -1
def close_station(user_id: int, question_id: int, group: int) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""Update users_stations
set question_opened = 0
where user_id = '{user_id}' and question_id = '{question_id}' and question_group = '{group}';""")
connection.commit()
connection.close()
def get_score(user_id: int) -> int:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"SELECT score FROM users where user_id = '{user_id}';")
string_result = cursor.fetchall()
result = string_result[0][0]
connection.close()
return result
def get_oi_leaders_list() -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""SELECT * FROM users where branch = 'ОИТ'
order by score desc, time;""")
string_result = cursor.fetchall()
connection.close()
return string_result[:3]
def get_oe_leaders_list() -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""SELECT * FROM users where branch = 'ОЭиС'
order by score desc, time;""")
string_result = cursor.fetchall()
connection.close()
return string_result[:3]
def get_om_leaders_list() -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""SELECT * FROM users where branch = 'ОМЭиКС'
order by score desc, time;""")
string_result = cursor.fetchall()
connection.close()
return string_result[:3]
def get_oo_leaders_list() -> []:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"""SELECT * FROM users where branch = 'ООПНиПТ'
order by score desc, time;""")
string_result = cursor.fetchall()
connection.close()
return string_result[:3]
def add_score(user_id: int, value: int) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
score = int(get_score(user_id)) + value
cursor.execute(f"""
Update users
set score = {score}
where user_id = '{user_id}';
""")
connection.commit()
connection.close()
def get_time(user_id: int) -> int:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"SELECT time FROM users where user_id = '{user_id}';")
string_result = cursor.fetchall()
result = string_result[0][0]
connection.close()
return result
def add_time(user_id: int, value: int) -> None:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
time = int(get_time(user_id)) + value
cursor.execute(f"""
Update users
set time = {time}
where user_id = '{user_id}';
""")
connection.commit()
connection.close()
def user_exist(user_id: int) -> bool:
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM users where user_id = {user_id};")
result = cursor.fetchall()
connection.close()
if len(result) != 0:
return True
else:
return False
def get_stat():
connection = sql.connect('userbase.db')
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM users where branch = 'ОИТ' order by score desc, time;")
result = cursor.fetchall()
connection.close()
temp1 = get_oo_leaders_list()
temp2 = get_oi_leaders_list()
temp3 = get_om_leaders_list()
temp4 = get_oe_leaders_list()
for elem in temp4:
print(f"{elem[2]} \t {elem[4]} \t {elem[5]}")