Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Created December 10, 2025 16:33
Show Gist options
  • Select an option

  • Save radgeRayden/4c1614dbfe63fe18a995bb332471fbf5 to your computer and use it in GitHub Desktop.

Select an option

Save radgeRayden/4c1614dbfe63fe18a995bb332471fbf5 to your computer and use it in GitHub Desktop.
#!/usr/bin/lua
local dir = (arg[0]):match("^(.+/).+$")
local logs, err = io.open(dir .. "game_session_logs.txt", "r+b")
if not logs then
print(err)
return
end
local games = {}
local game_array = {}
for line in logs:lines("*l") do
local time, mode, name, platform = line:match("(%d+)%s+(%w+)%s+(.*)%s+%((.+)%)$")
if not (time and mode and name and platform) then
print("malformed line:", line)
goto continue
end
local game = games[name]
if not game then
game = {
name = name,
platform = platform,
logs = {},
playtime = 0,
}
games[name] = game
table.insert(game_array, game)
end
table.insert(game.logs, {
time = tonumber(time),
mode = mode,
})
::continue::
end
logs:close()
for k, game in pairs(games) do
local open = false
local start_timestamp = 0
for i, entry in ipairs(game.logs) do
if entry.mode == "start" then
if open then
goto skip
end
start_timestamp = entry.time
open = true
elseif entry.mode == "end" then
if not open then
goto skip
end
game.playtime = game.playtime + (entry.time - start_timestamp)
open = false
else
end
::skip::
end
end
table.sort(game_array, function(a, b)
return a.playtime > b.playtime
end)
for i, game in ipairs(game_array) do
local t = game.playtime
local hours, minutes, seconds = math.modf(t / 3600), math.modf(t / 60) % 60, t % 60
local formatted_time = ("%02d:%02d:%02d"):format(hours, minutes, seconds)
local formatted_name = game.name .. " " .. string.rep(".", 50 - utf8.len(game.name) - 1)
print(("%3d. %10s | %-50s %s"):format(i, formatted_time, formatted_name, game.platform))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment