-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.py
279 lines (208 loc) · 7.25 KB
/
mysql.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
import pymysql
import keys
def connect_to_db():
connection = pymysql.connect(
host=keys.dbhost,
user=keys.dbuser,
password=keys.dbpass,
db=keys.db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return connection
def commit(sql, args):
connection = connect_to_db()
with connection.cursor() as cursor:
res = cursor.execute(sql, args)
connection.commit()
connection.close()
return res
def commit_and_check(sql, args):
connection = connect_to_db()
with connection.cursor() as cursor:
cursor.execute(sql, args)
cursor.execute("SELECT LAST_INSERT_ID()", ())
result = cursor.fetchone()
connection.commit()
connection.close()
return result
def fetch(sql, args, one: bool):
connection = connect_to_db()
with connection.cursor() as cursor:
cursor.execute(sql, args)
if one:
result = cursor.fetchone()
else:
result = cursor.fetchall()
connection.close()
return result
def fetchall(sql, args):
return fetch(sql, args, one=False)
def fetchone(sql, args):
return fetch(sql, args, one=True)
def values_subs(fields):
subs = "("
for n in range(len(fields)-1):
subs += "%s, "
subs += "%s)"
return subs
def inline_subs(fields):
subs = ""
for field in fields[:-1]:
subs += field + "=%s, "
subs += fields[-1] + "=%s"
return subs
def keys2str(hm):
fields = [key for key in hm]
str_fields = "("
for field in fields[:-1]:
str_fields += str(field) + ", "
str_fields += str(fields[-1]) + ")"
return str_fields
def insert_dict(table: str, dictionary: dict):
hm = dictionary.copy()
values = tuple([hm[key] for key in hm])
sql = "INSERT INTO " + table + " " + keys2str(hm) + " VALUES " + values_subs(values)
commit(sql, values)
def insert_dict_and_check(table: str, dictionary: dict):
hm = dictionary.copy()
values = tuple([hm[key] for key in hm])
sql = "INSERT INTO " + table + " " + keys2str(hm) + " VALUES " + values_subs(values)
return commit_and_check(sql, values)
def update_dict(table: str, game_hm: dict, where: tuple):
hm = game_hm.copy()
where_values = [hm[w] for w in where]
for w in where:
del hm[w]
fields = [key for key in hm]
where_str = ""
for where_arg in where[:-1]:
where_str += where_arg + "=%s AND "
where_str += where[-1] + "=%s"
sql = "UPDATE " + table + " SET " + inline_subs(fields) + " where " + where_str
args = [hm[key] for key in hm] + where_values
commit(sql, args)
# -------------- usable ----------------
# game
def update_game(game_hm: dict, where='game_id'):
update_dict('games', game_hm, (where,))
def get_game(value, field='game_id'):
sql = "SELECT * FROM games WHERE " + field + " = %s"
return fetchone(sql, (value,))
def insert_game(game):
return insert_dict_and_check('games', game)
def rank_game(game_id):
fields = ['ranked']
sql = "UPDATE games SET " + inline_subs(fields) + " where game_id=%s"
commit(sql, (1, game_id))
def unrank_game(game_id):
fields = ['ranked']
sql = "UPDATE games SET " + inline_subs(fields) + " where game_id=%s"
commit(sql, (0, game_id))
# player
def insert_player(player):
return insert_dict_and_check('player', player)
def get_player(name):
sql = "SELECT * FROM player WHERE name = %s"
return fetchone(sql, (name,))
def get_player_id(player_id):
sql = "SELECT * FROM player WHERE player_id = %s"
return fetchone(sql, (player_id,))
def get_player_bnet(bnet_tag):
sql = "SELECT * FROM player WHERE bnet_tag = %s OR bnet_tag2 =%s"
return fetchone(sql, (bnet_tag, bnet_tag,))
def get_player_discord_id(discord_id):
sql = "SELECT * FROM player WHERE discord_id = %s"
return fetchone(sql, (discord_id,))
def update_player(player: dict, where = 'player_id'):
update_dict('player', player, (where,))
# player game
def insert_player_game(player_game):
insert_dict('player_game', player_game)
def get_player_games(player_id):
sql = "SELECT * FROM player_game WHERE player_id=%s"
return fetchall(sql, (player_id,))
def update_player_game(player_game):
update_dict('player_game', player_game, ('player_id', 'game_id'))
testgame = {
'mode': 'cdzm',
'winner': 'scourge',
'duration': 2000,
'upload_time': '12345678_01h02m03s',
'ranked': 2,
'hash': None,
'team1_elo': 1050.0,
'team2_elo': 1025.0,
'team1_elo_change': 25.0,
'elo_alg': '1.0'
}
def get_elo_history(bnet_tag):
sql = """(
select
pg.elo_before
from
player_game pg,
player p
where
pg.player_id = p.player_id and
p.bnet_tag=%s
order by pg.game_id asc
) UNION (
select
elo
from
player
where
bnet_tag=%s
);
"""
return fetchall(sql, (bnet_tag, bnet_tag))
if __name__ == '__main__':
print(get_elo_history("ricefire#1366"))
"""
select g.game_id, g.ranked, g.upload_time, blue.name, pink.name from
games g, player_game bluepg, player_game pinkpg, player blue, player pink where
g.game_id=bluepg.game_id and
g.game_id=pinkpg.game_id and
bluepg.slot_nr=0 and
pinkpg.slot_nr=5 and
blue.player_id=bluepg.player_id and
pink.player_id=pinkpg.player_id;
select * from player where name='one_legion'
select g.game_id, g.ranked, g.upload_time, blue.name, pink.name from games g, player_game bluepg, player_game pinkpg, player blue, player pink where g.game_id=bluepg.game_id and g.game_id=pinkpg.game_id and bluepg.slot_nr=0 and pinkpg.slot_nr=5 and blue.player_id=bluepg.player_id and pink.player_id=pinkpg.player_id order by upload_time ASC;
select g.game_id, g.ranked, g.upload_time, blue.name as blue, pink.name as pink from games g, player_game bluepg, player_game pinkpg, player blue, player pink where g.game_id=bluepg.game_id and g.game_id=pinkpg.game_id and bluepg.slot_nr=0 and pinkpg.slot_nr=5 and blue.player_id=bluepg.player_id and pink.player_id=pinkpg.player_id order by upload_time ASC;
firstbl5_dotastats_v2.
# all games a certain player has played?
select g.game_id, g.ranked, g.winner, g.upload_time, blue.name as blue, pink.name as pink
from
games g, player_game bluepg, player_game pinkpg, player blue, player pink, player who
where
g.game_id=bluepg.game_id and
g.game_id=pinkpg.game_id and
bluepg.slot_nr=0 and
pinkpg.slot_nr=5 and
blue.player_id=bluepg.player_id and
pink.player_id=pinkpg.player_id and
who.name='one_legion'
order by upload_time ASC";
select g.game_id, g.winner, apg.slot_nr as a_slot_nr, bpg.slot_nr as b_slot_nr
from
games g, player_game apg, player_game bpg, player a, player b
where
a.name = 'wc3addict' and
b.name = 'fook' and
a.player_id = apg.player_id and
b.player_id = bpg.player_id and
apg.game_id = bpg.game_id and
g.game_id = apg.game_id;
# get captain win/loss
select g.game_id, g.winner, blue.name as blue, pink.name as pink
from
games g, player_game bluepg, player_game pinkpg, player blue, player pink
where
g.game_id=bluepg.game_id and
g.game_id=pinkpg.game_id and
bluepg.slot_nr=0 and
pinkpg.slot_nr=5 and
blue.player_id=bluepg.player_id and
pink.player_id=pinkpg.player_id;
"""