2017-06-01 12:14:13 +02:00
|
|
|
local exchange, formlib = ...
|
2016-02-26 04:47:05 +01:00
|
|
|
|
|
|
|
local mailbox_form = "global_exchange:digital_mailbox"
|
|
|
|
|
|
|
|
local mailbox_contents = {}
|
|
|
|
local selected_index = {}
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
-- Map from player names to their most recent search result
|
2016-02-26 04:47:05 +01:00
|
|
|
local function get_mail(p_name)
|
|
|
|
local mail_maybe = mailbox_contents[p_name]
|
2017-06-01 12:14:13 +02:00
|
|
|
|
|
|
|
if not mail_maybe then
|
|
|
|
local _,res = exchange:view_inbox(p_name)
|
|
|
|
mail_maybe = res or {}
|
|
|
|
mailbox_contents[p_name] = mail_maybe
|
|
|
|
selected_index[p_name] = math.min(selected_index[p_name] or 0, #mail_maybe)
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
2017-06-01 12:14:13 +02:00
|
|
|
|
|
|
|
return mail_maybe
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
local function wear_string(wear)
|
|
|
|
return "-" .. math.ceil(100 * wear / 65535) .. "%"
|
|
|
|
end
|
|
|
|
|
2016-02-26 04:47:05 +01:00
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
local function mk_inbox_list(fs, results, x, y, w, h)
|
|
|
|
fs("textlist[", x, ",", y, ";", w, ",", h, ";result_list;")
|
|
|
|
|
|
|
|
local sep = nil
|
2016-02-26 04:47:05 +01:00
|
|
|
for i, row in ipairs(results) do
|
2017-06-01 12:14:13 +02:00
|
|
|
fs(sep)
|
|
|
|
fs:escape(row.Amount, " ", row.Item)
|
|
|
|
if row.Wear > 0 then
|
|
|
|
fs:escape(" (", wear_string(row.Wear), ")")
|
|
|
|
end
|
|
|
|
sep = ","
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
fs("]")
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
local function mk_mail_fs(fs, p_name, results, err_str)
|
|
|
|
fs:size(8,8)
|
|
|
|
fs:label(0,0, "Inbox")
|
|
|
|
|
2016-02-26 04:47:05 +01:00
|
|
|
if err_str then
|
2017-06-01 12:14:13 +02:00
|
|
|
fs:label(3,0, "Error: " .. err_str)
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
mk_inbox_list(fs, results, 0, 1, 7.75, 6.25)
|
|
|
|
|
|
|
|
fs:button(3,7.35, 2,1, "claim", "Claim")
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
local function show_mail(p_name, err_str)
|
|
|
|
local fs = formlib.Builder()
|
|
|
|
mk_mail_fs(fs, p_name, get_mail(p_name), err_str)
|
|
|
|
minetest.show_formspec(p_name, mailbox_form, tostring(fs))
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-03-05 11:47:35 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2016-02-26 04:47:05 +01:00
|
|
|
if formname ~= mailbox_form then return end
|
2016-03-05 11:47:35 +01:00
|
|
|
if fields.quit then return true end
|
2016-02-26 04:47:05 +01:00
|
|
|
|
|
|
|
local p_name = player:get_player_name()
|
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.claim then
|
|
|
|
local idx = selected_index[p_name]
|
2016-02-26 04:47:05 +01:00
|
|
|
local row = get_mail(p_name)[idx]
|
|
|
|
|
|
|
|
if row then
|
|
|
|
local stack = ItemStack(row.Item)
|
|
|
|
stack:set_count(row.Amount)
|
2017-06-01 12:14:13 +02:00
|
|
|
stack:set_wear(row.Wear)
|
2016-02-26 04:47:05 +01:00
|
|
|
|
|
|
|
local p_inv = player:get_inventory()
|
2017-06-01 12:14:13 +02:00
|
|
|
local leftover = p_inv:add_item("main", stack)
|
|
|
|
local took_amount = row.Amount - leftover:get_count()
|
2016-02-26 04:47:05 +01:00
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
mailbox_contents[p_name] = nil
|
2016-02-26 04:47:05 +01:00
|
|
|
|
2017-06-01 12:14:13 +02:00
|
|
|
local succ, res = exchange:take_inbox(row.Id, took_amount)
|
|
|
|
if succ then
|
|
|
|
show_mail(p_name)
|
|
|
|
else
|
|
|
|
show_mail(p_name, res)
|
|
|
|
end
|
2016-02-26 04:47:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2016-03-05 11:47:35 +01:00
|
|
|
end)
|
2016-02-26 04:47:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
minetest.register_node("global_exchange:mailbox", {
|
|
|
|
description = "Digital Mailbox",
|
2017-06-01 12:14:13 +02:00
|
|
|
tiles = {
|
|
|
|
"global_exchange_box.png",
|
2016-03-05 12:34:02 +01:00
|
|
|
"global_exchange_box.png",
|
|
|
|
"global_exchange_box.png^global_exchange_mailbox_side.png",
|
2016-02-26 04:47:05 +01:00
|
|
|
},
|
2017-06-01 12:14:13 +02:00
|
|
|
is_ground_content = false,
|
|
|
|
stack_max = 1,
|
2016-02-26 04:47:05 +01:00
|
|
|
groups = {cracky=2},
|
|
|
|
on_rightclick = function(pos, node, clicker)
|
|
|
|
local p_name = clicker:get_player_name()
|
2017-06-01 12:14:13 +02:00
|
|
|
mailbox_contents[p_name] = nil
|
|
|
|
show_mail(p_name)
|
2016-02-26 04:47:05 +01:00
|
|
|
end,
|
|
|
|
})
|
2016-02-26 04:52:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
minetest.register_craft( {
|
|
|
|
output = "global_exchange:mailbox",
|
|
|
|
recipe = {
|
2016-02-27 05:55:48 +01:00
|
|
|
{ "default:stone", "default:gold_ingot", "default:stone" },
|
2017-06-01 12:14:13 +02:00
|
|
|
{ "default:stone", "default:chest", "default:stone" },
|
|
|
|
{ "default:stone", "default:stone", "default:stone" },
|
2016-02-26 04:52:00 +01:00
|
|
|
}
|
|
|
|
})
|
2017-06-01 12:14:13 +02:00
|
|
|
-- vim:set ts=4 sw=4 noet:
|