-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandManager.ts
More file actions
132 lines (102 loc) · 3.67 KB
/
CommandManager.ts
File metadata and controls
132 lines (102 loc) · 3.67 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
import {randomBytes} from 'crypto';
import {User} from 'discord.js';
import axios from 'axios';
const ARG_COMMAND_FAIL_INTERVAL = 5 * 1000; // 5 seconds
const SERVER_TIMEOUT_INTERVAL = 5 * 1000; // 5 seconds
export interface CommandData {
jobId: string;
sent: boolean;
command: string;
commandId: string;
args: any[];
issuer: unknown;
}
interface SendToServerReturnData {
foundPlayerServer: boolean;
chosenServer: string;
};
class CommandManager {
allCommands: CommandData[];
allServers: string[];
allPlayers: Map<string, string[]>;
serverTimeouts: Map<string, NodeJS.Timeout>;
constructor() {
this.allCommands = [];
this.allServers = [];
this.serverTimeouts = new Map();
this.allPlayers = new Map();
}
onCommandFailSend(commandData: CommandData) {
if (commandData.sent) return;
const chosenServer = this.allServers[Math.floor(Math.random() * this.allServers.length)];
commandData.jobId = chosenServer;
setTimeout(() => {
this.onCommandFailSend(commandData);
}, ARG_COMMAND_FAIL_INTERVAL);
}
async sendToServer(command: string, args: any[], issuer: User, username = '') : Promise<SendToServerReturnData> {
let chosenServer = '';
let foundPlayerServer = false;
let robloxUser: any;
try {
const {data: {robloxUsername, robloxId, status}} = await axios.get(`https://verify.eryn.io/api/user/${issuer.id}`);
if (status === 'ok') {
robloxUser = {robloxUsername, robloxId};
};
} catch {}
if (username) {
this.allPlayers.forEach((playerIds, jobId) => {
console.log(playerIds, jobId);
if (playerIds.includes(username)) {
chosenServer = jobId;
foundPlayerServer = true;
}
});
};
if (!foundPlayerServer) {
chosenServer = this.allServers[Math.floor(Math.random() * this.allServers.length)];
};
const commandData = {
jobId: chosenServer,
sent: false,
issuer: issuer.toJSON(),
command,
robloxUser,
commandId: randomBytes(8).toString('hex'),
args
};
this.allCommands.push(commandData);
setTimeout(() => {
this.onCommandFailSend(commandData);
}, ARG_COMMAND_FAIL_INTERVAL);
return {
foundPlayerServer,
chosenServer
}
}
getCommandsFor(jobId: string) {
return this.allCommands.filter(commandData => commandData.jobId === jobId);
}
removeCommands(commandIds: string[]) {
commandIds.forEach((commandId: string) => {
const index = this.allCommands.findIndex((commandData) => commandData.commandId === commandId);
if (index !== -1) {
this.allCommands[index].sent = true;
this.allCommands.splice(index, 1);
}
});
}
serverPing(jobId: string, players: string[]) {
if (!this.allServers.includes(jobId)) this.allServers.push(jobId);
const serverTimeout = this.serverTimeouts.get(jobId);
if (serverTimeout) clearTimeout(serverTimeout);
this.serverTimeouts.set(jobId, setTimeout(() => {
this.serverTimeouts.delete(jobId);
this.allServers.splice(this.allServers.indexOf(jobId), 1);
this.allPlayers.delete(jobId);
}, SERVER_TIMEOUT_INTERVAL));
this.allPlayers.set(jobId, players);
}
}
const commandManager = new CommandManager();
export default commandManager;