@@ -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}
0 commit comments