105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
import { EventEmitter } from 'events'
|
|
import { LOW_HEALTH, MAX_FOOD, MAX_HEALTH, MAX_SATURATION, SPRINT_FOOD } from '../utils/constants'
|
|
import { round } from '../utils/func'
|
|
import Module from '../utils/Module'
|
|
import Spawn from './Spawn'
|
|
|
|
interface IConf {
|
|
/** Warn if health is lower */
|
|
lowHealth: number,
|
|
/** Log health info */
|
|
showHealth: boolean,
|
|
/** Warn if food level is lower */
|
|
lowFood: number,
|
|
/** Log food level info */
|
|
showFood: boolean,
|
|
}
|
|
|
|
/** Monitor health, food and xp */
|
|
export default class Life extends Module<IConf> {
|
|
|
|
private _health = MAX_HEALTH
|
|
public get health() {
|
|
return this._health
|
|
}
|
|
|
|
private _food = MAX_FOOD
|
|
public get food() {
|
|
return this._food
|
|
}
|
|
|
|
private _saturation = MAX_SATURATION
|
|
public get saturation() {
|
|
return this._saturation
|
|
}
|
|
|
|
public get alive() {
|
|
return this.health > 0
|
|
}
|
|
|
|
private _experienceBar = 0
|
|
public get experienceBar() {
|
|
return this._experienceBar
|
|
}
|
|
|
|
private _level = 0
|
|
public get level() {
|
|
return this._level
|
|
}
|
|
|
|
private _totalExperience = 0
|
|
public get totalExperience() {
|
|
return this._totalExperience
|
|
}
|
|
|
|
private _events = new EventEmitter()
|
|
public get events() {
|
|
return this._events
|
|
}
|
|
|
|
protected mount() {
|
|
const spawn = this.load<Spawn>(Spawn)
|
|
spawn.addCheck('health')
|
|
|
|
this.client.on('update_health', data => {
|
|
this._health = round(data.health)
|
|
this._food = data.food
|
|
this._saturation = data.foodSaturation
|
|
|
|
spawn.validateCheck('health')
|
|
|
|
if (this.health <= this.conf.lowHealth) {
|
|
this.logger.warn({ msg: this.alive ? 'Low health' : 'Dead', type: 'health', value: this.health })
|
|
} else if (this.conf.showHealth) {
|
|
this.logger.info({ msg: 'Healthy', type: 'health', value: this.health })
|
|
}
|
|
|
|
if (this.food <= this.conf.lowFood) {
|
|
this.logger.warn({ msg: 'Hungry', type: 'food', value: this.food })
|
|
} else if (this.conf.showHealth) {
|
|
this.logger.info({ msg: 'Replete', type: 'food', value: this.food })
|
|
}
|
|
|
|
this._events.emit('update')
|
|
})
|
|
|
|
this.client.on('experience', data => {
|
|
this._experienceBar = data.experienceBar
|
|
this._level = data.level
|
|
this._totalExperience = data.totalExperience
|
|
})
|
|
}
|
|
|
|
// MAYBE: send client settings
|
|
|
|
protected getConf(): IConf {
|
|
return {
|
|
lowHealth: LOW_HEALTH,
|
|
showHealth: false,
|
|
lowFood: SPRINT_FOOD,
|
|
showFood: false,
|
|
}
|
|
}
|
|
|
|
}
|