sort users based on most recent action rather than lua table randomness

This commit is contained in:
Riley L. 2024-11-03 22:23:21 +01:00
parent 587b9361b9
commit ef820b584e

View file

@ -166,6 +166,34 @@ local function last_txns()
return users return users
end end
local function get_active_users()
local user_balances = {}
for time, username, amount, _, _, _ in read_log() do
user_balances[username] = {
time = time,
username = username,
balance = (user_balances[username] or {balance = 0}).balance + amount
}
end
local users = {}
for _, user in pairs(user_balances) do
table.insert(users, user)
end
table.sort(users, function(a,b) return a.time > b.time end)
for k, user in pairs(users) do
io.stderr:write(string.format("%s %s %s %s\n",
k,
os.date("!%Y-%m-%dT%H:%M:%SZ", user.time),
user.username,
user.balance
))
end
return users
end
local function error_box(message) local function error_box(message)
return string.format([[<div class="notif error"><p>Error: %s</p></div>]], message) return string.format([[<div class="notif error"><p>Error: %s</p></div>]], message)
end end
@ -339,17 +367,18 @@ local function r_index()
<h3>User Creation</h3> <h3>User Creation</h3>
<label for="username">Username: </label> <label for="username">Username: </label>
<input type="text" name="create_user" id="username" /> <input type="text" name="create_user" id="username" />
<input type="submit" value="Continue" /> <input type="submit" value="Continue" class="button amount-ntr" />
</form> </form>
]]) ]])
print("<ul>") print("<ul>")
for username, balance in pairs(balances()) do
for _, user in ipairs(get_active_users()) do
print(string.format([[ print(string.format([[
<li><a href="/%s"><span class="name">%s</span> <span class="amount amount-%s">%.02f</span></a></li> <li><a href="/%s"><span class="name">%s</span> <span class="amount amount-%s">%.02f</span></a></li>
]], ]],
urlencode(username), urlencode(user.username),
escape(username), escape(user.username),
balance >= 0 and "pos" or "neg", balance / 100 user.balance >= 0 and "pos" or "neg", user.balance / 100
)) ))
end end
print("</ul>") print("</ul>")