reward_inv/show_reward_list_fs.lua

94 lines
3.6 KiB
Lua

-- show a list of treasure chests and other rewards
-- complete with information if the player has found it already or not
reward_inv.get_reward_list_fs = function(player, reward_id)
if(not(player)) then
return ""
end
local formspec = {
"size[55,33]",
"real_coordinates[true]",
-- back to the list with that one precondition or effect
"label[20,1;List of treasures and rewards:]",
"button_exit[1,30.5;53,2;exit;Exit]",
"tablecolumns[",
-- #items taken, when found, status (not found, found, empty), hint
"text,align=right;text,align=center;color,span=2;text,align=center;text,align=left",
"]",
"table[1,2;53,28;reward_list;",
"Taken:,When found:,#FFFFFF,Status:,",
minetest.formspec_escape("Hint: (blue: not found / yellow: still contains rewards "..
"that can be claimed / green: empty / orange: hidden)"),
","
}
local reward_list = {}
for reward_id, reward_data in pairs(reward_inv.inv_data) do
table.insert(reward_list, reward_id)
end
-- sort by the date/time when this reward was first found (sort descending)
table.sort(reward_list, function (k1, k2)
if(not(reward_inv.inv_data[k1].first_found)) then
return false
elseif(not(reward_inv.inv_data[k2].first_found)) then
return true
end
return reward_inv.inv_data[k1].first_found > reward_inv.inv_data[k2].first_found end)
-- TODO: set this to players with the right priv
local show_hidden = true
local pname = player:get_player_name()
for i, reward_id in ipairs(reward_list) do
local reward_data = reward_inv.inv_data[reward_id]
local player_data = reward_inv.player_data[reward_id][pname]
if(not(player_data) and reward_data.hint) then
-- no items taken, not yet found,
table.insert(formspec, "0,-,#CCCCFF,not found,")
table.insert(formspec, minetest.formspec_escape(reward_data.hint))
table.insert(formspec, ",")
-- players with the right privs can see all
elseif(not(player_data) and not(reward_data.hint) and show_hidden) then
table.insert(formspec, "0,-,#orange,not found,")
table.insert(formspec, minetest.formspec_escape("(hidden) "..reward_id))
table.insert(formspec, ",")
elseif(player_data) then
-- amount of items taken out
table.insert(formspec, minetest.formspec_escape(player_data[5]))
table.insert(formspec, ",")
-- when was it first found?
table.insert(formspec, minetest.formspec_escape(os.date("%m/%d/%y %H:%M", player_data[1])))
table.insert(formspec, ",")
local t = reward_data.refill_after_t_seconds
if(player_data[2] == 1
and (not(t) or t < 1 or (player_data[4] + t > os.time()))) then
-- the reward inv has been fully claimed
table.insert(formspec, "#00FF00,empty,")
else
-- there is still something in that may be claimed
table.insert(formspec, "#FFFF00,found,")
end
if(reward_data.text_when_found and reward_data.text_when_found ~= "") then
table.insert(formspec, minetest.formspec_escape(reward_data.text_when_found))
elseif(reward_data.hint and reward_data.hint ~= "") then
table.insert(formspec, minetest.formspec_escape(reward_data.hint))
else
table.insert(formspec, minetest.formspec_escape("(hidden until found) "..reward_id))
end
table.insert(formspec, ",")
end
end
table.insert(formspec, "]")
return table.concat(formspec, "")
end
-- show a list of all rewards and if the player found it already
reward_inv.show_reward_list_fs = function(player, reward_id)
if(not(player)) then
return
end
local pname = player:get_player_name()
-- create the formspec
local formspec = reward_inv.get_reward_list_fs(player, reward_id)
-- actually show the reward formspec to the player
minetest.show_formspec(pname, "reward_inv:list", formspec)
end