2018-04-04 23:57:27 +02:00
|
|
|
|
-- Copyright (c) 2013-18 rubenwardy. MIT.
|
|
|
|
|
|
2018-04-04 18:37:43 +02:00
|
|
|
|
local S = awards.gettext
|
|
|
|
|
|
|
|
|
|
function awards.get_formspec(name, to, sid)
|
|
|
|
|
local formspec = ""
|
2021-01-10 17:24:45 +01:00
|
|
|
|
local awards_list = awards.get_award_states(name)
|
2018-04-04 18:37:43 +02:00
|
|
|
|
|
2018-04-05 17:44:22 +02:00
|
|
|
|
if #awards_list == 0 then
|
2018-04-05 18:09:42 +02:00
|
|
|
|
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No achivements available.")).."]"
|
2018-04-04 18:37:43 +02:00
|
|
|
|
formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]"
|
|
|
|
|
return formspec
|
|
|
|
|
end
|
2018-07-15 14:17:06 +02:00
|
|
|
|
sid = awards_list[sid] and sid or 1
|
2018-04-04 18:37:43 +02:00
|
|
|
|
|
|
|
|
|
-- Sidebar
|
2018-07-15 14:17:06 +02:00
|
|
|
|
local sitem = awards_list[sid]
|
|
|
|
|
local sdef = sitem.def
|
|
|
|
|
if sdef and sdef.secret and not sitem.unlocked then
|
|
|
|
|
formspec = formspec .. "label[1,2.75;"..
|
|
|
|
|
minetest.formspec_escape(S("(Secret Award)")).."]"..
|
|
|
|
|
"image[1,0;3,3;awards_unknown.png]"
|
|
|
|
|
if sdef and sdef.description then
|
|
|
|
|
formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;"..
|
|
|
|
|
minetest.formspec_escape(
|
|
|
|
|
S("Unlock this award to find out what it is."))..";]"
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
local title = sitem.name
|
|
|
|
|
if sdef and sdef.title then
|
|
|
|
|
title = sdef.title
|
|
|
|
|
end
|
|
|
|
|
local status = "%s"
|
|
|
|
|
if sitem.unlocked then
|
|
|
|
|
status = S("%s (unlocked)")
|
|
|
|
|
end
|
2018-04-04 18:37:43 +02:00
|
|
|
|
|
2018-09-13 18:31:58 +02:00
|
|
|
|
formspec = formspec .. "textarea[0.5,3.1;4.8,1.45;;" ..
|
2018-07-15 14:17:06 +02:00
|
|
|
|
string.format(status, minetest.formspec_escape(title)) ..
|
|
|
|
|
";]"
|
2018-04-04 18:37:43 +02:00
|
|
|
|
|
2018-07-15 14:17:06 +02:00
|
|
|
|
if sdef and sdef.icon then
|
2019-08-20 12:28:15 +02:00
|
|
|
|
formspec = formspec .. "image[0.45,0;3.5,3.5;" .. sdef.icon .. "]" -- adjusted values from 0.6,0;3,3
|
2018-07-15 14:17:06 +02:00
|
|
|
|
end
|
2021-01-10 17:24:45 +01:00
|
|
|
|
|
|
|
|
|
if sitem.progress then
|
|
|
|
|
local barwidth = 3.95
|
|
|
|
|
local perc = sitem.progress.current / sitem.progress.target
|
|
|
|
|
local label = sitem.progress.label
|
2018-07-15 14:17:06 +02:00
|
|
|
|
if perc > 1 then
|
|
|
|
|
perc = 1
|
2018-04-04 18:37:43 +02:00
|
|
|
|
end
|
2018-09-13 18:31:58 +02:00
|
|
|
|
formspec = formspec .. "background[0,8.24;" .. barwidth ..",0.4;awards_progress_gray.png;false]"
|
|
|
|
|
formspec = formspec .. "background[0,8.24;" .. (barwidth * perc) ..",0.4;awards_progress_green.png;false]"
|
2018-07-15 14:17:06 +02:00
|
|
|
|
if label then
|
2018-09-13 18:31:58 +02:00
|
|
|
|
formspec = formspec .. "label[1.6,8.15;" .. minetest.formspec_escape(label) .. "]"
|
2018-04-04 18:37:43 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-01-10 17:24:45 +01:00
|
|
|
|
|
2018-07-15 14:17:06 +02:00
|
|
|
|
if sdef and sdef.description then
|
2018-09-13 18:31:58 +02:00
|
|
|
|
formspec = formspec .. "box[-0.05,3.75;3.9,4.2;#000]"
|
|
|
|
|
formspec = formspec .. "textarea[0.25,3.75;3.9,4.2;;" ..
|
|
|
|
|
minetest.formspec_escape(sdef.description) .. ";]"
|
2018-07-15 14:17:06 +02:00
|
|
|
|
end
|
2018-04-04 18:37:43 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Create list box
|
2018-09-13 18:31:58 +02:00
|
|
|
|
formspec = formspec .. "textlist[4,0;3.8,8.6;awards;"
|
2018-04-04 18:37:43 +02:00
|
|
|
|
local first = true
|
2018-04-05 17:44:22 +02:00
|
|
|
|
for _, award in pairs(awards_list) do
|
|
|
|
|
local def = award.def
|
2018-04-04 18:37:43 +02:00
|
|
|
|
if def then
|
|
|
|
|
if not first then
|
|
|
|
|
formspec = formspec .. ","
|
|
|
|
|
end
|
|
|
|
|
first = false
|
|
|
|
|
|
2018-04-05 17:44:22 +02:00
|
|
|
|
if def.secret and not award.unlocked then
|
2018-04-05 18:56:01 +02:00
|
|
|
|
formspec = formspec .. "#707070"..minetest.formspec_escape(S("(Secret Award)"))
|
2018-04-04 18:37:43 +02:00
|
|
|
|
else
|
|
|
|
|
local title = award.name
|
|
|
|
|
if def and def.title then
|
|
|
|
|
title = def.title
|
|
|
|
|
end
|
2018-04-05 17:44:22 +02:00
|
|
|
|
-- title = title .. " [" .. award.score .. "]"
|
|
|
|
|
if award.unlocked then
|
2018-04-04 18:37:43 +02:00
|
|
|
|
formspec = formspec .. minetest.formspec_escape(title)
|
2018-04-05 17:48:33 +02:00
|
|
|
|
elseif award.started then
|
|
|
|
|
formspec = formspec .. "#c0c0c0".. minetest.formspec_escape(title)
|
2018-04-04 18:37:43 +02:00
|
|
|
|
else
|
2018-04-05 17:48:33 +02:00
|
|
|
|
formspec = formspec .. "#a0a0a0".. minetest.formspec_escape(title)
|
2018-04-04 18:37:43 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return formspec .. ";"..sid.."]"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function awards.show_to(name, to, sid, text)
|
|
|
|
|
if name == "" or name == nil then
|
|
|
|
|
name = to
|
|
|
|
|
end
|
2019-03-03 22:40:11 +01:00
|
|
|
|
local data = awards.player(name)
|
2018-04-04 18:37:43 +02:00
|
|
|
|
if name == to and data.disabled then
|
2018-04-05 18:56:01 +02:00
|
|
|
|
minetest.chat_send_player(name, S("You've disabled awards. Type /awards enable to reenable."))
|
2018-04-04 18:37:43 +02:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if text then
|
2021-01-10 17:24:45 +01:00
|
|
|
|
local awards_list = awards.get_award_states(name)
|
2018-04-05 17:44:22 +02:00
|
|
|
|
if #awards_list == 0 then
|
2018-04-05 18:56:01 +02:00
|
|
|
|
minetest.chat_send_player(to, S("Error: No award available."))
|
2018-04-04 18:37:43 +02:00
|
|
|
|
return
|
|
|
|
|
elseif not data or not data.unlocked then
|
2018-04-05 18:56:01 +02:00
|
|
|
|
minetest.chat_send_player(to, S("You have not unlocked any awards."))
|
2018-04-04 18:37:43 +02:00
|
|
|
|
return
|
|
|
|
|
end
|
2018-04-05 18:56:01 +02:00
|
|
|
|
minetest.chat_send_player(to, string.format(S("%s’s awards:"), name))
|
2018-04-04 18:37:43 +02:00
|
|
|
|
|
2018-04-05 17:44:22 +02:00
|
|
|
|
for str, _ in pairs(data.unlocked) do
|
2018-04-04 18:43:44 +02:00
|
|
|
|
local def = awards.registered_awards[str]
|
2018-04-04 18:37:43 +02:00
|
|
|
|
if def then
|
|
|
|
|
if def.title then
|
|
|
|
|
if def.description then
|
|
|
|
|
minetest.chat_send_player(to, string.format(S("%s: %s"), def.title, def.description))
|
|
|
|
|
else
|
|
|
|
|
minetest.chat_send_player(to, def.title)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
minetest.chat_send_player(to, str)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
local deco = ""
|
|
|
|
|
if minetest.global_exists("default") then
|
|
|
|
|
deco = default.gui_bg .. default.gui_bg_img
|
|
|
|
|
end
|
|
|
|
|
-- Show formspec to user
|
|
|
|
|
minetest.show_formspec(to,"awards:awards",
|
2018-09-13 18:31:58 +02:00
|
|
|
|
"size[8,8.6]" .. deco ..
|
2018-04-04 18:37:43 +02:00
|
|
|
|
awards.get_formspec(name, to, sid))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-09-13 18:31:58 +02:00
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
|
if formname ~= "awards:awards" then
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
if fields.quit then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
if fields.awards then
|
|
|
|
|
local event = minetest.explode_textlist_event(fields.awards)
|
|
|
|
|
if event.type == "CHG" then
|
|
|
|
|
awards.show_to(name, name, event.index, false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
end)
|
|
|
|
|
|
2018-04-04 18:37:43 +02:00
|
|
|
|
if minetest.get_modpath("sfinv") then
|
|
|
|
|
sfinv.register_page("awards:awards", {
|
2018-04-05 18:56:01 +02:00
|
|
|
|
title = S("Awards"),
|
2018-04-04 18:37:43 +02:00
|
|
|
|
on_enter = function(self, player, context)
|
|
|
|
|
context.awards_idx = 1
|
|
|
|
|
end,
|
2018-04-05 18:24:42 +02:00
|
|
|
|
is_in_nav = function(self, player, context)
|
|
|
|
|
local data = awards.player(player:get_player_name())
|
|
|
|
|
return not data.disabled
|
|
|
|
|
end,
|
2018-04-04 18:37:43 +02:00
|
|
|
|
get = function(self, player, context)
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
return sfinv.make_formspec(player, context,
|
2018-07-15 14:17:06 +02:00
|
|
|
|
awards.get_formspec(name, name, context.awards_idx),
|
2018-09-13 18:31:58 +02:00
|
|
|
|
false)
|
2018-04-04 18:37:43 +02:00
|
|
|
|
end,
|
|
|
|
|
on_player_receive_fields = function(self, player, context, fields)
|
|
|
|
|
if fields.awards then
|
|
|
|
|
local event = minetest.explode_textlist_event(fields.awards)
|
|
|
|
|
if event.type == "CHG" then
|
|
|
|
|
context.awards_idx = event.index
|
|
|
|
|
sfinv.set_player_inventory_formspec(player, context)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
})
|
2018-09-13 18:39:52 +02:00
|
|
|
|
|
|
|
|
|
local function check_and_reshow(name)
|
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
|
if not player then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local context = sfinv.get_or_create_context(player)
|
|
|
|
|
if context.page ~= "awards:awards" then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
sfinv.set_player_inventory_formspec(player, context)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
awards.register_on_unlock(check_and_reshow)
|
2018-04-04 18:37:43 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if minetest.get_modpath("unified_inventory") ~= nil then
|
|
|
|
|
unified_inventory.register_button("awards", {
|
|
|
|
|
type = "image",
|
|
|
|
|
image = "awards_ui_icon.png",
|
2018-04-05 18:56:01 +02:00
|
|
|
|
tooltip = S("Awards"),
|
2018-04-04 18:37:43 +02:00
|
|
|
|
action = function(player)
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
awards.show_to(name, name, nil, false)
|
|
|
|
|
end,
|
|
|
|
|
})
|
|
|
|
|
end
|