-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
205 lines (180 loc) · 5.57 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
import sqlite3
from random import shuffle
con = sqlite3.connect('db.db')
cur = con.cursor()
def insert_player(player_id, username):
con = sqlite3.connect('db.db')
cur = con.cursor()
sql = f'INSERT INTO(player_id, username) VALUES("{
player_id}", "{username}")'
con.commit()
con.close()
def players_amount():
con = sqlite3.connect('db.db')
cur = con.cursor()
sql = 'SELECT * FROM players'
cur.execute(sql)
result = cur.fetchall()
con.commit()
con.close()
return len(result)
def get_mafia_usernames():
con = sqlite3.connect('db.db')
cur = con.cursor()
sql = f'SELECT username FROM players WHERE role = "mafia"'
cur.execute(sql)
mafias = cur.fetchall()
mafia_names = ''
for maf in mafias:
name = maf[0]
mafia_names += name + '\n'
con.close()
return mafia_names
def get_all_alive():
con = sqlite3.connect('db.db')
cur = con.cursor()
sql = f'SELECT username FROM players WHERE dead = 0'
cur.execute(sql)
alive_players = cur.fetchall()
con.close()
alive_players = [row[0] for row in alive_players]
return alive_players
def set_roles(players):
game_roles = ['citizen'] * players
mafias = int(players * 0.3)
for i in range(mafias):
game_roles[i] = 'mafia'
shuffle(game_roles)
con = sqlite3.connect('db.db')
cur = con.cursor()
sql = f'SELECT player_id FROM players'
cur.execute(sql)
player_ids = cur.fetchall()
for role, row in zip(game_roles, player_ids):
sql = f'UPDATE players SET role = "{role}"'
cur.execute(sql)
con.commit()
con.close()
def get_players_roles():
con = sqlite3.connect('db.db')
cur = con.cursor()
sql = f'SELECT player_id, role FROM players'
cur.execute(sql)
data = cur.fetchall()
con.close()
return data
def vote(type, username, player_id):
con = sqlite3.connect('db.db')
cur = con.cursor()
cur.execute(f'''SELECT username
FROM players
WHERE player_id = {player_id}
AND dead = 0
AND voted = 0''')
can_vote = cur.fetchone()
if (can_vote):
cur.execute(
f'''UPDATE players
SET {type} = {type} + 1
WHERE username = "{username}"'''
)
cur.execute(
f'''UPDATE players
SET voted = 1
WHERE player_id = "{player_id}"'''
)
con.commit()
con.close()
return True
con.close
return True
def mafia_kill():
con = sqlite3.connect("db.db")
cur = con.cursor()
cur.execute(
f'''SELECT MAX(mafia_vote)
FROM players'''
)
max_votes = cur.fetchone()[0]
cur.execute(
f'''SELECT COUNT(*)
FROM players
WHERE role = "mafia"
AND dead = 0'''
)
mafia_alive = cur.fetchone()[0]
if (max_votes == mafia_alive):
cur.execute(
f'''SELECT username
FROM players
WHERE mafia_vote = {max_votes}'''
)
username_killed = cur.fetchone()[0]
cur.execute(
f'''UPDATE players
SET dead = 1
WHERE username = "{username_killed}"'''
)
con.commit()
con.close()
return username_killed
def citizens_kill():
con = sqlite3.connect('db.db')
cur = con.cursor()
cur.execute(f'''
SELECT MAX (citizen_vote)
FROM players
''')
max_votes = cur.fetchone()[0]
cur.execute(f'''
SELECT COUNT(*)
WHERE citizen_vote = {max_votes}
FROM players
''')
max_votes_count = cur.fetchone()[0]
username_killed = 'никого'
# проверяем, что только 1 человек с макс. кол-вом голосов
if max_votes_count == 1:
cur.execute(f'''
SELECT username
WHERE citizen_vote = {max_votes}
FROM player
''')
username_killed = cur.fetchone()[0]
# обновляем по полученному нику столбец dead на 1
cur.execute(
f"UPDATE players SET dead = 1 WHERE username = '{username_killed}' ")
# сохраняем изменения
con.commit()
# отключаемся от бд
con.close()
# возвращаем ник изгнанного человека
return username_killed
def clear(dead=False):
con = sqlite3.connect("db.db")
cur = con.cursor()
sql = f'''
UPDATE players
SET citizen_vote = 0,
SET mafia_vote = 0,
SET voted = 0
'''
if dead:
sql += ', SET dead = 0'
cur.execute(sql)
con.commit()
con.close()
def check_winner():
con = sqlite3.connect("db.db")
cur = con.cursor()
sql = f'''SELECT COUNT(*) FROM players
WHERE role = "mafia" AND dead = 0'''
cur.execute(sql)
mafia_alive = cur.fetchone()[0]
sql = f'''SELECT COUNT(*) FROM players
WHERE role != "mafia" AND dead = 0'''
citizen_alive = cur.fetchone()[0]
if mafia_alive >= citizen_alive:
return 'Мафия'
elif mafia_alive == 0:
return 'Мирные жители'