This repository was archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoseCore_Events.lua
More file actions
131 lines (114 loc) · 3.42 KB
/
RoseCore_Events.lua
File metadata and controls
131 lines (114 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Rose.Events = {
moduleName = "Rose.Events",
}
local self = Rose.Events
self.currentACR = "None"
self.options = {
priorities = {
low = {
minimumTickLength = 1000,
},
medium = {
minimumTickLength = 500,
},
high = {
minimumTickLength = 250,
},
}
}
self.eventCache = {}
local function log (...)
local message = Rose.Debug.Console.parse(arg)
if(message ~= nil) then
if(message ~= nil and Rose.Debug ~= nil) then
if(Rose.Debug.Console.log ~= nil) then
Rose.Debug.Console.log(message, self.moduleName)
end
end
end
end
local function logError(...)
local message = Rose.Debug.Console.parse(arg)
if(#message > 0) then
if(message ~= nil and Rose.Debug ~= nil) then
if(Rose.Debug.Console.error ~= nil) then
Rose.Debug.Console.error(message, self.moduleName)
end
end
end
end
local function fire(eventName, eventArguments)
if(eventArguments ~= nil and #eventArguments > 0) then
QueueEvent(eventName, unpack(eventArguments))
else
QueueEvent(eventName, "")
end
end
local function validateEventArguments(eventArguments)
-- check event arguments table values are strings
local argVal = {}
for k, v in pairs(eventArguments) do
if(type(v) == 'string' or type(v) == 'table') then
argVal[k] = true
else
logError('Event arguments must be strings or tables. ' .. type(v) .. ' given.')
argVal[k] = false
end
end
for _, boolean in pairs(argVal) do
if(boolean == false) then
return false
end
end
return true
end
local function transposeToStrings(eventArguments)
local args = {}
for k, v in pairs(eventArguments) do
if(type(v) == 'table') then
args[k] = json.encode(v)
else
args[k] = v
end
end
return args
end
function self.dispatch(eventName, eventArguments, priority)
if(not validateEventArguments(eventArguments)) then
return
end
local transposedArguments = transposeToStrings(eventArguments)
if(priority == nil) then
priority = 'low'
end
local priorityTickLength = self.options.priorities[priority].minimumTickLength
local currentTick = Now()
if(self.eventCache[eventName] == nil) then
self.eventCache[eventName] = {
lastTick = currentTick,
}
fire(eventName, transposedArguments)
else
local eventCache = self.eventCache[eventName]
-- Last time this event was fired, was more than the minimum tick length ago
if(eventCache.lastTick + priorityTickLength < currentTick) then
eventCache.lastTick = currentTick
fire(eventName, transposedArguments)
end
end
end
function self.onInitialize(_, ...)
log('onInitialize')
end
function checkCurrentACR()
local activeACR = gACRSelectedProfiles[Player.job]
if(self.currentACR ~= activeACR) then
self.dispatch('Rose.Events.ACRChanged', {self.currentACR, activeACR, 'checkCurrentACR()'}, 'low')
end
self.currentACR = activeACR
end
function self.onUpdate(_, ...)
checkCurrentACR()
end
RegisterEventHandler("Module.Initalize", self.onInitialize, "Rose.Events.onInitialize")
RegisterEventHandler("Gameloop.Update", self.onUpdate, "Rose.Events.onUpdate")