cubbot/src/modules/Life.ts

105 lines
2.7 KiB
TypeScript
Raw Normal View History

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