-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabases.py
More file actions
462 lines (393 loc) · 14.1 KB
/
Copy pathdatabases.py
File metadata and controls
462 lines (393 loc) · 14.1 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import asyncio
import os
import sqlite3
import discord
from utils import Utils
class Database:
bot: discord.Bot | None = None
TEST_CHANNEL_TO_PRINT_DB_LOGS = 1417887751080116234
REAL_CHANNEL_TO_PRINT_DB_LOGS = 1417887126695182397
GAMES_DB = "dbs/games.db"
TASKS_DB = "dbs/tasks.db"
EVENTS_DB = "dbs/events.db"
@classmethod
def init(cls, bot: discord.Bot):
cls.bot = bot
@classmethod
def _log(cls, message: str):
try:
if cls.bot is None:
print(f"{message}")
return
channel = (
cls.bot.get_channel(cls.TEST_CHANNEL_TO_PRINT_DB_LOGS)
if Utils.is_test_environment()
else cls.bot.get_channel(cls.REAL_CHANNEL_TO_PRINT_DB_LOGS)
)
if channel:
asyncio.run_coroutine_threadsafe(channel.send(message), cls.bot.loop)
print(f"{message}")
except Exception as e:
print(f"{e}: here is the message that didn't get logged:\n\t{message}")
@staticmethod
def add_game(name, repo_name, channel_id, owner):
Database.insert_into_db(
Database.GAMES_DB,
"games",
name=name,
repo_name=repo_name,
channel_id=channel_id,
owner=owner.name,
owner_display_name=owner.display_name,
)
@staticmethod
def get_game_info(channel_id):
return Database.fetch_one_as_dict(
Database.GAMES_DB, "games", "channel_id = ?", (channel_id,)
)
@staticmethod
def get_game_channel(game_id):
game = Database.fetch_one_as_dict(
Database.GAMES_DB, "games", "id = ?", (game_id,)
)
return game["channel_id"] if game else None
@staticmethod
def get_default_game_info():
return Database.fetch_one_as_dict(Database.GAMES_DB, "games", "id = ?", (1,))
@staticmethod
def get_game_leads(game_id):
list = []
game = Database.fetch_one_as_dict(
Database.GAMES_DB, "games", "id = ?", (game_id,)
)
list.append(game["owner"])
contributors = Database.fetch_all_as_dict_arr(
Database.GAMES_DB,
"game_contributors",
"game_id = ? AND role = 'Project Lead'",
(game_id,),
)
for contributor in contributors:
contributor_info = Database.fetch_one_as_dict(
Database.GAMES_DB,
"contributors",
"id = ?",
(contributor["contributor_id"],),
)
if contributor_info:
list.append(contributor_info["discord_username"])
return list
@staticmethod
def add_task(user_id, description, deadline=None, event_id=None):
Database.insert_into_db(
Database.TASKS_DB,
"tasks",
user_id=user_id,
description=description,
deadline=deadline,
event_id=event_id,
)
@staticmethod
def register_contributor(
discord_username,
credit_name,
discord_display_name=None,
itch_io_link=None,
alt_link=None,
time_zone=None,
):
# if time_zone is provided, clamp it to valid range
if time_zone is not None:
time_zone = max(-12, min(14, time_zone))
Database.insert_into_db(
Database.GAMES_DB,
"contributors",
discord_username=discord_username,
credit_name=credit_name,
discord_display_name=discord_display_name,
itch_io_link=itch_io_link,
alt_link=alt_link,
time_zone=time_zone,
)
@staticmethod
def add_asset_request(game_id, asset_type, content, context, requested_by):
Database.insert_into_db(
Database.GAMES_DB,
"asset_requests",
game_id=game_id,
asset_type=asset_type,
content=content,
context=context,
requested_by=requested_by,
status="Pending",
)
@staticmethod
def mark_request_accepted(request_id, user):
Database.update_field(
Database.GAMES_DB, "asset_requests", request_id, "status", "Accepted"
)
Database.update_field(
Database.GAMES_DB, "asset_requests", request_id, "accepted_by", user
)
@staticmethod
def mark_request_finished(request_id):
Database.update_field(
Database.GAMES_DB, "asset_requests", request_id, "status", "Finished"
)
@staticmethod
def is_request_pending(request: dict) -> bool:
row = Database.fetch_one_as_dict(
Database.GAMES_DB,
"asset_requests",
"id = ? AND status = 'Pending'",
(request["id"],),
)
return row is not None
@staticmethod
def get_asset_requests_by_type(type, status="Pending", user=None):
if user:
return Database.fetch_all_as_dict_arr(
Database.GAMES_DB,
"asset_requests",
# "asset_type = ? AND status = ? AND substr(requested_by, 1, instr(requested_by, ' ') - 1) = ?",
"asset_type = ? AND status = ? AND requested_by = ?",
(type, status, user),
)
else:
return Database.fetch_all_as_dict_arr(
Database.GAMES_DB,
"asset_requests",
f"asset_type = ? AND status = '{status}'",
(type,),
)
@staticmethod
def remove_asset_requests_for_game(game_id):
Database.delete_from_db(
Database.GAMES_DB,
"asset_requests",
"game_id = ?",
(game_id,),
)
@staticmethod
def remove_contributor_requests_for_game(game_id):
Database.delete_from_db(
Database.GAMES_DB,
"contributor_requests",
"game_id = ?",
(game_id,),
)
@staticmethod
def insert_into_db(db_path, table, **columns) -> bool:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
keys = ", ".join(columns.keys())
placeholders = ", ".join(["?"] * len(columns))
values = tuple(columns.values())
new_lines = "\n".join(f" {k}: {v!r}" for k, v in columns.items())
try:
cursor.execute(
f"INSERT INTO {table} ({keys}) VALUES ({placeholders})", values
)
conn.commit()
Database._log(f"**New entry in `{table}`**\n```\n{new_lines}\n```\n")
return True
except sqlite3.IntegrityError as e:
Database._log(
f"**New entry failed to insert in `{table}`**: {e}\n```\n{new_lines}\n```\n"
)
return False
finally:
conn.close()
@staticmethod
def update_field(db_path: str, table: str, row_id: int, field: str, value):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table} WHERE id = ?", (row_id,))
old_row = cursor.fetchone()
col_names = [desc[0] for desc in cursor.description]
old_dict = dict(zip(col_names, old_row)) if old_row else {}
# Use parameterized query to avoid SQL injection
query = f"UPDATE {table} SET {field} = ? WHERE id = ?"
cursor.execute(query, (value, row_id))
conn.commit()
cursor.execute(f"SELECT * FROM {table} WHERE id = ?", (row_id,))
new_row = cursor.fetchone()
new_dict = dict(zip(col_names, new_row)) if new_row else {}
conn.close()
old_lines = "\n".join(f" {k}: {v!r}" for k, v in old_dict.items())
new_lines = "\n".join(f" {k}: {v!r}" for k, v in new_dict.items())
log_message = f"**Updated `{table}`**\nBefore:\n```\n{old_lines}\n```\nAfter:\n```\n{new_lines}\n```"
Database._log(log_message)
@staticmethod
def fetch_one_as_dict(
db_path: str, table: str, where: str, params: tuple = ()
) -> dict | None:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
query = f"SELECT * FROM {table} WHERE {where} LIMIT 1"
print(f"Executing query: {query} with params: {params}")
cursor.execute(query, params)
row = cursor.fetchone()
if not row:
print("No row found")
conn.close()
return None
col_names = [desc[0] for desc in cursor.description]
conn.close()
return dict(zip(col_names, row))
@staticmethod
def fetch_all_as_dict_arr(
db_path: str, table: str, where: str = "1=1", params: tuple = ()
) -> list[dict]:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
query = f"SELECT * FROM {table} WHERE {where}"
print(f"Executing query: {query} with params: {params}")
cursor.execute(query, params)
rows = cursor.fetchall()
if not rows:
print("No rows found")
conn.close()
return []
col_names = [desc[0] for desc in cursor.description]
conn.close()
return [dict(zip(col_names, row)) for row in rows]
@staticmethod
def delete_from_db(db_path: str, table: str, where: str, params: tuple = ()):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table} WHERE {where}", params)
rows = cursor.fetchall()
col_names = [d[0] for d in cursor.description]
query = f"DELETE FROM {table} WHERE {where}"
print(f"Executing query: {query} with params: {params}")
cursor.execute(query, params)
conn.commit()
conn.close()
if rows:
msg_lines = [f"**Deleted from `{table}`**\n```\n"]
for row in rows:
row_dict = dict(zip(col_names, row))
formatted_row = "\n".join(
f" {k}: {v!r}" for k, v in row_dict.items()
)
msg_lines.append(formatted_row)
Database._log("\n```\n".join(msg_lines))
else:
Database._log(
f"Delete executed on `{table}` with no matches (where={where})"
)
@staticmethod
def entry_exists(db_path: str, table: str, field: str, value) -> bool:
return (
Database.fetch_one_as_dict(db_path, table, f"{field} = ?", (value,))
is not None
)
@staticmethod
def execute(db_path: str, query: str, params: tuple = ()) -> list[tuple]:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(query, params)
rows = cursor.fetchall()
conn.close()
return rows
def setup_db(db_name, table_schemas):
conn = sqlite3.connect(db_name)
conn.close()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
for schema in table_schemas:
cursor.execute(
f"CREATE TABLE IF NOT EXISTS {schema['name']} ({schema['columns']})"
)
conn.commit()
conn.close()
# Add new fields to existing tables
NEW_FIELDS = {
"contributors": [
("time_zone", "INTEGER DEFAULT NULL"),
("trust_points", "INTEGER DEFAULT 0"),
("trust_remarks", "TEXT DEFAULT NULL"),
],
"games": [
("gdd_link", "TEXT DEFAULT NULL"),
("state", "INTEGER DEFAULT 0"),
],
}
def apply_schema_updates(db_path=Database.GAMES_DB):
conn = sqlite3.connect(db_path)
c = conn.cursor()
for table, new_columns in NEW_FIELDS.items():
# skip tables that don't exist
c.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table,)
)
if not c.fetchone():
print(f"Table '{table}' not found, skipping.")
continue
# get existing columns
c.execute(f"PRAGMA table_info({table})")
existing = [row[1] for row in c.fetchall()]
# add any missing columns
for name, coltype in new_columns:
if name not in existing:
print(f"Adding column '{name}' to table '{table}'")
c.execute(f"ALTER TABLE {table} ADD COLUMN {name} {coltype}")
conn.commit()
conn.close()
os.makedirs("dbs", exist_ok=True)
setup_db(
Database.GAMES_DB,
[
{
"name": "games",
"columns": "id INTEGER PRIMARY KEY, name TEXT, repo_name TEXT, channel_id INTEGER, owner TEXT, owner_display_name TEXT, itch_io_link TEXT",
}
],
)
setup_db(
Database.TASKS_DB,
[
{
"name": "tasks",
"columns": "id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT NOT NULL, description TEXT NOT NULL, deadline TEXT, finished INTEGER DEFAULT 0, event_id INTEGER DEFAULT NULL",
}
],
)
setup_db(
Database.EVENTS_DB,
[
{
"name": "events",
"columns": "id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, triggered INTEGER DEFAULT 0",
}
],
)
setup_db(
Database.GAMES_DB,
[
{
"name": "contributors",
"columns": "id INTEGER PRIMARY KEY AUTOINCREMENT, discord_username TEXT NOT NULL, discord_display_name TEXT, credit_name TEXT NOT NULL, itch_io_link TEXT, alt_link TEXT",
}
],
)
setup_db(
Database.GAMES_DB,
[
{
"name": "game_contributors",
"columns": "game_id INTEGER NOT NULL, contributor_id INTEGER NOT NULL, role TEXT NOT NULL, PRIMARY KEY (game_id, contributor_id, role), FOREIGN KEY (game_id) REFERENCES games(id), FOREIGN KEY (contributor_id) REFERENCES contributors(id)",
}
],
)
setup_db(
Database.GAMES_DB,
[
{
"name": "asset_requests",
"columns": "id INTEGER PRIMARY KEY AUTOINCREMENT, game_id INTEGER NOT NULL, asset_type TEXT NOT NULL, content TEXT NOT NULL, context TEXT, requested_by INTEGER NOT NULL, accepted_by INTEGER, status TEXT NOT NULL, FOREIGN KEY (game_id) REFERENCES games(id), FOREIGN KEY (requested_by) REFERENCES contributors(id), FOREIGN KEY (accepted_by) REFERENCES contributors(id)",
}
],
)
apply_schema_updates()