mirror of
https://gitlab.com/rubenwardy/awards.git
synced 2024-11-26 09:13:55 +01:00
Clean up player data validation
This commit is contained in:
parent
ef19940edc
commit
eeee4a8398
60
api.lua
60
api.lua
@ -95,11 +95,9 @@ local function run_trigger_callbacks(self, player, data, table_func)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function awards.register_trigger(tname, tdef)
|
function awards.register_trigger(tname, tdef)
|
||||||
if type(tdef) == "function" then
|
assert(type(tdef) == "table",
|
||||||
tdef = {
|
"Passing a callback to register_trigger is not supported in 3.0")
|
||||||
on_register = tdef
|
|
||||||
}
|
|
||||||
end
|
|
||||||
tdef.name = tname
|
tdef.name = tname
|
||||||
tdef.run_callbacks = run_trigger_callbacks
|
tdef.run_callbacks = run_trigger_callbacks
|
||||||
|
|
||||||
@ -139,7 +137,7 @@ function awards.register_trigger(tname, tdef)
|
|||||||
print(dump(data))
|
print(dump(data))
|
||||||
|
|
||||||
-- Increment counter
|
-- Increment counter
|
||||||
local currentVal = data[datakey] + 1
|
local currentVal = (data[datakey] or 0) + 1
|
||||||
data[datakey] = currentVal
|
data[datakey] = currentVal
|
||||||
|
|
||||||
tdef:run_callbacks(player, data, function(entry)
|
tdef:run_callbacks(player, data, function(entry)
|
||||||
@ -179,22 +177,6 @@ function awards.get_total_keyed_count(data, field)
|
|||||||
return data[field].__total or 0
|
return data[field].__total or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function awards.get_total_item_count(data, field)
|
|
||||||
local i = 0
|
|
||||||
if data and field then
|
|
||||||
awards.assertPlayer(data)
|
|
||||||
awards.tbv(data, field)
|
|
||||||
for mod,_ in pairs(data[field]) do
|
|
||||||
awards.tbv(data[field], mod)
|
|
||||||
for item,_ in pairs(data[field][mod]) do
|
|
||||||
awards.tbv(data[field][mod], item, 0)
|
|
||||||
i = i + data[field][mod][item]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return i
|
|
||||||
end
|
|
||||||
|
|
||||||
function awards.register_on_unlock(func)
|
function awards.register_on_unlock(func)
|
||||||
table.insert(awards.on_unlock, func)
|
table.insert(awards.on_unlock, func)
|
||||||
end
|
end
|
||||||
@ -245,23 +227,12 @@ end
|
|||||||
-- award - the name of the award to give
|
-- award - the name of the award to give
|
||||||
function awards.unlock(name, award)
|
function awards.unlock(name, award)
|
||||||
-- Access Player Data
|
-- Access Player Data
|
||||||
local data = awards.players[name]
|
local data = awards.player(name)
|
||||||
local awdef = awards.def[award]
|
local awdef = awards.def[award]
|
||||||
|
assert(awdef, "Unable to unlock an award which doesn't exist!")
|
||||||
|
|
||||||
-- Perform checks
|
if data.disabled or
|
||||||
if not data then
|
(data.unlocked[award] and data.unlocked[award] == award) then
|
||||||
return
|
|
||||||
end
|
|
||||||
if not awdef then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if data.disabled then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
awards.tbv(data,"unlocked")
|
|
||||||
|
|
||||||
-- Don't give the achievement if it has already been given
|
|
||||||
if data.unlocked[award] and data.unlocked[award] == award then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -307,10 +278,10 @@ function awards.unlock(name, award)
|
|||||||
-- Do Notification
|
-- Do Notification
|
||||||
if sound then
|
if sound then
|
||||||
-- Enforce sound delay to prevent sound spamming
|
-- Enforce sound delay to prevent sound spamming
|
||||||
local lastsound = awards.players[name].lastsound
|
local lastsound = awards.player(name).lastsound
|
||||||
if lastsound == nil or os.difftime(os.time(), lastsound) >= 1 then
|
if lastsound == nil or os.difftime(os.time(), lastsound) >= 1 then
|
||||||
minetest.sound_play(sound, {to_player=name})
|
minetest.sound_play(sound, {to_player=name})
|
||||||
awards.players[name].lastsound = os.time()
|
awards.player(name).lastsound = os.time()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -395,7 +366,7 @@ awards.give_achievement = awards.unlock
|
|||||||
function awards.getFormspec(name, to, sid)
|
function awards.getFormspec(name, to, sid)
|
||||||
local formspec = ""
|
local formspec = ""
|
||||||
local listofawards = awards._order_awards(name)
|
local listofawards = awards._order_awards(name)
|
||||||
local playerdata = awards.players[name]
|
local playerdata = awards.player(name)
|
||||||
|
|
||||||
if #listofawards == 0 then
|
if #listofawards == 0 then
|
||||||
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No awards available.")).."]"
|
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No awards available.")).."]"
|
||||||
@ -497,13 +468,13 @@ function awards.show_to(name, to, sid, text)
|
|||||||
if #listofawards == 0 then
|
if #listofawards == 0 then
|
||||||
minetest.chat_send_player(to, S("Error: No awards available."))
|
minetest.chat_send_player(to, S("Error: No awards available."))
|
||||||
return
|
return
|
||||||
elseif not awards.players[name] or not awards.players[name].unlocked then
|
elseif not awards.player(name) or not awards.player(name).unlocked then
|
||||||
minetest.chat_send_player(to, S("You have not unlocked any awards."))
|
minetest.chat_send_player(to, S("You have not unlocked any awards."))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
minetest.chat_send_player(to, string.format(S("%s’s awards:"), name))
|
minetest.chat_send_player(to, string.format(S("%s’s awards:"), name))
|
||||||
|
|
||||||
for _, str in pairs(awards.players[name].unlocked) do
|
for _, str in pairs(awards.player(name).unlocked) do
|
||||||
local def = awards.def[str]
|
local def = awards.def[str]
|
||||||
if def then
|
if def then
|
||||||
if def.title then
|
if def.title then
|
||||||
@ -553,11 +524,6 @@ end)
|
|||||||
|
|
||||||
awards.load()
|
awards.load()
|
||||||
|
|
||||||
minetest.register_on_newplayer(function(player)
|
|
||||||
local playern = player:get_player_name()
|
|
||||||
awards.assertPlayer(playern)
|
|
||||||
end)
|
|
||||||
|
|
||||||
minetest.register_on_shutdown(function()
|
minetest.register_on_shutdown(function()
|
||||||
awards.save()
|
awards.save()
|
||||||
end)
|
end)
|
||||||
|
@ -1,40 +1,12 @@
|
|||||||
function awards.tbv(tb,value,default)
|
|
||||||
if not default then
|
|
||||||
default = {}
|
|
||||||
end
|
|
||||||
if not tb or type(tb) ~= "table" then
|
|
||||||
if not value then
|
|
||||||
value = "[NULL]"
|
|
||||||
end
|
|
||||||
minetest.log("error", "awards.tbv - table "..dump(value).." is null, or not a table! Dump: "..dump(tb))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if not value then
|
|
||||||
error("[ERROR] awards.tbv was not used correctly!\n"..
|
|
||||||
"Value: '"..dump(value).."'\n"..
|
|
||||||
"Dump:"..dump(tb))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if not tb[value] then
|
|
||||||
tb[value] = default
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function awards.assertPlayer(playern)
|
|
||||||
awards.tbv(awards.players, playern)
|
|
||||||
awards.tbv(awards.players[playern], "name", playern)
|
|
||||||
awards.tbv(awards.players[playern], "unlocked")
|
|
||||||
awards.tbv(awards.players[playern], "place")
|
|
||||||
awards.tbv(awards.players[playern], "count")
|
|
||||||
awards.tbv(awards.players[playern], "craft")
|
|
||||||
awards.tbv(awards.players[playern], "eat")
|
|
||||||
awards.tbv(awards.players[playern], "deaths", 0)
|
|
||||||
awards.tbv(awards.players[playern], "joins", 0)
|
|
||||||
awards.tbv(awards.players[playern], "chats", 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
function awards.player(name)
|
function awards.player(name)
|
||||||
awards.assertPlayer(name)
|
local data = awards.players[name] or {}
|
||||||
|
awards.players[name] = data
|
||||||
|
data.name = data.name or name
|
||||||
|
data.unlocked = data.unlocked or {}
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
|
||||||
|
function awards.player_or_nil(name)
|
||||||
return awards.players[name]
|
return awards.players[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ old fork in Carbone, under same license.
|
|||||||
* awards.run_trigger_callbacks(player, data, trigger, table_func(entry))
|
* awards.run_trigger_callbacks(player, data, trigger, table_func(entry))
|
||||||
* Goes through and checks all triggers registered to a trigger type,
|
* Goes through and checks all triggers registered to a trigger type,
|
||||||
unlocking the award if conditions are met.
|
unlocking the award if conditions are met.
|
||||||
* data is the player's award data, ie: awards.players[player_name]
|
* data is the player's award data, ie: awards.player(player_name)
|
||||||
* trigger is the name of the trigger type. Ie: awards.on[trigger]
|
* trigger is the name of the trigger type. Ie: awards.on[trigger]
|
||||||
* table_func is called if the trigger is a table - simply return an
|
* table_func is called if the trigger is a table - simply return an
|
||||||
award name to unlock it
|
award name to unlock it
|
||||||
@ -51,7 +51,7 @@ old fork in Carbone, under same license.
|
|||||||
* add to an item's statistic count
|
* add to an item's statistic count
|
||||||
* for example, (data, "place", "default:stone") will add 1 to the number of
|
* for example, (data, "place", "default:stone") will add 1 to the number of
|
||||||
times default:stone has been placed.
|
times default:stone has been placed.
|
||||||
* data is the player's award data, ie: awards.players[player_name]
|
* data is the player's award data, ie: awards.player(player_name)
|
||||||
* returns true on success, false on failure (eg: cannot get modname and item from itemname)
|
* returns true on success, false on failure (eg: cannot get modname and item from itemname)
|
||||||
* awards.register_on_unlock(func(name, def))
|
* awards.register_on_unlock(func(name, def))
|
||||||
* name is the player name
|
* name is the player name
|
||||||
|
Loading…
Reference in New Issue
Block a user