1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2024-12-29 12:10:38 +01:00

register_on_unlock and on_unlock

This commit is contained in:
rubenwardy 2015-06-10 19:15:16 +01:00
parent cfe11a57c7
commit 30aee5090f
4 changed files with 205 additions and 143 deletions

233
api.lua
View File

@ -33,6 +33,7 @@ function awards.init()
awards.def = {} awards.def = {}
awards.trigger_types = {} awards.trigger_types = {}
awards.on = {} awards.on = {}
awards.on_unlock = {}
end end
function awards.load() function awards.load()
@ -56,18 +57,24 @@ function awards.register_trigger(name, func)
end end
end end
function awards.register_on_unlock(func)
table.insert(awards.on_unlock, func)
end
-- API Functions -- API Functions
function awards._additional_triggers(name, def) function awards._additional_triggers(name, def)
-- Depreciated! -- Depreciated!
end end
function awards.register_achievement(name, def) function awards.register_achievement(name, def)
def.name = name
-- Add Triggers -- Add Triggers
if def.trigger and def.trigger.type then if def.trigger and def.trigger.type then
local func = awards.trigger_types[def.trigger.type] local func = awards.trigger_types[def.trigger.type]
if func then if func then
func(name, def) func(def)
else else
awards._additional_triggers(name, def) awards._additional_triggers(name, def)
end end
@ -85,7 +92,6 @@ function awards.register_achievement(name, def)
end end
-- add the achievement to the definition table -- add the achievement to the definition table
def.name = name
awards.def[name] = def awards.def[name] = def
end end
@ -103,126 +109,139 @@ end
-- award - the name of the award to give -- award - the name of the award to give
function awards.give_achievement(name, award) function awards.give_achievement(name, award)
-- Access Player Data -- Access Player Data
local data = awards.players[name] local data = awards.players[name]
local awdef = awards.def[award]
-- Perform checks -- Perform checks
if not data then if not data then
return return
end end
if not awards.def[award] then if not awdef then
return return
end end
awards.tbv(data,"unlocked") awards.tbv(data,"unlocked")
-- check to see if the player does not already have that achievement -- Don't give the achievement if it has already been given
if not data.unlocked[award] or data.unlocked[award]~=award then if data.unlocked[award] and data.unlocked[award] == award then
-- Set award flag return
data.unlocked[award]=award end
-- Give Prizes -- Set award flag
if awards.def[award] and awards.def[award].prizes then data.unlocked[award] = award
for i = 1, #awards.def[award].prizes do
local itemstack = ItemStack(awards.def[award].prizes[i]) -- Give Prizes
if itemstack:is_empty() or not itemstack:is_known() then if awdef and awdef.prizes then
return for i = 1, #awdef.prizes do
end local itemstack = ItemStack(awdef.prizes[i])
local receiverref = core.get_player_by_name(name) if itemstack:is_empty() or not itemstack:is_known() then
if receiverref == nil then return
return
end
receiverref:get_inventory():add_item("main", itemstack)
end end
end local receiverref = core.get_player_by_name(name)
if not receiverref then
-- Get data from definition tables return
local title = award
local desc = ""
local background = ""
local icon = ""
local custom_announce = ""
if awards.def[award].title then
title = awards.def[award].title
end
if awards.def[award].custom_announce then
custom_announce = awards.def[award].custom_announce
end
if awards.def[award].background then
background = awards.def[award].background
end
if awards.def[award].icon then
icon = awards.def[award].icon
end
if awards.def[award] and awards.def[award].description then
desc = awards.def[award].description
end
-- send the won award message to the player
if awards.show_mode == "formspec" then
-- use a formspec to send it
minetest.show_formspec(name, "achievements:unlocked", "size[4,2]"..
"image_button_exit[0,0;4,2;"..background..";close1; ]"..
"image_button_exit[0.2,0.8;1,1;"..icon..";close2; ]"..
"label[1.1,1;"..title.."]"..
"label[0.3,0.1;"..custom_announce.."]")
elseif awards.show_mode == "chat" then
-- use the chat console to send it
minetest.chat_send_player(name, "Achievement Unlocked: "..title)
if desc~="" then
minetest.chat_send_player(name, desc)
end end
else receiverref:get_inventory():add_item("main", itemstack)
local player = minetest.get_player_by_name(name)
local one = player:hud_add({
hud_elem_type = "image",
name = "award_bg",
scale = {x = 1, y = 1},
text = background,
position = {x = 0.5, y = 0},
offset = {x = 0, y = 138},
alignment = {x = 0, y = -1}
})
local two = player:hud_add({
hud_elem_type = "text",
name = "award_au",
number = 0xFFFFFF,
scale = {x = 100, y = 20},
text = "Achievement Unlocked!",
position = {x = 0.5, y = 0},
offset = {x = 0, y = 40},
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},
offset = {x = 30, y = 100},
alignment = {x = 0, y = -1}
})
local four = player:hud_add({
hud_elem_type = "image",
name = "award_icon",
scale = {x = 4, y = 4},
text = icon,
position = {x = 0.5, y = 0},
offset = {x = -81.5, y = 126},
alignment = {x = 0, y = -1}
})
minetest.after(3, function()
player:hud_remove(one)
player:hud_remove(two)
player:hud_remove(three)
player:hud_remove(four)
end)
end end
end
-- record this in the log -- Get data from definition tables
minetest.log("action", name.." has unlocked award "..title) local title = award
local desc = ""
local background = ""
local icon = ""
local custom_announce = ""
if awdef.title then
title = awdef.title
end
if awdef.custom_announce then
custom_announce = awdef.custom_announce
end
if awdef.background then
background = awdef.background
end
if awdef.icon then
icon = awdef.icon
end
if awdef and awdef.description then
desc = awdef.description
end
-- save playertable -- Record this in the log
awards.save() minetest.log("action", name.." has unlocked award "..title)
-- Save playertable
awards.save()
-- 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
-- send the won award message to the player
if awards.show_mode == "formspec" then
-- use a formspec to send it
minetest.show_formspec(name, "achievements:unlocked", "size[4,2]"..
"image_button_exit[0,0;4,2;"..background..";close1; ]"..
"image_button_exit[0.2,0.8;1,1;"..icon..";close2; ]"..
"label[1.1,1;"..title.."]"..
"label[0.3,0.1;"..custom_announce.."]")
elseif awards.show_mode == "chat" then
-- use the chat console to send it
minetest.chat_send_player(name, "Achievement Unlocked: "..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 = 1, y = 1},
text = background,
position = {x = 0.5, y = 0},
offset = {x = 0, y = 138},
alignment = {x = 0, y = -1}
})
local two = player:hud_add({
hud_elem_type = "text",
name = "award_au",
number = 0xFFFFFF,
scale = {x = 100, y = 20},
text = "Achievement Unlocked!",
position = {x = 0.5, y = 0},
offset = {x = 0, y = 40},
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},
offset = {x = 30, y = 100},
alignment = {x = 0, y = -1}
})
local four = player:hud_add({
hud_elem_type = "image",
name = "award_icon",
scale = {x = 4, y = 4},
text = icon,
position = {x = 0.5, y = 0},
offset = {x = -81.5, y = 126},
alignment = {x = 0, y = -1}
})
minetest.after(3, function()
player:hud_remove(one)
player:hud_remove(two)
player:hud_remove(three)
player:hud_remove(four)
end)
end end
end end

View File

@ -1,19 +1,3 @@
-- 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.
--
function awards.tbv(tb,value,default) function awards.tbv(tb,value,default)
if not default then if not default then
default = {} default = {}

View File

@ -23,27 +23,86 @@ old fork in Carbone, under same license.
* (for all types) target - how many to dig / place * (for all types) target - how many to dig / place
* See Triggers * See Triggers
* secret [optional] - if true, then player needs to unlock to find out what it is. * secret [optional] - if true, then player needs to unlock to find out what it is.
* on_unlock [optional] - func(name, def)
* name is player name
* return true to cancel register_on_unlock callbacks and HUD
* awards.register_trigger(name, func(awardname, def)) * awards.register_trigger(name, func(awardname, def))
* Note: awards.on[name] is automatically created for triggers * Note: awards.on[name] is automatically created for triggers
* awards.give_achievement(name,award) * awards.register_on_unlock(func(name, def))
* -- gives an award to a player * name is the player name
* def is the award def.
* return true to cancel HUD
* awards.give_achievement(name, award)
* gives an award to a player
* name is the player name
# Included in the Mod # Included in the Mod
The API, above, allows you to register awards
and triggers (things that look for events and unlock awards, they need
to be registered in order to get details from award_def.trigger).
However, all awards and triggers are separate from the API.
They can be found in init.lua and triggers.lua
## Triggers ## Triggers
Callbacks (register a function to be run)
### dig
trigger = {
type = "dig",
node = "default:dirt",
target = 50
}
### place
trigger = {
type = "place",
node = "default:dirt",
target = 50
}
### death
trigger = {
type = "death",
target = 5
}
### chat
trigger = {
type = "chat",
target = 100
}
### join
trigger = {
type = "join",
target = 100
}
## Callbacks relating to triggers
* awards.register_on_dig(func(player, data)) * awards.register_on_dig(func(player, data))
* -- return award name or null * data is player data (see below)
* return award name or null
* awards.register_on_place(func(player, data)) * awards.register_on_place(func(player, data))
* -- return award name or null * data is player data (see below)
* return award name or null
* awards.register_on_death(func(player, data)) * awards.register_on_death(func(player, data))
* -- return award name or null * data is player data (see below)
* return award name or null
* awards.register_on_chat(func(player, data)) * awards.register_on_chat(func(player, data))
* -- return award name or null * data is player data (see below)
* awards.register_on_join(func(player, data)) * return award name or null
* -- return award name or null * awards.register_on_join(func(player, data)
* awards.register_onCraft(func(player,data)) * data is player data (see below)
* -- return award name or null * return award name or null
# Player Data # Player Data

View File

@ -14,43 +14,43 @@
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-- --
awards.register_trigger("dig", function(name, def) awards.register_trigger("dig", function(def)
local tmp = { local tmp = {
award = name, award = def.name,
node = def.trigger.node, node = def.trigger.node,
target = def.trigger.target target = def.trigger.target
} }
table.insert(awards.on.dig, tmp) table.insert(awards.on.dig, tmp)
end) end)
awards.register_trigger("place", function(name, def) awards.register_trigger("place", function(def)
local tmp = { local tmp = {
award = name, award = def.name,
node = def.trigger.node, node = def.trigger.node,
target = def.trigger.target target = def.trigger.target
} }
table.insert(awards.on.place, tmp) table.insert(awards.on.place, tmp)
end) end)
awards.register_trigger("death", function(name, def) awards.register_trigger("death", function(def)
local tmp = { local tmp = {
award = name, award = def.name,
target = def.trigger.target target = def.trigger.target
} }
table.insert(awards.on.death, tmp) table.insert(awards.on.death, tmp)
end) end)
awards.register_trigger("chat", function(name, def) awards.register_trigger("chat", function(def)
local tmp = { local tmp = {
award = name, award = def.name,
target = def.trigger.target target = def.trigger.target
} }
table.insert(awards.on.chat, tmp) table.insert(awards.on.chat, tmp)
end) end)
awards.register_trigger("join", function(name, def) awards.register_trigger("join", function(def)
local tmp = { local tmp = {
award = name, award = def.name,
target = def.trigger.target target = def.trigger.target
} }
table.insert(awards.on.join, tmp) table.insert(awards.on.join, tmp)