-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLounge.py
240 lines (200 loc) · 8.28 KB
/
Lounge.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
235
236
237
238
239
240
"""
Created on Jun 29, 2021
@author: willg
"""
import dill as pkl
import os
from datetime import datetime, timedelta
from typing import Set
import common
from collections import namedtuple
MKW_LOUNGE_RT_UPDATE_PREVIEW_LINK = "https://www.mkwlounge.gg/ladder/tabler.php?ladder_type=rt&event_data="
MKW_LOUNGE_CT_UPDATE_PREVIEW_LINK = "https://www.mkwlounge.gg/ladder/tabler.php?ladder_type=ct&event_data="
MKW_LOUNGE_RT_UPDATER_LINK = MKW_LOUNGE_RT_UPDATE_PREVIEW_LINK
MKW_LOUNGE_CT_UPDATER_LINK = MKW_LOUNGE_CT_UPDATE_PREVIEW_LINK
MKW_LOUNGE_RT_UPDATER_CHANNEL = 758161201682841610
MKW_LOUNGE_CT_UPDATER_CHANNEL = 758161224202059847
UpdateChannels = namedtuple(
"UpdateChannels",
[
"updater_channel_id_primary",
"updater_link_primary",
"preview_link_primary",
"type_text_primary",
"updater_channel_id_secondary",
"updater_link_secondary",
"preview_link_secondary",
"type_text_secondary",
],
)
lounge_channel_mappings = {
common.MKW_LOUNGE_SERVER_ID: UpdateChannels(
updater_channel_id_primary=MKW_LOUNGE_RT_UPDATER_CHANNEL,
updater_link_primary=MKW_LOUNGE_RT_UPDATER_LINK,
preview_link_primary=MKW_LOUNGE_RT_UPDATE_PREVIEW_LINK,
type_text_primary="RT",
updater_channel_id_secondary=MKW_LOUNGE_CT_UPDATER_CHANNEL,
updater_link_secondary=MKW_LOUNGE_CT_UPDATER_LINK,
preview_link_secondary=MKW_LOUNGE_CT_UPDATE_PREVIEW_LINK,
type_text_secondary="CT",
),
common.TABLE_BOT_DISCORD_SERVER_ID: UpdateChannels(
updater_channel_id_primary=common.TABLE_BOT_SERVER_BETA_TWO_CHANNEL_ID,
updater_link_primary=MKW_LOUNGE_RT_UPDATER_LINK,
preview_link_primary=MKW_LOUNGE_RT_UPDATE_PREVIEW_LINK,
type_text_primary="RT",
updater_channel_id_secondary=common.TABLE_BOT_SERVER_BETA_TWO_CHANNEL_ID,
updater_link_secondary=MKW_LOUNGE_CT_UPDATER_LINK,
preview_link_secondary=MKW_LOUNGE_CT_UPDATE_PREVIEW_LINK,
type_text_secondary="CT",
),
}
DEFAULT_UPDATE_COOLDOWN_TIME = timedelta(seconds=20)
class Lounge:
def __init__(
self,
id_counter_file,
table_reports_file,
server_id,
report_authority_check,
update_cooldown_time=DEFAULT_UPDATE_COOLDOWN_TIME,
):
self.table_reports = {}
self.table_id_counter = 1
self.id_counter_file = id_counter_file
self.table_reports_file = table_reports_file
self.load_pkl()
self.server_id = server_id
self.report_table_authority_check = report_authority_check
if self.server_id not in lounge_channel_mappings:
raise Exception("Created a Lounge abomination")
self.channels_mapping: UpdateChannels = lounge_channel_mappings[self.server_id]
self.update_cooldowns = {}
self.update_cooldown_time = update_cooldown_time
def get_user_update_submit_cooldown(self, author_id):
if author_id not in self.update_cooldowns:
return -1
curTime = datetime.now()
time_passed = curTime - self.update_cooldowns[author_id]
return int(self.update_cooldown_time.total_seconds()) - int(
time_passed.total_seconds()
)
def add_counter(self):
self.table_id_counter += 1
def get_counter(self):
return self.table_id_counter
def add_report(self, report_id, sent_message, summary_channel_id, json_data=None):
self.table_reports[report_id] = [
sent_message.id,
sent_message.channel.id,
summary_channel_id,
"PENDING",
json_data,
]
def clear_user_cooldown(self, author):
self.update_cooldowns.pop(author.id, None)
def update_user_cooldown(self, author):
self.update_cooldowns[author.id] = datetime.now()
def has_submission_id(self, submissionID):
return submissionID in self.table_reports
def get_submission_id(self, submissionID):
if len(self.table_reports[submissionID]) == 4: # To support legacy submissions
return [*self.table_reports[submissionID], None]
return self.table_reports[submissionID]
def get_submission_id_json_data(self, submissionID):
if len(self.table_reports[submissionID]) == 4:
return None
return self.table_reports[submissionID][4]
def submission_id_of_last_matching_json(self, submissionID):
json_data = self.get_submission_id_json_data(submissionID)
if json_data is None:
return None
for sub_id in reversed(self.table_reports):
if sub_id < submissionID and json_data == self.get_submission_id_json_data(
sub_id
):
return sub_id
return None # redundant, but putting this here for novice Python devs
def remove_submission_id(self, submissionID):
self.table_reports.pop(submissionID, None)
def approve_submission_id(self, submissionID):
self.table_reports[submissionID][3] = "APPROVED"
def deny_submission_id(self, submissionID):
self.table_reports[submissionID][3] = "DENIED"
def submission_id_is_pending(self, submissionID):
return (
self.has_submission_id(submissionID)
and self.table_reports[submissionID][3] == "PENDING"
)
def submission_id_is_denied(self, submissionID):
return (
self.has_submission_id(submissionID)
and self.table_reports[submissionID][3] == "DENIED"
)
def submission_id_is_approved(self, submissionID):
return (
self.has_submission_id(submissionID)
and self.table_reports[submissionID][3] == "APPROVED"
)
def get_updater_channel_ids(self) -> Set[int]:
return {
self.channels_mapping.updater_channel_id_primary,
self.channels_mapping.updater_channel_id_secondary,
}
def get_primary_information(self):
return (
self.channels_mapping.updater_channel_id_primary,
self.channels_mapping.updater_link_primary,
self.channels_mapping.preview_link_primary,
self.channels_mapping.type_text_primary,
)
def get_secondary_information(self):
return (
self.channels_mapping.updater_channel_id_secondary,
self.channels_mapping.updater_link_secondary,
self.channels_mapping.preview_link_secondary,
self.channels_mapping.type_text_secondary,
)
def get_information(self, is_primary=True):
return (
self.get_primary_information()
if is_primary
else self.get_secondary_information()
)
def __load__(self, table_reports, table_id_counter):
self.table_reports = table_reports
self.table_id_counter = table_id_counter
def load_pkl(self):
if len(self.table_reports) == 0:
if os.path.exists(self.id_counter_file):
with open(self.id_counter_file, "rb") as pickle_in:
try:
self.table_id_counter = pkl.load(pickle_in)
except:
print(
"Could not read in the pickle for lounge update table counter."
)
if os.path.exists(self.table_reports_file):
with open(self.table_reports_file, "rb") as pickle_in:
try:
self.table_reports = pkl.load(pickle_in)
except:
print("Could not read in the pickle for lounge update tables.")
def dump_pkl(self):
if len(self.table_reports) > 0:
with open(self.id_counter_file, "wb") as pickle_out:
try:
pkl.dump(self.table_id_counter, pickle_out)
except:
print(
"Could not dump pickle for counter ID. Current counter",
self.table_id_counter,
)
with open(self.table_reports_file, "wb") as pickle_out:
try:
pkl.dump(self.table_reports, pickle_out)
except:
print(
"Could not dump counter dictionary. Current dict:",
self.table_reports,
)