-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (190 loc) · 7.56 KB
/
index.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { globalShortcut } = require('electron');
module.exports = function pinger(mod) {
const channelIndex = 6;
const channelId = -3;
let privateloaded = false;
let interval = 1000;
let timeout = null;
let lastSent = 0;
let hooks = [];
let pingInProgress = false;
const commandsInfo = {
"pinger toggle": "Enable or disable the pinger module.",
"pinger hotkey [hotkey]": "Set your hotkey to enable or disable the module.",
"pinger color": "Toggle colored output for ping values.",
"pinger info": "Show information about available commands and the current hotkey."
};
function registerHotkey(hotkey) {
try {
if (globalShortcut.isRegistered(hotkey)) {
globalShortcut.unregister(hotkey);
}
globalShortcut.register(hotkey, () => {
mod.command.exec('pinger toggle');
});
} catch (e) {
mod.command.message(`Failed to register hotkey <font color="#FF4500">${hotkey}</font>: ${e.message}`);
}
}
mod.hook('S_LOGIN', 'raw', () => {
privateloaded = false;
});
mod.hook('S_SPAWN_ME', 'raw', () => {
if (privateloaded) return;
privateloaded = true;
mod.send('S_JOIN_PRIVATE_CHANNEL', 2, {
index: channelIndex,
channelId: channelId,
unk: [],
name: "Ping"
});
if (mod.settings.enabled) pingcheck();
let infoMessage = "Pinger module loaded. Available commands:\n";
for (const [command, description] of Object.entries(commandsInfo)) {
infoMessage += `-<font color="#00FF00"> ${command}</font>: ${description}\n`;
}
infoMessage += `\nCurrent hotkey: ${
mod.settings.hotkey
? `<font color="#00FF00">${mod.settings.hotkey}</font>`
: `<font color="#FF4500">not installed</font>`
}`;
mod.command.message(infoMessage);
});
mod.hook('S_JOIN_PRIVATE_CHANNEL', 2, event => {
if (event.index === channelIndex) return false;
});
mod.hook('C_LEAVE_PRIVATE_CHANNEL', 1, event => {
if (event.index === channelIndex) return false;
});
mod.hook('C_REQUEST_PRIVATE_CHANNEL_INFO', 2, event => {
if (event.channelId === channelId) {
mod.send('S_REQUEST_PRIVATE_CHANNEL_INFO', 2, {
owner: true,
password: 0,
members: [],
friends: []
});
return false;
}
});
mod.hook('C_CHAT', 1, { order: -100 }, event => {
if (event.channel === 11 + channelIndex) return false;
});
mod.command.add('pinger', (cmd, ...args) => {
switch (cmd) {
case 'toggle':
const enabled = mod.settings.enabled = !mod.settings.enabled;
mod.saveSettings();
if (enabled) {
pingInProgress = false;
pingcheck();
} else {
mod.clearTimeout(timeout);
unhookAll();
}
mod.command.message(`Pinger ${enabled ? `<font color="#00FF00">enabled</font>` : `<font color="#FF4500">disabled</font>`}`);
break;
case 'hotkey':
const hotkey = args.join(" ");
if (!hotkey) {
mod.command.message(`Current hotkey: <font color="#00FF00">${mod.settings.hotkey}</font>`);
} else {
if (hotkey.toLowerCase() !== mod.settings.hotkey.toLowerCase()) {
const formattedHotkey = hotkey.toLowerCase().split("+").map(w => w[0].toUpperCase() + w.substr(1)).join("+");
try {
registerHotkey(formattedHotkey);
mod.settings.hotkey = formattedHotkey;
mod.saveSettings();
mod.command.message(`New hotkey: <font color="#00FF00">${mod.settings.hotkey}</font>`);
} catch (e) {
mod.command.message(`Invalid hotkey: <font color="#FF4500">${hotkey}</font>`);
}
}
}
break;
case 'color':
mod.settings.coloredOutput = !mod.settings.coloredOutput;
mod.saveSettings();
mod.command.message(`Colored output ${mod.settings.coloredOutput ? `<font color="#00FF00">enabled</font>` : `<font color="#FF4500">disabled</font>`}.`);
break;
case 'info':
let infoMessage = "Available commands:\n";
for (const [command, description] of Object.entries(commandsInfo)) {
infoMessage += `-<font color="#00FF00"> ${command}</font>: ${description}\n`;
}
infoMessage += `\nCurrent hotkey: ${
mod.settings.hotkey
? `<font color="#00FF00">${mod.settings.hotkey}</font>`
: `<font color="#FF4500">not installed</font>`
}`;
mod.command.message(infoMessage);
break;
default:
mod.command.message("Unknown subcommand. Use '<font color=\"#00FF00\">pinger info</font>' to see available commands.");
break;
}
});
function ping() {
if (pingInProgress) return;
pingInProgress = true;
mod.send('C_REQUEST_GAMESTAT_PING', 1);
lastSent = Date.now();
timeout = mod.setTimeout(() => {
pingInProgress = false;
ping();
}, 24000);
}
function pingcheck() {
if (mod.settings.enabled) {
ping();
hook('S_SPAWN_ME', 'raw', () => {
mod.clearTimeout(timeout);
timeout = mod.setTimeout(() => {
pingInProgress = false;
ping();
}, interval);
});
hook('C_REQUEST_GAMESTAT_PING', 'raw', () => { return false });
hook('S_RESPONSE_GAMESTAT_PONG', 'raw', { order: -9999 }, (event) => {
const result = Date.now() - lastSent;
printPing(result);
mod.clearTimeout(timeout);
timeout = mod.setTimeout(() => {
pingInProgress = false;
ping();
}, Math.max(0, interval - result));
return false;
});
}
}
function printPing(pingValue) {
let color = 'FFFFFF';
if (mod.settings.coloredOutput) {
if (pingValue < 70) color = '00FF00';
else if (pingValue >= 70 && pingValue < 100) color = 'CCFF33';
else if (pingValue >= 100 && pingValue < 200) color = 'FFFF00';
else color = 'FF0000';
}
mod.send('S_PRIVATE_CHAT', 1, {
channel: channelId,
authorID: 0,
authorName: '',
message: mod.settings.coloredOutput
? `<font color="#${color}"> ${pingValue} ms</font>`
: ` ${pingValue} ms`
});
}
function hook() {
hooks.push(mod.hook(...arguments));
}
function unhookAll() {
hooks.forEach(hook => mod.unhook(hook));
hooks = [];
}
mod.game.on('leave_game', () => {
mod.clearTimeout(timeout);
unhookAll();
});
if (mod.settings.enabled) pingcheck();
registerHotkey(mod.settings.hotkey);
};