separate playtime file

This commit is contained in:
olcxja 2026-09-26 09:55:18 +02:00
commit 78ef651ca7
3 changed files with 47 additions and 10 deletions

View file

@ -77,15 +77,16 @@ function parseArgs(args) {
const GRACE_MS = 60000
// Live playtime: update stats while the game is still running (not only on exit).
function startPlaytimeTicker({ game, startedAt, onTick }) {
// Live playtime: while the game runs, update ONLY the tiny playtime file
// (a number of seconds) every 30s - apps.json is left untouched to spare the
// disk. lastPlayed is stamped once at start, final totals are mirrored into
// the registry once on exit.
function startPlaytimeTicker({ game, startedAt }) {
const library = require('./library')
const base = game.playSeconds || 0
const timer = setInterval(() => {
const counted = Math.max(0, Math.round((Date.now() - startedAt - GRACE_MS) / 1000))
require('./library').updateGame(game.id, {
lastPlayed: startedAt,
playSeconds: (game.playSeconds || 0) + counted
})
if (onTick) onTick()
library.writePlaySeconds(game.id, base + counted)
}, 30000)
return () => clearInterval(timer)
}
@ -147,6 +148,8 @@ function runGame({ game, wine, systemWine, onExit }) {
child.unref()
const startedAt = Date.now()
const stopTicker = startPlaytimeTicker({ game, startedAt })
// stamp "last played" once at start so it is correct even if we crash later
require('./library').updateGame(game.id, { lastPlayed: startedAt })
child.on('exit', (code, signal) => {
stopTicker()
ws.end()