only latest.log, better notification animation, better playtime updates
This commit is contained in:
parent
53a02cc7da
commit
e082fedf19
4 changed files with 88 additions and 27 deletions
|
|
@ -77,6 +77,38 @@ 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 }) {
|
||||
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()
|
||||
}, 30000)
|
||||
return () => clearInterval(timer)
|
||||
}
|
||||
|
||||
function latestLogPath(gameId) {
|
||||
return path.join(DIRS.DATA, 'logs', `${gameId}-latest.log`)
|
||||
}
|
||||
|
||||
function openLatestLog(gameId) {
|
||||
const logFile = latestLogPath(gameId)
|
||||
fs.mkdirSync(path.dirname(logFile), { recursive: true })
|
||||
// Keep only the latest run's log: truncate the file from the previous session
|
||||
// and clean up logs left by older versions (<id>-<timestamp>.log).
|
||||
try {
|
||||
for (const f of fs.readdirSync(path.dirname(logFile))) {
|
||||
if (f.startsWith(`${gameId}-`) && f.endsWith('.log') && f !== `${gameId}-latest.log`) {
|
||||
fs.rmSync(path.join(path.dirname(logFile), f), { force: true })
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
return fs.createWriteStream(logFile, { flags: 'w' })
|
||||
}
|
||||
|
||||
function runGame({ game, wine, systemWine, onExit }) {
|
||||
const isProton = wine && wine.type === 'proton'
|
||||
const { env } = buildEnv({ game, wine })
|
||||
|
|
@ -107,20 +139,20 @@ function runGame({ game, wine, systemWine, onExit }) {
|
|||
} catch (err) {
|
||||
return reject(err)
|
||||
}
|
||||
const logFile = path.join(DIRS.DATA, 'logs', `${game.id}-${Date.now()}.log`)
|
||||
fs.mkdirSync(path.dirname(logFile), { recursive: true })
|
||||
const ws = fs.createWriteStream(logFile)
|
||||
const logFile = latestLogPath(game.id)
|
||||
const ws = openLatestLog(game.id)
|
||||
child.stdout.pipe(ws)
|
||||
child.stderr.pipe(ws)
|
||||
child.on('error', reject)
|
||||
child.unref()
|
||||
const startedAt = Date.now()
|
||||
const stopTicker = startPlaytimeTicker({ game, startedAt })
|
||||
child.on('exit', (code, signal) => {
|
||||
stopTicker()
|
||||
ws.end()
|
||||
const playedMs = Date.now() - startedAt
|
||||
const counted = playedMs > GRACE_MS ? Math.round((playedMs - GRACE_MS) / 1000) : 0
|
||||
const counted = Math.max(0, Math.round((Date.now() - startedAt - GRACE_MS) / 1000))
|
||||
require('./library').updateGame(game.id, {
|
||||
lastPlayed: Date.now(),
|
||||
lastPlayed: startedAt,
|
||||
playSeconds: (game.playSeconds || 0) + counted
|
||||
})
|
||||
if (onExit) onExit(code, signal)
|
||||
|
|
@ -156,4 +188,4 @@ function runTool({ game, wine, tool, args, systemWine }) {
|
|||
})
|
||||
}
|
||||
|
||||
module.exports = { buildEnv, runGame, runTool, parseArgs }
|
||||
module.exports = { buildEnv, runGame, runTool, parseArgs, latestLogPath }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue