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

Refactor awards.unlock

This commit is contained in:
rubenwardy 2015-06-11 17:35:32 +01:00
parent 79818c1355
commit 56d08f2f49
5 changed files with 90 additions and 115 deletions

73
api.lua
View File

@ -29,6 +29,11 @@ function awards.save()
end end
end end
local S = function(s) return s end
function awards.set_intllib(locale)
S = locale
end
function awards.init() function awards.init()
awards.players = awards.load() awards.players = awards.load()
awards.def = {} awards.def = {}
@ -79,26 +84,10 @@ function awards.register_achievement(name, def)
end end
end end
-- check icon, background and custom_announce data -- Add Award
if not def.icon or def.icon == "" then
def.icon = "unknown.png"
end
if not def.background or def.background == "" then
def.background = "bg_default.png"
end
if not def.custom_announce or def.custom_announce == "" then
def.custom_announce = "Achievement Unlocked:"
end
-- add the achievement to the definition table
awards.def[name] = def awards.def[name] = def
end end
-- run a function when an item is crafted
function awards.register_onCraft(func)
table.insert(awards.onCraft,func)
end
-- This function is called whenever a target condition is met. -- This function is called whenever a target condition is met.
-- It checks if a player already has that achievement, and if they do not, -- It checks if a player already has that achievement, and if they do not,
-- it gives it to them -- it gives it to them
@ -106,7 +95,7 @@ end
--awards.give_achievement(name, award) --awards.give_achievement(name, award)
-- name - the name of the player -- name - the name of the player
-- award - the name of the award to give -- award - the name of the award to give
function awards.give_achievement(name, award) function awards.unlock(name, award)
-- Access Player Data -- Access Player Data
local data = awards.players[name] local data = awards.players[name]
local awdef = awards.def[award] local awdef = awards.def[award]
@ -125,8 +114,10 @@ function awards.give_achievement(name, award)
return return
end end
-- Set award flag -- Unlock Award
minetest.log("action", name.." has unlocked award "..name)
data.unlocked[award] = award data.unlocked[award] = award
awards.save()
-- Give Prizes -- Give Prizes
if awdef and awdef.prizes then if awdef and awdef.prizes then
@ -143,34 +134,6 @@ function awards.give_achievement(name, award)
end end
end end
-- Get data from definition tables
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
-- Record this in the log
minetest.log("action", name.." has unlocked award "..title)
-- Save playertable
awards.save()
-- Run callbacks -- Run callbacks
if awdef.on_unlock and awdef.on_unlock(name, awdef) then if awdef.on_unlock and awdef.on_unlock(name, awdef) then
return return
@ -181,7 +144,14 @@ function awards.give_achievement(name, award)
end end
end end
-- send the won award message to the player -- Get Notification Settings
local title = awdef.title or award
local desc = awdef.description or ""
local background = awdef.background or "bg_default.png"
local icon = awdef.icon or "unknown.png"
local custom_announce = awdef.custom_announce or S("Achievement Unlocked:")
-- Do Notification
if awards.show_mode == "formspec" then if awards.show_mode == "formspec" then
-- use a formspec to send it -- use a formspec to send it
minetest.show_formspec(name, "achievements:unlocked", "size[4,2]".. minetest.show_formspec(name, "achievements:unlocked", "size[4,2]"..
@ -191,7 +161,7 @@ function awards.give_achievement(name, award)
"label[0.3,0.1;"..custom_announce.."]") "label[0.3,0.1;"..custom_announce.."]")
elseif awards.show_mode == "chat" then elseif awards.show_mode == "chat" then
-- use the chat console to send it -- use the chat console to send it
minetest.chat_send_player(name, "Achievement Unlocked: "..title) minetest.chat_send_player(name, S("Achievement Unlocked:")..title)
if desc~="" then if desc~="" then
minetest.chat_send_player(name, desc) minetest.chat_send_player(name, desc)
end end
@ -244,11 +214,14 @@ function awards.give_achievement(name, award)
end end
end end
-- Backwards compatibility
awards.give_achievement = awards.unlock
--[[minetest.register_chatcommand("gawd", { --[[minetest.register_chatcommand("gawd", {
params = "award name", params = "award name",
description = "gawd: give award to self", description = "gawd: give award to self",
func = function(name, param) func = function(name, param)
awards.give_achievement(name,param) awards.unlock(name,param)
end end
})]]-- })]]--

View File

@ -26,6 +26,7 @@ end
dofile(minetest.get_modpath("awards").."/api.lua") dofile(minetest.get_modpath("awards").."/api.lua")
dofile(minetest.get_modpath("awards").."/chat_commands.lua") dofile(minetest.get_modpath("awards").."/chat_commands.lua")
dofile(minetest.get_modpath("awards").."/triggers.lua") dofile(minetest.get_modpath("awards").."/triggers.lua")
awards.set_intllib(S)
-- Light it up -- Light it up
awards.register_achievement("award_lightitup",{ awards.register_achievement("award_lightitup",{

View File

@ -1,3 +1,4 @@
Achievement Unlocked: =
Light It Up = Light It Up =
Place 100 torches. = Place 100 torches. =
Light ALL The Things! = Light ALL The Things! =

View File

@ -32,7 +32,7 @@ old fork in Carbone, under same license.
* name is the player name * name is the player name
* def is the award def. * def is the award def.
* return true to cancel HUD * return true to cancel HUD
* awards.give_achievement(name, award) * awards.unlock(name, award)
* gives an award to a player * gives an award to a player
* name is the player name * name is the player name

View File

@ -113,7 +113,7 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
end end
if res then if res then
awards.give_achievement(playern,res) awards.unlock(playern,res)
end end
end end
end) end)
@ -167,7 +167,7 @@ minetest.register_on_placenode(function(pos, node, digger)
end end
if res then if res then
awards.give_achievement(playern,res) awards.unlock(playern,res)
end end
end end
end) end)
@ -253,7 +253,7 @@ minetest.register_on_dieplayer(function(player)
end end
end end
if res ~= nil then if res ~= nil then
awards.give_achievement(name,res) awards.unlock(name,res)
end end
end end
end) end)
@ -285,7 +285,7 @@ minetest.register_on_joinplayer(function(player)
end end
end end
if res ~= nil then if res ~= nil then
awards.give_achievement(name,res) awards.unlock(name,res)
end end
end end
end) end)
@ -318,7 +318,7 @@ minetest.register_on_chat_message(function(name, message)
end end
end end
if res ~= nil then if res ~= nil then
awards.give_achievement(name,res) awards.unlock(name,res)
end end
end end
end) end)