cubbot/src/modules/Time.ts

58 lines
1.3 KiB
TypeScript

import { Client } from 'minecraft-protocol'
import { longToNumber } from '../core/utils'
import Module from './Module'
interface IUpdateTime {
age: number[]
time: number[]
}
export default class Time extends Module {
private _age!: number
public get age() {
return this._age
}
private _time!: number
public get time() {
return this._time
}
private _tps!: number
public get tps() {
return this._tps
}
private _tpsLocal!: number
public get tpsLocal() {
return this._tpsLocal
}
private _at: number = Date.now()
public mount() {
this.client.on('update_time', (data: IUpdateTime) => {
const now = Date.now()
const age = longToNumber(data.age)
if (this._age) {
this._tps = age - this._age
if (this.tps < 20) {
this.logger.debug('Server Lag: %s', this.tps)
}
this._tpsLocal = (now - this._at) / 50
if (this._tpsLocal > 20 + .5) {
this.logger.debug('Client Lag: %s', this.tpsLocal)
}
}
this._age = age
this._at = now
this._time = Math.abs(data.time[1]) % 24000
})
}
}