105 lines
2.9 KiB
TypeScript
105 lines
2.9 KiB
TypeScript
import {resolve} from "path";
|
||
import {PublicShared} from "../../shared/public/public";
|
||
import {Scheduler, schedulerType} from "./scheduler";
|
||
import {existsSync, readFileSync} from "fs";
|
||
import {addWatchDogLog, errorLogDB} from "../../gameLog";
|
||
|
||
export class WatchDog extends Scheduler {
|
||
id: schedulerType = "watch_dog";
|
||
time = 420; // 监控频率,每x秒一次
|
||
name = "数据看门狗"
|
||
type = ""
|
||
|
||
async read() {
|
||
await this.ctorStartTime();
|
||
this.isReady = false;
|
||
this.startTime = this.nextTime;
|
||
}
|
||
|
||
async start() {
|
||
let confFile = resolve(__dirname, '../../oss/watchdog.json');
|
||
if (!existsSync(confFile)) {
|
||
//配置文件不存在
|
||
await this.ctorStartTime()
|
||
return;
|
||
}
|
||
let conf;
|
||
try {
|
||
conf = await JSON.parse(readFileSync(confFile, 'utf-8'));
|
||
} catch (e) {
|
||
//配置文件不是json
|
||
await this.ctorStartTime()
|
||
return;
|
||
}
|
||
//获取今天0点之后在线的玩家
|
||
let users = await G.mongodb.collection("user").find({
|
||
lv: {$gt: 10},
|
||
newonlinetime: {$gte: G.time - 1800, $lte: G.time + 30}
|
||
}, {
|
||
projection: {
|
||
uid: 1,
|
||
name: 1,
|
||
lv: 1,
|
||
vip: 1,
|
||
sid: 1
|
||
}
|
||
}).toArray();
|
||
|
||
if (!users.length) {
|
||
//没有符合的玩家
|
||
await this.ctorStartTime()
|
||
return;
|
||
}
|
||
|
||
let uids = users.map(i => i.uid);
|
||
let uinfo = {};
|
||
users.map(i => uinfo[i.uid] = i);
|
||
|
||
let actionLogs = await G.mongodb.collection("actionLog").find({
|
||
type: 'day',
|
||
uid: {$in: uids},
|
||
}, {
|
||
projection: {
|
||
uid: 1,
|
||
log: 1
|
||
}
|
||
}).toArray();
|
||
|
||
let warns = [];
|
||
for (let j = 0; j < conf.length; j++) {
|
||
for (let i = 0; i < actionLogs.length; i++) {
|
||
let key = conf[j].key;
|
||
let myval = actionLogs[i]?.log?.[key] || 0
|
||
let uid = actionLogs[i].uid
|
||
if (myval > conf[j].limit) {
|
||
//触发了警报
|
||
warns.push({
|
||
type: 'watchDog',
|
||
uid: uid,
|
||
name: uinfo[uid]?.name,
|
||
lv: uinfo[uid]?.lv,
|
||
vip: uinfo[uid]?.vip,
|
||
key: key,
|
||
value: myval,
|
||
tips: conf[j].tips
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
if (warns.length > 0) {
|
||
addWatchDogLog(warns);
|
||
}
|
||
|
||
await this.ctorStartTime()
|
||
}
|
||
|
||
get nextTime(): number {
|
||
return G.time + this.time;
|
||
}
|
||
|
||
async ctorStartTime() {
|
||
this.isStart = false;
|
||
this.startTime = this.nextTime;
|
||
}
|
||
} |