-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping_auto.js
508 lines (427 loc) · 16.7 KB
/
ping_auto.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// ==UserScript==
// @name 多骰联合延迟测试
// @author 错误
// @version 1.0.2
// @description 需要先用【.乒乓 set main @要设置的骰子】设置一个主机。使用【.乒乓 @骰子】获取帮助。主机需要加载依赖:错误:team:>=4.0.0
// @timestamp 1737173515
// 2025-01-18 12:11:55
// @license MIT
// @homepageURL https://github.com/error2913/sealdice-js/
// @updateUrl https://mirror.ghproxy.com/https://raw.githubusercontent.com/error2913/sealdice-js/main/ping_auto.js
// @updateUrl https://raw.githubusercontent.com/error2913/sealdice-js/main/ping_auto.js
// ==/UserScript==
let ext = seal.ext.find('ping_auto');
if (!ext) {
ext = seal.ext.new('ping_auto', '错误', '1.0.2');
seal.ext.register(ext);
seal.ext.registerStringConfig(ext, '定时任务cron表达式', '0 */4 * * *', '修改后保存并重载js');
seal.ext.registerIntConfig(ext, '超时时间/s', 60, '修改后保存并重载js');
seal.ext.registerIntConfig(ext, '一级test延时/ms', 3000, '修改后保存并重载js');
seal.ext.registerIntConfig(ext, '二级test延时/ms', 1000, '修改后保存并重载js');
}
const data = {};
const list = JSON.parse(ext.storageGet(`list`) || '[]');
const cron = seal.ext.getStringConfig(ext, '定时任务cron表达式');
const timeout = seal.ext.getIntConfig(ext, '超时时间/s') * 1000;
const interval1 = seal.ext.getIntConfig(ext, '一级test延时/ms');
const interval2 = seal.ext.getIntConfig(ext, '二级test延时/ms');
seal.ext.registerTask(ext, "cron", cron, () => {
for (let i = 0; i < list.length; i++) {
const { gid, epId } = list[i];
const msg = getMsg(gid, epId);
const ctx = getCtx(epId, msg);
const ping = Ping.getPing(gid);
start(ctx, msg, gid, ping);
}
});
function getMsg(gid, uid) {
let msg = seal.newMessage();
msg.groupId = gid;
msg.guildId = '';
msg.messageType = 'group';
msg.sender.userId = uid;
return msg;
}
function getCtx(epId, msg) {
const eps = seal.getEndPoints();
for (let i = 0; i < eps.length; i++) {
if (eps[i].userId === epId) {
return seal.createTempCtx(eps[i], msg);
}
}
return undefined;
}
function start(ctx, msg, gid, ping) {
const team = teamManager.getTeamList(gid)[0];
if (team.members.length < 2) {
seal.replyToSender(ctx, msg, '骰数不足,无法进行测试');
return seal.ext.newCmdExecuteResult(true);
}
ping.members = team.members;
ping.data = [];
for (let i = 0; i < ping.members.length; i++) {
ping.data.push(new Array(ping.members.length - i).fill(0));
}
ping.incomplete = ping.members.length;
test1(ctx, msg, ping, 0);
}
function test1(ctx, msg, ping, index) {
setTimeout(() => {
const text = ping.members.slice(index).map(item => `[CQ:at,qq=${item.replace(/\D+/g, '')}]`).join(' ');
ping.data[0][index] = Date.now();
seal.replyToSender(ctx, msg, `.乒乓 ping ${text}`);
ping.timeoutId = setTimeout(() => {
const lost_uid = ping.members[index];
teamManager.remove(ctx, [lost_uid], []);
seal.replyToSender(ctx, msg, `.乒乓 dong [CQ:at,qq=${lost_uid.replace(/\D+/g, '')}] 失踪了,移除后重新进行检测`);
ctx.notice(`${lost_uid}失踪了`);
start(ctx, msg, gid, ping);
}, timeout);
Ping.savePing(gid);
}, interval1);
}
function test2(ctx, msg, ping, index) {
setTimeout(() => {
ping.row[index] = Date.now();
seal.replyToSender(ctx, msg, `.乒乓 ping [CQ:at,qq=${ping.members[index].replace(/\D+/g, '')}]`);
ping.timeoutId = setTimeout(() => {
seal.replyToSender(ctx, msg, `.乒乓 beep [CQ:at,qq=${ping.members[index].replace(/\D+/g, '')}]`);
Ping.savePing(gid);
}, timeout);
Ping.savePing(gid);
}, interval2);
}
function settlement(ctx, msg, gid, ping) {
const result = ping.calculate();
const text = result.map((item, index) => {
let name = '';
if (index === 0) {
name = seal.formatTmpl(ctx, "核心:骰子名字");
} else {
const epId = ctx.endPoint.userId;
const uid = ping.members[index - 1];
const mmsg = getMsg(gid, uid);
const mctx = getCtx(epId, mmsg);
name = mctx.player.name;
}
return `${name}:${item}ms`;
}).join('\n');
seal.replyToSender(ctx, msg, text);
ctx.notice(`检测报告:\n${text}`);
}
class Ping {
constructor() {
this.main = false;
this.members = []; // n个成员
this.incomplete = 0; // 未完成的成员数
this.row = []; // 一级从机获取的数据
this.data = [];// [[1, ..., n], [2,..., n], ..., [n]]
this.matrix = [];
this.timeoutId = null;
}
static parse(data) {
const ping = new Ping();
ping.main = data.main || false;
ping.members = data.members || [];
ping.incomplete = data.incomplete || 0;
ping.row = data.row || [];
ping.data = data.data || [];
ping.matrix = data.matrix || [];
ping.timeoutId = data.timeoutId || null;
return ping;
}
static getPing(gid) {
if (!data.hasOwnProperty(gid)) {
let data1 = {};
try {
data1 = JSON.parse(ext.storageGet(`ping_${gid}`) || '{}');
} catch (error) {
console.error(`从数据库中获取${`ping_${gid}`}失败:`, error);
}
data[gid] = Ping.parse(data1);
}
return data[gid];
}
static savePing(gid) {
if (data.hasOwnProperty(gid)) {
const ping = data[gid];
ext.storageSet(`ping_${gid}`, JSON.stringify(ping));
}
}
handleData() {
this.matrix = [];
for (let i = 0; i < this.data.length + 1; i++) {
const row = []; // 第i行的数据,0 1 2 ... i | i+1 i+2 ... n,前半部分从其他获取
for (let j = 0; j < i; j++) {
row.push(this.data[j][i - j - 1]); // a[i][j] = a[j][i]
}
row.push(0); // a[i][i]
if (i !== this.data.length) {
row.push(...this.data[i]); // a[i][i+1] ... a[i][n]
}
this.matrix.push(row);
}
}
average(arr) {
return arr.reduce((a, b) => a + b, 0) / arr.length;
}
calculate() {
this.handleData();
const result = [];
for (let i = 0; i < this.matrix.length; i++) {
const row = this.matrix[i];
const arr = [];
for (let j = 0; j < row.length; j++) {
if (i === j) {
continue;
}
for (let k = j + 1; k < row.length; k++) {
if (i === k) {
continue;
}
arr.push((row[j] + row[k] - this.matrix[j][k]) / 2);
}
}
result.push(this.average(arr).toFixed(2));
}
return result;
}
}
const cmd = seal.ext.newCmdItemInfo();
cmd.name = '乒乓';
cmd.help = `帮助:
【.乒乓 <main|sub> @要设置的骰子】设置为主机或从机,默认为从机
【.乒乓 ding】立即开始检测
【.乒乓 dong】停止检测
【.乒乓 click】开启自动检测
【.乒乓 clack】关闭自动检测
【.乒乓 status】查看自动检测状态`;
cmd.allowDelegate = true;
cmd.disabledInPrivate = true;
cmd.solve = (ctx, msg, cmdArgs) => {
ctx.delegateText = '';
const gid = ctx.group.groupId;
const uid = ctx.player.userId;
const ping = Ping.getPing(gid);
const val = cmdArgs.getArgN(1);
switch (val) {
// 广播指令
case 'main': {// 来源:用户
if (!cmdArgs.amIBeMentionedFirst) {
return seal.ext.newCmdExecuteResult(true);
}
const extteam = seal.ext.find('team');
if (!extteam || parseInt(extteam.version[0]) < 4) {
seal.replyToSender(ctx, msg, '未找到team插件');
return seal.ext.newCmdExecuteResult(true);
}
ping.main = true;
seal.replyToSender(ctx, msg, '已设置为主机');
Ping.savePing(gid);
return seal.ext.newCmdExecuteResult(true);
}
case 'sub': {// 来源:用户
if (!cmdArgs.amIBeMentionedFirst) {
return seal.ext.newCmdExecuteResult(true);
}
ping.main = false;
seal.replyToSender(ctx, msg, '已设置为从机');
Ping.savePing(gid);
return seal.ext.newCmdExecuteResult(true);
}
case 'dong': {// 来源:用户、主机
if (cmdArgs.amIBeMentionedFirst) {
seal.replyToSender(ctx, msg, `.乒乓 boom`);
return seal.ext.newCmdExecuteResult(true);
}
clearTimeout(ping.timeoutId);
ping.timeoutId = null;
ping.members = [];
seal.replyToSender(ctx, msg, '检测已停止');
Ping.savePing(gid);
return seal.ext.newCmdExecuteResult(true);
}
case 'pong': {// 来源:一级从机、二级从机
if (!cmdArgs.amIBeMentionedFirst) {
return seal.ext.newCmdExecuteResult(true);
}
const index = ping.members.indexOf(uid);
if (index === -1) {
return seal.ext.newCmdExecuteResult(true);
}
if (ping.main) {// index范围: 0~n-1
ping.data[0][index] = Date.now() - ping.data[0][index];
clearTimeout(ping.timeoutId);
ping.timeoutId = null;
if (index !== ping.members.length - 1) {
test1(ctx, msg, ping, index + 1);
return seal.ext.newCmdExecuteResult(true);
} else {
ping.incomplete--;
if (ping.incomplete === 0) {
settlement(ctx, msg, gid, ping);
return seal.ext.newCmdExecuteResult(true);
}
}
} else {// index范围: 0~n-i-1 => i+1~n
ping.row[index] = Date.now() - ping.row[index];
ping.incomplete--;
clearTimeout(ping.timeoutId);
ping.timeoutId = null;
if (index !== ping.members.length - 1) {
test2(ctx, msg, ping, index + 1);
return seal.ext.newCmdExecuteResult(true);
} else if (ping.incomplete === 0) {
seal.replyToSender(ctx, msg, `.乒乓 zing ${JSON.stringify(ping.row)}`);
return seal.ext.newCmdExecuteResult(true);
}
}
return seal.ext.newCmdExecuteResult(true);
}
// 仅限主机
case 'ding': {// 来源:用户
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
if (ctx.privilegeLevel < 100) {
seal.replyToSender(ctx, msg, seal.formatTmpl(ctx, "核心:提示_无权限"));
return seal.ext.newCmdExecuteResult(true);
}
start(ctx, msg, gid, ping);
return seal.ext.newCmdExecuteResult(true);
}
case 'click': {// 来源:用户
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
if (ctx.privilegeLevel < 100) {
seal.replyToSender(ctx, msg, seal.formatTmpl(ctx, "核心:提示_无权限"));
return seal.ext.newCmdExecuteResult(true);
}
const epId = ctx.endPoint.userId;
const index = list.findIndex(item => item.gid === gid);
if (index === -1) {
list.push({
gid: gid,
epId: epId
});
ext.storageSet(`list`, JSON.stringify(list));
}
seal.replyToSender(ctx, msg, '自动检测开启');
return seal.ext.newCmdExecuteResult(true);
}
case 'clack': {// 来源:用户
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
const index = list.findIndex(item => item.gid === gid);
if (index !== -1) {
list.splice(index, 1);
ext.storageSet(`list`, JSON.stringify(list));
}
seal.replyToSender(ctx, msg, '自动检测关闭');
return seal.ext.newCmdExecuteResult(true);
}
case 'status': {// 来源:用户
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
const index = list.findIndex(item => item.gid === gid);
if (index !== -1) {
seal.replyToSender(ctx, msg, '自动检测已经开启');
} else {
seal.replyToSender(ctx, msg, '自动检测已经关闭');
}
return seal.ext.newCmdExecuteResult(true);
}
case 'zing': {// 来源:一级从机 奇怪的拟声词增加了
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
const index = ping.members.indexOf(uid);
if (index === -1) {
return seal.ext.newCmdExecuteResult(true);
}
const val2 = cmdArgs.getArgN(2);
let row = [];
try {
row = JSON.parse(val2);
} catch (err) {
seal.replyToSender(ctx, msg, '数据格式错误');
return seal.ext.newCmdExecuteResult(true);
}
if (ping.data[index + 1].length !== row.length) {
seal.replyToSender(ctx, msg, '数据长度错误');
return seal.ext.newCmdExecuteResult(true);
}
ping.data[index + 1] = row;
ping.incomplete--;
if (ping.incomplete === 0) {
settlement(ctx, msg, gid, ping);
return seal.ext.newCmdExecuteResult(true);
}
Ping.savePing(gid);
return seal.ext.newCmdExecuteResult(true);
}
case 'beep': {// 来源:一级从机
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
const lost_uid = cmdArgs.at[0].userId;
const index = ping.members.indexOf(lost_uid);
if (index === -1) {
return seal.ext.newCmdExecuteResult(true);
}
clearTimeout(ping.timeoutId);
ping.timeoutId = null;
teamManager.remove(ctx, [lost_uid], []);
seal.replyToSender(ctx, msg, `.乒乓 dong [CQ:at,qq=${lost_uid.replace(/\D+/g, '')}] 失踪了,移除后重新进行检测`);
ctx.notice(`${lost_uid}失踪了`);
start(ctx, msg, gid, ping);
return seal.ext.newCmdExecuteResult(true);
}
case 'boom': {// 来源:一级从机、二级从机
if (!ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
const uid = ctx.player.userId;
const index = ping.members.indexOf(uid);
if (index !== -1) {
return seal.ext.newCmdExecuteResult(true);
}
clearTimeout(ping.timeoutId);
ping.timeoutId = null;
teamManager.add(ctx, [uid]);
seal.replyToSender(ctx, msg, `.乒乓 dong`);
ctx.notice(`${uid}加入了`);
start(ctx, msg, gid, ping);
return seal.ext.newCmdExecuteResult(true);
}
// 仅限从机
case 'ping': {// 来源:主机、一级从机
if (ping.main) {
return seal.ext.newCmdExecuteResult(true);
}
if (!cmdArgs.amIBeMentionedFirst) {
return seal.ext.newCmdExecuteResult(true);
}
seal.replyToSender(ctx, msg, `.乒乓 pong [CQ:at,qq=${uid.replace(/\D+/g, '')}]`);
ping.members = cmdArgs.at.slice(1).map(item => item.userId);
if (ping.members.length !== 0) {
ping.row = new Array(ping.members.length).fill(0);
ping.incomplete = ping.members.length;
test2(ctx, msg, ping, 0);
}
return seal.ext.newCmdExecuteResult(true);
}
default: {
if (!cmdArgs.amIBeMentionedFirst) {
return seal.ext.newCmdExecuteResult(true);
}
const ret = seal.ext.newCmdExecuteResult(true);
ret.showHelp = true;
return ret;
}
}
};
ext.cmdMap['乒乓'] = cmd;