write something there
This commit is contained in:
commit
b4b6c08f4f
8546 changed files with 309825 additions and 0 deletions
379
mods/awards/src/api_awards.lua
Normal file
379
mods/awards/src/api_awards.lua
Normal file
|
@ -0,0 +1,379 @@
|
|||
-- Copyright (c) 2013-18 rubenwardy. MIT.
|
||||
|
||||
local S = awards.translator
|
||||
local GOAL_ID_SEPARATOR = string.char(31)
|
||||
|
||||
function awards.register_award(name, def)
|
||||
def.name = name
|
||||
def.goals = def.goals or {}
|
||||
|
||||
-- Add legacy trigger as a goal
|
||||
if def.trigger and def.trigger.type then
|
||||
table.insert(def.goals,{
|
||||
description = def.description or def.trigger.type,
|
||||
trigger = def.trigger,
|
||||
})
|
||||
def.trigger = nil
|
||||
end
|
||||
|
||||
-- Add goals
|
||||
def.goals.show_unlocked = def.goals.show_unlocked == nil and true or def.goals.show_unlocked
|
||||
def.goals.show_locked = def.goals.show_locked == nil and true or def.goals.show_locked
|
||||
for i = 1, #def.goals do
|
||||
local goal = def.goals[i]
|
||||
local goal_id = name .. GOAL_ID_SEPARATOR .. (goal.id or i)
|
||||
goal.id = goal_id
|
||||
goal.name = goal.id
|
||||
function goal:can_unlock(data)
|
||||
return def:can_unlock(data)
|
||||
end
|
||||
if goal.trigger then
|
||||
local tdef = awards.registered_triggers[goal.trigger.type]
|
||||
assert(tdef, "Trigger not found: " .. goal.trigger.type)
|
||||
tdef:on_register(goal)
|
||||
end
|
||||
end
|
||||
|
||||
def.goals.target = def.goals.target or #def.goals
|
||||
|
||||
function def:can_unlock(data)
|
||||
if not self.requires then
|
||||
return true
|
||||
end
|
||||
|
||||
for i=1, #self.requires do
|
||||
if not data.unlocked[self.requires[i]] then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Add Award
|
||||
awards.registered_awards[name] = def
|
||||
|
||||
local tdef = awards.registered_awards[name]
|
||||
if def.description == nil and tdef.getDefaultDescription then
|
||||
def.description = tdef:getDefaultDescription()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- This function is called whenever a target condition is met.
|
||||
-- It checks if a player already has that award, and if they do not,
|
||||
-- it gives it to them
|
||||
----------------------------------------------
|
||||
--awards.unlock(name, award)
|
||||
-- name - the name of the player
|
||||
-- award - the name of the award to give
|
||||
function awards.unlock(name, award, goal)
|
||||
-- Ensure the player is online.
|
||||
if not minetest.get_player_by_name(name) then
|
||||
return
|
||||
end
|
||||
|
||||
-- Access Player Data
|
||||
local data = awards.player(name)
|
||||
|
||||
-- Do nothing if awards are disabled for the player
|
||||
if data.disabled then
|
||||
return
|
||||
end
|
||||
|
||||
-- Check for full award+goal string submitted by triggers
|
||||
local separator_pos = award:find(GOAL_ID_SEPARATOR)
|
||||
if separator_pos then
|
||||
goal = award:sub(separator_pos + 1,-1)
|
||||
award = award:sub(1,separator_pos - 1)
|
||||
end
|
||||
|
||||
-- Generate full goal string if we're unlocking a goal
|
||||
if goal then
|
||||
goal = award .. GOAL_ID_SEPARATOR .. goal
|
||||
end
|
||||
|
||||
local awdef = awards.registered_awards[award]
|
||||
assert(awdef, "Unable to unlock an award which doesn't exist!")
|
||||
|
||||
if not awdef:can_unlock(data) then
|
||||
minetest.log("warning", "can_unlock returned false in unlock of " ..
|
||||
award .. " for " .. name)
|
||||
return
|
||||
end
|
||||
|
||||
-- Complete goal and proceed with award if target goals are complete
|
||||
if goal then
|
||||
if data.unlocked[goal] then
|
||||
return
|
||||
end
|
||||
data.unlocked[goal] = goal
|
||||
awards.save()
|
||||
local goals_unlocked = 0
|
||||
for _,goal in ipairs(awdef.goals) do
|
||||
if data.unlocked[goal.id] then
|
||||
goals_unlocked = goals_unlocked + 1
|
||||
end
|
||||
end
|
||||
if goals_unlocked < awdef.goals.target then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Return if award is already unlocked
|
||||
if data.unlocked[award] then
|
||||
return
|
||||
end
|
||||
|
||||
-- Unlock Award
|
||||
minetest.log("action", name.." has unlocked award "..award)
|
||||
data.unlocked[award] = award
|
||||
awards.save()
|
||||
|
||||
-- Give Prizes
|
||||
if awdef and awdef.prizes then
|
||||
for i = 1, #awdef.prizes do
|
||||
local itemstack = ItemStack(awdef.prizes[i])
|
||||
if not itemstack:is_empty() then
|
||||
local receiverref = minetest.get_player_by_name(name)
|
||||
if receiverref then
|
||||
receiverref:get_inventory():add_item("main", itemstack)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Run callbacks
|
||||
if awdef.on_unlock and awdef.on_unlock(name, awdef) then
|
||||
return
|
||||
end
|
||||
for _, callback in pairs(awards.on_unlock) do
|
||||
if callback(name, awdef) then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Get Notification Settings
|
||||
local title = awdef.title or award
|
||||
local desc = awdef.description or ""
|
||||
local background = awdef.hud_background or awdef.background or "awards_bg_default.png"
|
||||
local icon = (awdef.icon or "awards_unknown.png") .. "^[resize:32x32"
|
||||
local sound = awdef.sound
|
||||
if sound == nil then
|
||||
-- Explicit check for nil because sound could be `false` to disable it
|
||||
sound = {name="awards_got_generic", gain=0.25}
|
||||
end
|
||||
|
||||
-- Do Notification
|
||||
if sound then
|
||||
-- Enforce sound delay to prevent sound spamming
|
||||
local lastsound = data.lastsound
|
||||
if lastsound == nil or os.difftime(os.time(), lastsound) >= 1 then
|
||||
minetest.sound_play(sound, {to_player=name})
|
||||
data.lastsound = os.time()
|
||||
end
|
||||
end
|
||||
|
||||
if awards.show_mode == "chat" then
|
||||
local chat_announce
|
||||
if awdef.secret then
|
||||
chat_announce = S("Secret Award Unlocked: %s")
|
||||
else
|
||||
chat_announce = S("Award Unlocked: %s")
|
||||
end
|
||||
-- use the chat console to send it
|
||||
minetest.chat_send_player(name, string.format(chat_announce, title))
|
||||
if desc~="" then
|
||||
minetest.chat_send_player(name, desc)
|
||||
end
|
||||
else
|
||||
local player = minetest.get_player_by_name(name)
|
||||
local one = player:hud_add({
|
||||
hud_elem_type = "image",
|
||||
name = "award_bg",
|
||||
scale = {x = 2, y = 1},
|
||||
text = background,
|
||||
position = {x = 0.5, y = 0.05},
|
||||
offset = {x = 0, y = 138},
|
||||
alignment = {x = 0, y = -1}
|
||||
})
|
||||
local hud_announce
|
||||
if awdef.secret then
|
||||
hud_announce = S("Secret Award Unlocked!")
|
||||
else
|
||||
hud_announce = S("Award Unlocked!")
|
||||
end
|
||||
local two = player:hud_add({
|
||||
hud_elem_type = "text",
|
||||
name = "award_au",
|
||||
number = 0xFFFFFF,
|
||||
scale = {x = 100, y = 20},
|
||||
text = hud_announce,
|
||||
position = {x = 0.5, y = 0.05},
|
||||
offset = {x = 0, y = 45},
|
||||
alignment = {x = 0, y = -1}
|
||||
})
|
||||
local three = player:hud_add({
|
||||
hud_elem_type = "text",
|
||||
name = "award_title",
|
||||
number = 0xFFFFFF,
|
||||
scale = {x = 100, y = 20},
|
||||
text = title,
|
||||
position = {x = 0.5, y = 0.05},
|
||||
offset = {x = 0, y = 100},
|
||||
alignment = {x = 0, y = -1}
|
||||
})
|
||||
local four = player:hud_add({
|
||||
hud_elem_type = "image",
|
||||
name = "award_icon",
|
||||
scale = {x = 2, y = 2}, -- adjusted for 32x32 from x/y = 4
|
||||
text = icon,
|
||||
position = {x = 0.5, y = 0.05},
|
||||
offset = {x = -200.5, y = 126},
|
||||
alignment = {x = 0, y = -1}
|
||||
})
|
||||
minetest.after(4, function()
|
||||
local player2 = minetest.get_player_by_name(name)
|
||||
if player2 then
|
||||
player2:hud_remove(one)
|
||||
player2:hud_remove(two)
|
||||
player2:hud_remove(three)
|
||||
player2:hud_remove(four)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function awards.remove(name, award)
|
||||
local data = awards.player(name)
|
||||
local awdef = awards.registered_awards[award]
|
||||
assert(awdef, "Unable to remove an award which doesn't exist!")
|
||||
|
||||
if data.disabled or
|
||||
(not data.unlocked[award]) then
|
||||
return
|
||||
end
|
||||
|
||||
minetest.log("action", "Award " .. award .." has been removed from ".. name)
|
||||
data.unlocked[award] = nil
|
||||
awards.save()
|
||||
end
|
||||
|
||||
local function sorter(a,b)
|
||||
return a.score < b.score
|
||||
end
|
||||
|
||||
function awards.get_award_states(name)
|
||||
local hash_is_unlocked = {}
|
||||
local retval = {}
|
||||
|
||||
-- Add all unlocked awards
|
||||
local data = awards.player(name)
|
||||
if data and data.unlocked then
|
||||
for awardname, _ in pairs(data.unlocked) do
|
||||
local def = awards.registered_awards[awardname]
|
||||
if def then
|
||||
hash_is_unlocked[awardname] = true
|
||||
local difficulty = def.difficulty or 1
|
||||
local score = -1000000 + difficulty
|
||||
|
||||
retval[#retval + 1] = {
|
||||
name = awardname,
|
||||
def = def,
|
||||
goals = (function()
|
||||
local goals = {
|
||||
show_locked = def.goals.show_locked,
|
||||
show_unlocked = def.goals.show_unlocked,
|
||||
}
|
||||
if def.goals then
|
||||
for i = 1, #def.goals do
|
||||
local goal = def.goals[i]
|
||||
local progress = goal.get_progress and goal:get_progress(data)
|
||||
table.insert(goals,{
|
||||
def = goal,
|
||||
progress = progress,
|
||||
unlocked = data.unlocked[goal.id] and true or false,
|
||||
})
|
||||
end
|
||||
end
|
||||
return #goals > 0 and goals or nil
|
||||
end)(),
|
||||
unlocked = true,
|
||||
started = true,
|
||||
score = score,
|
||||
progress = nil,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Add all locked awards
|
||||
local in_progress = {}
|
||||
local not_started = {}
|
||||
for _, def in pairs(awards.registered_awards) do
|
||||
if not hash_is_unlocked[def.name] and def:can_unlock(data) then
|
||||
local total_progress = { current = 0, target = 0 }
|
||||
local started = false
|
||||
local difficulty = def.difficulty or 1
|
||||
local score = difficulty
|
||||
if def.secret then
|
||||
score = 1000000
|
||||
end
|
||||
|
||||
local award = {
|
||||
name = def.name,
|
||||
def = def,
|
||||
goals = (function()
|
||||
local goals = {
|
||||
show_locked = def.goals.show_locked,
|
||||
show_unlocked = def.goals.show_unlocked,
|
||||
}
|
||||
if def.goals then
|
||||
local target = def.goals.target
|
||||
for i = 1, #def.goals do
|
||||
local goal = def.goals[i]
|
||||
local progress = goal.get_progress and goal:get_progress(data) or { current = data.unlocked[goal.id] and 1 or 0, target = 1 }
|
||||
total_progress.current = total_progress.current + progress.current
|
||||
total_progress.target = total_progress.target + progress.target
|
||||
local perc = progress.current / progress.target
|
||||
if perc > 0 then
|
||||
started = true
|
||||
end
|
||||
table.insert(goals,{
|
||||
def = goal,
|
||||
progress = progress,
|
||||
unlocked = data.unlocked[goal.id] and true or false,
|
||||
})
|
||||
end
|
||||
end
|
||||
return #goals > 0 and goals or nil
|
||||
end)(),
|
||||
unlocked = false,
|
||||
started = started,
|
||||
score = score,
|
||||
progress = total_progress,
|
||||
}
|
||||
|
||||
if award.started then
|
||||
table.insert(in_progress,award)
|
||||
else
|
||||
table.insert(not_started,award)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Sort award categories
|
||||
table.sort(retval,sorter)
|
||||
table.sort(in_progress,sorter)
|
||||
table.sort(not_started,sorter)
|
||||
|
||||
-- Append categories to the return value
|
||||
for _,award in ipairs(in_progress) do
|
||||
table.insert(retval,award)
|
||||
end
|
||||
for _,award in ipairs(not_started) do
|
||||
table.insert(retval,award)
|
||||
end
|
||||
|
||||
-- Return complete list of awards
|
||||
return retval
|
||||
end
|
222
mods/awards/src/api_triggers.lua
Normal file
222
mods/awards/src/api_triggers.lua
Normal file
|
@ -0,0 +1,222 @@
|
|||
-- Copyright (c) 2013-18 rubenwardy. MIT.
|
||||
|
||||
local function ngettext(msgid, msgid_plural, n, ...)
|
||||
return awards.translator(n==1 and msgid or msgid_plural, ...)
|
||||
end
|
||||
|
||||
local S, NS = awards.translator, ngettext
|
||||
|
||||
awards.on = {}
|
||||
|
||||
local default_def = {}
|
||||
|
||||
function default_def:run_callbacks(player, data, table_func)
|
||||
for i = 1, #self.on do
|
||||
local res = nil
|
||||
local entry = self.on[i]
|
||||
if type(entry) == "function" then
|
||||
res = entry(player, data)
|
||||
elseif type(entry) == "table" and entry.award then
|
||||
res = table_func(entry)
|
||||
end
|
||||
|
||||
if res then
|
||||
awards.unlock(player:get_player_name(), res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function awards.register_trigger(tname, tdef)
|
||||
assert(type(tdef) == "table",
|
||||
"Passing a callback to register_trigger is not supported in 3.0")
|
||||
|
||||
tdef.name = tname
|
||||
for key, value in pairs(default_def) do
|
||||
tdef[key] = value
|
||||
end
|
||||
|
||||
if tdef.type == "counted" then
|
||||
local old_reg = tdef.on_register
|
||||
|
||||
function tdef:on_register(def)
|
||||
local tmp = {
|
||||
award = def.name,
|
||||
target = def.trigger.target,
|
||||
}
|
||||
tdef.register(tmp)
|
||||
|
||||
function def.get_progress(_, data)
|
||||
local current = math.min(data[tname] or 0, tmp.target)
|
||||
return {
|
||||
current = current,
|
||||
target = tmp.target,
|
||||
label = S(tdef.progress, current, tmp.target),
|
||||
}
|
||||
end
|
||||
|
||||
function def.getDefaultDescription(_)
|
||||
local n = def.trigger.target
|
||||
return NS(tdef.auto_description[1], tdef.auto_description[2], n, n)
|
||||
end
|
||||
|
||||
if old_reg then
|
||||
return old_reg(tdef, def)
|
||||
end
|
||||
end
|
||||
|
||||
function tdef.notify(player)
|
||||
assert(player and player.is_player and player:is_player())
|
||||
local name = player:get_player_name()
|
||||
local data = awards.player(name)
|
||||
|
||||
-- Increment counter
|
||||
local currentVal = (data[tname] or 0) + 1
|
||||
data[tname] = currentVal
|
||||
|
||||
tdef:run_callbacks(player, data, function(entry)
|
||||
if entry.target and entry.award and currentVal and
|
||||
currentVal >= entry.target then
|
||||
return entry.award
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
awards["notify_" .. tname] = tdef.notify
|
||||
|
||||
elseif tdef.type == "counted_key" then
|
||||
if tdef.key_is_item then
|
||||
tdef.watched_groups = {}
|
||||
end
|
||||
|
||||
-- On award register
|
||||
local old_reg = tdef.on_register
|
||||
function tdef:on_register(def)
|
||||
-- Register trigger
|
||||
local tmp = {
|
||||
award = def.name,
|
||||
key = tdef:get_key(def),
|
||||
target = def.trigger.target,
|
||||
}
|
||||
tdef.register(tmp)
|
||||
|
||||
-- If group, add it to watch list
|
||||
if tdef.key_is_item and tmp.key and tmp.key:sub(1, 6) == "group:" then
|
||||
tdef.watched_groups[tmp.key:sub(7, #tmp.key)] = true
|
||||
end
|
||||
|
||||
-- Called to get progress values and labels
|
||||
function def.get_progress(_, data)
|
||||
data[tname] = data[tname] or {}
|
||||
|
||||
local done
|
||||
if tmp.key then
|
||||
done = data[tname][tmp.key] or 0
|
||||
else
|
||||
done = data[tname].__total or 0
|
||||
end
|
||||
done = math.min(done, tmp.target)
|
||||
|
||||
return {
|
||||
current = done,
|
||||
target = tmp.target,
|
||||
label = S(tdef.progress, done, tmp.target),
|
||||
}
|
||||
end
|
||||
|
||||
-- Build description if none is specificed by the award
|
||||
function def.getDefaultDescription(_)
|
||||
local n = def.trigger.target
|
||||
if tmp.key then
|
||||
local nname = tmp.key
|
||||
return NS(tdef.auto_description[1],
|
||||
tdef.auto_description[2], n, n, nname)
|
||||
else
|
||||
return NS(tdef.auto_description_total[1],
|
||||
tdef.auto_description_total[2], n, n)
|
||||
end
|
||||
end
|
||||
|
||||
-- Call on_register in trigger type definition
|
||||
if old_reg then
|
||||
return old_reg(tdef, def)
|
||||
end
|
||||
end
|
||||
|
||||
function tdef.notify(player, key, n)
|
||||
n = n or 1
|
||||
|
||||
if tdef.key_is_item and key:sub(1, 6) ~= "group:" then
|
||||
local itemdef = minetest.registered_items[key]
|
||||
if itemdef then
|
||||
for groupname,rating in pairs(itemdef.groups or {}) do
|
||||
if rating ~= 0 and tdef.watched_groups[groupname] then
|
||||
tdef.notify(player, "group:" .. groupname, n)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert(player and player.is_player and player:is_player() and key)
|
||||
local name = player:get_player_name()
|
||||
local data = awards.player(name)
|
||||
|
||||
-- Increment counter
|
||||
data[tname] = data[tname] or {}
|
||||
local currentVal = (data[tname][key] or 0) + n
|
||||
data[tname][key] = currentVal
|
||||
data[tname].__total = (data[tname].__total or 0)
|
||||
if key:sub(1, 6) ~= "group:" then
|
||||
data[tname].__total = data[tname].__total + n
|
||||
end
|
||||
|
||||
tdef:run_callbacks(player, data, function(entry)
|
||||
local current
|
||||
if entry.key == key then
|
||||
current = currentVal
|
||||
elseif entry.key == nil then
|
||||
current = data[tname].__total
|
||||
else
|
||||
return
|
||||
end
|
||||
if current >= entry.target then
|
||||
return entry.award
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
awards["notify_" .. tname] = tdef.notify
|
||||
|
||||
elseif tdef.type and tdef.type ~= "custom" then
|
||||
error("Unrecognised trigger type " .. tdef.type)
|
||||
end
|
||||
|
||||
awards.registered_triggers[tname] = tdef
|
||||
|
||||
tdef.on = {}
|
||||
tdef.register = function(func)
|
||||
table.insert(tdef.on, func)
|
||||
end
|
||||
|
||||
-- Backwards compat
|
||||
awards.on[tname] = tdef.on
|
||||
awards['register_on_' .. tname] = tdef.register
|
||||
return tdef
|
||||
end
|
||||
|
||||
function awards.increment_item_counter(data, field, itemname, count)
|
||||
itemname = minetest.registered_aliases[itemname] or itemname
|
||||
data[field][itemname] = (data[field][itemname] or 0) + (count or 1)
|
||||
end
|
||||
|
||||
function awards.get_item_count(data, field, itemname)
|
||||
itemname = minetest.registered_aliases[itemname] or itemname
|
||||
return data[field][itemname] or 0
|
||||
end
|
||||
|
||||
function awards.get_total_keyed_count(data, field)
|
||||
return data[field].__total or 0
|
||||
end
|
||||
|
||||
function awards.register_on_unlock(func)
|
||||
table.insert(awards.on_unlock, func)
|
||||
end
|
1112
mods/awards/src/awards.lua
Normal file
1112
mods/awards/src/awards.lua
Normal file
File diff suppressed because it is too large
Load diff
61
mods/awards/src/chat_commands.lua
Normal file
61
mods/awards/src/chat_commands.lua
Normal file
|
@ -0,0 +1,61 @@
|
|||
-- Copyright (c) 2013-18 rubenwardy. MIT.
|
||||
|
||||
local S = awards.translator
|
||||
|
||||
minetest.register_chatcommand("awards", {
|
||||
params = S("[c|clear|disable|enable]"),
|
||||
description = S("Show, clear, disable or enable your awards"),
|
||||
func = function(name, param)
|
||||
if param == "clear" then
|
||||
awards.clear_player(name)
|
||||
minetest.chat_send_player(name,
|
||||
S("All your awards and statistics have been cleared. You can now start again."))
|
||||
elseif param == "disable" then
|
||||
awards.disable(name)
|
||||
minetest.chat_send_player(name, S("You have disabled awards."))
|
||||
elseif param == "enable" then
|
||||
awards.enable(name)
|
||||
minetest.chat_send_player(name, S("You have enabled awards."))
|
||||
elseif param == "c" then
|
||||
awards.show_to(name, name, nil, true)
|
||||
else
|
||||
awards.show_to(name, name, nil, false)
|
||||
end
|
||||
|
||||
if (param == "disable" or param == "enable") and minetest.global_exists("sfinv") then
|
||||
local player = minetest.get_player_by_name(name)
|
||||
if player then
|
||||
sfinv.set_player_inventory_formspec(player)
|
||||
end
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
minetest.register_chatcommand("awd", {
|
||||
params = S("<award ID>"),
|
||||
description = S("Show details of an award"),
|
||||
func = function(name, param)
|
||||
local def = awards.registered_awards[param]
|
||||
if def then
|
||||
minetest.chat_send_player(name, string.format("%s: %s", def.title, def.description))
|
||||
else
|
||||
minetest.chat_send_player(name, S("Award not found."))
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
minetest.register_chatcommand("awpl", {
|
||||
privs = {
|
||||
server = true
|
||||
},
|
||||
params = S("<name>"),
|
||||
description = S("Get the awards statistics for the given player or yourself"),
|
||||
func = function(name, param)
|
||||
if not param or param == "" then
|
||||
param = name
|
||||
end
|
||||
minetest.chat_send_player(name, param)
|
||||
local player = awards.player(param)
|
||||
minetest.chat_send_player(name, dump(player))
|
||||
end
|
||||
})
|
113
mods/awards/src/data.lua
Normal file
113
mods/awards/src/data.lua
Normal file
|
@ -0,0 +1,113 @@
|
|||
|
||||
local storage = minetest.get_mod_storage()
|
||||
local __player_data
|
||||
|
||||
-- Table Save Load Functions
|
||||
function awards.save()
|
||||
storage:set_string("player_data", minetest.write_json(__player_data))
|
||||
end
|
||||
|
||||
local function convert_data()
|
||||
minetest.log("warning", "Importing awards data from previous version")
|
||||
|
||||
local old_players = __player_data
|
||||
__player_data = {}
|
||||
for name, data in pairs(old_players) do
|
||||
while name.name do
|
||||
name = name.name
|
||||
end
|
||||
data.name = name
|
||||
print("Converting data for " .. name)
|
||||
|
||||
-- Just rename counted
|
||||
local counted = {
|
||||
chats = "chat",
|
||||
deaths = "death",
|
||||
joins = "join",
|
||||
}
|
||||
for from, to in pairs(counted) do
|
||||
data[to] = data[from]
|
||||
data[from] = nil
|
||||
end
|
||||
|
||||
data.death = {
|
||||
unknown = data.death,
|
||||
__total = data.death,
|
||||
}
|
||||
|
||||
-- Convert item db to new format
|
||||
local counted_items = {
|
||||
count = "dig",
|
||||
place = "place",
|
||||
craft = "craft",
|
||||
}
|
||||
for from, to in pairs(counted_items) do
|
||||
local ret = {}
|
||||
|
||||
local count = 0
|
||||
if data[from] then
|
||||
for modname, items in pairs(data[from]) do
|
||||
for itemname, value in pairs(items) do
|
||||
itemname = modname .. ":" .. itemname
|
||||
local key = minetest.registered_aliases[itemname] or itemname
|
||||
ret[key] = value
|
||||
count = count + value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ret.__total = count
|
||||
data[from] = nil
|
||||
data[to] = ret
|
||||
end
|
||||
|
||||
__player_data[name] = data
|
||||
end
|
||||
end
|
||||
|
||||
function awards.load()
|
||||
local old_save_path = minetest.get_worldpath().."/awards.txt"
|
||||
local file = io.open(old_save_path, "r")
|
||||
if file then
|
||||
local table = minetest.deserialize(file:read("*all"))
|
||||
if type(table) == "table" then
|
||||
__player_data = table
|
||||
convert_data()
|
||||
else
|
||||
__player_data = {}
|
||||
end
|
||||
file:close()
|
||||
os.rename(old_save_path, minetest.get_worldpath().."/awards.bk.txt")
|
||||
awards.save()
|
||||
else
|
||||
local json = storage:get("player_data")
|
||||
__player_data = json and minetest.parse_json(json) or {}
|
||||
end
|
||||
end
|
||||
|
||||
function awards.player(name)
|
||||
assert(type(name) == "string")
|
||||
local data = __player_data[name] or {}
|
||||
__player_data[name] = data
|
||||
|
||||
data.name = data.name or name
|
||||
data.unlocked = data.unlocked or {}
|
||||
return data
|
||||
end
|
||||
|
||||
function awards.player_or_nil(name)
|
||||
return __player_data[name]
|
||||
end
|
||||
|
||||
function awards.enable(name)
|
||||
awards.player(name).disabled = nil
|
||||
end
|
||||
|
||||
function awards.disable(name)
|
||||
awards.player(name).disabled = true
|
||||
end
|
||||
|
||||
function awards.clear_player(name)
|
||||
__player_data[name] = {}
|
||||
awards.save()
|
||||
end
|
251
mods/awards/src/gui.lua
Normal file
251
mods/awards/src/gui.lua
Normal file
|
@ -0,0 +1,251 @@
|
|||
-- Copyright (c) 2013-18 rubenwardy. MIT.
|
||||
|
||||
local S = awards.translator
|
||||
|
||||
function awards.get_formspec(name, to, sid)
|
||||
local formspec = ""
|
||||
local awards_list = awards.get_award_states(name)
|
||||
|
||||
if #awards_list == 0 then
|
||||
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No achivements available.")).."]"
|
||||
formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]"
|
||||
return formspec
|
||||
end
|
||||
sid = awards_list[sid] and sid or 1
|
||||
|
||||
-- Sidebar
|
||||
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 = "@1"
|
||||
if sitem.unlocked then
|
||||
-- Don't actually use translator here. We define empty S() to fool the update_translations script
|
||||
-- into extracting that string for the templates.
|
||||
local function S(str)
|
||||
return str
|
||||
end
|
||||
status = S("@1 (unlocked)")
|
||||
end
|
||||
|
||||
formspec = formspec .. "textarea[0.5,3.1;4.8,1.45;;" ..
|
||||
S(status, minetest.formspec_escape(title)) ..
|
||||
";]"
|
||||
|
||||
if sdef and sdef.icon then
|
||||
formspec = formspec .. "image[0.45,0;3.5,3.5;" .. minetest.formspec_escape(sdef.icon) .. "]" -- adjusted values from 0.6,0;3,3
|
||||
end
|
||||
|
||||
-- List out goal progress
|
||||
if sitem.goals then
|
||||
local goal_list = "textlist[-0.05,5;3.9,3.7;goals;"
|
||||
local has_visible_goals = false
|
||||
local goals_target = sdef.goals.target
|
||||
local goals_unlocked = 0
|
||||
for i = 1, #sitem.goals do
|
||||
local goal = sitem.goals[i]
|
||||
local goal_status
|
||||
local goal_progress = goal.progress and (" (" .. goal.progress.current .. "/" .. goal.progress.target .. ")") or ""
|
||||
if goal.unlocked then
|
||||
goals_unlocked = goals_unlocked + 1
|
||||
if not sitem.goals.show_unlocked then
|
||||
goto continue
|
||||
else
|
||||
goal_status = "#25fc34✓ "
|
||||
end
|
||||
else
|
||||
if not sitem.goals.show_locked then
|
||||
goto continue
|
||||
else
|
||||
goal_status = goal.progress and (goal.progress.current == 0 and "#aaaaaa☐ " or "#ffffff☐ ") or "#aaaaaa☐ "
|
||||
end
|
||||
end
|
||||
goal_list = goal_list .. goal_status .. minetest.formspec_escape(goal.def.description:split("\n")[1]) .. goal_progress .. ","
|
||||
has_visible_goals = true
|
||||
::continue::
|
||||
end
|
||||
|
||||
-- Goal progress bar
|
||||
local goal_progress_bar = "box[-0.05,4.65;3.9,0.3;#191919]"
|
||||
local gpb_width = 3.9 * math.min(goals_unlocked / goals_target,1.0)
|
||||
if gpb_width > 0 then
|
||||
goal_progress_bar = goal_progress_bar .. "box[-0.05,4.65;" .. gpb_width .. ",0.3;#25fc34]"
|
||||
end
|
||||
--goal_progress_bar = goal_progress_bar .. "box[-0.05,5.05;3.9,0.05;#000000]"
|
||||
goal_progress_bar = goal_progress_bar .. "hypertext[0.295,4.7;3.9,0.4;;<global halign=center>" .. goals_unlocked .. " / " .. goals_target .. "]"
|
||||
|
||||
-- Append formspec
|
||||
formspec = formspec .. goal_progress_bar .. (has_visible_goals and goal_list:sub(1,-2) or "") .. "]"
|
||||
end
|
||||
|
||||
if sdef and sdef.description then
|
||||
formspec = formspec .. "box[-0.05,3.75;3.9,0.85;#000]"
|
||||
formspec = formspec .. "textarea[0.25,3.75;3.9,0.85;;" ..
|
||||
minetest.formspec_escape(sdef.description) .. ";]"
|
||||
end
|
||||
end
|
||||
|
||||
-- Create list box
|
||||
formspec = formspec .. "textlist[4,0;3.8,8.7;awards;"
|
||||
local first = true
|
||||
for _, award in pairs(awards_list) do
|
||||
local def = award.def
|
||||
if def then
|
||||
if not first then
|
||||
formspec = formspec .. ","
|
||||
end
|
||||
first = false
|
||||
|
||||
if def.secret and not award.unlocked then
|
||||
formspec = formspec .. "#707070"..minetest.formspec_escape(S("(Secret Award)"))
|
||||
else
|
||||
local title = award.name
|
||||
if def and def.title then
|
||||
title = def.title
|
||||
end
|
||||
-- title = title .. " [" .. award.score .. "]"
|
||||
if award.unlocked then
|
||||
formspec = formspec .. "#25fc34" .. minetest.formspec_escape(title)
|
||||
elseif award.started then
|
||||
formspec = formspec .. "#ffffff".. minetest.formspec_escape(title)
|
||||
else
|
||||
formspec = formspec .. "#aaaaaa".. minetest.formspec_escape(title)
|
||||
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
|
||||
local data = awards.player(to)
|
||||
if name == to and data.disabled then
|
||||
minetest.chat_send_player(name, S("You've disabled awards. Type /awards enable to reenable."))
|
||||
return
|
||||
end
|
||||
if text then
|
||||
local awards_list = awards.get_award_states(name)
|
||||
if #awards_list == 0 then
|
||||
minetest.chat_send_player(to, S("Error: No award available."))
|
||||
return
|
||||
elseif not data or not data.unlocked then
|
||||
minetest.chat_send_player(to, S("You have not unlocked any awards."))
|
||||
return
|
||||
end
|
||||
minetest.chat_send_player(to, string.format(S("%s’s awards:"), name))
|
||||
|
||||
for str, _ in pairs(data.unlocked) do
|
||||
local def = awards.registered_awards[str]
|
||||
if def then
|
||||
if def.title then
|
||||
if def.description then
|
||||
minetest.chat_send_player(to, string.format("%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",
|
||||
"size[8,8.6]" .. deco ..
|
||||
awards.get_formspec(name, to, sid))
|
||||
end
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
if minetest.get_modpath("sfinv") then
|
||||
sfinv.register_page("awards:awards", {
|
||||
title = S("Awards"),
|
||||
on_enter = function(self, player, context)
|
||||
context.awards_idx = 1
|
||||
end,
|
||||
is_in_nav = function(self, player, context)
|
||||
local data = awards.player(player:get_player_name())
|
||||
return not data.disabled
|
||||
end,
|
||||
get = function(self, player, context)
|
||||
local name = player:get_player_name()
|
||||
return sfinv.make_formspec(player, context,
|
||||
awards.get_formspec(name, name, context.awards_idx),
|
||||
false)
|
||||
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
|
||||
})
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("unified_inventory") ~= nil then
|
||||
unified_inventory.register_button("awards", {
|
||||
type = "image",
|
||||
image = "awards_ui_icon.png",
|
||||
tooltip = S("Awards"),
|
||||
action = function(player)
|
||||
local name = player:get_player_name()
|
||||
awards.show_to(name, name, nil, false)
|
||||
end,
|
||||
})
|
||||
end
|
151
mods/awards/src/triggers.lua
Normal file
151
mods/awards/src/triggers.lua
Normal file
|
@ -0,0 +1,151 @@
|
|||
-- AWARDS
|
||||
--
|
||||
-- Copyright (C) 2013-2015 rubenwardy
|
||||
-- This program is free software; you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU Lesser General Public License as published by
|
||||
-- the Free Software Foundation; either version 2.1 of the License, or
|
||||
-- (at your option) any later version.
|
||||
-- This program is distributed in the hope that it will be useful,
|
||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
-- GNU Lesser General Public License for more details.
|
||||
-- You should have received a copy of the GNU Lesser General Public License along
|
||||
-- with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
--
|
||||
|
||||
-- Don't actually use translator here. We define empty S() to fool the update_translations script
|
||||
-- into extracting those strings for the templates. Actual translation is done in api_triggers.lua.
|
||||
local S = function (str)
|
||||
return str
|
||||
end
|
||||
|
||||
-- Check if a player object is valid for awards.
|
||||
local function player_ok(player)
|
||||
return player and player.is_player and player:is_player() and not player.is_fake_player
|
||||
end
|
||||
|
||||
awards.register_trigger("chat", {
|
||||
type = "counted",
|
||||
progress = S("@1/@2 chat messages"),
|
||||
auto_description = { S("Send a chat message"), S("Chat @1 times") },
|
||||
})
|
||||
minetest.register_on_chat_message(function(name, message)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
if not player_ok(player) or string.find(message, "/") then
|
||||
return
|
||||
end
|
||||
|
||||
awards.notify_chat(player)
|
||||
end)
|
||||
|
||||
|
||||
awards.register_trigger("join", {
|
||||
type = "counted",
|
||||
progress = S("@1/@2 joins"),
|
||||
auto_description = { S("Join once"), S("Join @1 times") },
|
||||
})
|
||||
minetest.register_on_joinplayer(awards.notify_join)
|
||||
|
||||
|
||||
awards.register_trigger("death", {
|
||||
type = "counted_key",
|
||||
progress = S("@1/@2 deaths"),
|
||||
auto_description = { S("Die once of @2"), S("Die @1 times of @2") },
|
||||
auto_description_total = { S("Die @1 times."), S("Mine @1 times") },
|
||||
get_key = function(self, def)
|
||||
return def.trigger.reason
|
||||
end,
|
||||
})
|
||||
minetest.register_on_dieplayer(function(player, reason)
|
||||
if reason then
|
||||
reason = reason.type
|
||||
else
|
||||
reason = "unknown"
|
||||
end
|
||||
awards.notify_death(player, reason)
|
||||
end)
|
||||
|
||||
|
||||
awards.register_trigger("dig", {
|
||||
type = "counted_key",
|
||||
progress = S("@1/@2 dug"),
|
||||
auto_description = { S("Mine: @2"), S("Mine: @1×@2") },
|
||||
auto_description_total = { S("Mine @1 block."), S("Mine @1 blocks.") },
|
||||
get_key = function(self, def)
|
||||
return minetest.registered_aliases[def.trigger.node] or def.trigger.node
|
||||
end,
|
||||
key_is_item = true,
|
||||
})
|
||||
minetest.register_on_dignode(function(pos, node, player)
|
||||
if not player_ok(player) or not pos or not node then
|
||||
return
|
||||
end
|
||||
|
||||
local node_name = node.name
|
||||
node_name = minetest.registered_aliases[node_name] or node_name
|
||||
awards.notify_dig(player, node_name)
|
||||
end)
|
||||
|
||||
|
||||
awards.register_trigger("place", {
|
||||
type = "counted_key",
|
||||
progress = S("@1/@2 placed"),
|
||||
auto_description = { S("Place: @2"), S("Place: @1×@2") },
|
||||
auto_description_total = { S("Place @1 block."), S("Place @1 blocks.") },
|
||||
get_key = function(self, def)
|
||||
return minetest.registered_aliases[def.trigger.node] or def.trigger.node
|
||||
end,
|
||||
key_is_item = true,
|
||||
})
|
||||
minetest.register_on_placenode(function(pos, node, player)
|
||||
if not player_ok(player) or not pos or not node then
|
||||
return
|
||||
end
|
||||
|
||||
local node_name = node.name
|
||||
node_name = minetest.registered_aliases[node_name] or node_name
|
||||
awards.notify_place(player, node_name)
|
||||
end)
|
||||
|
||||
|
||||
awards.register_trigger("craft", {
|
||||
type = "counted_key",
|
||||
progress = S("@1/@2 crafted"),
|
||||
auto_description = { S("Craft: @2"), S("Craft: @1×@2") },
|
||||
auto_description_total = { S("Craft @1 item"), S("Craft @1 items.") },
|
||||
get_key = function(self, def)
|
||||
return minetest.registered_aliases[def.trigger.item] or def.trigger.item
|
||||
end,
|
||||
key_is_item = true,
|
||||
})
|
||||
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
|
||||
if not player_ok(player) or itemstack:is_empty() then
|
||||
return
|
||||
end
|
||||
|
||||
local itemname = itemstack:get_name()
|
||||
itemname = minetest.registered_aliases[itemname] or itemname
|
||||
awards.notify_craft(player, itemname, itemstack:get_count())
|
||||
end)
|
||||
|
||||
|
||||
awards.register_trigger("eat", {
|
||||
type = "counted_key",
|
||||
progress = S("@1/@2 eaten"),
|
||||
auto_description = { S("Eat @2"), S("Eat @1×@2") },
|
||||
auto_description_total = { S("Eat @1 item"), S("Eat @1 items.") },
|
||||
get_key = function(self, def)
|
||||
return minetest.registered_aliases[def.trigger.item] or def.trigger.item
|
||||
end,
|
||||
key_is_item = true,
|
||||
})
|
||||
minetest.register_on_item_eat(function(_, _, itemstack, player, _)
|
||||
if not player_ok(player) or itemstack:is_empty() then
|
||||
return
|
||||
end
|
||||
|
||||
local itemname = itemstack:get_name()
|
||||
itemname = minetest.registered_aliases[itemname] or itemname
|
||||
awards.notify_eat(player, itemname)
|
||||
end)
|
Loading…
Add table
Add a link
Reference in a new issue