Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly update to event_callbacks.lua style #3960

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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