Skip to content

Commit

Permalink
Slightly update to event_callbacks.lua style (#3960)
Browse files Browse the repository at this point in the history
  • Loading branch information
EPuncker authored Feb 18, 2022
1 parent 27e2b46 commit 7f8d0d4
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions data/scripts/lib/event_callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ local pack = table.pack
local EventCallbackData, callbacks, updateableParameters, autoID = {}, {}, {}, 0
-- This metatable creates an auto-configuration mechanism to create new types of EventCallbacks
local ec = setmetatable({}, { __newindex = function (self, key, value)
autoID = autoID +1
autoID = autoID + 1
callbacks[key] = autoID
local info, update = {}, {}
for k, v in pairs(value) do if type(k)=="string" then info[k]=v else update[k]=v end end
for k, v in pairs(value) do
if type(k) == "string" then
info[k] = v
else
update[k] = v
end
end
updateableParameters[autoID] = update
callbacks[autoID] = info
EventCallbackData[autoID] = {maxn=0}
EventCallbackData[autoID] = {maxn = 0}
EVENT_CALLBACK_LAST = autoID
end})

Expand All @@ -29,10 +35,10 @@ ec.onDisband = {}
ec.onShareExperience = {}
-- Player
ec.onBrowseField = {}
ec.onLook = {[5]=1}
ec.onLookInBattleList = {[4]=1}
ec.onLookInTrade = {[5]=1}
ec.onLookInShop = {[4]=1}
ec.onLook = {[5] = 1}
ec.onLookInBattleList = {[4] = 1}
ec.onLookInTrade = {[5] = 1}
ec.onLookInShop = {[4] = 1}
ec.onLookInMarket = {}
ec.onTradeRequest = {}
ec.onTradeAccept = {}
Expand All @@ -43,9 +49,9 @@ ec.onMoveCreature = {}
ec.onReportRuleViolation = {}
ec.onReportBug = {}
ec.onTurn = {}
ec.onGainExperience = {[3]=1}
ec.onLoseExperience = {[2]=1}
ec.onGainSkillTries = {[3]=1}
ec.onGainExperience = {[3] = 1}
ec.onLoseExperience = {[2] = 1}
ec.onGainSkillTries = {[3] = 1}
ec.onWrapItem = {}
ec.onInventoryUpdate = {}
-- Monster
Expand All @@ -63,7 +69,7 @@ EventCallback = {
end

local eventData = EventCallbackData[eventType]
eventData.maxn = #eventData +1
eventData.maxn = #eventData + 1
eventData[eventData.maxn] = {
callback = callback,
triggerIndex = tonumber(triggerIndex) or 0
Expand All @@ -77,7 +83,7 @@ EventCallback = {
clear = function (self)
EventCallbackData = {}
for i = 1, EVENT_CALLBACK_LAST do
EventCallbackData[i] = {maxn=0}
EventCallbackData[i] = {maxn = 0}
end
end
}
Expand Down Expand Up @@ -126,13 +132,24 @@ setmetatable(EventCallback, {
results = {event.callback(unpack(args))}
local output = results[1]
-- If the call returns nil then we continue with the next call
if output == nil then break end
if output == nil then
break
end
-- If the call returns false then we exit the loop
if output == false then return false end
if output == false then
return false
end
-- If the call of type returnvalue returns noerror then we continue the loop
if info.returnValue then if output == RETURNVALUE_NOERROR then break end return output end
if info.returnValue then
if output == RETURNVALUE_NOERROR then
break
end
return output
end
-- We left the loop why have we reached the end
if index == eventData.maxn then return unpack(results) end
if index == eventData.maxn then
return unpack(results)
end
until true
-- Update the results for the next call
for index, value in pairs(updateableParameters[callback]) do
Expand Down

0 comments on commit 7f8d0d4

Please sign in to comment.