Add register_trigger_counted for homogenous events

This commit is contained in:
rubenwardy 2018-04-04 16:03:34 +01:00
parent dd2a5d2d1f
commit 8b024accdd
4 changed files with 85 additions and 116 deletions

26
api.lua
View File

@ -54,6 +54,32 @@ function awards.register_trigger(name, tfunc)
end end
end end
-- Registers a trigger which replies on counting
function awards.register_trigger_counted(tname, tfunc)
awards.register_trigger(tname, tfunc)
local key = tname .. "s"
awards["notify_" .. tname] = function(player)
assert(player and player.is_player and player:is_player())
local name = player:get_player_name()
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data[key] = data[key] + 1
local currentVal = data[key]
awards.run_trigger_callbacks(player, data, tname, function(entry)
if entry.target and entry.award and currentVal and
currentVal >= entry.target then
return entry.award
end
end)
end
end
function awards.run_trigger_callbacks(player, data, trigger, table_func) function awards.run_trigger_callbacks(player, data, trigger, table_func)
for i = 1, #awards.on[trigger] do for i = 1, #awards.on[trigger] do
local res = nil local res = nil

View File

@ -1 +0,0 @@
Adds achievements to Minetest, and an API to register new ones.

View File

@ -2,6 +2,7 @@ name = awards
title = Achievements title = Achievements
author = rubenwardy author = rubenwardy
description = Adds achievements to Minetest, and an API to register new ones. description = Adds achievements to Minetest, and an API to register new ones.
license = LGPL 2.1 or later optional_depends = intllib,sfinv,unified_inventory,default,stairs,farming,dye,beds,wool,vessels,moreblocks,fire,flowers
license = LGPLv2.1+
forum = https://forum.minetest.net/viewtopic.php?t=4870 forum = https://forum.minetest.net/viewtopic.php?t=4870
version = 2.3.0 version = 2.3.0

View File

@ -118,63 +118,6 @@ awards.register_trigger("eat", function(def)
end end
end) end)
awards.register_trigger("death", function(def)
local tmp = {
award = def.name,
target = def.trigger.target,
}
table.insert(awards.on.death, tmp)
def.getProgress = function(self, data)
local itemcount = data.deaths or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 deaths", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Die.", "Die @1 times.", n, n)
end
end)
awards.register_trigger("chat", function(def)
local tmp = {
award = def.name,
target = def.trigger.target,
}
table.insert(awards.on.chat, tmp)
def.getProgress = function(self, data)
local itemcount = data.chats or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 chat messages", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Write something in chat.", "Write @1 chat messages.", n, n)
end
end)
awards.register_trigger("join", function(def)
local tmp = {
award = def.name,
target = def.trigger.target,
}
table.insert(awards.on.join, tmp)
def.getProgress = function(self, data)
local itemcount = data.joins or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 game joins", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Join the game.", "Join the game @1 times.", n, n)
end
end)
awards.register_trigger("craft", function(def) awards.register_trigger("craft", function(def)
local tmp = { local tmp = {
award = def.name, award = def.name,
@ -326,69 +269,69 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
end) end)
end) end)
minetest.register_on_dieplayer(function(player) awards.register_trigger_counted("death", function(def)
-- Run checks local tmp = {
local name = player:get_player_name() award = def.name,
if not player or not name or name=="" then target = def.trigger.target,
return }
table.insert(awards.on.death, tmp)
def.getProgress = function(self, data)
local itemcount = data.deaths or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 deaths", itemcount, tmp.target),
}
end end
def.getDefaultDescription = function(self)
-- Get player local n = self.trigger.target
awards.assertPlayer(name) return NS("Die.", "Die @1 times.", n, n)
local data = awards.players[name]
-- Increment counter
data.deaths = data.deaths + 1
awards.run_trigger_callbacks(player, data, "death", function(entry)
if entry.target and entry.award and data.deaths and
data.deaths >= entry.target then
return entry.award
end end
end) end)
end) minetest.register_on_dieplayer(awards.notify_death)
minetest.register_on_joinplayer(function(player) awards.register_trigger_counted("chat", function(def)
-- Run checks local tmp = {
local name = player:get_player_name() award = def.name,
if not player or not name or name=="" then target = def.trigger.target,
return }
table.insert(awards.on.chat, tmp)
def.getProgress = function(self, data)
local itemcount = data.chats or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 chat messages", itemcount, tmp.target),
}
end end
def.getDefaultDescription = function(self)
-- Get player local n = self.trigger.target
awards.assertPlayer(name) return NS("Write something in chat.", "Write @1 chat messages.", n, n)
local data = awards.players[name]
-- Increment counter
data.joins = data.joins + 1
awards.run_trigger_callbacks(player, data, "join", function(entry)
if entry.target and entry.award and data.joins and
data.joins >= entry.target then
return entry.award
end end
end) end)
end)
minetest.register_on_chat_message(function(name, message) minetest.register_on_chat_message(function(name, message)
-- Run checks local player = minetest.get_player_by_name(name)
local idx = string.find(message,"/") if not player or string.find(message, "/") then
if not name or (idx ~= nil and idx <= 1) then
return return
end end
-- Get player awards.notify_chat(player)
awards.assertPlayer(name) end)
local data = awards.players[name]
local player = minetest.get_player_by_name(name)
-- Increment counter awards.register_trigger_counted("join", function(def)
data.chats = data.chats + 1 local tmp = {
award = def.name,
awards.run_trigger_callbacks(player, data, "chat", function(entry) target = def.trigger.target,
if entry.target and entry.award and data.chats and }
data.chats >= entry.target then table.insert(awards.on.join, tmp)
return entry.award def.getProgress = function(self, data)
local itemcount = data.joins or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 game joins", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Join the game.", "Join the game @1 times.", n, n)
end end
end) end)
end) minetest.register_on_joinplayer(awards.notify_join)