-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
153 lines (115 loc) · 4.03 KB
/
tools.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
import argparse
import sqlite3 as sql
from random import choice
from scrap import get_user_score
DB_NAME = 'codewars_tracking.db'
RAND_LIST = [chr(i) for i in range(97, 123)] + [chr(i) for i in range(65, 91)]
def get_score(iden):
score_data = get_user_score(iden)
if not score_data['valid']:
code = score_data['code']
print(f'Error when fetching player score: {code}')
exit()
return score_data['score']
def init_db(cur):
cur.execute(
'CREATE TABLE Players '
'(ID text PRIMARY KEY, Name text, Start int, Curr int) WITHOUT ROWID'
)
def add(cur, iden, name):
score = get_score(iden)
cur.execute(
'INSERT INTO Players '
'(ID, Name, Start, Curr) '
f"VALUES('{iden}', '{name}', {score}, {score})"
)
print(f'Player "{iden}" has been registered with display name "{name}"')
print(f'Starting score: {score}')
def reset(cur, iden, check_exists=True):
if check_exists:
length = cur.execute(f"SELECT ID = '{iden}' FROM Players").fetchall()
if length[0][0] != 1:
print(f'Player "{iden}" is not registered in this competition.')
return
score = get_score(iden)
cur.execute(
'UPDATE Players '
f'SET Start = {score}, Curr = {score} '
f"WHERE ID = '{iden}'"
)
print(f'Player "{iden}"\'s progress in this competition has been reset.')
print(f'New starting score: {score}')
def remove(cur, iden):
cur.execute(f"DELETE FROM Players WHERE ID = '{iden}'")
print(f'Player "{iden}" has been removed.')
def reset_all(cur):
# cuz making ppl type random stuff is fun
random = ''.join(choice(RAND_LIST) for i in range(8))
ans = input(
'This will restart the competition tracking by syncing '
'everyone\'s starting score to their current score.\nAre you sure?\n'
f'Enter "{random}" to confirm: '
)
if ans != random:
print('Confirmation message does not match. Aborting.')
return
ids = [i[0] for i in cur.execute('SELECT ID FROM Players').fetchall()]
for i in ids:
reset(cur, i, False)
def sync(cur):
ids = [i[0] for i in cur.execute('SELECT ID FROM Players').fetchall()]
for i in ids:
score = get_score(i)
cur.execute(
'UPDATE Players '
f'SET Curr = {score} '
f"WHERE ID = '{i}'"
)
print(f'Player "{i}" has been updated with current score {score}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Tools for manipulating the Codewars tracking process.'
)
subparsers = parser.add_subparsers(dest='action')
subparsers.required = True
sync_parser = subparsers.add_parser(
'sync', help='sync all players\' score from Codewars'
)
init_parser = subparsers.add_parser('init', help='initializes a player')
reset_parser = subparsers.add_parser(
'resetAll',
help='resets all player\'s score'
)
add_parser = subparsers.add_parser('add', help='adds a player')
set_parser = subparsers.add_parser(
'reset', help='resets the score of a player'
)
remove_parser = subparsers.add_parser('remove', help='removes a player')
id_params = {
'dest': 'id',
'type': str,
'help': 'the player\'s Codewars ID'
}
add_parser.add_argument(**id_params)
add_parser.add_argument(
'name',
type=str,
help='the player\'s display name'
)
set_parser.add_argument(**id_params)
remove_parser.add_argument(**id_params)
args = parser.parse_args()
with sql.connect(DB_NAME) as con:
cur = con.cursor()
if args.action == 'init':
init_db(cur)
elif args.action == 'add':
add(cur, args.id, args.name)
elif args.action == 'reset':
reset(cur, args.id)
elif args.action == 'remove':
remove(cur, args.id)
elif args.action == 'resetAll':
reset_all(cur)
elif args.action == 'sync':
sync(cur)