-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
192 lines (172 loc) · 6.15 KB
/
init.js
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
const MAX_TIMEOUT_QUEUE_POOL = 30 * 60 * 1000;
setInterval(() => {
updateAllDashboards();
}, 5 * 60 * 1000);
/**
* @typedef {Object.<string, import('./utils/ticket.js').Q_Ticket>} TQUEUE_POOL_SCHEM
* @typedef {{getSortedKeys: function()=>void}} getSortedKeys - Returns sorted keys
* @typedef {TQUEUE_POOL_SCHEM & getSortedKeys} TQUEUE_POOL
*/
// CREATE POOLS
/** @type {Object.<string, import('./utils/ticket.js').IR_Ticket>} */
export const IS_REPLAY_POOL = createPool('IS_REPLAY_POOL');
/** @type {Object.<string, import('./utils/ticket.js').DV_Ticket>} */
export const DATA_VALIDATION_POOL = createPool('DATA_VALIDATION_POOL');
/** @type {TQUEUE_POOL} */
export const QUEUE_POOL = createPool('QUEUE_POOL', [
'getSortedKeys',
() => updateQueuePool(),
]);
/** @type {Object.<string, import('./utils/ticket.js').D_Ticket>} */
export const DASHBOARD_POOL = createPool('DASHBOARD_POOL');
/** @type {Object.<string, import('./utils/ticket.js').CL_Ticket>} */
export const COACHLOG_POOL = createPool('COACHLOG_POOL');
/** @type {Object.<string, import('./utils/ticket.js').DES_Ticket>} */
export const DESCRIPTION_POOL = createPool('DESCRIPTION_POOL');
// EMOJI INTERACTIONS
registerEmojiInteraction(IS_REPLAY_POOL, { binaryAction: { emojis: ['✅', '🛑'] } });
registerEmojiInteraction(DATA_VALIDATION_POOL, {
race: {
emojis: [raceEmojis.terran.id, raceEmojis.zerg.id, raceEmojis.protoss.id],
onAdd: (ticket, emoji) => onAddHelper(ticket, emoji, 'race', raceEmojis),
onDel: ticket => {
ticket.race = false;
},
},
rank: {
emojis: [
rankEmojis.bronze.id,
rankEmojis.silver.id,
rankEmojis.gold.id,
rankEmojis.platinum.id,
rankEmojis.diamond.id,
],
onAdd: (ticket, emoji) => onAddHelper(ticket, emoji, 'rank', rankEmojis),
onDel: ticket => {
ticket.rank = false;
},
},
vsrace: {
emojis: [vsRaceEmojis.vsTerran.id, vsRaceEmojis.vsZerg.id, vsRaceEmojis.vsProtoss.id],
onAdd: (ticket, emoji) => onAddHelper(ticket, emoji, 'vsRace', vsRaceEmojis),
onDel: ticket => {
ticket.vsRace = false;
},
},
});
registerEmojiInteraction(DASHBOARD_POOL, {
prevPage: {
emojis: ['◀'],
onAdd: goToPrevPage,
},
nextPage: {
emojis: ['▶️'],
onAdd: goToNextPage,
},
selectStudent: {
emojis: ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣'],
onAdd: selectStudent,
onDel: finishedCoachingStudent,
},
});
registerEmojiInteraction(COACHLOG_POOL, {
prevPage: {
emojis: ['✅', '🛑'],
onAdd: handleAfterCoachingInter,
},
});
registerEmojiInteraction(DESCRIPTION_POOL, {
binaryAction: { emojis: ['✅', '🛑'], onAdd: handleAddDesc },
});
const init = async () => {
// LOAD COACHES
const initCache = [getDashboards(await getCoaches()), Queue_PoolEntry.find({})];
/** @type {[{value: Message[]}, {value: import('./Models/Queue_Pool.js').QPE_Opts[]}]} */
const [resMessages, { value: allQueueEntries }] = await Promise.allSettled(initCache);
const { value: dashboards } = resMessages;
const qPEntries = [...allQueueEntries];
const userFetchCache = [];
dashboards.forEach(dash => {
// find out if have to recreate dashboard
const foundEmoji = [];
dash.reactions.cache.every(react => {
foundEmoji.push(
DashEmojis.includes(react.emoji.name) | DashEmojis.includes(react.emoji.name)
);
return react.count === 1;
});
if (foundEmoji.length !== 7) {
userFetchCache.push(putAllReactsOnDash(dash));
}
// find queuepool ticket that is connected to dashboard
const studentTicket = qPEntries.find(
entry => entry.coachID === dash.channel.recipient.id
);
// build a dashboardTicket with the queuepool if applicable
buildTicket(DASHBOARD_POOL, {
id: dash.id,
coachID: studentTicket?.coachID,
studentQTicketID: studentTicket?.id,
startedCoaching: studentTicket?.startedCoaching,
lockedEmojiInteractionGroups: studentTicket ? ['selectStudent'] : [],
});
// if there are no queuepool entries return => if looping is too slow the loop can happen after
// if (qPEntries.length === 0) return;
});
qPEntries.forEach(qPEntry =>
userFetchCache.push(
(async () => {
// find the dashboard? that is connected to a queuePoolEntry
const dashOfCoach = dashboards.find(
/** @param {Message} dash */
dash => dash.channel.recipient.id === qPEntry.coachID
);
// build queuepool ticket with dashboards id if applicable
/** @type {import('./utils/ticket.js').Q_Ticket} */
const options = {
student: await new Discord.User(client, { id: qPEntry.studentID }).fetch(),
id: qPEntry.id,
activatedAt: qPEntry.activatedAt,
content: qPEntry.content,
attachArr: qPEntry.attachArr,
race: qPEntry.race,
rank: qPEntry.rank,
vsRace: qPEntry.vsRace,
coach: dashOfCoach?.channel?.recipient,
emergency: Date.now() - qPEntry.activatedAt > MAX_TIMEOUT_QUEUE_POOL,
url: qPEntry.url,
startedCoaching: qPEntry.startedCoaching,
};
buildTicket(
QUEUE_POOL,
options,
false,
Math.max(10, getTicketTimeout(QUEUE_POOL) - Date.now() + options.activatedAt)
);
})()
)
);
await Promise.allSettled(userFetchCache);
updateQueuePool();
await updateAllDashboards();
};
import { rankEmojis, raceEmojis, vsRaceEmojis, DashEmojis } from './Emojis.js';
import { registerEmojiInteraction, onAddHelper } from './utils/emojiInteraction.js';
import { createPool, updateQueuePool } from './utils/pool.js';
import {
finishedCoachingStudent,
goToPrevPage,
selectStudent,
goToNextPage,
updateAllDashboards,
putAllReactsOnDash,
getDashboards,
} from './utils/dash.js';
import { getTicketTimeout, buildTicket } from './utils/ticket.js';
import Queue_PoolEntry from './Models/Queue_Pool.js';
import Discord, { Message } from 'discord.js';
import { client } from './app.js';
import { handleAfterCoachingInter } from './utils/coachlog.js';
import { handleAddDesc } from './utils/description.js';
import { getCoaches } from './provider/provider.js';
export default init;