cubbot/src/modules/Life.ts

127 lines
3.6 KiB
TypeScript

import { LOW_HEALTH, MAX_HEALTH, REGENERATE_FOOD, SPRINT_FOOD } from '../utils/constants'
import { round } from '../utils/func'
import Module from '../utils/Module'
interface IConf {
/** Respawn on world join */
spawn: boolean,
/** Warn if health is lower */
lowHealth: number,
/** Life to regenerate */
targetHealth: number,
/** Log health info */
showHealth: boolean,
/** Use food if needed */
eat: boolean,
/** Prefer saturation over food power */
preferSaturation: boolean,
/** Warn and eat if food level is lower */
lowFood: number,
/** Food level needed to regenerate */
regenerateFood: number,
/** Log food level info */
showFood: boolean,
}
/** Monitor health, food and xp */
export default class Life extends Module<IConf> {
public get health() {
return this._health
}
public get food() {
return this._food
}
public get saturation() {
return this._saturation
}
public get alive() {
return this.health > 0
}
public get mustEat() {
return this.food <= this.conf.lowFood ||
(this.health <= this.conf.targetHealth && this.food <= this.conf.regenerateFood)
}
public get experienceBar() {
return this._experienceBar
}
public get level() {
return this._level
}
public get totalExperience() {
return this._totalExperience
}
private _health!: number
private _food!: number
private _saturation!: number
private _experienceBar!: number
private _level!: number
private _totalExperience!: number
public respawn() {
this.logger.info('respawn')
this.client.write('client_command', { action: 1 })
}
protected mount() {
//TODO: const inventory = this.conf.eat ? this.load('inventory') : undefined
this.client.on('update_health', data => {
if (this.conf.spawn && this._health == null) {
setTimeout(this.respawn.bind(this), 500)
}
this._health = round(data.health)
this._food = data.food
this._saturation = data.foodSaturation
if (this.health <= this.conf.lowHealth) {
this.logger.warn({ type: 'health', status: this.alive ? 'low' : 'ko', health: this.health })
} else if (this.conf.showHealth) {
this.logger.info({ type: 'health', status: 'ok', health: this.health })
}
if (this.food <= this.conf.lowFood) {
this.logger.warn({ type: 'food', status: 'low', food: this.food, saturation: this.saturation })
} else if (this.conf.showHealth) {
this.logger.info({ type: 'food', status: 'ok', food: this.food, saturation: this.saturation })
}
if (this.conf.eat && this.mustEat) {
this.logger.warn('TODO: must Eat')
// TODO: inventory.items.filter(food).orderBy(this.conf.preferSaturation)
}
})
this.client.on('experience', data => {
this._experienceBar = data.experienceBar
this._level = data.level
this._totalExperience = data.totalExperience
})
}
//TODO: send client settings
protected getConf(): IConf {
return {
spawn: true,
lowHealth: LOW_HEALTH,
targetHealth: MAX_HEALTH,
showHealth: false,
eat: true,
preferSaturation: true,
lowFood: SPRINT_FOOD,
regenerateFood: REGENERATE_FOOD,
showFood: false,
}
}
}