From 82e76462305d8c433befb341fa372a73efb8894d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 30 Oct 2024 23:09:58 +0100 Subject: [PATCH] config and transaction sounds --- strichliste.lua | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/strichliste.lua b/strichliste.lua index 4e941b9..c0f73a2 100755 --- a/strichliste.lua +++ b/strichliste.lua @@ -29,6 +29,22 @@ local function parse_query(q) return data end +local function load_config() + local log = io.open("config", "r") + if log == nil then return {} end + local config = {} + for l in log:lines("l") do + if l ~= "" and l[0] ~= "#" then + local key, value = string.match(l, "^([^=]+)=([^=]*)") + if key ~= nil and value ~= nil then + config[key] = value + end + end + end + return config +end + +local config = load_config() local path = os.getenv("PATH_INFO") local method = os.getenv("REQUEST_METHOD") local query = parse_query(os.getenv("QUERY_STRING")) @@ -47,7 +63,6 @@ local stylesheet = [[ form h3 { margin: 5px; } ]] --- local script = io.open("main.js"):read("a") local script = [[ document.addEventListener("keypress", ev => { if (!(document.activeElement instanceof HTMLInputElement)) { @@ -70,6 +85,7 @@ local function respond(status, title, body) + %s - ]], escape(title), stylesheet, script)) + ]], escape(title), stylesheet, script, config.head_extra or "")) body() print("") end @@ -187,9 +203,13 @@ local function r_user_post(username) log:write(string.format("%d,%s,%d,%s\n", time, username, amount, comment)) log:flush() log:close() - return string.format( - "

Transaction successful: %.02f€ (%s)

", - amount >= 0 and "pos" or "neg", amount / 100, escape(comment) + return string.format([[ +

Transaction successful: %.02f€ (%s)

+ + ]], + amount >= 0 and "pos" or "neg", amount / 100, + escape(comment), + config.transaction_sound or "" ) end @@ -204,18 +224,19 @@ local function r_user(username) local last_txn = last_txns()[username] local new_user = balance == nil balance = balance or 0 + if notif then print(notif) end if new_user then print([[

This user account does not exist yet. It will only be created after the first transaction.

]]) + else + print(string.format([[ +

Current balance: %.02f€

+ ]], balance >= 0 and "pos" or "neg", balance / 100)) + print(string.format([[ +

Last transaction added %s ago. View user log + ]], format_duration(os.time() - last_txn), username)) end - if notif then print(notif) end - print(string.format([[ -

Current balance: %.02f€

- ]], balance >= 0 and "pos" or "neg", balance / 100)) - print(string.format([[ -

Last transaction added %s ago. View user log - ]], format_duration(os.time() - last_txn), username)) print([[

Create Transaction