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

75
api.lua
View File

@ -29,6 +29,11 @@ function awards.save()
end
end
local S = function(s) return s end
function awards.set_intllib(locale)
S = locale
end
function awards.init()
awards.players = awards.load()
awards.def = {}
@ -79,26 +84,10 @@ function awards.register_achievement(name, def)
end
end
-- check icon, background and custom_announce data
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
-- Add Award
awards.def[name] = def
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.
-- It checks if a player already has that achievement, and if they do not,
-- it gives it to them
@ -106,7 +95,7 @@ end
--awards.give_achievement(name, award)
-- name - the name of the player
-- award - the name of the award to give
function awards.give_achievement(name, award)
function awards.unlock(name, award)
-- Access Player Data
local data = awards.players[name]
local awdef = awards.def[award]
@ -116,7 +105,7 @@ function awards.give_achievement(name, award)
return
end
if not awdef then
return
return
end
awards.tbv(data,"unlocked")
@ -125,8 +114,10 @@ function awards.give_achievement(name, award)
return
end
-- Set award flag
-- Unlock Award
minetest.log("action", name.." has unlocked award "..name)
data.unlocked[award] = award
awards.save()
-- Give Prizes
if awdef and awdef.prizes then
@ -143,34 +134,6 @@ function awards.give_achievement(name, award)
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
if awdef.on_unlock and awdef.on_unlock(name, awdef) then
return
@ -181,7 +144,14 @@ function awards.give_achievement(name, award)
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
-- use a formspec to send it
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.."]")
elseif awards.show_mode == "chat" then
-- 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
minetest.chat_send_player(name, desc)
end
@ -244,11 +214,14 @@ function awards.give_achievement(name, award)
end
end
-- Backwards compatibility
awards.give_achievement = awards.unlock
--[[minetest.register_chatcommand("gawd", {
params = "award name",
description = "gawd: give award to self",
func = function(name, param)
awards.give_achievement(name,param)
awards.unlock(name,param)
end
})]]--

View File

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

View File

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

View File

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

View File

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