-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinserts.py
162 lines (133 loc) · 7.41 KB
/
inserts.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
import psycopg2
from lib import *
tables = ['article', 'comment', 'onlinegame', 'onlineuser', 'playerinrealgame',
'playerintournament', 'realgame', 'realplayer', 'task', 'tournament',
'userinonlinegame']
def get_att_list(table, conn):
with conn.cursor() as cursor:
query = "select column_name from information_schema.columns where table_name = '{}'".format(table)
cursor.execute(query)
return [e[0] for e in cursor.fetchall()]
def clean_db(conn):
with conn.cursor() as cursor:
for t in tables:
cursor.execute('TRUNCATE {} CASCADE;'.format(t))
def get_last_id(table, conn):
with conn.cursor() as cursor:
query = "select max(id) from {};".format(table)
cursor.execute(query)
return cursor.fetchone()[0]
def get_attr(table, id, attr, conn):
with conn.cursor() as cursor:
query = "select {} from {} where id={};".format(attr, table, id)
cursor.execute(query)
return cursor.fetchone()[0]
def add_real_player(name, surname, country, conn):
att_list = get_att_list('realplayer', conn)[1:]
with conn.cursor() as cursor:
query = "insert into realplayer ({}) values ('{}', '{}', '{}', {});"\
.format(','.join(att_list), name, surname, country, '1000,' * 6 + '0,' * 5 + '0')
cursor.execute(query)
return get_last_id('realplayer', conn)
def add_online_user(username, email, password, conn, player_id=None):
with conn.cursor() as cursor:
att_list = get_att_list('onlineuser', conn)
query = "insert into onlineuser ({}) values ('{}', {}, '{}', '{}', {})".format(
','.join(att_list), username, "NULL" if player_id is None else player_id, email, password, '0,' * 10 + '0'
)
cursor.execute(query)
return username
def add_tournament(name, date_from, date_to, rule, country, tournament_info, conn):
with conn.cursor() as cursor:
query = "insert into tournament (name, date_from, date_to, rule, country, tournament_info) values "
query += "('{}', {}, {}, '{}', '{}', '{}')"\
.format(name, sql_date(date_from), sql_date(date_to), rule, country, tournament_info)
cursor.execute(query)
return get_last_id('tournament', conn)
def add_real_game(white_id, black_id, result, time_left1, time_left2, date, time_control, control_type,
tournament_id, conn):
# result: 0 - white win, 1 - draw, 2 - black win
with conn.cursor() as cursor:
query = "insert into realgame (tournament_id, game_date, time_control," \
" game_result, control_type) values ({}, {}, '{}', {}, '{}')".format(
tournament_id, sql_date(date), time_control, result, control_type
)
cursor.execute(query)
game_id = get_last_id('realgame', conn)
typo = 'world'
r1 = get_attr('realplayer', white_id, '{}_chess_{}_rating'.format(typo, control_type), conn)
r2 = get_attr('realplayer', black_id, '{}_chess_{}_rating'.format(typo, control_type), conn)
new_r1, new_r2 = new_ratings(r1, r2, result)
query = 'update realplayer set {}_chess_{}_rating = {} WHERE id = {}'\
.format(typo, control_type, str(new_r1), white_id)
cursor.execute(query)
query = 'update realplayer set {}_chess_{}_rating = {} WHERE id = {}'\
.format(typo, control_type, str(new_r2), black_id)
cursor.execute(query)
query = "insert into playerinrealgame (real_player_id, real_game_id, color, time_left, rating_delta)" \
" values ({}, {}, '{}', '{}', {})".format(
white_id, game_id, 'white', time_left1, str(new_r1 - r1)
)
cursor.execute(query)
query = "insert into playerinrealgame (real_player_id, real_game_id, color, time_left, rating_delta) values" \
" ({}, {}, '{}', '{}', {})".format(
black_id, game_id, 'black', time_left2, str(new_r2 - r2)
)
cursor.execute(query)
if result == 0:
wins_as_white = get_attr('realplayer', white_id, 'wins_as_white', conn)
losses_as_black = get_attr('realplayer', black_id, 'losses_as_black', conn)
query = 'update realplayer set wins_as_white = {} where id = {}'\
.format(str(wins_as_white + 1), str(white_id))
cursor.execute(query)
query = 'update realplayer set losses_as_black = {} where id = {}'\
.format(str(losses_as_black + 1), str(black_id))
cursor.execute(query)
elif result == 2:
losses_as_white = get_attr('realplayer', white_id, 'losses_as_white', conn)
wins_as_black = get_attr('realplayer', black_id, 'wins_as_black', conn)
query = 'update realplayer set losses_as_white = {} where id = {}'\
.format(str(losses_as_white + 1), str(white_id))
cursor.execute(query)
query = 'update realplayer set wins_as_black = {} where id = {}'\
.format(str(wins_as_black + 1), str(black_id))
cursor.execute(query)
elif result == 1:
draws_as_white = get_attr('realplayer', white_id, 'draws_as_white', conn)
draws_as_black = get_attr('realplayer', black_id, 'draws_as_black', conn)
query = 'update realplayer set draws_as_white = {} where id = {}'\
.format(str(draws_as_white + 1), str(white_id))
cursor.execute(query)
query = 'update realplayer set draws_as_black = {} where id = {}'\
.format(str(draws_as_black + 1), str(black_id))
cursor.execute(query)
return get_last_id('realgame', conn)
def add_player_to_tournament(player_id, tournament_id, conn):
with conn.cursor() as cursor:
query = "insert into playerintournament (tournament_id, real_player_id) values ({}, {})".format(
str(tournament_id), str(player_id)
)
cursor.execute(query)
return get_last_id('playerintournament', conn)
def add_task(author_username, online_game_from_id, real_game_from_id, task_body, difficulty, goal, task_type, conn):
with conn.cursor() as cursor:
real_game_from_id = ("NULL" if real_game_from_id is None else real_game_from_id)
online_game_from_id = ("NULL" if online_game_from_id is None else online_game_from_id)
query = 'insert into task (author_username, online_game_from_id,' \
' real_game_from_id, task_body, difficulty, goal, task_type) '
query += "values ('{}', {}, {}, '{}', {}, '{}', '{}')".format(author_username, online_game_from_id,
real_game_from_id, task_body, str(difficulty), goal,
task_type)
cursor.execute(query)
def all_players(conn):
with conn.cursor() as cursor:
query = "select name from realplayer;"
cursor.execute(query)
ret = cursor.fetchall()
return ret
# with psycopg2.connect(dbname='chessnet', user='max', password='max', host='localhost') as conn:
# clean_db(conn)
# p1 = add_real_player('maksim', 'lavrik', 'Russia', conn)
# p2 = add_real_player('lektor', 'vektor', 'Russia', conn)
# t1 = add_tournament('match of the century', '11.05.2019', '11.05.2019', 'match bo5', 'Russia', 'im just playing russia you know i love you')
# add_real_game(p1, p2, 1, '0:01', '0:01', '11.05.2019', '2h +30s fisher', 'standard', t1, conn)