Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 02245d5

Browse files
authored
Merge pull request #17 from LingMuQingYu/master
Daemon端增加记录10小时内在线人数统计
2 parents 1f3b1a4 + 4c99a64 commit 02245d5

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/entity/commands/task/players.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default class RefreshPlayer implements ILifeCycleTask {
2727
public status: number = 0;
2828

2929
private task: any = null;
30+
private playersChartTask: any = null;
31+
private playersChart: Array<{ value: string }> = [];
3032

3133
async start(instance: Instance) {
3234
this.task = setInterval(async () => {
@@ -46,14 +48,44 @@ export default class RefreshPlayer implements ILifeCycleTask {
4648
instance.info.maxPlayers = result.max_players ? result.max_players : -1;
4749
instance.info.currentPlayers = result.current_players ? result.current_players : -1;
4850
instance.info.version = result.version ? result.version : "";
49-
} catch (error) {}
51+
} catch (error) { }
5052
}, 3000);
53+
54+
// 提前填充在线人数报表数据
55+
while (this.playersChart.length < 60) {
56+
this.playersChart.push({ value: "0" });
57+
}
58+
instance.info.playersChart = this.playersChart;
59+
60+
// 启动的时候先执行一次
61+
this.getPlayersChartData(instance).then(() => { });
62+
63+
// 开启查询在线人数报表数据定时器
64+
this.playersChartTask = setInterval(() => {
65+
this.getPlayersChartData(instance).then(() => { });
66+
}, 600000);
67+
}
68+
69+
async getPlayersChartData(instance: Instance) {
70+
try {
71+
const result = await instance.execPreset("getPlayer");
72+
if (!result) return;
73+
this.playersChart.push({
74+
value: result.current_players ? result.current_players : 0
75+
});
76+
if (this.playersChart.length > 60) {
77+
this.playersChart = this.playersChart.slice(1, this.playersChart.length);
78+
}
79+
instance.info.playersChart = this.playersChart;
80+
} catch (error) { }
5181
}
5282

5383
async stop(instance: Instance) {
84+
clearInterval(this.task);
85+
clearInterval(this.playersChartTask);
5486
instance.info.maxPlayers = -1;
5587
instance.info.currentPlayers = -1;
5688
instance.info.version = "";
57-
clearInterval(this.task);
89+
instance.info.playersChart = [];
5890
}
5991
}

src/entity/instance/instance.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ interface IInstanceInfo {
3939
maxPlayers: number;
4040
version: string;
4141
fileLock: number;
42+
playersChart: Array<{ value: string }>
4243
}
4344

4445
// 实例类
@@ -77,7 +78,8 @@ export default class Instance extends EventEmitter {
7778
currentPlayers: -1,
7879
maxPlayers: -1,
7980
version: "",
80-
fileLock: 0
81+
fileLock: 0,
82+
playersChart: []
8183
};
8284

8385
// 实例的真实进程

0 commit comments

Comments
 (0)