-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
234 lines (212 loc) · 6.29 KB
/
database.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
import sqlite3
database = 'database.sqlite'
def init_db():
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("""
CREATE table stats (
id integer primary key,
chat text,
count integer
);
""")
cursor.execute("""
CREATE table commands (
id integer primary key,
command text,
count integer
);
""")
cursor.execute("""
CREATE table user (
id integer primary key,
user_id text
);
""")
cursor.execute("""
CREATE table level (
id integer primary key,
chat text,
level text,
game text
);
""")
cursor.execute("""
CREATE table game (
id integer primary key,
chat text,
game text
);
""")
connect.commit()
connect.close()
def check_user(user_id):
result = {}
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM user WHERE user_id='" + str(user_id)+"'")
res = cursor.fetchall()
connect.close()
try:
if res[0]:
result["not_exist"] = False
result["user_id"] = res[0][1]
except:
result["not_exist"] = True
return result
def add_user(user_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM user")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into user values ("+str(new_id)+",'"+str(user_id)+"')")
connect.commit()
connect.close()
def add_level(chat,level,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM level")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into level values ("+str(new_id)+",'"+str(chat)+"','"+str(level)+"','"+str(game)+"')")
connect.commit()
connect.close()
def set_level(chat,level,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM level WHERE chat='"+chat+"' and game='"+game+"'")
result = cursor.fetchall()
connect.close()
if result:
change_level(chat,level,game)
else:
add_level(chat,level,game)
def change_level(chat,level,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE level SET level='"+str(level)+"' WHERE chat='"+chat+"' and game='"+game+"'")
connect.commit()
connect.close()
def add_game(chat,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM game")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into game values ("+str(new_id)+",'"+str(chat)+"','"+str(game)+"')")
connect.commit()
connect.close()
def set_game(chat,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM game WHERE chat='"+chat+"'")
result = cursor.fetchall()
connect.close()
if result:
change_game(chat,game)
else:
add_game(chat,game)
def change_game(chat,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE game SET game='"+str(game)+"' WHERE chat='"+chat+"'")
connect.commit()
connect.close()
def set_stat(chat):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM stats WHERE chat='"+chat+"'")
result = cursor.fetchall()
connect.close()
if result:
increase(chat,result[0][-1]+1)
else:
add(chat)
def increase(chat,count):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE stats SET count="+str(count)+" WHERE chat='"+chat+"'")
connect.commit()
connect.close()
def add(chat):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM stats")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into stats values ("+str(new_id)+",'"+str(chat)+"',1)")
connect.commit()
connect.close()
def set_stat_command(command):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM commands WHERE command='"+command+"'")
result = cursor.fetchall()
connect.close()
if result:
increase_command(command,result[0][-1]+1)
else:
add_command(command)
def increase_command(command,count):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE commands SET count="+str(count)+" WHERE command='"+command+"'")
connect.commit()
connect.close()
def add_command(command):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM commands")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into commands values ("+str(new_id)+",'"+str(command)+"',1)")
connect.commit()
connect.close()
def get_db(table):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM "+table)
result = cursor.fetchall()
connect.close()
return result
def get_game(chat):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM game WHERE chat = '"+chat+"'")
result = cursor.fetchall()
connect.close()
return result
def get_level(chat,game):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM level WHERE game = '"+game+"' and chat = '"+chat+"'")
result = cursor.fetchall()
connect.close()
return result
def get_chats_up_10():
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM stats WHERE count >= 10")
result = cursor.fetchall()
connect.close()
return result
def get_chats_down_10():
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM stats WHERE count < 10")
result = cursor.fetchall()
connect.close()
return result
if __name__ == '__main__':
init_db()