standardize logs

This commit is contained in:
2025-01-23 12:11:26 +09:00
parent 55e5e8117c
commit de4c82fd8b
7 changed files with 57 additions and 32 deletions
+12 -10
View File
@@ -35,7 +35,7 @@ export default class Game {
action(id, options) {
const entity = this.entities.find((it) => it.id == id)
if (entity == null) {
console.error({ error: 'Invalid ID' })
console.info({ info: 'action_invalid_id', id, options })
return
}
@@ -54,7 +54,7 @@ export default class Game {
if (object instanceof Entity) { this.despawnEntity(object) }
else if (object instanceof Terrain) { this.removeTerrain(object) }
else if (object instanceof Projectile) { this.despawnProjectile(object) }
else { console.error({ error: { reason: 'Can\'t despawn object', object } }) }
else { console.error({ error: 'despawn_unknown_object', object }) }
}
despawnEntity(entity) {
@@ -92,7 +92,7 @@ export default class Game {
if (object instanceof Entity) { this.spawnEntity(object) }
else if (object instanceof Terrain) { this.addTerrain(object) }
else if (object instanceof Projectile) { this.spawnProjectile(object) }
else { console.error({ error: { reason: 'Can\'t spawn object', object } }) }
else { console.error({ error: 'spawn_unknown_object', object }) }
}
spawnEntity(entity) {
@@ -109,7 +109,7 @@ export default class Game {
if (this.#gameLoopIntervalId != null) { return }
this.#startTimestamp = performance.now() + (this.currentTick * this.tickBudget)
console.info(`Started game ${this.id} with ${this.tickRate} tps. Starting on tick ${this.currentTick}.`)
console.info({ event: 'game_start', id: this.id, tickRate: this.tickRate, currentTick: this.currentTick })
this.#gameLoopIntervalId = setInterval(this.#gameLoopCall.bind(this), this.tickBudget / 5)
}
@@ -118,7 +118,7 @@ export default class Game {
clearInterval(this.#gameLoopIntervalId)
this.#gameLoopIntervalId = null
console.info(`Stopped game ${this.id}. Stopped on tick ${this.currentTick}.`)
console.info({ event: 'game_stop', id: this.id, currentTick: this.currentTick })
}
subscription(websocket, id) {
@@ -126,7 +126,10 @@ export default class Game {
const game = this
const entity = game.entities.find((it) => it.id == id)
if (entity == null) { return }
if (entity == null) {
websocket.close()
return
}
const team = entity.team
const state = game.visionByTeam(team)
@@ -189,15 +192,14 @@ export default class Game {
const before = performance.now()
this.update()
const after = performance.now()
const taken = (after - before)
const tickTaken = after - before
const useAbsoluteBehind = true
const absoluteBehind = Math.max(0, (start - this.#startTimestamp) - ((this.currentTick) * tickBudget))
this.#nextTickAt = start + tickBudget - (useAbsoluteBehind ? absoluteBehind : prevBehind)
if (after - before > tickBudget) {
const behindNotice = absoluteBehind > 0.1 ? `(Was already behind ${absoluteBehind.toFixed(1)} ms)` : ``
console.warn(`Can't keep up! A tick took ${taken.toFixed(1)} ms / ${tickBudget.toFixed(1)} ms. ${behindNotice}`)
if (tickTaken > tickBudget) {
console.warn({ warn: 'overload', tickTaken, tickBudget, absoluteBehind })
}
}
}