aboutsummaryrefslogtreecommitdiff
path: root/collapse_log.lua
blob: 19afe3dcb0d71ea9bf6000faee74d3210f9c030d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local function read_log()
    local log = io.open("log", "r")
    if log == nil then
        return function() return nil end
    end
    local lines = log:lines("l")
    return function()
        local l = lines()
        if l == "" or l == nil then
            return nil
        end
        local time, username, amount, comment = string.match(l, "(%d+),([%w_ -]+),(-?%d+),([%w_ -]*)")
        return tonumber(time), username, tonumber(amount), comment
    end
end

local function balances()
    local users = {}
    for _, username, amount, _ in read_log() do
        users[username] = (users[username] or 0) + amount
    end
    return users
end

local newlog = io.open("log_collapsed","w+")
if newlog == nil then
    return print("error failed to open log")
end
for username, amount in pairs(balances()) do
    newlog:write(string.format("%d,%s,%d,%s\n", os.time(), username, amount, "Collapsed transaction history"))
end
newlog:close()