-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.lua
343 lines (226 loc) · 7.72 KB
/
Memory.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
MemoryCore = StormwindLibrary_v1_12_2.new({
command = 'memoryaddon',
name = 'Memory',
version = '1.6.0',
})
local events = MemoryCore.events
--[[
Fires up the addon.
@since 0.1.0-alpha
]]
events:listen(events.EVENT_NAME_PLAYER_LOGIN, function ()
-- initializes the memory data set that stores all the players memories
if not MemoryAddon_DataSet then MemoryAddon_DataSet = {} end
-- initializes the settings saved variable that stores all the players settings
if not MemoryAddon_Settings then MemoryAddon_Settings = {} end
-- the addon name
MemoryCore.ADDON_NAME = 'Memory';
-- the default hex color used highlight text
MemoryCore.HIGHLIGHT_COLOR = 'ffee77';
-- the ArrayHelper instance
MemoryCore.arrayHelper = nil;
-- the CompatibilityHelper instance
MemoryCore.compatibilityHelper = nil;
-- the DateHelper instance
MemoryCore.dateHelper = nil;
-- the main event frame used to trigger all the Memory listeners
MemoryCore.eventFrame = CreateFrame( 'Frame' );
-- the memory event listeners that will add memories
MemoryCore.eventListeners = {};
-- the unique logger instance
MemoryCore.logger = nil;
-- the unique moment repository instance
MemoryCore.momentRepository = nil;
-- the unique repository instance
MemoryCore.repository = nil;
-- the unique screenshot controller instance
MemoryCore.screenshotController = nil;
-- the unique settings repository instance
MemoryCore.settingsRepository = nil;
-- the StringHelper instance
MemoryCore.stringHelper = nil;
-- the tooltip controller
MemoryCore.tooltipController = nil;
--[[
Attaches an event listener to the core.
Listeners will be triggered by player actions in the game.
@param MemoryAddon_MemoryEvent listener
]]
function MemoryCore:addEventListener( listener )
table.insert( self.eventListeners, listener );
for i, event in ipairs( listener.events ) do
-- removes the event to avoid registering them twice
MemoryCore.eventFrame:UnregisterEvent( event );
-- adds the event to the memory frame
MemoryCore.eventFrame:RegisterEvent( event );
end
end
--[[
Adds a moment to the player's memory.
@since 1.2.4
]]
function MemoryCore:addMoment( moment )
self:getMomentRepository():addMoment( moment );
MemoryCore:print( 'Current moment updated' );
end
--[[
Gets the unique array helper instance.
@since 0.4.0-alpha
@return MemoryAddon_ArrayHelper
]]
function MemoryCore:getArrayHelper()
return self.arrayHelper;
end
--[[
Gets the unique compatibility helper instance.
@since 0.4.0-alpha
@return MemoryAddon_CompatibilityHelper
]]
function MemoryCore:getCompatibilityHelper()
return self.compatibilityHelper;
end
--[[
Gets the unique date helper instance.
@since 0.6.0-beta
@return MemoryAddon_DateHelper
]]
function MemoryCore:getDateHelper()
return self.dateHelper;
end
--[[
Gets the unique logger helper instance.
@since 1.0.0
@return MemoryAddon_DateHelper
]]
function MemoryCore:getLogger()
return self.logger;
end
--[[
Gets the unique moment repository instance.
@since 1.1.0
@return MemoryAddon_MomentRepository
]]
function MemoryCore:getMomentRepository()
return self.momentRepository;
end
--[[
Gets the unique repository instance.
@since 0.2.0-alpha
@return MemoryAddon_MemoryRepository
]]
function MemoryCore:getRepository()
return self.repository;
end
--[[
Gets the unique screenshot controller instance.
@since 1.2.0
@return MemoryAddon_ScreenshotController
]]
function MemoryCore:getScreenshotController()
return self.screenshotController;
end
--[[
Gets the unique string helper instance.
@since 0.6.0-beta
@return MemoryAddon_StringHelper
]]
function MemoryCore:getStringHelper()
return self.stringHelper;
end
--[[
Gets the unique tooltip controller instance.
@since 1.1.0
@return MemoryAddon_TooltipController
]]
function MemoryCore:getTooltipController()
return self.tooltipController;
end
--[[
Highlights a string using the addon highlight color.
@since 0.1.0-alpha
@param string value
@param string hexColor accept any hexadecimal color to override the default highlight color (optional)
@return string
]]
function MemoryCore:highlight( value, --[[optional]] hexColor )
-- may use the default color if no hex color is informed
hexColor = hexColor or MemoryCore.HIGHLIGHT_COLOR;
return string.gsub( '\124cff' .. hexColor .. '{0}\124r', '{0}', value );
end
--[[
Initializes all the singleton instances in the MemoryCore object.
@since 0.1.0-alpha
]]
function MemoryCore:initializeSingletons()
self.arrayHelper = MemoryAddon_ArrayHelper:new();
self.compatibilityHelper = MemoryAddon_CompatibilityHelper:new();
self.dateHelper = MemoryAddon_DateHelper:new();
self.logger = MemoryAddon_LoggerHelper:new();
self.momentRepository = MemoryAddon_MomentRepository:new();
self.repository = MemoryAddon_MemoryRepository:new( UnitGUID( 'player' ), GetRealmName() );
self.screenshotController = MemoryAddon_ScreenshotController:new();
self.settingsRepository = MemoryAddon_SettingsRepository:new();
self.stringHelper = MemoryAddon_StringHelper:new();
self.tooltipController = MemoryAddon_TooltipController:new();
end
--[[
Prints a string in the default chat frame with the highlighted addon name as the prefix.
@since 0.1.0-alpha
@param string value
@param string prefix (optional)
]]
function MemoryCore:print( value, --[[optional]] prefix )
-- creates a default prefix if not informed
local prefix = prefix or MemoryCore:highlight( '<' .. MemoryCore.ADDON_NAME .. '>' );
DEFAULT_CHAT_FRAME:AddMessage( prefix .. ' ' .. value );
end
--[[
Prints the Memory addon version number.
@since 0.1.0-alpha
]]
function MemoryCore:printVersion()
MemoryCore:print( MemoryCore.addon.version );
end
--[[
Gets or sets a setting value.
@since 1.0.0
@param string key setting's key
@param mixed value the value to be set (optional)
@param bool override whether to replace the current value (optional)
@return mixed
]]
function MemoryCore:setting( key, --[[optional]] default, --[[optional]] override )
if override then return self.settingsRepository:set( key, default ); end
return self.settingsRepository:get( key, default );
end
--[[
Dispatch every registered events to the registered listeners.
@since 0.3.0-alpha
]]
MemoryCore.eventFrame:SetScript( 'OnEvent',
function( self, event, ... )
-- This weird code below was the only way I found to convert ... to an array
local params = {}; local param = nil; local counter = 1; repeat
param = select(counter, ...);
if nil ~= param then table.insert(params, param); end
counter = counter + 1;
until param == nil;
for i, listener in ipairs( MemoryCore.eventListeners ) do
-- dispatch the event to a listener
listener:maybeTrigger( event, params );
end
end
);
MemoryCore:initializeSingletons();
MemoryCore:printVersion();
-- adds the memory text formatter prototype to core
MemoryAddon_addMemoryTextFormatterPrototype( MemoryCore );
-- adds the player prototype to core
MemoryAddon_addPlayerPrototype( MemoryCore );
-- will add all event listeners to this core instance
MemoryAddon_addEvents( MemoryCore );
-- stores a symbolic memory (this is the first memory stored by the addon!)
MemoryCore:getRepository():store( 'misc', {}, 'login' );
-- prints a debug message confirming the end of the initialization
MemoryCore:getLogger():debug( 'MemoryCore initialized' );
end)