-
Notifications
You must be signed in to change notification settings - Fork 1
/
State.lua
516 lines (475 loc) · 15.4 KB
/
State.lua
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
local ADDON_NAME, Internal = ...
local External = _G[ADDON_NAME]
local function tMap(tbl, func)
local result = {}
for k,v in pairs(tbl) do
result[k] = func(k, v, tbl)
end
return result
end
-- Base Mixin for State
local StateMixin = {}
function StateMixin:Init(id)
self.id = id;
end
function StateMixin:GetID()
return self.id;
end
function StateMixin:GetDisplayName(supportsCallback)
end
function StateMixin:GetUniqueKey()
-- Return a unique key for the states table so advanced scripts can access by key instead of by index
-- built in states use the [StateProvider id]:[State id] for example quest:63819
end
function StateMixin:SetCharacter(character)
self.character = character;
end
function StateMixin:GetCharacter()
if self.character then
return self.character
elseif self.driver then
return self.driver:GetCharacter()
else
return Internal.GetPlayer()
end
end
function StateMixin:SetDriver(driver)
self.driver = driver
end
-- Override this to register events to trigger updating completed and text
-- target might be a state driver or a ui element
-- isPlayer == true: Register events for updating the character that is online
-- isPlayer == true: Register events for updating for characters not online
-- isPlayer == nil: Register events for updating anyone
function StateMixin:RegisterEventsFor(target, isPlayer)
-- target:RegisterEvents("PLAYER_ENTERING_WORLD", "MY_CUSTOM_EVENT")
end
External.StateMixin = StateMixin
-- Base Mixin for State Providers
local StateProviderMixin = {}
function StateProviderMixin:Init(id, name, mixin)
self.id = id
self.name = name
self.mixin = mixin
end
function StateProviderMixin:GetID()
return self.id
end
function StateProviderMixin:GetName()
return self.name
end
function StateProviderMixin:RequiresID()
return true
end
-- Returns the title and optional description used for the config panel when adding a new state
function StateProviderMixin:GetAddTitle()
return string.format(BTWTODO_ADD_ITEM, self:GetName())
end
function StateProviderMixin:Acquire(...)
return CreateAndInitFromMixin(self.mixin, ...)
end
function StateProviderMixin:Supported(...)
return true
end
-- Returns data describing the possible basic functions
function StateProviderMixin:GetFunctions()
return {}
end
-- Returns the default functions for completed and text functions used for basic options
function StateProviderMixin:GetDefaults()
return nil, nil
end
function StateProviderMixin:ParseInput(input)
-- return true plus one or more values that can be passed to Aquire based on input
end
function StateProviderMixin:FillAutoComplete(tbl, text, offset, length)
-- Add items to tbl that are filted by text for auto completing adding an item, values can be passed to ParseInput
end
External.StateProviderMixin = StateProviderMixin
function External.CreateBasicStateProvider(id, name, mixin)
assert(type(id) == "string", "Usage: CreateBasicStateProvider(id, name, mixin): expected id to be string")
assert(type(name) == "string", "Usage: CreateBasicStateProvider(id, name, mixin): expected name to be string")
assert(type(mixin) == "table", "Usage: CreateBasicStateProvider(id, name, mixin): expected mixin to be table")
return CreateAndInitFromMixin(StateProviderMixin, id, name, mixin)
end
local stateProviders = {}
function External.RegisterStateProvider(provider)
stateProviders[provider:GetID()] = provider
Internal.TriggerEvent("REGISTER_STATE_PROVIDER")
end
local internalStateProviders = {}
function Internal.RegisterStateProvider(provider)
internalStateProviders[provider:GetID()] = provider
end
function Internal.GetStateProvider(provider)
assert(type(provider) == "string", "Usage: GetStateProvider(provider): expected provider to be string")
return internalStateProviders[provider] or stateProviders[provider]
end
function Internal.CreateState(provider, id, ...)
assert(type(provider) == "string", "Usage: CreateState(provider, id, ...): expected provider to be string")
local state
if internalStateProviders[provider] then
state = internalStateProviders[provider]:Acquire(id, ...)
elseif stateProviders[provider] then
state = stateProviders[provider]:Acquire(id, ...)
else
error("Usage: CreateState(provider, id, ...): provider " .. tostring(provider) .. " has not been registered")
end
return state
end
function Internal.IterateStateProviders()
local tbl = Mixin({}, stateProviders, internalStateProviders)
return next, tbl, nil
end
local CustomStateFunctions = {}
function Internal.RegisterCustomStateFunction(name, callback)
if type(name) ~= "string" then
error("Usage: RegisterCustomStateFunction(name, callback): name must be a string")
end
if CustomStateFunctions[name] ~= nil then
error("Usage: RegisterCustomStateFunction(name, callback): function with name \"" .. name .. "\" already registered")
end
CustomStateFunctions[name] = callback
end
local Colors = {
COMPLETE = CreateColor(0,1,0,1),
STALLED = CreateColor(1,1,0,1),
STARTED = ARTIFACT_GOLD_COLOR,
COMMON = COMMON_GRAY_COLOR,
UNCOMMON = UNCOMMON_GREEN_COLOR,
RARE = RARE_BLUE_COLOR,
EPIC = EPIC_PURPLE_COLOR,
LEGENDARY = LEGENDARY_ORANGE_COLOR,
ARTIFACT = ARTIFACT_GOLD_COLOR,
HEIRLOOM = HEIRLOOM_BLUE_COLOR,
WOWTOKEN = HEIRLOOM_BLUE_COLOR,
}
local Images = {
PADDING = [[|T982414:0|t]],
COMPLETE = "|A:achievementcompare-GreenCheckmark:0:0|a",
STALLED = "|A:achievementcompare-YellowCheckmark:0:0|a",
QUEST_PICKUP = "|A:QuestNormal:0:0|a",
QUEST_TURN_IN = "|A:QuestTurnin:0:0|a",
}
Internal.Images = Images
local EnvironmentMixin = {
print = print,
format = format,
ipairs = ipairs,
concat = table.concat,
select = select,
math = math,
tCount = function (tbl, func, from, to, every, ...)
from = from or 1
to = to or #tbl
every = every or 1
local result = 0
for i=from,to,every do
local item = tbl[i]
if item[func](item, ...) then
result = result + 1
end
end
return result
end,
tFirst = function (tbl, func, from, to, every, ...)
from = from or 1
to = to or #tbl
every = every or 1
for i=from,to,every do
local item = tbl[i]
if item[func](item, ...) then
return true, item
end
end
return false
end,
Custom = CustomStateFunctions,
Colors = Colors,
Images = Images,
table = table,
tFilter = tFilter,
tInvert = tInvert,
tMap = tMap,
GetMoneyString = GetMoneyString,
SecondsToTime = SecondsToTime,
IsAltKeyDown = IsAltKeyDown,
IsShiftKeyDown = IsShiftKeyDown,
IsControlKeyDown = IsControlKeyDown,
IsModifierKeyDown = IsModifierKeyDown,
IsLeftShiftKeyDown = IsLeftShiftKeyDown,
IsRightShiftKeyDown = IsRightShiftKeyDown,
}
local function CreateStateDriverFunction(driver, type, source, required, args)
if not required and not source then
return
end
local func, err = loadstring('local self, character, states' .. (args ~= nil and (', ' .. args) or '') .. ' = ...;' .. source, '[' .. driver:GetName() .. ':' .. type .. ']')
if not func then
return false, err
end
setfenv(func, CreateFromMixins(EnvironmentMixin))
return func
end
Internal.CreateStateDriverFunction = CreateStateDriverFunction
-- {3, "IsWeeklyCapped", 1, 2.3, "test"}
local function GenerateFunctionCall(tbl)
local values = {}
for i=3,#tbl do
local value = tbl[i]
if type(value) == "string" then
value = string.format("%q", value)
end
values[i] = value
end
return "states[" .. tbl[1] .. "]:" .. tbl[2] .. "(" .. table.concat(values, ", ") .. ")"
end
local function GenerateTextFunctionCalls(tbl)
local merger = "strjoin"
local arg = ", "
local index = 1
if type(tbl[index]) == "string" then
merger = tbl[index]
arg = tbl[index+1]
index = index + 2
end
local values = {}
for i=index,#tbl do
local value = tbl[i]
if type(value[1]) == "number" then
value = GenerateFunctionCall(value)
else
value = GenerateTextFunctionCalls(value)
end
values[#values+1] = value
end
return format("%s(%q, %s)", merger, arg, table.concat(values, ", "))
end
--[[
{
"and",
{1, "IsCapped"},
{2, "IsCapped"},
{
"or",
{3, "IsWeeklyCapped"},
{3, "IsCapped"},
}
}
]]
local function GenerateCompletedFunctionCalls(tbl)
local merger = "and"
local index = 1
if type(tbl[index]) == "string" then
merger = tbl[index]
index = index + 1
end
local values = {}
for i=index,#tbl do
local value = tbl[i]
if type(value[1]) == "number" then
value = GenerateFunctionCall(value)
else
value = "(" .. GenerateCompletedFunctionCalls(value) .. ")"
end
values[#values+1] = value
end
return table.concat(values, " " .. merger .. " ")
end
function Internal.GenerateFunctionFromTable(mode, tbl)
if mode == "completed" then
return "return " .. GenerateCompletedFunctionCalls(tbl)
else
return "return " .. GenerateTextFunctionCalls(tbl)
end
end
-- print(Internal.GenerateFunctionFromTable("completed", {
-- "and",
-- {1, "IsCapped"},
-- {2, "IsCapped"},
-- {
-- "or",
-- {3, "IsWeeklyCapped"},
-- {3, "IsCapped"},
-- }
-- }))
-- print(Internal.GenerateFunctionFromTable("text", {
-- "strjoin", ", ",
-- {1, "GetLevel", 1},
-- }))
local StateDriverMixin = CreateFromMixins(Internal.ScriptHandlerMixin)
function StateDriverMixin:Init(id, name, states, completed, text, click, tooltip)
Internal.ScriptHandlerMixin.OnLoad(self)
self:RegisterSupportedScriptHandlers("OnEvent")
self.id = id
self.name = name
self.states = states
for _,state in ipairs(states) do
state:SetDriver(self)
end
local err
self.completed, err = CreateStateDriverFunction(self, "Completed", completed, true)
if not self.completed then
error(err)
end
self.text, err = CreateStateDriverFunction(self, "Text", text, true, 'L')
if not self.text then
error(err)
end
self.click, err = CreateStateDriverFunction(self, "Click", click, false, 'button')
if self.click == false then
error(err)
end
self.tooltip, err = CreateStateDriverFunction(self, "Tooltip", tooltip, false, 'L, tooltip')
if self.tooltip == false then
error(err)
end
end
function StateDriverMixin:Deinit()
Internal.UnregisterEventsFor(self)
end
function StateDriverMixin:GetID()
return self.id
end
function StateDriverMixin:GetName()
return self.name
end
function StateDriverMixin:SetCharacter(character)
self.character = character
end
function StateDriverMixin:GetCharacter()
return self.character
end
function StateDriverMixin:SetFlaggedCompleted(value)
value = value and true or false
local character = self:GetCharacter()
assert(character ~= nil, "Call driver:SetCharacter before calling SetFlaggedCompleted")
character:SetData("todoFlaggedCompleted", self:GetID(), value and true or nil)
External.TriggerEvent("TODO_FLAGGED_COMPLETED", self:GetID(), value)
self:OnEvent("TODO_FLAGGED_COMPLETED", value) -- We dont actually register TODO_FLAGGED_COMPLETED so we will just trigger it manually here
end
function StateDriverMixin:IsFlaggedCompleted() -- Has the todo been clicked on
local character = self:GetCharacter()
assert(character ~= nil, "Call driver:SetCharacter before calling IsFlaggedCompleted")
return character:GetData("todoFlaggedCompleted", self:GetID())
end
function StateDriverMixin:IsCompleted()
local success, result = xpcall(function ()
return self.completed({
GetName = function ()
return self:GetName()
end,
IsFlaggedCompleted = function ()
return self:IsFlaggedCompleted()
end,
}, self:GetCharacter(), self.states) == true
end, geterrorhandler())
if success then
return result
else
return false
end
end
local SUCCESS_TEXT_WRAPPER = "|cff00ff00%s|r"
function StateDriverMixin:GetText()
local success, result = xpcall(function ()
local result = self.text({
GetName = function ()
return self:GetName()
end,
IsCompleted = function ()
return self:IsCompleted()
end,
IsFlaggedCompleted = function ()
return self:IsFlaggedCompleted()
end,
}, self:GetCharacter(), self.states, Internal.L)
if self:IsCompleted() then
return string.format(SUCCESS_TEXT_WRAPPER, result or "")
else
return result or ""
end
end, geterrorhandler())
if success then
return result
else
return ""
end
end
function StateDriverMixin:SupportsTooltip()
return self.tooltip ~= nil
end
function StateDriverMixin:UpdateTooltip(tooltip)
local success, result = xpcall(function ()
return self.tooltip({
GetName = function ()
return self:GetName()
end,
IsCompleted = function ()
return self:IsCompleted()
end,
IsFlaggedCompleted = function ()
return self:IsFlaggedCompleted()
end,
}, self:GetCharacter(), self.states, Internal.L, tooltip)
end, geterrorhandler())
if success then
return result
else
return false
end
end
function StateDriverMixin:SupportsClick()
return self.click ~= nil
end
function StateDriverMixin:Click(button)
xpcall(function ()
self.click({
GetName = function ()
return self:GetName()
end,
IsCompleted = function ()
return self:IsCompleted()
end,
SetFlaggedCompleted = function (_, ...)
return self:SetFlaggedCompleted(...)
end,
IsFlaggedCompleted = function ()
return self:IsFlaggedCompleted()
end,
}, self:GetCharacter(), self.states, button)
end, geterrorhandler())
end
function StateDriverMixin:OnEvent(...)
self:RunScript("OnEvent", ...)
end
function StateDriverMixin:RegisterEvents(...)
for i=1,select('#', ...) do
Internal.RegisterEvent(self, (select(i, ...)))
end
end
function StateDriverMixin:ClearEvents()
Internal.UnregisterEventsFor(self)
end
function StateDriverMixin:RegisterEventsFor(target, isPlayer)
for _,state in ipairs(self.states) do
state:RegisterEventsFor(target, isPlayer)
end
end
function Internal.CreateStateDriver(id, name, states, completed, text, click, tooltip)
local buildStates = {}
for index, source in ipairs(states) do
local state
if source.values then
state = Internal.CreateState(source.type, source.id, unpack(source.values))
else
state = Internal.CreateState(source.type, source.id)
end
local key = state:GetUniqueKey()
buildStates[index] = state
if key and not buildStates[key] then
buildStates[key] = state
end
end
return CreateAndInitFromMixin(StateDriverMixin, id, name, buildStates, completed, text, click, tooltip), buildStates
end