This repository was archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
143 lines (122 loc) · 4.51 KB
/
utils.py
File metadata and controls
143 lines (122 loc) · 4.51 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
"""Utility functions provided to make repeated actions easier."""
import collections
from configparser import ConfigParser
import mariadb
ExpInfo = collections.namedtuple(
'ExpInfo', ['exp', 'level', 'remaining', 'rank', 'next_level', 'color']
)
GuildConfig = collections.namedtuple(
'GuildConfig', [
"exp_active",
"exp_levelup_channel"
], defaults=(None,) * 2
)
def get_all_words():
"""Get a list of almost every word in the English language."""
with open("resources/text/words_alpha.txt", encoding="utf8") as file:
return file.read().splitlines()
class Database:
"""
A basic wrapper for MariaDB with context manager support,
designed for use with PhotonBot.
"""
def __init__(self, database_config: ConfigParser):
"""
Opens a connection to a database with the details specified in the
`DatabaseConnection` section of the config file.
"""
self._database = mariadb.connect(
host=database_config['Host'],
user=database_config['Username'],
password=database_config['Password'],
database=database_config['DatabaseName']
)
self._cursor = self._database.cursor(buffered=True)
def __enter__(self):
return self
def __exit__(self, _, __, ___):
self.close()
return False
def fetch(self, *args, **kwargs) -> list[tuple]:
"""
Execute an SQL statement and return the result. Designed for statements
using `SELECT`.
"""
self._cursor.execute(*args, **kwargs)
return self._cursor.fetchall()
def modify(self, *args, **kwargs) -> int:
"""
Execute an SQL statement and return the number of rows modified.
Designed for statements such as `UPDATE`, `INSERT` and `DELETE`.
Modifications are automatically committed to the database server.
"""
self._cursor.execute(*args, **kwargs)
self._database.commit()
return self._cursor.rowcount
def close(self):
"""Close the connection to the database."""
self._cursor.close()
self._database.close()
def get_guild_config(database_config: ConfigParser, guild_id: int):
"""Get the configuration for a guild with a specified ID."""
with Database(database_config) as database:
try:
response = database.fetch(
f"SELECT {','.join(GuildConfig._fields)} FROM guild_config "
+ "WHERE guild_id = %s;", (guild_id,)
)[0]
except IndexError:
return GuildConfig()
return GuildConfig(*response)
def single_level_exp(level: int):
"""
Get the amount of EXP needed to progress to a level from the previous one.
"""
return 5 * level ** 2 + 50 * level + 100
def calculate_level(exp: int):
"""
Calculate a user's level based off of the amount of EXP they have.
Also returns the amount of "excess" EXP the user has progressing them
toward the next level.
"""
remainder = exp
level = 0
while remainder >= single_level_exp(level + 1):
level += 1
remainder -= single_level_exp(level)
return level, remainder
def total_exp_for_level(level: int):
"""Calculate the minimum EXP required for a particular level."""
return sum(single_level_exp(x) for x in range(1, level + 1))
def get_exp_info(database_config: ConfigParser, guild_id: int, user_id: int):
"""
Get a user's EXP in a particular guild.
Also calculates level, amount of EXP gained toward the next level,
numeric rank compared to other guild members, EXP needed to progress from
the current level to the next, and rank card color.
"""
with Database(database_config) as database:
exp_response = database.fetch(
"SELECT exp FROM user_exp WHERE guild_id = %s AND user_id = %s;",
(guild_id, user_id)
)
if len(exp_response) == 0:
exp = 0
else:
exp = exp_response[0][0]
level, remaining = calculate_level(exp)
rank = database.fetch(
"SELECT COUNT(*) FROM user_exp WHERE guild_id = %s AND exp >= %s;",
(guild_id, exp)
)[0][0]
card_response = database.fetch(
"SELECT red, green, blue FROM user_exp_card WHERE user_id = %s;",
(user_id,)
)
if len(card_response) == 0:
color = (None,) * 3
else:
color = card_response[0]
return ExpInfo(
exp, level, remaining, rank, single_level_exp(level + 1), color
)