Primo commit

master
Zughy 2020-08-13 22:47:58 +02:00
parent 943e1076b4
commit 77ef73a3dd
4 changed files with 160 additions and 0 deletions

150
api.lua Normal file
View File

@ -0,0 +1,150 @@
achievements_lib = {}
----------------------------------------------
---------------DICHIARAZIONI------------------
----------------------------------------------
local function update_storage() end
local function generate_access_key() end
local storage = minetest.get_mod_storage()
local achievements = {} -- KEY: mod; VALUE: {KEY: achievement ID; VALUE: {whatever properties}
local p_achievements = {} -- KEY: p_name; VALUE: {KEY: mod, VALUE: {KEY: achievement ID VALUE: true/nil}}}
local access_keys = {} -- KEY: random ID; VALUE: mod name
-- inizializzo storage caricando tutti i giocatori
for pl_name, mods in pairs(storage:to_table().fields) do
p_achievements[pl_name] = minetest.deserialize(mods)
end
minetest.log("action", dump(p_achievements))
----------------------------------------------
-------------------CORPO----------------------
----------------------------------------------
function achievements_lib.register_achievements(mod, mod_achievements)
assert(achievements[mod] == nil, "[ACHIEVEMENTS_LIB] There was an attempt to register the mod " .. mod .. " more than once! Be sure you didn't call this function twice and you didn't install any suspicious mod")
achievements[mod] = mod_achievements
return generate_access_key(mod)
end
function achievements_lib.add_achievement(p_name, mod_key, achvmt_ID)
local mod = access_keys[mod_key]
local achievement = achievements[mod][achvmt_ID]
if achievement == nil then return end
if p_achievements[p_name][mod][achvmt_ID] ~= nil then return end
p_achievements[p_name][mod][achvmt_ID] = true
update_storage(p_name)
end
----------------------------------------------
--------------------UTILS---------------------
----------------------------------------------
function achievements_lib.has_player_achievement(p_name, mod, achvmt_ID)
return p_achievements[p_name][mod][achvmt_ID] ~= nil
end
function achievements_lib.is_player_in_storage(p_name, mod)
if p_achievements[p_name] then
if mod and p_achievements[p_name][mod] then
return true
else
return false
end
else
return false
end
end
function achievements_lib.add_player_to_storage(p_name, mod)
if not minetest.get_player_by_name(p_name) then
minetest.log("Warning", "[ACHIEVEMENTS_LIB] Player " .. p_name .. " must be online in order to be added to the storage!")
return end
if p_achievements[p_name] then
if mod and p_achievements[p_name][mod] then
minetest.log("Warning", "[ACHIEVEMENTS_LIB] Can't add player to storage, an entry within this mod already exists!")
elseif mod then
p_achievements[p_name][mod] = {}
storage:set_string(p_name, minetest.serialize(p_achievements[p_name]))
else
minetest.log("Warning", "[ACHIEVEMENTS_LIB] Can't add player to storage, an entry already exists!")
end
else
p_achievements[p_name] = {}
storage:set_string(p_name, minetest.serialize(p_achievements[p_name]))
end
end
----------------------------------------------
-----------------GETTERS----------------------
----------------------------------------------
function achievements_lib.get_achievement(mod_key, achvmt_ID)
local mod = access_keys[mod_key]
return achievements[mod][achvmt_ID]
end
function achievements_lib.get_player_achievements(p_name, mod_key)
local mod = access_keys[mod_key]
return p_achievements[p_name][mod]
end
----------------------------------------------
---------------FUNZIONI LOCALI----------------
----------------------------------------------
function update_storage(p_name)
storage:set_string(p_name, minetest.serialize(p_achievements[p_name]))
end
function generate_access_key(mod)
local access_key = ""
-- se si è così sfigati da generare due chiavi uguali, continua finché la nuova non è univoca
repeat
for i = 1, 10 do
access_key = access_key .. string.char(math.random(33,126))
end
until (access_keys[access_key] == nil)
access_keys[access_key] = mod
return access_key
end

6
init.lua Normal file
View File

@ -0,0 +1,6 @@
local version = "0.beta"
dofile(minetest.get_modpath("achievements_lib") .. "/api.lua")
dofile(minetest.get_modpath("achievements_lib") .. "/player_manager.lua")
minetest.log("action", "[ACHIEVEMENTS_LIB] Mod initialised, running version " .. version)

1
mod.conf Normal file
View File

@ -0,0 +1 @@
name=achievements_lib

3
player_manager.lua Normal file
View File

@ -0,0 +1,3 @@
minetest.register_on_newplayer(function(ObjectRef)
achievements_lib.add_player_to_storage(ObjectRef:get_player_name())
end)