mirror of
https://github.com/minetest-mods/global_exchange.git
synced 2025-07-04 00:30:23 +02:00
Cleanup code
e.g. do not use table.insert because it's slow
This commit is contained in:
@ -19,37 +19,37 @@ end
|
||||
|
||||
|
||||
local function mk_inbox_list(results, x, y, w, h)
|
||||
local res = {}
|
||||
table.insert(res, "textlist[")
|
||||
table.insert(res, tostring(x))
|
||||
table.insert(res, ",")
|
||||
table.insert(res, tostring(y))
|
||||
table.insert(res, ";")
|
||||
table.insert(res, tostring(w))
|
||||
table.insert(res, ",")
|
||||
table.insert(res, tostring(h))
|
||||
table.insert(res, ";result_list;")
|
||||
local res = {
|
||||
"textlist[",
|
||||
tostring(x),
|
||||
",",
|
||||
tostring(y),
|
||||
";",
|
||||
tostring(w),
|
||||
",",
|
||||
tostring(h),
|
||||
";result_list;"
|
||||
}
|
||||
|
||||
for i, row in ipairs(results) do
|
||||
table.insert(res, row.Amount .. " " .. row.Item)
|
||||
table.insert(res, ",")
|
||||
res[i*2+8] = row.Amount .. " " .. row.Item
|
||||
res[i*2+9] = ","
|
||||
end
|
||||
table.insert(res, "]")
|
||||
res[#res+1] = "]"
|
||||
|
||||
return table.concat(res)
|
||||
end
|
||||
|
||||
|
||||
local function mk_mail_fs(p_name, results, err_str)
|
||||
fs = "size[6,8]"
|
||||
fs = fs .. "label[0,0;Inbox]"
|
||||
fs = "size[6,8]" ..
|
||||
"label[0,0;Inbox]"
|
||||
if err_str then
|
||||
fs = fs .. "label[3,0;Error: " .. err_str .. "]"
|
||||
end
|
||||
fs = fs .. mk_inbox_list(results, 0, 1, 6, 6)
|
||||
fs = fs .. "button[0,7;2,1;claim;Claim]"
|
||||
|
||||
return fs
|
||||
return fs .. mk_inbox_list(results, 0, 1, 6, 6) ..
|
||||
"button[0,7;2,1;claim;Claim]"
|
||||
end
|
||||
|
||||
|
||||
@ -58,14 +58,15 @@ local function show_mail(p_name, results, err_str)
|
||||
end
|
||||
|
||||
|
||||
local function handle_fields(player, formname, fields)
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= mailbox_form then return end
|
||||
if fields["quit"] then return true end
|
||||
if fields.quit then return true end
|
||||
|
||||
local p_name = player:get_player_name()
|
||||
local idx = selected_index[p_name]
|
||||
|
||||
if fields["claim"] and idx then
|
||||
if fields.claim
|
||||
and idx then
|
||||
local row = get_mail(p_name)[idx]
|
||||
|
||||
if row then
|
||||
@ -77,7 +78,7 @@ local function handle_fields(player, formname, fields)
|
||||
show_mail(p_name, get_mail(p_name), "Not enough room.")
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
local succ, res = exchange:take_inbox(row.Id, row.Amount)
|
||||
if not succ then
|
||||
show_mail(p_name, get_mail(p_name), res)
|
||||
@ -92,8 +93,8 @@ local function handle_fields(player, formname, fields)
|
||||
end
|
||||
end
|
||||
|
||||
if fields["result_list"] then
|
||||
local event = minetest.explode_textlist_event(fields["result_list"])
|
||||
if fields.result_list then
|
||||
local event = minetest.explode_textlist_event(fields.result_list)
|
||||
|
||||
if event.type == "CHG" then
|
||||
selected_index[p_name] = event.index
|
||||
@ -101,10 +102,7 @@ local function handle_fields(player, formname, fields)
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
minetest.register_on_player_receive_fields(handle_fields)
|
||||
end)
|
||||
|
||||
|
||||
minetest.register_node("global_exchange:mailbox", {
|
||||
@ -116,7 +114,7 @@ minetest.register_node("global_exchange:mailbox", {
|
||||
groups = {cracky=2},
|
||||
on_rightclick = function(pos, node, clicker)
|
||||
local p_name = clicker:get_player_name()
|
||||
local succ, res = exchange:view_inbox(p_name)
|
||||
local _,res = exchange:view_inbox(p_name)
|
||||
mailbox_contents[p_name] = res
|
||||
minetest.show_formspec(p_name, mailbox_form, mk_mail_fs(p_name, res))
|
||||
end,
|
||||
|
Reference in New Issue
Block a user