diff --git a/.gitignore b/.gitignore index 345ceec..3564e40 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ !/readme.md !/strichliste.lua !/.gitignore +!/collapse_log.lua diff --git a/collapse_log.lua b/collapse_log.lua new file mode 100644 index 0000000..cfba345 --- /dev/null +++ b/collapse_log.lua @@ -0,0 +1,34 @@ + +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()