1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2024-12-28 19:50:37 +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

75
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]
@ -116,7 +105,7 @@ function awards.give_achievement(name, award)
return return
end end
if not awdef then if not awdef then
return return
end end
awards.tbv(data,"unlocked") awards.tbv(data,"unlocked")
@ -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,58 +1,59 @@
Light It Up = Achievement Unlocked: =
Place 100 torches. = Light It Up =
Light ALL The Things! = Place 100 torches. =
Place 1,000 torches. = Light ALL The Things! =
Saint-Maclou = Place 1,000 torches. =
Place 20 coal checkers. = Saint-Maclou =
Castorama = Place 20 coal checkers. =
Place 20 iron checkers. = Castorama =
Sam the Trapper = Place 20 iron checkers. =
Place 2 trap stones. = Sam the Trapper =
Obsessed with Obsidian = Place 2 trap stones. =
Mine 50 obsidian. = Obsessed with Obsidian =
On The Way = Mine 50 obsidian. =
Place 100 rails. = On The Way =
Lumberjack = Place 100 rails. =
Dig 100 tree blocks. = Lumberjack =
Semi-pro Lumberjack = Dig 100 tree blocks. =
Dig 1,000 tree blocks. = Semi-pro Lumberjack =
Professional Lumberjack = Dig 1,000 tree blocks. =
Dig 10,000 tree blocks. = Professional Lumberjack =
L33T Lumberjack = Dig 10,000 tree blocks. =
Dig 100,000 tree blocks. = L33T Lumberjack =
Junglebaby = Dig 100,000 tree blocks. =
Dig 100 jungle tree blocks. = Junglebaby =
Jungleman = Dig 100 jungle tree blocks. =
Dig 1,000 jungle tree blocks. = Jungleman =
First Mese Find = Dig 1,000 jungle tree blocks. =
Find some Mese. = First Mese Find =
You're a copper = Find some Mese. =
Dig 1,000 copper ores. = You're a copper =
YOU'RE A WINNER! = Dig 1,000 copper ores. =
Dig 1 mossy cobblestone. = YOU'RE A WINNER! =
OMG, Nyan Cat! = Dig 1 mossy cobblestone. =
Find a nyan cat. = OMG, Nyan Cat! =
Mini Miner = Find a nyan cat. =
Dig 100 stone blocks. = Mini Miner =
Hardened Miner = Dig 100 stone blocks. =
Dig 1,000 stone blocks = Hardened Miner =
Master Miner = Dig 1,000 stone blocks =
Dig 10,000 stone blocks. = Master Miner =
Marchand De Sable = Dig 10,000 stone blocks. =
Dig 1,000 sand. = Marchand De Sable =
Frequent Visitor = Dig 1,000 sand. =
Connect to the server 50 times. = Frequent Visitor =
Dying Spree = Connect to the server 50 times. =
Die 5 times. = Dying Spree =
Bot-like = Die 5 times. =
Die 10 times. = Bot-like =
You Suck! = Die 10 times. =
Die 100 times. = You Suck! =
You're a witch! = Die 100 times. =
Burn to death in a fire. = You're a witch! =
In the Flow = Burn to death in a fire. =
Die in flowing lava. = In the Flow =
This is Sad = Die in flowing lava. =
Die near diamond ore. = This is Sad =
The Stack = Die near diamond ore. =
Die near bones. = The Stack =
Die near bones. =

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)