forked from ar1ocker/SquadJS-Timer-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrally-timer.js
More file actions
325 lines (267 loc) · 12.9 KB
/
rally-timer.js
File metadata and controls
325 lines (267 loc) · 12.9 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
import BasePlugin from "./base-plugin.js";
export default class RallyTimer extends BasePlugin {
static get description() {
return "Rally timer plugin";
}
static get defaultEnabled() {
return false;
}
static get optionsSpecification() {
return {
commands_to_start: {
required: false,
description: "List of commands. 'rally' is always added to the list of commands to start the timer",
default: ["r", "rly", "raly"],
}, commands_to_stop: {
required: false,
description: "List of commands to start the rally timer (the first entry is used in the reminder message as a note!)",
default: ["sr", "stop", "rs", "rts"],
}, commands_to_pause: {
required: false,
description: "List of commands to pause the rally timer (the first entry is used in the reminder message as a note!)",
default: ["pr", "pause", "rp", "rtp"],
}, time_before_spawn: {
required: false, description: "Default time before spawn at rally point", default: 20,
}, commands_to_accept_squad: {
required: false,
description: "List of dedicated commands to accept a squad rally invitation",
default: ["rtyes"],
}, max_time: {
required: false, description: "Maximum timer time in minutes", default: 120
},
};
}
constructor(server, options, connectors) {
super(server, options, connectors);
this.playerTimer = new Map();
this.rallyTimerPaused = new Map();
this.optedOutPlayers = new Set();
this.pendingSquadInvites = new Map();
this.warn = this.warn.bind(this);
this.startIntervalMessages = this.startIntervalMessages.bind(this);
this.stopIntervalMessages = this.stopIntervalMessages.bind(this);
this.clearAllTimeouts = this.clearAllTimeouts.bind(this);
this.activateIntervalMessagesAboutRally = this.activateIntervalMessagesAboutRally.bind(this);
this.sendMessageAboutRally = this.sendMessageAboutRally.bind(this);
this.handleSquadRally = this.handleSquadRally.bind(this);
this.handleAcceptInvite = this.handleAcceptInvite.bind(this);
this.handleOptOut = this.handleOptOut.bind(this);
}
async mount() {
let commandsToStart = this.options.commands_to_start;
commandsToStart.push('rally');
for (const command of commandsToStart) {
this.server.on(`CHAT_COMMAND:${command}`, (data) => {
this.startIntervalMessages(data);
});
}
for (const command of this.options.commands_to_stop) {
this.server.on(`CHAT_COMMAND:${command}`, (data) => {
this.stopIntervalMessages(data.player.steamID);
});
}
for (const command of this.options.commands_to_pause) {
this.server.on(`CHAT_COMMAND:${command}`, (data) => {
this.togglePauseIntervalMessages(data.player.steamID);
});
}
for (const command of this.options.commands_to_accept_squad) {
this.server.on(`CHAT_COMMAND:${command}`, (data) => {
this.handleAcceptInvite(data.player);
});
}
this.server.on("ROUND_ENDED", () => {
this.clearAllTimeouts();
});
}
async startIntervalMessages(data) {
if (data.player) {
const message = data.message.toLowerCase();
// split by spaces and remove empty entries
const commandSplit = message.trim().split(/\s+/).filter(Boolean);
// Handle squad invite accept
if (commandSplit[0] === "yes") {
this.handleAcceptInvite(data.player);
return;
}
// Handle squad invite opt-out
if (commandSplit[0] === "optout") {
this.handleOptOut(data.player);
return;
}
// Set new timer
let isTimerSet = false;
if (commandSplit.length > 0) {
const rallyTime = parseInt(commandSplit[0]);
if (rallyTime && rallyTime > 0 && rallyTime <= this.options.max_time) {
// Check if second param is squad mode
const secondParam = commandSplit[1];
if (secondParam && (secondParam === "sq" || secondParam === "squad")) {
this.handleSquadRally(data.player, rallyTime);
return;
}
// clear old timeout
clearTimeout(this.playerTimer.get(data.player.steamID));
this.rallyTimerPaused.delete(data.player.steamID);
let timeBeforeSpawn = this.options.time_before_spawn;
const customTimeBeforeSpawn = parseInt(secondParam);
if (customTimeBeforeSpawn && customTimeBeforeSpawn > 0) {
timeBeforeSpawn = customTimeBeforeSpawn;
}
const firstMessageDelay = rallyTime > timeBeforeSpawn ? (rallyTime - timeBeforeSpawn) * 1000 : (60 - timeBeforeSpawn + rallyTime) * 1000;
this.activateIntervalMessagesAboutRally(firstMessageDelay, data.player, timeBeforeSpawn);
isTimerSet = true;
}
}
// Toggle timer, if command used without time and timer is already set
if (!isTimerSet && this.playerTimer.has(data.player.steamID)) {
this.togglePauseIntervalMessages(data.player.steamID);
return;
}
if (!isTimerSet) {
this.warn(data.player.steamID, `Enter the CURRENT rally time (from 0 to ${this.options.max_time})\n\nFor example:\nTimer shows 30 seconds, then: !rally 30\nSquad rally: !rally 30 sq`);
await new Promise((resolve) => setTimeout(resolve, 6 * 1000));
this.warn(data.player.steamID, `Custom reminder time. For example:\n!rally 30 25\nThis will set a reminder 25 seconds before spawn.`);
}
}
}
stopIntervalMessages(steamID) {
if (steamID) {
clearTimeout(this.playerTimer.get(steamID));
this.playerTimer.delete(steamID);
this.rallyTimerPaused.delete(steamID);
this.warn(steamID, "Stopped sending rally reminders");
}
}
togglePauseIntervalMessages(steamID) {
// Resume from pause, if player has an active timer and paused the timer before
if (this.playerTimer.has(steamID) && this.rallyTimerPaused.has(steamID)) {
this.rallyTimerPaused.delete(steamID);
this.warn(steamID, `Rally reminder RESUMED.`);
}
// Pause the timer, if player has an active timer and did not pause the timer before
else if (this.playerTimer.has(steamID) && !this.rallyTimerPaused.has(steamID)) {
this.rallyTimerPaused.set(steamID, true);
this.warn(steamID, `Rally reminder PAUSED!\nTo resume, just use the command again.`);
} else {
this.warn(steamID, `You don't have an active rally reminder to pause or resume.`);
}
}
clearAllTimeouts() {
for (const timeout of this.playerTimer.values()) {
clearTimeout(timeout);
}
this.playerTimer.clear();
this.rallyTimerPaused.clear();
this.pendingSquadInvites.clear();
}
handleSquadRally(initiator, rallyTime) {
if (!initiator.squadID) {
this.warn(initiator.steamID, "You must be in a squad to use squad rally mode.");
return;
}
// Start timer for the initiator
clearTimeout(this.playerTimer.get(initiator.steamID));
this.rallyTimerPaused.delete(initiator.steamID);
const timeBeforeSpawn = this.options.time_before_spawn;
const firstMessageDelay = rallyTime > timeBeforeSpawn ? (rallyTime - timeBeforeSpawn) * 1000 : (60 - timeBeforeSpawn + rallyTime) * 1000;
this.activateIntervalMessagesAboutRally(firstMessageDelay, initiator, timeBeforeSpawn);
// Anchor for syncing squad members to the same 60s reminder cycle
const cycleAnchor = Date.now() + firstMessageDelay;
// Find squad members (excluding initiator)
const squadMembers = this.server.players.filter(
p => p.squadID === initiator.squadID && p.teamID === initiator.teamID && p.steamID !== initiator.steamID
);
let invitedCount = 0;
const commandAcceptPrefix = '!' + this.options.commands_to_accept_squad[0];
const commandStartPrefix = '!' + this.options.commands_to_start[0];
for (const member of squadMembers) {
if (this.optedOutPlayers.has(member.steamID)) {
continue;
}
this.pendingSquadInvites.set(member.steamID, {
timeBeforeSpawn,
initiatorName: initiator.name,
cycleAnchor,
});
this.warn(member.steamID,
`${initiator.name} started a squad rally timer!` +
`\nAccept: ${commandStartPrefix} yes or ${commandAcceptPrefix}` +
`\nOpt out (no invites): ${commandStartPrefix} optout`
);
invitedCount++;
}
this.warn(initiator.steamID, `Squad rally started! Invitations sent to ${invitedCount} squad member(s).`);
}
handleAcceptInvite(player) {
if (!player) return;
const invite = this.pendingSquadInvites.get(player.steamID);
if (!invite) {
this.warn(player.steamID, "You don't have a pending squad rally invitation.");
return;
}
this.pendingSquadInvites.delete(player.steamID);
// Clear any existing timer
clearTimeout(this.playerTimer.get(player.steamID));
this.rallyTimerPaused.delete(player.steamID);
// Sync to the initiator's 60s reminder cycle
const now = Date.now();
const elapsed = now - invite.cycleAnchor;
const cycleMs = 60 * 1000;
let syncedDelay;
if (elapsed < 0) {
// First reminder hasn't fired yet
syncedDelay = -elapsed;
} else {
// Align to the next tick in the 60s cycle
syncedDelay = cycleMs - (elapsed % cycleMs);
}
this.activateIntervalMessagesAboutRally(syncedDelay, player, invite.timeBeforeSpawn);
}
handleOptOut(player) {
if (!player) return;
this.optedOutPlayers.add(player.steamID);
this.pendingSquadInvites.delete(player.steamID);
this.warn(player.steamID, "You have opted out of squad rally invitations.");
}
activateIntervalMessagesAboutRally(delay, player, timeBeforeSpawn) {
let commandPausePrefix = this.getCommandPausePrefixString();
let commandStopPrefix = this.getCommandStopPrefixString();
this.warn(
player.steamID,
`Get a reminder ${timeBeforeSpawn} seconds before spawn at the rally.
\nPAUSE with: ${commandPausePrefix}
\nSTOP with: ${commandStopPrefix}`
);
this.playerTimer.set(player.steamID, setTimeout(() => {
this.sendMessageAboutRally(player.steamID, timeBeforeSpawn);
const intervalId = setInterval(() => this.sendMessageAboutRally(player.steamID, timeBeforeSpawn), 60 * 1000);
this.playerTimer.set(player.steamID, intervalId);
}, delay));
}
async sendMessageAboutRally(steamID, timeBeforeSpawn) {
// Do not send message if paused
if (this.rallyTimerPaused.get(steamID)) {
return;
}
await this.warn(
steamID,
`Rally spawn in ${timeBeforeSpawn} seconds! (!` + this.options.commands_to_stop[0] + ` or !` + this.options.commands_to_pause[0] + `)`
);
}
async warn(playerID, message, repeat = 1, frequency = 5) {
for (let i = 0; i < repeat; i++) {
// repeat is used so that squad displays all messages and does not hide them just because they are identical.
await this.server.rcon.warn(playerID, message + "\u{00A0}".repeat(i));
if (i !== repeat - 1) {
await new Promise((resolve) => setTimeout(resolve, frequency * 1000));
}
}
}
getCommandStopPrefixString() {
return '!' + this.options.commands_to_stop.join(', !');
}
getCommandPausePrefixString() {
return '!' + this.options.commands_to_pause.join(', !');
}
}