vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | Guild Event Manager by Kiki of European Cho'gall (Alliance) |
||
3 | Subscribers module (Sorting/Adding/Managing) |
||
4 | ]] |
||
5 | |||
6 | |||
7 | --------------- Local variables --------------- |
||
8 | |||
9 | GEM_SUB_Plugins = {}; |
||
10 | |||
11 | |||
12 | --------------- Internal functions --------------- |
||
13 | |||
14 | local function _GEM_SUB_IsTitular(ev_id,pl_name) |
||
15 | for i,tab in GEM_Events.realms[GEM_Realm].events[ev_id].titulars do |
||
16 | if(tab.name == pl_name) |
||
17 | then |
||
18 | return true; |
||
19 | end |
||
20 | end |
||
21 | return false; |
||
22 | end |
||
23 | |||
24 | local function _GEM_SUB_IsSubstitute(ev_id,pl_name) |
||
25 | for i,tab in GEM_Events.realms[GEM_Realm].events[ev_id].substitutes do |
||
26 | if(tab.name == pl_name) |
||
27 | then |
||
28 | return true; |
||
29 | end |
||
30 | end |
||
31 | return false; |
||
32 | end |
||
33 | |||
34 | local function _GEM_SUB_IsReplacement(ev_id,pl_name) |
||
35 | for i,tab in GEM_Events.realms[GEM_Realm].events[ev_id].replacements do |
||
36 | if(tab.name == pl_name) |
||
37 | then |
||
38 | return true; |
||
39 | end |
||
40 | end |
||
41 | return false; |
||
42 | end |
||
43 | |||
44 | |||
45 | --------------- Exported functions --------------- |
||
46 | |||
47 | function GEM_SUB_AddToReplacementList(ev_id,pl_name,pl_guild,pl_class,pl_level) |
||
48 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
49 | if(event == nil) |
||
50 | then |
||
51 | return; |
||
52 | end |
||
53 | table.insert(event.replacements,{name=pl_name,guild=pl_guild,class=pl_class,level=pl_level}); |
||
54 | event.classes[pl_class].repl_count = event.classes[pl_class].repl_count + 1; |
||
55 | GEM_COM_ReplacementPlayer(ev_id,pl_name); |
||
56 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_AddToReplacementList : Added "..pl_name.." to Replacement list"); |
||
57 | end |
||
58 | |||
59 | function GEM_SUB_RemoveFromReplacementList(ev_id,pl_name,pl_class) |
||
60 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
61 | if(event == nil) |
||
62 | then |
||
63 | return; |
||
64 | end |
||
65 | for i,tab in event.replacements do |
||
66 | if(tab.name == pl_name) |
||
67 | then |
||
68 | table.remove(event.replacements,i); |
||
69 | event.classes[pl_class].repl_count = event.classes[pl_class].repl_count - 1; |
||
70 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_RemoveFromReplacementList : Removed "..pl_name.." from replacement list"); |
||
71 | return; |
||
72 | end |
||
73 | end |
||
74 | end |
||
75 | |||
76 | function GEM_SUB_CreateSubscriber(ev_id,pl_stamp,pl_name,pl_class,pl_guild,pl_level,pl_comment,forcesub,forcetit) |
||
77 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
78 | if(event == nil) |
||
79 | then |
||
80 | return; |
||
81 | end |
||
82 | if(event.players[pl_name] ~= nil) |
||
83 | then |
||
84 | event.players[pl_name].update_time = pl_stamp; |
||
85 | event.players[pl_name].level = pl_level; |
||
86 | event.players[pl_name].comment = pl_comment; |
||
87 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_CreateSubscriber : Player "..pl_name.." already in my list. Updating stamp, level and comment"); |
||
88 | if(GEM_SUB_SortPlayers(ev_id)) |
||
89 | then |
||
90 | GEM_COM_NotifyEventUpdate(ev_id); -- Schedule an event update |
||
91 | end |
||
92 | return; |
||
93 | end |
||
94 | event.players[pl_name] = {}; |
||
95 | event.players[pl_name].class = pl_class; |
||
96 | event.players[pl_name].level = pl_level; |
||
97 | event.players[pl_name].update_time = pl_stamp; -- Leader side only |
||
98 | event.players[pl_name].guild = pl_guild; -- Leader side only |
||
99 | event.players[pl_name].comment = pl_comment; -- Leader side only |
||
100 | event.players[pl_name].forcesub = forcesub; -- Leader side only |
||
101 | event.players[pl_name].forcetit = forcetit; -- Leader side only |
||
102 | if(event.players[pl_name].forcetit == nil) |
||
103 | then |
||
104 | event.players[pl_name].forcetit = 0; |
||
105 | end |
||
106 | |||
107 | if(forcesub == 1) |
||
108 | then |
||
109 | GEM_SUB_AddToReplacementList(ev_id,pl_name,pl_guild,pl_class,pl_level); |
||
110 | else |
||
111 | GEM_SUB_SortPlayers(ev_id); -- Sort players |
||
112 | end |
||
113 | GEM_COM_NotifyEventUpdate(ev_id); -- Schedule an event update |
||
114 | end |
||
115 | |||
116 | function GEM_SUB_RemoveSubscriber(ev_id,pl_name,comment) |
||
117 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
118 | if(event == nil) |
||
119 | then |
||
120 | return; |
||
121 | end |
||
122 | if(event.players[pl_name] == nil) -- Unknown player |
||
123 | then |
||
124 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_RemoveSubscriber : Unknown player "..pl_name); |
||
125 | return; |
||
126 | end |
||
127 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_RemoveSubscriber : Removing player "..pl_name); |
||
128 | |||
129 | local pl_class = event.players[pl_name].class; |
||
130 | |||
131 | event.players[pl_name] = nil; -- Remove player |
||
132 | |||
133 | if(_GEM_SUB_IsReplacement(ev_id,pl_name)) -- Player was a replacement ? |
||
134 | then |
||
135 | GEM_SUB_RemoveFromReplacementList(ev_id,pl_name,pl_class); |
||
136 | else |
||
137 | GEM_SUB_SortPlayers(ev_id); -- Sort players |
||
138 | end |
||
139 | |||
140 | event.assistants[pl_name] = nil; -- Remove assistant status |
||
141 | GEM_COM_NotifyEventUpdate(ev_id); -- Schedule an event update |
||
142 | end |
||
143 | |||
144 | function GEM_SUB_SetTitular(ev_id,pl_stamp,pl_name) |
||
145 | if(GEM_Events.realms[GEM_Realm].events[ev_id] ~= nil) |
||
146 | then |
||
147 | if(GEM_Events.realms[GEM_Realm].subscribed[ev_id] ~= nil) |
||
148 | then |
||
149 | GEM_Events.realms[GEM_Realm].subscribed[ev_id].state = "1"; |
||
150 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_SetTitular : I'm now a TITULAR from EventID "..ev_id); |
||
151 | GEM_NotifyGUI(GEM_NOTIFY_MY_SUBSCRIPTION,ev_id); |
||
152 | end |
||
153 | end |
||
154 | end |
||
155 | |||
156 | function GEM_SUB_SetSubstitute(ev_id,pl_stamp,pl_name) |
||
157 | if(GEM_Events.realms[GEM_Realm].events[ev_id] ~= nil) |
||
158 | then |
||
159 | if(GEM_Events.realms[GEM_Realm].subscribed[ev_id] ~= nil) |
||
160 | then |
||
161 | GEM_Events.realms[GEM_Realm].subscribed[ev_id].state = "2"; |
||
162 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_SetSubstitute : I'm now a SUBSTITUTE from EventID "..ev_id); |
||
163 | GEM_NotifyGUI(GEM_NOTIFY_MY_SUBSCRIPTION,ev_id); |
||
164 | end |
||
165 | end |
||
166 | end |
||
167 | |||
168 | function GEM_SUB_SetReplacement(ev_id,pl_stamp,pl_name) |
||
169 | if(GEM_Events.realms[GEM_Realm].events[ev_id] ~= nil) |
||
170 | then |
||
171 | if(GEM_Events.realms[GEM_Realm].subscribed[ev_id] ~= nil) |
||
172 | then |
||
173 | GEM_Events.realms[GEM_Realm].subscribed[ev_id].state = "3"; |
||
174 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_SetReplacement : I'm now a REPLACEMENT from EventID "..ev_id); |
||
175 | GEM_NotifyGUI(GEM_NOTIFY_MY_SUBSCRIPTION,ev_id); |
||
176 | end |
||
177 | end |
||
178 | end |
||
179 | |||
180 | function GEM_SUB_CheckPlayersLevel(ev_id) |
||
181 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
182 | if(event == nil) |
||
183 | then |
||
184 | GEM_ChatWarning("GEM_SUB_CheckPlayersLevel : Trying to sort a nil event ("..ev_id..")"); |
||
185 | return; |
||
186 | end |
||
187 | for name,tab in event.players do |
||
188 | if(tab.level < event.min_lvl or tab.level > event.max_lvl) -- Not in level range anymore |
||
189 | then |
||
190 | GEM_COM_KickPlayer(ev_id,name,GEM_TEXT_ERR_LEVEL_FAILED); |
||
191 | end |
||
192 | end |
||
193 | end |
||
194 | |||
195 | function GEM_SUB_RegisterPlugin(plugin) |
||
196 | if(type(plugin) ~= "table") |
||
197 | then |
||
198 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin : Passed plugin is not a table"); |
||
199 | return; |
||
200 | end |
||
201 | if(plugin.Name == nil) |
||
202 | then |
||
203 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin : Passed plugin has no 'Name' parameter"); |
||
204 | return; |
||
205 | end |
||
206 | if(type(plugin.Name) ~= "string") |
||
207 | then |
||
208 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin : Passed plugin's 'Name' value is not a string"); |
||
209 | return; |
||
210 | end |
||
211 | local name = plugin.Name; |
||
212 | if(plugin.SortType == nil) |
||
213 | then |
||
214 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin '"..name.."' : Passed plugin has no 'SortType' parameter"); |
||
215 | return; |
||
216 | end |
||
217 | if(type(plugin.SortType) ~= "string") |
||
218 | then |
||
219 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin '"..name.."' : Passed plugin's 'SortType' value is not a string"); |
||
220 | return; |
||
221 | end |
||
222 | if(plugin.Sort == nil) |
||
223 | then |
||
224 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin '"..name.."' : Passed plugin has no 'Sort' function"); |
||
225 | return; |
||
226 | end |
||
227 | if(type(plugin.Sort) ~= "function") |
||
228 | then |
||
229 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin '"..name.."' : Passed plugin's 'Sort' value is not a function"); |
||
230 | return; |
||
231 | end |
||
232 | if(plugin.Configure and type(plugin.Configure) ~= "function") |
||
233 | then |
||
234 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin '"..name.."' : Passed plugin's 'Configure' value is not a function"); |
||
235 | return; |
||
236 | end |
||
237 | if(plugin.Help and type(plugin.Help) ~= "string") |
||
238 | then |
||
239 | GEM_ChatPrint("GEM_SUB_RegisterPlugin : Failed to register plugin '"..name.."' : Passed plugin's 'Help' value is not a string"); |
||
240 | return; |
||
241 | end |
||
242 | GEM_SUB_Plugins[name] = plugin; |
||
243 | GEM_ChatDebug(GEM_DEBUG_GLOBAL,"GEM_SUB_RegisterPlugin : Successfully loaded plugin '"..name.."'"); |
||
244 | end |
||
245 | |||
246 | function GEM_SUB_GetPlugin(sorttype) |
||
247 | for name,plugin in GEM_SUB_Plugins do |
||
248 | if(plugin.SortType == sorttype) |
||
249 | then |
||
250 | return plugin; |
||
251 | end |
||
252 | end |
||
253 | return nil; |
||
254 | end |
||
255 | |||
256 | function GEM_SUB_GetSortType(sortname) |
||
257 | if(sortname == nil) |
||
258 | then |
||
259 | return nil; |
||
260 | end |
||
261 | |||
262 | local plugin = GEM_SUB_Plugins[sortname]; |
||
263 | if(plugin ~= nil) |
||
264 | then |
||
265 | return plugin.SortType; |
||
266 | end |
||
267 | return nil; |
||
268 | end |
||
269 | |||
270 | |||
271 | --[[ |
||
272 | Main sorting function. |
||
273 | Returns True if order changed, false otherwise |
||
274 | ]] |
||
275 | function GEM_SUB_SortPlayers(ev_id) |
||
276 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
277 | if(event == nil) |
||
278 | then |
||
279 | GEM_ChatWarning("GEM_SUB_SortPlayers : Trying to sort a nil event ("..ev_id..")"); |
||
280 | return false; |
||
281 | end |
||
282 | local plugin = GEM_SUB_GetPlugin(event.sorttype); |
||
283 | |||
284 | if(plugin == nil) |
||
285 | then |
||
286 | GEM_ChatWarning("GEM_SUB_SortPlayers : Cannot find selected sorting plugin ("..event.sorttype..") !"); |
||
287 | return false; |
||
288 | end |
||
289 | local players = {}; |
||
290 | for name,tab in event.players do |
||
291 | if(tab.forcesub == 0) -- Only add non-forcesub players |
||
292 | then |
||
293 | players[name] = tab; |
||
294 | end |
||
295 | end |
||
296 | |||
297 | -- Build classes limits |
||
298 | local classes = {}; |
||
299 | for name,tab in event.classes do |
||
300 | local max = tab.max; |
||
301 | local min = tab.min; |
||
302 | if(max == nil or max == -1) |
||
303 | then |
||
304 | max = 666; |
||
305 | end |
||
306 | if(min == nil or min == -1) |
||
307 | then |
||
308 | min = 0; |
||
309 | end |
||
310 | classes[name] = { min=min,max=max,count=0 }; |
||
311 | end |
||
312 | |||
313 | local res_tit,res_sub = plugin.Sort(players,classes,event.max_count); |
||
314 | |||
315 | -- Check if order has changed |
||
316 | local update = false; |
||
317 | if(event.titular_count ~= table.getn(res_tit) or table.getn(event.substitutes) ~= table.getn(res_sub)) |
||
318 | then |
||
319 | update = true; |
||
320 | end |
||
321 | |||
322 | for name,tab in event.classes do |
||
323 | event.classes[name].tit_count = 0; |
||
324 | event.classes[name].sub_count = 0; |
||
325 | end |
||
326 | |||
327 | -- Rebuild the lists |
||
328 | local new_tit = {}; |
||
329 | for i,name in res_tit do |
||
330 | if(event.titulars[i] == nil or event.titulars[i].name ~= name) -- Not the same order, tag for update |
||
331 | then |
||
332 | update = true; |
||
333 | if(not _GEM_SUB_IsTitular(ev_id,name)) -- Was not a titular previously |
||
334 | then |
||
335 | GEM_COM_TitularPlayer(ev_id,name); |
||
336 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_SortPlayers : Added "..name.." to Titular list"); |
||
337 | end |
||
338 | end |
||
339 | local player = event.players[name]; |
||
340 | if(player == nil) |
||
341 | then |
||
342 | GEM_ChatWarning("GEM_SUB_SortPlayers : Failed to find titular player '"..name.."' in my players list !"); |
||
343 | else |
||
344 | table.insert(new_tit,{name=name,stamp=player.update_time,guild=player.guild,class=player.class,level=player.level}); |
||
345 | event.classes[player.class].tit_count = event.classes[player.class].tit_count + 1; |
||
346 | end |
||
347 | end |
||
348 | event.titulars = new_tit; |
||
349 | event.titular_count = table.getn(new_tit); |
||
350 | |||
351 | local new_sub = {}; |
||
352 | for i,name in res_sub do |
||
353 | if(event.substitutes[i] == nil or event.substitutes[i].name ~= name) -- Not the same order, tag for update |
||
354 | then |
||
355 | update = true; |
||
356 | if(not _GEM_SUB_IsSubstitute(ev_id,name)) -- Was not a substitute previously |
||
357 | then |
||
358 | GEM_COM_SubstitutePlayer(ev_id,name); |
||
359 | GEM_ChatDebug(GEM_DEBUG_SUBSCRIBERS,"GEM_SUB_SortPlayers : Added "..name.." to Substitute list"); |
||
360 | end |
||
361 | end |
||
362 | local player = event.players[name]; |
||
363 | if(player == nil) |
||
364 | then |
||
365 | GEM_ChatWarning("GEM_SUB_SortPlayers : Failed to find substitute player '"..name.."' in my players list !"); |
||
366 | else |
||
367 | table.insert(new_sub,{name=name,stamp=player.update_time,guild=player.guild,class=player.class,level=player.level}); |
||
368 | event.classes[player.class].sub_count = event.classes[player.class].sub_count + 1; |
||
369 | end |
||
370 | end |
||
371 | event.substitutes = new_sub; |
||
372 | |||
373 | return update; |
||
374 | end |
||
375 | |||
376 | function GEM_SUB_RecoverSubscribers(ev_id) |
||
377 | local event = GEM_Events.realms[GEM_Realm].events[ev_id]; |
||
378 | if(event == nil) |
||
379 | then |
||
380 | GEM_ChatWarning("GEM_SUB_RecoverSubscribers : Trying to recover a nil event ("..ev_id..")"); |
||
381 | return false; |
||
382 | end |
||
383 | local plugin = GEM_SUB_GetPlugin(event.sorttype); |
||
384 | |||
385 | if(plugin == nil) |
||
386 | then |
||
387 | GEM_ChatWarning("GEM_SUB_RecoverSubscribers : Cannot find selected sorting plugin ("..event.sorttype..") !"); |
||
388 | return false; |
||
389 | end |
||
390 | |||
391 | if(plugin.Recover == nil) |
||
392 | then |
||
393 | GEM_ChatWarning("GEM_SUB_RecoverSubscribers : Cannot find 'Recover' function for selected sorting plugin ("..event.sorttype..") !"); |
||
394 | return false; |
||
395 | end |
||
396 | |||
397 | -- Rebuild 'players' tab |
||
398 | for i,tab in event.titulars |
||
399 | do |
||
400 | if(event.players[tab.name] == nil) |
||
401 | then |
||
402 | event.players[tab.name] = {}; |
||
403 | event.players[tab.name].update_time = i+1; |
||
404 | event.players[tab.name].class = tab.class; |
||
405 | event.players[tab.name].guild = GEM_NA_FORMAT; |
||
406 | event.players[tab.name].level = tab.level; |
||
407 | event.players[tab.name].comment = ""; |
||
408 | event.players[tab.name].forcesub = 0; |
||
409 | end |
||
410 | end |
||
411 | for i,tab in event.substitutes |
||
412 | do |
||
413 | if(event.players[tab.name] == nil) |
||
414 | then |
||
415 | event.players[tab.name] = {}; |
||
416 | event.players[tab.name].update_time = event.titular_count + i + 1; |
||
417 | event.players[tab.name].class = tab.class; |
||
418 | event.players[tab.name].guild = GEM_NA_FORMAT; |
||
419 | event.players[tab.name].level = tab.level; |
||
420 | event.players[tab.name].comment = ""; |
||
421 | event.players[tab.name].forcesub = 0; |
||
422 | end |
||
423 | end |
||
424 | for i,tab in event.replacements |
||
425 | do |
||
426 | if(event.players[tab.name] == nil) |
||
427 | then |
||
428 | event.players[tab.name] = {}; |
||
429 | event.players[tab.name].update_time = 1; |
||
430 | event.players[tab.name].class = tab.class; |
||
431 | event.players[tab.name].guild = GEM_NA_FORMAT; |
||
432 | event.players[tab.name].level = tab.level; |
||
433 | event.players[tab.name].comment = ""; |
||
434 | event.players[tab.name].forcesub = 1; |
||
435 | end |
||
436 | end |
||
437 | |||
438 | -- Build classes limits |
||
439 | local classes = {}; |
||
440 | for name,tab in event.classes do |
||
441 | local max = tab.max; |
||
442 | local min = tab.min; |
||
443 | if(max == nil or max == -1) |
||
444 | then |
||
445 | max = 666; |
||
446 | end |
||
447 | if(min == nil or min == -1) |
||
448 | then |
||
449 | min = 0; |
||
450 | end |
||
451 | classes[name] = { min=min,max=max,count=0 }; |
||
452 | end |
||
453 | |||
454 | plugin.Recover(event.players,classes,event.max_count); |
||
455 | GEM_SUB_SortPlayers(ev_id); |
||
456 | |||
457 | return true; |
||
458 | end |