Make new scheme work basically

This commit is contained in:
metamuffin 2024-11-05 01:13:19 +01:00
parent 28e9e15e42
commit 83275a5131
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654

View file

@ -17,14 +17,18 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
]] -- ]] --
-- replace german chars with a better %w that allows unicode -- TODO: allow unicode
-- TODO: somehow remove _opt variants
local matchers = { local matchers = {
time = "(%d+)", time = "(%d+)",
user = "([%w_@ -öäüÖÄÜßẞ]+)", user = "([%w_@ -]+)",
amount = "(-?%d+)", amount = "(-?%d+)",
comment = "([%w_ -öäüÖÄÜßẞ]*)", amount_opt = "(-?%d*)",
barcode = "([%w_-]*)", comment = "([%w_ -]+)",
name = "([%w_ -öäüÖÄÜßẞ]*)", comment_opt = "([%w_ -]*)",
barcode = "([%w_-]+)",
barcode_opt = "([%w_-]*)",
name = "([%w_ -]+)",
} }
local matchers_global = (function() local matchers_global = (function()
local s = {} local s = {}
@ -235,7 +239,7 @@ local function read_log()
return nil return nil
end end
local time, user_src, user_dst, amount, pcode, pcount, comment = string.match(l, local time, user_src, user_dst, amount, pcode, pcount, comment = string.match(l,
format("{time},{user},{user},{amount},{barcode},{amount},{comment}", matchers)) format("^{time},{user},{user},{amount},{barcode_opt},{amount_opt},{comment_opt}$", matchers))
return tonumber(time), user_src, user_dst, tonumber(amount), pcode, tonumber(pcount), comment return tonumber(time), user_src, user_dst, tonumber(amount), pcode, tonumber(pcount), comment
end end
end end
@ -251,7 +255,7 @@ local function read_products()
if l == "" or l == nil then if l == "" or l == nil then
return nil return nil
end end
local barcode, price, user, name = string.match(l, "{barcode},{amount},{user},{name}") local barcode, price, user, name = string.match(l, format("^{barcode},{amount},{user}?,{name}$", matchers))
return barcode, tonumber(price), user, name return barcode, tonumber(price), user, name
end end
end end
@ -289,12 +293,12 @@ local function get_active_users()
for time, user_src, user_dst, amount, _, _, _ in read_log() do for time, user_src, user_dst, amount, _, _, _ in read_log() do
user_balances[user_src] = { user_balances[user_src] = {
time = time, time = time,
user_src = user_src, name = user_src,
balance = (user_balances[user_src] or { balance = 0 }).balance - amount balance = (user_balances[user_src] or { balance = 0 }).balance - amount
} }
user_balances[user_dst] = { user_balances[user_dst] = {
time = time, time = time,
user_dst = user_dst, name = user_dst,
balance = (user_balances[user_dst] or { balance = 0 }).balance + amount balance = (user_balances[user_dst] or { balance = 0 }).balance + amount
} }
end end
@ -308,15 +312,15 @@ local function get_active_users()
return users return users
end end
local function r_user_post(username) local function r_transaction_post()
local data = form_data() local data = form_data()
local user_src = data.user_src or username local user_src = data.user_src
local user_dst = data.user_dst local user_dst = data.user_dst
local amount = tonumber(data.amount) local amount = tonumber(data.amount)
local pcode = data.pcode local pcode = data.pcode
local pcount = tonumber(data.pcount) local pcount = tonumber(data.pcount)
local comment = data.comment local comment = data.comment
if pcode then if pcode ~= nil and pcode ~= "" then
local exists = false local exists = false
for p_barcode, p_amount, p_user, p_name in read_products() do for p_barcode, p_amount, p_user, p_name in read_products() do
if p_barcode == pcode then if p_barcode == pcode then
@ -332,11 +336,11 @@ local function r_user_post(username)
return error_box("unknown product") return error_box("unknown product")
end end
end end
user_dst = user_dst or "@global" user_src = user_src or "@global"
if amount == nil then if amount == nil then
return error_box("amount invalid") return error_box("amount invalid")
end end
if comment == nil or comment:match(matchers_global.comment) == nil then if comment == nil or comment:match(matchers_global.comment_opt) == nil then
return error_box("comment invalid") return error_box("comment invalid")
end end
if user_src == nil or user_src:match(matchers_global.user) == nil then if user_src == nil or user_src:match(matchers_global.user) == nil then
@ -368,7 +372,7 @@ end
local function r_user(username) local function r_user(username)
local notif = nil local notif = nil
if method == "POST" then if method == "POST" then
notif = r_user_post(username) notif = r_transaction_post()
end end
return respond(200, string.format("Abrechenbarheit: %s", username), function() return respond(200, string.format("Abrechenbarheit: %s", username), function()
print(format("<h1>{username}</h1>", { username = username })) print(format("<h1>{username}</h1>", { username = username }))
@ -393,10 +397,12 @@ local function r_user(username)
for _, amount in ipairs({ 50, 100, 150, 200, 500, 1000 }) do for _, amount in ipairs({ 50, 100, 150, 200, 500, 1000 }) do
local a = amount * type local a = amount * type
print(format([[<form action="" method="POST"> print(format([[<form action="" method="POST">
<input type="number" name="amount" id="amount" value="{a_raw}" hidden /> <input type="text" name="user_dst" value="{!username}" hidden />
<input type="text" name="comment" id="comment" value="" hidden /> <input type="number" name="amount" value="{a_raw}" hidden />
<input type="text" name="comment" value="" hidden />
<input type="submit" value="{amount}" class="amount-{sign} button" /> <input type="submit" value="{amount}" class="amount-{sign} button" />
</form>]], { </form>]], {
username = username,
a_raw = a, a_raw = a,
amount = format_amount(a), amount = format_amount(a),
sign = a < 0 and "neg" or "pos" sign = a < 0 and "neg" or "pos"
@ -407,6 +413,7 @@ local function r_user(username)
print(format([[ print(format([[
<form class="transaction box backgroundbox" action="" method="POST"> <form class="transaction box backgroundbox" action="" method="POST">
<h3>{+user.form.transaction}</h3> <h3>{+user.form.transaction}</h3>
<input type="text" name="user_dst" value="{!username}" hidden />
<label for="amount">Amount (ct): </label> <label for="amount">Amount (ct): </label>
<input type="number" name="amount" id="amount" /> <input type="number" name="amount" id="amount" />
<label for="comment">Comment: </label> <label for="comment">Comment: </label>
@ -415,6 +422,7 @@ local function r_user(username)
</form> </form>
<form class="transaction box backgroundbox" action="" method="POST" id="buy_product"> <form class="transaction box backgroundbox" action="" method="POST" id="buy_product">
<h3>{+user.form.buy}</h3> <h3>{+user.form.buy}</h3>
<input type="text" name="user_dst" value="{!username}" hidden />
<input type="text" name="negate_pcount" value="1" hidden /> <input type="text" name="negate_pcount" value="1" hidden />
<label for="pcount">Count: </label> <label for="pcount">Count: </label>
<input type="number" name="pcount" id="pcount" value="1" /> <input type="number" name="pcount" id="pcount" value="1" />
@ -424,6 +432,7 @@ local function r_user(username)
</form> </form>
<form class="transaction box backgroundbox" action="" method="POST" id="buy_product"> <form class="transaction box backgroundbox" action="" method="POST" id="buy_product">
<h3>{+user.form.restock}</h3> <h3>{+user.form.restock}</h3>
<input type="text" name="user_dst" value="{!username}" hidden />
<label for="pcount">{+field.count}: </label> <label for="pcount">{+field.count}: </label>
<input type="number" name="pcount" id="pcount" value="1" /> <input type="number" name="pcount" id="pcount" value="1" />
<label for="amount">{+field.upstream_price}: </label> <label for="amount">{+field.upstream_price}: </label>
@ -432,13 +441,18 @@ local function r_user(username)
<input type="text" name="pcode" id="pcode" /> <input type="text" name="pcode" id="pcode" />
<input type="submit" value="{+user.form.restock.submit}" class="button amount-pos" /> <input type="submit" value="{+user.form.restock.submit}" class="button amount-pos" />
</form> </form>
]])) ]], { username = username }))
print("</div>") print("</div>")
end) end)
end end
local function r_log(filter) local function r_log(filter)
local notif = nil
if method == "POST" then
notif = r_transaction_post()
end
return respond(200, "Abrechnungen", function() return respond(200, "Abrechnungen", function()
if notif then print(notif) end
print([[<table class="log"]]) print([[<table class="log"]])
print(format([[<thead><tr> print(format([[<thead><tr>
<th>{+field.time}</th> <th>{+field.time}</th>
@ -455,17 +469,17 @@ local function r_log(filter)
print(format([[ print(format([[
<tr> <tr>
<td>{time} ({time_delta})</td> <td>{time} ({time_delta})</td>
<td>{user_src} {user_dst</td> <td>{user_src} {user_dst}</td>
{amount} {amount}
<td>{pcode}</td> <td>{pcode}</td>
<td>{pcount}</td> <td>{pcount}</td>
<td>{comment}</td> <td>{comment}</td>
<td> <td>
<form action="/?transaction" method="POST"> <form action="/?log" method="POST">
<input type="number" name="user_src" value="{user_src}" hidden /> <input type="text" name="user_src" value="{user_src}" hidden />
<input type="number" name="user_dst" value="{user_dst}" hidden /> <input type="text" name="user_dst" value="{user_dst}" hidden />
<input type="number" name="amount" value="{revert_amount}" hidden /> <input type="number" name="amount" value="{revert_amount}" hidden />
<input type="number" name="pcode" value="{pcode}" hidden /> <input type="text" name="pcode" value="{pcode}" hidden />
<input type="number" name="pcount" value="{revert_pcount}" hidden /> <input type="number" name="pcount" value="{revert_pcount}" hidden />
<input type="text" name="comment" value="Revert {comment}" hidden /> <input type="text" name="comment" value="Revert {comment}" hidden />
<input type="submit" class="amount-ntr button" value="Revert" /> <input type="submit" class="amount-ntr button" value="Revert" />
@ -483,7 +497,7 @@ local function r_log(filter)
(pcount and tostring(math.abs(pcount)) or ""), (pcount and tostring(math.abs(pcount)) or ""),
comment = escape(comment), comment = escape(comment),
revert_amount = -amount, revert_amount = -amount,
revert_pcount = -pcount, revert_pcount = -(pcount or 0),
})) }))
end end
end end
@ -505,17 +519,19 @@ local function r_index()
print([[<div class="userlist"></div>]]) -- for printing print([[<div class="userlist"></div>]]) -- for printing
print([[<ul class="userlist">]]) print([[<ul class="userlist">]])
for _, user in ipairs(get_active_users()) do for _, user in ipairs(get_active_users()) do
if user.name.sub(1, 1) ~= "@" then
print(format([[<li> print(format([[<li>
<a href="/{username_url}"> <a href="/{username_url}">
<span class="name">{!username}</span> <span class="name">{!username}</span>
{balance} {balance}
</a> </a>
</li>]], { </li>]], {
username_url = urlencode(user.username), username_url = urlencode(user.name),
username = user.username, username = user.name,
balance = format_amount(user.balance, "span") balance = format_amount(user.balance, "span")
})) }))
end end
end
print("</ul>") print("</ul>")
end) end)
end end
@ -610,7 +626,7 @@ local function r_products()
</tr>]]) </tr>]])
local pbals = product_balances() local pbals = product_balances()
for barcode, price, user, name in read_products() do for barcode, price, user, name in read_products() do
print(string.format([[<tr> print(format([[<tr>
<td>{!name}</td> <td>{!name}</td>
{price} {price}
<td>{!barcode}</td> <td>{!barcode}</td>
@ -618,7 +634,7 @@ local function r_products()
<td>{!user}</td> <td>{!user}</td>
</tr>]], { </tr>]], {
name = name, name = name,
price = format_amount(-price), price = format_amount(-price, "td"),
barcode = barcode, barcode = barcode,
count = pbals[barcode] or "0", count = pbals[barcode] or "0",
user = user, user = user,