cleanup player tracking
This commit is contained in:
parent
07b5dc1f17
commit
f5a9769cc6
2 changed files with 68 additions and 43 deletions
45
src/main.js
45
src/main.js
|
@ -1,6 +1,7 @@
|
|||
import mcProtocol from "minecraft-protocol"
|
||||
import sqlite from "node:sqlite"
|
||||
import migrateDB from "./migrations.js"
|
||||
import PlayerTracker from "./player-tracker.js"
|
||||
|
||||
import Chat from "./modules/chat.js"
|
||||
import Death from "./modules/death.js"
|
||||
|
@ -84,49 +85,7 @@ function newClient() {
|
|||
death.respawn()
|
||||
})
|
||||
|
||||
// TODO: Move this into another file and organize it better?
|
||||
players.addListener("player-add", player => {
|
||||
console.log(`${player.username} (${player.uuid}) joined the game`)
|
||||
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO player (uuid, username, first_online, last_online, playtime)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT (uuid) DO UPDATE SET
|
||||
username = excluded.username;
|
||||
`)
|
||||
|
||||
const now = Date.now()
|
||||
stmt.run(
|
||||
player.uuid,
|
||||
player.username,
|
||||
now,
|
||||
now,
|
||||
0,
|
||||
)
|
||||
})
|
||||
|
||||
players.addListener("player-remove", player => {
|
||||
console.log(`${player.username} left the game`)
|
||||
})
|
||||
|
||||
setInterval(() => {
|
||||
const now = Date.now()
|
||||
const stmt = db.prepare(`
|
||||
UPDATE player
|
||||
SET
|
||||
last_online = ?,
|
||||
playtime = playtime + 1
|
||||
WHERE
|
||||
uuid = ?;
|
||||
`)
|
||||
|
||||
for (const [, player] of players.players) {
|
||||
stmt.run(
|
||||
now,
|
||||
player.uuid,
|
||||
)
|
||||
}
|
||||
}, 1_000)
|
||||
new PlayerTracker(client, players, db)
|
||||
|
||||
// Commands
|
||||
const commands = new Commands(chat, "!")
|
||||
|
|
66
src/player-tracker.js
Normal file
66
src/player-tracker.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
export default class PlayerTracker {
|
||||
|
||||
#client
|
||||
#players
|
||||
|
||||
#insertStmt
|
||||
#updateStmt
|
||||
|
||||
#trackInterval
|
||||
|
||||
constructor(client, players, db) {
|
||||
this.#client = client
|
||||
this.#players = players
|
||||
|
||||
this.#insertStmt = db.prepare(`
|
||||
INSERT INTO player (uuid, username, first_online, last_online, playtime)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT (uuid) DO UPDATE SET
|
||||
username = excluded.username;
|
||||
`)
|
||||
|
||||
this.#updateStmt = db.prepare(`
|
||||
UPDATE player
|
||||
SET
|
||||
last_online = ?,
|
||||
playtime = playtime + 1
|
||||
WHERE
|
||||
uuid = ?;
|
||||
`)
|
||||
|
||||
this.#players.addListener("player-add", this.#playerAdd.bind(this))
|
||||
this.#players.addListener("player-remove", this.#playerRemove.bind(this))
|
||||
|
||||
this.#trackInterval = setInterval(this.#track.bind(this), 1_000)
|
||||
this.#client.addListener("end", this.#stop.bind(this))
|
||||
}
|
||||
|
||||
#stop() {
|
||||
clearInterval(this.#trackInterval)
|
||||
}
|
||||
|
||||
#playerAdd(player) {
|
||||
console.log(`${player.username} (${player.uuid}) joined the game`)
|
||||
|
||||
const now = Date.now()
|
||||
this.#insertStmt.run(
|
||||
player.uuid,
|
||||
player.username,
|
||||
now,
|
||||
now,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
#playerRemove(player) {
|
||||
console.log(`${player.username} left the game`)
|
||||
}
|
||||
|
||||
#track() {
|
||||
const now = Date.now()
|
||||
|
||||
for (const [, player] of this.#players.players) {
|
||||
this.#updateStmt.run(now, player.uuid)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue