config and transaction sounds

This commit is contained in:
metamuffin 2024-10-30 23:09:58 +01:00
parent c3dadcc5a3
commit 82e7646230
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654

View file

@ -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)
<meta charset="utf-8" />
<style>%s</style>
<script>%s</script>
%s
</head>
<body>
<nav>
@ -77,7 +93,7 @@ local function respond(status, title, body)
<span><a href="/?log">View Log</a></span>
<span><a href="https://codeberg.org/metamuffin/strichliste">Source</a></span>
</nav>
]], escape(title), stylesheet, script))
]], escape(title), stylesheet, script, config.head_extra or ""))
body()
print("</body></html>")
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(
"<div class=\"notif\"><p>Transaction successful: <strong class=\"amount-%s\">%.02f€</strong> (%s)</p></div>",
amount >= 0 and "pos" or "neg", amount / 100, escape(comment)
return string.format([[
<div class=\"notif\"><p>Transaction successful: <strong class=\"amount-%s\">%.02f€</strong> (%s)</p></div>
<audio src="%s" autoplay></audio>
]],
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([[
<div class="notif"><p><i>This user account does not exist yet. It will only be created after the first transaction.</i></p></div>
]])
end
if notif then print(notif) end
else
print(string.format([[
<p>Current balance: <span class="amount-%s">%.02f</p>
]], balance >= 0 and "pos" or "neg", balance / 100))
print(string.format([[
<p>Last transaction added %s ago. <a href="/%s?log">View user log</a>
]], format_duration(os.time() - last_txn), username))
end
print([[
<form class="transaction box" action="" method="POST">
<h3>Create Transaction</h3>