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

Commit c48cd42

Browse files
committed
优化 玩家数量图表数据
1 parent d2f6bd4 commit c48cd42

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/entity/commands/task/players.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,42 @@ export default class RefreshPlayer implements ILifeCycleTask {
4343
// latency: 1
4444
// }
4545
try {
46+
// 获取玩家人数版本等信息
4647
const result = await instance.execPreset("getPlayer");
4748
if (!result) return;
4849
instance.info.maxPlayers = result.max_players ? result.max_players : -1;
4950
instance.info.currentPlayers = result.current_players ? result.current_players : -1;
5051
instance.info.version = result.version ? result.version : "";
51-
} catch (error) { }
52+
53+
// 当第一次正确获取用户人数时,初始化玩家人数图表
54+
if (this.playersChart.length === 0) {
55+
this.initPlayersChart(instance);
56+
}
57+
} catch (error) {}
5258
}, 3000);
5359

54-
// 提前填充在线人数报表数据
60+
// 开启查询在线人数报表数据定时器
61+
this.playersChartTask = setInterval(() => {
62+
this.getPlayersChartData(instance);
63+
}, 600000);
64+
}
65+
66+
initPlayersChart(instance: Instance) {
5567
while (this.playersChart.length < 60) {
5668
this.playersChart.push({ value: "0" });
5769
}
5870
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);
71+
this.getPlayersChartData(instance);
6772
}
6873

69-
async getPlayersChartData(instance: Instance) {
74+
getPlayersChartData(instance: Instance) {
7075
try {
71-
const result = await instance.execPreset("getPlayer");
72-
if (!result) return;
76+
this.playersChart.shift();
7377
this.playersChart.push({
74-
value: result.current_players ? result.current_players : 0
78+
value: String(instance.info.currentPlayers) ?? "0"
7579
});
76-
if (this.playersChart.length > 60) {
77-
this.playersChart = this.playersChart.slice(1, this.playersChart.length);
78-
}
7980
instance.info.playersChart = this.playersChart;
80-
} catch (error) { }
81+
} catch (error) {}
8182
}
8283

8384
async stop(instance: Instance) {
@@ -87,5 +88,7 @@ export default class RefreshPlayer implements ILifeCycleTask {
8788
instance.info.currentPlayers = -1;
8889
instance.info.version = "";
8990
instance.info.playersChart = [];
91+
this.playersChart = [];
92+
this.playersChartTask = null;
9093
}
9194
}

0 commit comments

Comments
 (0)