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