vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Name: Glory-2.0
3 Revision: $Rev: 11849 $
4 Author(s): ckknight (ckknight@gmail.com)
5 Elkano (elkano@gmx.de)
6 hyperactiveChipmunk (hyperactiveChipmunk@gmail.com)
7 Website: http://ckknight.wowinterface.com/
8 Documentation: http://wiki.wowace.com/index.php/Glory-2.0
9 SVN: http://svn.wowace.com/root/trunk/GloryLib/Glory-2.0
10 Description: A library for PvP and Battlegrounds.
11 Dependencies: AceLibrary, Babble-Zone-2.0, Deformat-2.0, AceEvent-2.0, AceConsole-2.0 (optional)
12  
13 Notes: To use this library, the per-character saved variable Glory2DB must be available.
14 ]]
15  
16 local MAJOR_VERSION = "Glory-2.0"
17 local MINOR_VERSION = "$Revision: 11849 $"
18  
19 if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary") end
20 if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
21  
22 if not AceLibrary:HasInstance("Babble-Zone-2.0") then error(MAJOR_VERSION .. " requires Babble-Zone-2.0") end
23 if not AceLibrary:HasInstance("Deformat-2.0") then error(MAJOR_VERSION .. " requires Deformat-2.0") end
24 if not AceLibrary:HasInstance("AceEvent-2.0") then error(MAJOR_VERSION .. " requires AceEvent-2.0") end
25  
26 local table_setn
27 do
28 local version = GetBuildInfo()
29 if string.find(version, "^2%.") then
30 -- 2.0.0
31 table_setn = function() end
32 else
33 table_setn = table.setn
34 end
35 end
36  
37 local math_mod = math.fmod or math.mod
38  
39 local new, del
40 do
41 local list = setmetatable({}, {__mode="k"})
42 function new()
43 local t = next(list)
44 if t then
45 list[t] = nil
46 return t
47 else
48 return {}
49 end
50 end
51 function del(t)
52 setmetatable(t, nil)
53 for k in pairs(t) do
54 t[k] = nil
55 end
56 table_setn(t, 0)
57 list[t] = true
58 end
59 end
60  
61 local PATTERN_HORDE_FLAG_PICKED_UP, PATTERN_HORDE_FLAG_DROPPED, PATTERN_HORDE_FLAG_CAPTURED, PATTERN_ALLIANCE_FLAG_PICKED_UP, PATTERN_ALLIANCE_FLAG_DROPPED, PATTERN_ALLIANCE_FLAG_CAPTURED, FACTION_DEFILERS, FACTION_FROSTWOLF_CLAN, FACTION_WARSONG_OUTRIDERS, FACTION_LEAGUE_OF_ARATHOR, FACTION_STORMPIKE_GUARD, FACTION_SILVERWING_SENTINELS
62  
63 local PATTERN_GWSUII_SCORE, PATTERN_GWSUII_BASES
64 local BGObjectiveDescriptions, BGChatAnnouncements, BGPatternReplacements, BGAcronyms, BattlefieldZoneObjectiveTimes, BattlefieldZoneResourceData --hC
65  
66 local deformat = AceLibrary("Deformat-2.0")
67 local Z = AceLibrary("Babble-Zone-2.0")
68 local AceEvent = AceLibrary("AceEvent-2.0")
69  
70 local WARSONG_GULCH = Z["Warsong Gulch"]
71 local ALTERAC_VALLEY = Z["Alterac Valley"]
72 local ARATHI_BASIN = Z["Arathi Basin"]
73  
74 local locale = GetLocale()
75 if locale ~= "deDE" then
76 locale = "enUS"
77 end
78  
79 if locale == "enUS" then
80 PATTERN_HORDE_FLAG_PICKED_UP = "The Horde [Ff]lag was picked up by ([^!]+)!"
81 PATTERN_HORDE_FLAG_DROPPED = "The Horde [Ff]lag was dropped by (%a+)!"
82 PATTERN_HORDE_FLAG_CAPTURED = "(%a+) captured the Horde [Ff]lag!"
83 PATTERN_ALLIANCE_FLAG_PICKED_UP = "The Alliance [Ff]lag was picked up by (%a+)!"
84 PATTERN_ALLIANCE_FLAG_DROPPED = "The Alliance [Ff]lag was dropped by (%a+)!"
85 PATTERN_ALLIANCE_FLAG_CAPTURED = "(%a+) captured the Alliance [Ff]lag!"
86  
87 FACTION_DEFILERS = "Defilers"
88 FACTION_FROSTWOLF_CLAN = "Frostwolf Clan"
89 FACTION_WARSONG_OUTRIDERS = "Warsong Outriders"
90  
91 FACTION_LEAGUE_OF_ARATHOR = "League of Arathor"
92 FACTION_STORMPIKE_GUARD = "Stormpike Guard"
93 FACTION_SILVERWING_SENTINELS = "Silverwing Sentinels"
94  
95 BGObjectiveDescriptions = {
96 ALLIANCE_CONTROLLED = "Alliance Controlled",
97 HORDE_CONTROLLED = "Horde Controlled",
98 IN_CONFLICT = "In Conflict",
99 UNCONTROLLED = "Uncontrolled",
100 DESTROYED = "Destroyed",
101 }
102  
103 BGChatAnnouncements = {
104 BGObjectiveClaimedAnnouncements = {
105 PATTERN_OBJECTIVE_CLAIMED_AB = "claims the ([%w ]+).* (%a+) will control",
106 },
107  
108 BGObjectiveAttackedAnnouncements = {
109 PATTERN_OBJECTIVE_ATTACKED_AB = "assaulted the ([%w ]+)",
110 PATTERN_OBJECTIVE_ATTACKED_AV0 = "The ([%w ]+) is under attack",
111 PATTERN_OBJECTIVE_ATTACKED_AV1 = "^([%w ]+) is under attack",
112 },
113  
114 BGObjectiveDefendedAnnouncements = {
115 PATTERN_OBJECTIVE_DEFENDED_AB = "defended the ([%w ]+)",
116 },
117  
118 BGObjectiveCapturedAnnouncements = {
119 PATTERN_OBJECTIVE_CAPTURED_AB = "The (%a+) has taken the ([%w ]+)",
120 PATTERN_OBJECTIVE_CAPTURED_AV0 = "The ([%w ]+) was taken by the (%a+)",
121 PATTERN_OBJECTIVE_CAPTURED_AV1 = "^([%w ]+) was taken by the (%a+)",
122 PATTERN_OBJECTIVE_CAPTURED_AV2 = "the ([%w ]+) is.*MINE", --and the Irondeep Mine is...MINE!
123 PATTERN_OBJECTIVE_CAPTURED_AV3 = "claims the ([%w ]+)", --Snivvle claims the Coldtooth Mine!
124 },
125  
126 BGObjectiveDestroyedAnnouncements = {
127 PATTERN_OBJECTIVE_DESTROYED_AV0 = "The ([%w ]+) was destroyed",
128 PATTERN_OBJECTIVE_DESTROYED_AV1 = "^([%w ]+) was destroyed",
129 },
130 }
131  
132 BGPatternReplacements = {
133 ["mine"] = "gold mine",
134 ["southern farm"] = "farm"
135 }
136  
137 BGAcronyms = {
138 [ALTERAC_VALLEY] = "AV",
139 [ARATHI_BASIN] = "AB",
140 [WARSONG_GULCH] = "WSG",
141 }
142  
143 PATTERN_GWSUII_SCORE = "(%d+/%d+)" --for lifting the score out of the first return value of GetWorldStateUIInfo(index)
144 PATTERN_GWSUII_BASES = "Bases: (%d+)" --for lifting the number of bases held in Arathi Basin
145 PATTERN_GWSUII_RESOURCES = "Resources: (%d+)" --for lifting the number of bases held in Arathi Basin
146 PATTERN_OBJECTIVE_HOLDER = "([%w ]+) Controlled"
147  
148 elseif locale == "deDE" then
149 PATTERN_HORDE_FLAG_PICKED_UP = "([^!]+) hat die [Ff]lagge der Horde aufgenommen!"
150 PATTERN_HORDE_FLAG_DROPPED = "(%a+) hat die [Ff]lagge der Horde fallen lassen!"
151 PATTERN_HORDE_FLAG_CAPTURED = "(%a+) hat die [Ff]lagge der Horde errungen!"
152 PATTERN_ALLIANCE_FLAG_PICKED_UP = "(%a+) hat die [Ff]lagge der Allianz aufgenommen!"
153 PATTERN_ALLIANCE_FLAG_DROPPED = "(%a+) hat die [Ff]lagge der Allianz fallen lassen!"
154 PATTERN_ALLIANCE_FLAG_CAPTURED = "(%a+) hat die [Ff]lagge der Allianz errungen!"
155  
156 FACTION_DEFILERS = "Die Entweihten"
157 FACTION_FROSTWOLF_CLAN = "Frostwolfklan"
158 FACTION_WARSONG_OUTRIDERS = "Warsongvorhut"
159  
160 FACTION_LEAGUE_OF_ARATHOR = "Liga von Arathor"
161 FACTION_STORMPIKE_GUARD = "Stormpike Garde"
162 FACTION_SILVERWING_SENTINELS = "Silverwing Schildwache"
163  
164 BGObjectiveDescriptions = {
165 ALLIANCE_CONTROLLED = "Kontrolliert von der Allianz",
166 HORDE_CONTROLLED = "Kontrolliert von der Horde",
167 IN_CONFLICT = "Umk\195\164mpft",
168 UNCONTROLLED = "Unkontrolliert",
169 DESTROYED = "Zerst\195\182rt",
170 }
171  
172 BGChatAnnouncements = {
173 BGObjectiveClaimedAnnouncements = {
174 PATTERN_OBJECTIVE_CLAIMED_AB0 = "hat das (.+) besetzt.* die (%a+) in",
175 PATTERN_OBJECTIVE_CLAIMED_AB1 = "hat den (.+) besetzt.* die (%a+) in",
176 PATTERN_OBJECTIVE_CLAIMED_AB2 = "hat die (.+) besetzt.* die (%a+) in",
177 PATTERN_OBJECTIVE_CLAIMED_AB3 = "hat (S\195\164gewerk) besetzt.* die (%a+) in",
178 PATTERN_OBJECTIVE_CLAIMED_AV0 = "hat den (.+) besetzt.* erlangt die (%a+) die Kontrolle"
179 },
180  
181 BGObjectiveAttackedAnnouncements = {
182 PATTERN_OBJECTIVE_ATTACKED_AB0 = "das (.+) angegriffen",
183 PATTERN_OBJECTIVE_ATTACKED_AB1 = "den (.+) angegriffen",
184 PATTERN_OBJECTIVE_ATTACKED_AB2 = "die (.+) angegriffen",
185 PATTERN_OBJECTIVE_ATTACKED_AV0 = "Das (.+) wird angegriffen.*wird die (%a+) es",
186 PATTERN_OBJECTIVE_ATTACKED_AV1 = "Der (.+) wird angegriffen.*wird die (%a+) ihn",
187 PATTERN_OBJECTIVE_ATTACKED_AV2 = "Die (.+) wird angegriffen.*wird die (%a+) sie",
188 },
189  
190 BGObjectiveDefendedAnnouncements = {
191 PATTERN_OBJECTIVE_DEFENDED_AB0 = "das (.+) verteidigt",
192 PATTERN_OBJECTIVE_DEFENDED_AB1 = "den (.+) verteidigt",
193 PATTERN_OBJECTIVE_DEFENDED_AB2 = "die (.+) verteidigt",
194 },
195  
196 BGObjectiveCapturedAnnouncements = {
197 PATTERN_OBJECTIVE_CAPTURED_AB0 = "Die (%a+) hat das (.+) eingenommen",
198 PATTERN_OBJECTIVE_CAPTURED_AB1 = "Die (%a+) hat den (.+) eingenommen",
199 PATTERN_OBJECTIVE_CAPTURED_AB2 = "Die (%a+) hat die (.+) eingenommen",
200 PATTERN_OBJECTIVE_CAPTURED_AV0 = "Das (.+) wurde von der (%a+) erobert",
201 PATTERN_OBJECTIVE_CAPTURED_AV1 = "Der (.+) wurde von der (%a+) erobert",
202 PATTERN_OBJECTIVE_CAPTURED_AV2 = "geh\195\182rt jetzt die (.+)!",
203 },
204  
205 BGObjectiveDestroyedAnnouncements = {
206 PATTERN_OBJECTIVE_DESTROYED_AV0 = "Der (.+) wurde von der (%a+) zerst\195\182rt",
207 },
208 }
209  
210 BGPatternReplacements = {
211 ["Schmiede"] = "Schmied",
212 ["Mine"] = "Goldmine",
213 ["s\195\188dlichen Hof"] = "Hof",
214 }
215  
216 BGAcronyms = {
217 [ALTERAC_VALLEY] = "AV", -- CHECK
218 [ARATHI_BASIN] = "AB", -- CHECK
219 [WARSONG_GULCH] = "WSG", -- CHECK
220 }
221  
222 PATTERN_GWSUII_SCORE = "(%d+/%d+)" --for lifting the score out of the first return value of GetWorldStateUIInfo(index)
223 PATTERN_GWSUII_BASES = "Basen: (%d+)" --for lifting the number of bases held in Arathi Basin
224 PATTERN_GWSUII_RESOURCES = "Ressourcen: (%d+)" --for lifting the number of bases held in Arathi Basin -- CHECK
225 PATTERN_OBJECTIVE_HOLDER = "Kontrolliert von der ([%w ]+)"
226 end
227  
228 BattlefieldZoneObjectiveTimes = {
229 [ARATHI_BASIN] = 62.5,
230 [ALTERAC_VALLEY] = 302.5,
231 }
232  
233 BattlefieldZoneResourceData = {
234 [ARATHI_BASIN] = { [0]=0, 5/6, 10/9, 5/3, 10/3, 30, 2000 }
235 }
236  
237 local Glory = {}
238 local events = {}
239  
240 AceEvent:embed(events)
241  
242 local _,race = UnitRace("player")
243 local isHorde = (race == "Orc" or race == "Troll" or race == "Tauren" or race == "Scourge")
244 local playerName = UnitName("player")
245 local playerRealm = GetRealmName()
246  
247 local enemyList = {}
248  
249 local function CheckNewWeek(self)
250 local _,_,lastWeekHonor,_ = GetPVPLastWeekStats()
251 if lastWeekHonor ~= self.data.lastWeek then
252 self.data.lastWeek = lastWeekHonor
253 events:TriggerEvent("Glory_NewWeek")
254 end
255 end
256  
257 local function CheckNewDay(self)
258 local _,_,yesterdayHonor = GetPVPYesterdayStats()
259 local lifetimeHK,_,_ = GetPVPLifetimeStats()
260 if yesterdayHonor ~= self.data.yesterday and not (yesterdayHonor == 0 and lifetimeHK == 0) then
261 self.data.yesterday = yesterdayHonor
262 self.data.hks = {}
263 self.data.todayHK = 0
264 self.data.todayHKHonor = 0
265 self.data.todayBonusHonor = 0
266 self.data.todayDeaths = 0
267 events:TriggerEvent("Glory_NewDay")
268 end
269 end
270  
271 local function IncreaseHKs(self, person)
272 self.data.todayHK = self.data.todayHK + 1
273 self.data.hks[person] = (self.data.hks[person] or 0) + 1
274 return self.data.hks[person]
275 end
276  
277 local function IncreaseHKHonor(self, amount)
278 self.data.todayHKHonor = self.data.todayHKHonor + amount
279 end
280  
281 local function IncreaseBonusHonor(self, amount)
282 self.data.todayBonusHonor = self.data.todayBonusHonor + amount
283 end
284  
285 local function IncreaseBattlegroundsWins(self)
286 if self:IsInAlteracValley() then
287 self.data.avWin = self.data.avWin + 1
288 events:TriggerEvent("Glory_BGWinAV")
289 elseif self:IsInArathiBasin() then
290 self.data.abWin = self.data.abWin + 1
291 events:TriggerEvent("Glory_BGWinAB")
292 else
293 self.data.wsgWin = self.data.wsgWin + 1
294 events:TriggerEvent("Glory_BGWinWSG")
295 end
296 events:TriggerEvent("Glory_BGWin")
297 end
298  
299 local function IncreaseBattlegroundsLosses(self)
300 if self:IsInAlteracValley() then
301 self.data.avLoss = self.data.avLoss + 1
302 events:TriggerEvent("Glory_BGLossAV")
303 elseif self:IsInArathiBasin() then
304 self.data.abLoss = self.data.abLoss + 1
305 events:TriggerEvent("Glory_BGLossAB")
306 else
307 self.data.wsgLoss = self.data.wsgLoss + 1
308 events:TriggerEvent("Glory_BGLossWSG")
309 end
310 events:TriggerEvent("Glory_BGLoss")
311 end
312  
313 local function IncreaseDeaths(self)
314 self.data.todayDeaths = self.data.todayDeaths + 1
315 events:TriggerEvent("Glory_Death")
316 end
317  
318 local db
319  
320 local function VerifyData(self)
321 if not self.data then
322 if type(Glory2DB) ~= "table" then
323 Glory2DB = {}
324 end
325 db = Glory2DB
326 if type(db[MAJOR_VERSION]) ~= "table" then
327 db[MAJOR_VERSION] = {}
328 end
329 self.data = db[MAJOR_VERSION]
330 elseif db ~= Glory2DB then
331 local old = db
332 local new = Glory2DB
333 if type(new) ~= "table" then
334 Glory2DB = old
335 else
336 for k in pairs(old) do
337 if not new[k] then
338 new[k] = old[k]
339 elseif new[k].time == nil then
340 new[k] = old[k]
341 elseif old[k].time == nil then
342 -- keep new
343 elseif new[k].time < old[k].time then
344 new[k] = old[k]
345 end
346 end
347 db = new
348 self.data = db[MAJOR_VERSION]
349 end
350 end
351 if not self.data.hks then self.data.hks = {} end
352 if not self.data.todayDeaths then self.data.todayDeaths = 0 end
353 if not self.data.todayHK then self.data.todayHK = 0 end
354 if not self.data.todayHKHonor then self.data.todayHKHonor = 0 end
355 if not self.data.todayBonusHonor then self.data.todayBonusHonor = 0 end
356 if not self.data.wsgWin then self.data.wsgWin = 0 end
357 if not self.data.wsgLoss then self.data.wsgLoss = 0 end
358 if not self.data.abWin then self.data.abWin = 0 end
359 if not self.data.abLoss then self.data.abLoss = 0 end
360 if not self.data.avWin then self.data.avWin = 0 end
361 if not self.data.avLoss then self.data.avLoss = 0 end
362 if not self.data.yesterday then self.data.yesterday = 0 end
363 if not self.data.lastWeek then self.data.lastWeek = 0 end
364  
365 CheckNewDay(self)
366 CheckNewWeek(self)
367 events:UNIT_PVP_UPDATE()
368 end
369  
370 function events:ADDON_LOADED()
371 VerifyData(Glory)
372 end
373  
374 function events:VARIABLES_LOADED()
375 VerifyData(Glory)
376 end
377  
378 function events:PLAYER_LOGOUT()
379 Glory.data.time = time()
380 end
381  
382 function events:CHAT_MSG_COMBAT_HONOR_GAIN(text)
383 CheckNewDay(Glory)
384 local name, rank, honor = deformat(text, COMBATLOG_HONORGAIN)
385 if name then
386 local realm = enemyList[name] or playerRealm
387 if realm ~= playerRealm then
388 name = name .. "-" .. realm
389 end
390 local kills = IncreaseHKs(Glory, name)
391 local factor
392 factor = (11 - kills) / 10
393 if factor < 0 then
394 factor = 0
395 end
396 local realHonor = ceil(honor * factor)
397 IncreaseHKHonor(Glory, realHonor)
398 events:TriggerEvent("Glory_GainHK", rank, name, realHonor, kills)
399 return
400 end
401  
402 local bonus = deformat(text, COMBATLOG_HONORAWARD)
403 if bonus then
404 bonus = tonumber(bonus)
405 IncreaseBonusHonor(Glory, bonus)
406 events:TriggerEvent("Glory_GainBonusHonor", bonus)
407 end
408 end
409  
410 function events:CHAT_MSG_BG_SYSTEM_NEUTRAL(text)
411 if string.find(string.lower(text), string.lower(VICTORY_TEXT0)) then
412 if isHorde then
413 IncreaseBattlegroundsWins(Glory)
414 else
415 IncreaseBattlegroundsLosses(Glory)
416 end
417 elseif string.find(string.lower(text), string.lower(VICTORY_TEXT1)) then
418 if not isHorde then
419 IncreaseBattlegroundsWins(Glory)
420 else
421 IncreaseBattlegroundsLosses(Glory)
422 end
423 end
424 end
425  
426 function events:CHAT_MSG_BG_SYSTEM_HORDE(text)
427 if Glory:IsInWarsongGulch() then
428 local _, _, hordeFC = string.find(text, PATTERN_ALLIANCE_FLAG_PICKED_UP)
429 if hordeFC then
430 Glory.hordeFC = hordeFC
431 events:TriggerEvent("Glory_AllianceFlagPickedUp", Glory.hordeFC)
432 events:TriggerEvent("Glory_AllianceFlagCarrierUpdate", Glory.hordeFC)
433 if not isHorde then
434 events:TriggerEvent("Glory_FriendlyFlagPickedUp", Glory.hordeFC)
435 events:TriggerEvent("Glory_FriendlyFlagCarrierUpdate", Glory.hordeFC)
436 else
437 events:TriggerEvent("Glory_HostileFlagPickedUp", Glory.hordeFC)
438 events:TriggerEvent("Glory_HostileFlagCarrierUpdate", Glory.hordeFC)
439 end
440 return
441 end
442  
443 if string.find(text, PATTERN_ALLIANCE_FLAG_CAPTURED) then
444 local hordeFC = Glory.hordeFC
445 Glory.allianceFC = nil
446 Glory.hordeFC = nil
447 events:TriggerEvent("Glory_AllianceFlagCaptured", hordeFC)
448 if not isHorde then
449 events:TriggerEvent("Glory_FriendlyFlagCaptured", hordeFC)
450 else
451 events:TriggerEvent("Glory_HostileFlagCaptured", hordeFC)
452 end
453 events:TriggerEvent("Glory_FriendlyFlagCarrierUpdate", nil)
454 events:TriggerEvent("Glory_HostileFlagCarrierUpdate", nil)
455 events:TriggerEvent("Glory_AllianceFlagCarrierUpdate", nil)
456 events:TriggerEvent("Glory_HordeFlagCarrierUpdate", nil)
457 return
458 end
459  
460 if string.find(text, PATTERN_HORDE_FLAG_DROPPED) then
461 local allianceFC = Glory.allianceFC
462 Glory.allianceFC = nil
463 events:TriggerEvent("Glory_HordeFlagDropped", allianceFC)
464 if isHorde then
465 events:TriggerEvent("Glory_FriendlyFlagDropped", allianceFC)
466 events:TriggerEvent("Glory_HostileFlagCarrierUpdate", nil)
467 else
468 events:TriggerEvent("Glory_HostileFlagDropped", allianceFC)
469 events:TriggerEvent("Glory_FriendlyFlagCarrierUpdate", nil)
470 end
471 return
472 end
473 elseif Glory:IsInArathiBasin() or Glory:IsInAlteracValley() then
474 events:BattlefieldObjectiveEventProcessing(text)
475 end
476 end
477  
478 function events:CHAT_MSG_BG_SYSTEM_ALLIANCE(text)
479 if Glory:IsInWarsongGulch() then
480 local _, _, allianceFC = string.find(text, PATTERN_HORDE_FLAG_PICKED_UP)
481 if allianceFC then
482 Glory.allianceFC = allianceFC
483 events:TriggerEvent("Glory_HordeFlagPickedUp", Glory.allianceFC)
484 if isHorde then
485 events:TriggerEvent("Glory_FriendlyFlagPickedUp", Glory.allianceFC)
486 events:TriggerEvent("Glory_HostileFlagCarrierUpdate", Glory.allianceFC)
487 else
488 events:TriggerEvent("Glory_HostileFlagPickedUp", Glory.allianceFC)
489 events:TriggerEvent("Glory_FriendlyFlagCarrierUpdate", Glory.allianceFC)
490 end
491 return
492 end
493  
494 if string.find(text, PATTERN_HORDE_FLAG_CAPTURED) then
495 local alliance = Glory.allianceFC
496 Glory.allianceFC = nil
497 Glory.hordeFC = nil
498 events:TriggerEvent("Glory_HordeFlagCaptured", allianceFC)
499 if isHorde then
500 events:TriggerEvent("Glory_FriendlyFlagCaptured", allianceFC)
501 else
502 events:TriggerEvent("Glory_HostileFlagCaptured", allianceFC)
503 end
504 events:TriggerEvent("Glory_FriendlyFlagCarrierUpdate", nil)
505 events:TriggerEvent("Glory_HostileFlagCarrierUpdate", nil)
506 events:TriggerEvent("Glory_AllianceFlagCarrierUpdate", nil)
507 events:TriggerEvent("Glory_HordeFlagCarrierUpdate", nil)
508 return
509 end
510  
511 if string.find(text, PATTERN_ALLIANCE_FLAG_DROPPED) then
512 local hordeFC = Glory.hordeFC
513 Glory.hordeFC = nil
514 events:TriggerEvent("Glory_AllianceFlagDropped", hordeFC)
515 if not isHorde then
516 events:TriggerEvent("Glory_FriendlyFlagDropped", hordeFC)
517 events:TriggerEvent("Glory_HostileFlagCarrierUpdate", nil)
518 else
519 events:TriggerEvent("Glory_HostileFlagDropped", hordeFC)
520 events:TriggerEvent("Glory_FriendlyFlagCarrierUpdate", nil)
521 end
522 return
523 end
524 elseif Glory:IsInArathiBasin() or Glory:IsInAlteracValley() then
525 events:BattlefieldObjectiveEventProcessing(text)
526 end
527 end
528  
529 function events:CHAT_MSG_MONSTER_YELL(text)
530 if Glory:IsInAlteracValley() then
531 if string.find(string.lower(text), string.lower(VICTORY_TEXT0)) then
532 if isHorde then
533 IncreaseBattlegroundsWins(Glory)
534 else
535 IncreaseBattlegroundsLosses(Glory)
536 end
537 elseif string.find(string.lower(text), string.lower(VICTORY_TEXT1)) then
538 if not isHorde then
539 IncreaseBattlegroundsWins(Glory)
540 else
541 IncreaseBattlegroundsLosses(Glory)
542 end
543 else
544 events:BattlefieldObjectiveEventProcessing(text)
545 end
546 end
547 end
548  
549 function events:BattlefieldObjectiveEventProcessing(text)
550 local node, faction
551 for k, pattern in pairs(BGChatAnnouncements.BGObjectiveClaimedAnnouncements) do
552 _, _, node, faction = string.find(text, pattern)
553 if node then
554 if node == FACTION_ALLIANCE or node == FACTION_HORDE then
555 node, faction = faction, node
556 end
557 events:OnObjectiveClaimed(BGPatternReplacements[node] or node, faction)
558 events:TriggerEvent("Glory_ObjectiveClaimed", BGPatternReplacements[node] or node, faction)
559 return
560 end
561 end
562 for k, pattern in pairs(BGChatAnnouncements.BGObjectiveCapturedAnnouncements) do
563 _, _, node, faction = string.find(text, pattern)
564 if node then
565 if node == FACTION_ALLIANCE or node == FACTION_HORDE then
566 node, faction = faction, node
567 end
568 events:OnObjectiveCaptured(BGPatternReplacements[node] or node, faction)
569 events:TriggerEvent("Glory_ObjectiveCaptured", BGPatternReplacements[node] or node, faction)
570 return
571 end
572 end
573 for k, pattern in pairs(BGChatAnnouncements.BGObjectiveAttackedAnnouncements) do
574 _, _, node = string.find(text, pattern)
575 if node then
576 events:OnObjectiveAttacked(BGPatternReplacements[node] or node)
577 events:TriggerEvent("Glory_ObjectiveAttacked", BGPatternReplacements[node] or node)
578 return
579 end
580 end
581 for k, pattern in pairs(BGChatAnnouncements.BGObjectiveDefendedAnnouncements) do
582 _, _, node = string.find(text, pattern)
583 if node then
584 events:OnObjectiveDefended(BGPatternReplacements[node] or node)
585 events:TriggerEvent("Glory_ObjectiveDefended", BGPatternReplacements[node] or node)
586 return
587 end
588 end
589 for k, pattern in pairs(BGChatAnnouncements.BGObjectiveDestroyedAnnouncements) do
590 _, _, node = string.find(text, pattern)
591 if node then
592 events:OnObjectiveDestroyed(BGPatternReplacements[node] or node)
593 events:TriggerEvent("Glory_ObjectiveDestroyed", BGPatternReplacements[node] or node)
594 return
595 end
596 end
597 end
598  
599 function events:CHAT_MSG_COMBAT_FACTION_CHANGE(text)
600 local faction, rep = deformat(text, FACTION_STANDING_INCREASED)
601 if faction and rep then
602 if faction == FACTION_DEFILERS or faction == FACTION_FROSTWOLF_CLAN or faction == FACTION_WARSONG_OUTRIDERS or faction == FACTION_LEAGUE_OF_ARATHOR or faction == FACTION_STORMPIKE_GUARD or faction == FACTION_SILVERWING_SENTINELS then
603 events:TriggerEvent("Glory_FactionGain", faction, rep)
604 end
605 end
606 end
607  
608 function events:CHAT_MSG_COMBAT_HOSTILEPLAYER_HITS()
609 Glory.lastHostileTime = GetTime()
610 end
611 events.CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE = events.CHAT_MSG_COMBAT_HOSTILEPLAYER_HITS
612  
613 function events:UNIT_PVP_UPDATE()
614 if not UnitIsPVP("player") and Glory.permaPvP then
615 Glory.permaPvP = false
616 events:TriggerEvent("Glory_UpdatePermanentPvP", Glory.permaPvP)
617 end
618 if UnitIsPVP("player") or Glory:IsInBattlegrounds() then
619 Glory.pvpTime = GetTime()
620 events:TriggerEvent("Glory_UpdatePvPCooldown", Glory:GetPvPCooldown())
621 else
622 events:TriggerEvent("Glory_UpdatePvPCooldown", 0)
623 end
624 end
625  
626 function events:UPDATE_WORLD_STATES()
627 local resData = BattlefieldZoneResourceData[Glory:GetActiveBattlefieldZone()]
628 if resData and Glory:GetNumAllianceBases() and Glory:GetNumHordeBases() then
629 -- Common
630 local goalResources = resData[table.getn(resData)]
631 -- Alliance
632 _, _, resources = string.find(Glory:GetAllianceScoreString(), "(%d+)/")
633 bases = Glory:GetNumAllianceBases()
634 if resources and bases and (resources ~= Glory.aLastResources or bases ~= Glory.aLastBases) then
635 Glory.aResourceTTV = (goalResources - resources) / resData[bases]
636 Glory.aLastResources = resources
637 Glory.aLastBases = bases
638 Glory.aLastUpdate = GetTime()
639 end
640 -- Horde
641 _, _, resources = string.find(Glory:GetHordeScoreString(), "(%d+)/")
642 bases = Glory:GetNumHordeBases()
643 if resources and bases and (resources ~= Glory.hLastResources or bases ~= Glory.hLastBases) then
644 Glory.hResourceTTV = (goalResources - resources) / resData[bases]
645 Glory.hLastResources = resources
646 Glory.hLastBases = bases
647 Glory.hLastUpdate = GetTime()
648 end
649 end
650 end
651  
652 function events:PLAYER_ENTERING_WORLD()
653 events:UNIT_PVP_UPDATE()
654 SetMapToCurrentZone()
655 if Glory:IsInBattlegrounds() and GetNumMapLandmarks() > 0 then
656 events:InitializeBattlefieldObjectives()
657 else
658 events:ClearBattlefieldObjectives()
659 end
660 end
661  
662 function events:PLAYER_DEAD()
663 if GetTime() <= Glory.lastHostileTime + 15 then
664 IncreaseDeaths(Glory)
665 end
666 end
667  
668 function events:UPDATE_BATTLEFIELD_SCORE()
669 for k,v in pairs(enemyList) do
670 enemyList[k] = nil
671 end
672 for i = 1, GetNumBattlefieldScores() do
673 local name, _, _, _, _, faction = GetBattlefieldScore(i)
674 if faction == (isHorde and 1 or 0) then
675 local _,_,realName, realm = string.find(name, "(.*)%-(.*)")
676 if not realName then
677 realName = name
678 realm = playerRealm
679 end
680 enemyList[realName] = realm
681 end
682 end
683 end
684  
685 function Glory:IsInBattlegrounds()
686 -- local zone = GetRealZoneText()
687 -- return zone == WARSONG_GULCH or zone == ARATHI_BASIN or zone == ALTERAC_VALLEY
688 return (MiniMapBattlefieldFrame.status == "active")
689 end
690  
691 function Glory:IsInWarsongGulch()
692 return GetRealZoneText() == WARSONG_GULCH
693 end
694  
695 function Glory:IsInArathiBasin()
696 return GetRealZoneText() == ARATHI_BASIN
697 end
698  
699 function Glory:IsInAlteracValley()
700 return GetRealZoneText() == ALTERAC_VALLEY
701 end
702  
703 local function copyTable(to, from)
704 for k, v in pairs(from) do
705 to[k] = v
706 end
707 return to
708 end
709  
710 local tdate, start, done
711 local firstbg = { [2006] = 2, }
712 local function GetBattlegroundWeek(bgdate)
713 Glory:assert(bgdate.year >= 2006, "Cannot calculate battleground weekends for dates before year 2006. A date in year %s was given.", bgdate.year)
714 local bgweekday = math_mod(bgdate.wday + 4, 7) + 1
715 local bgweek = math.floor((bgdate.yday + 6 - bgweekday) / 7) + 1
716 if not firstbg[bgdate.year] then
717 if not tdate then
718 tdate = {}
719 end
720 tdate = copyTable(tdate, bgdate)
721 tdate.day = 1
722 tdate.month = 1
723 tdate = date("*t", time(tdate))
724 local d = math_mod(tdate.wday + 4, 7) + 1
725 tdate.day = 31
726 tdate.month = 12
727 tdate.year = tdate.year - 1
728 tdate = date("*t", time(tdate))
729 local _, _, bg = GetBattlegroundWeek(tdate)
730 if d == 1 then
731 firstbg[bgdate.year] = math_mod(bg, 4) + 1
732 else
733 firstbg[bgdate.year] = bg
734 end
735 end
736 local bg = math_mod(bgweek -1 + firstbg[bgdate.year], 4) + 1
737 return bgweekday, bgweek, bg
738 end
739  
740 local function GetCurrentOrNextBattlegroundWeekend(week)
741 local now = date("*t")
742 local bgweekday, bgweek, bg = GetBattlegroundWeek(now)
743 local bginweeks
744 if bg <= week then
745 bginweeks = week - bg
746 else
747 bginweeks = week + 4 - bg
748 end
749 if not start then
750 start = {}
751 end
752 start = copyTable(start, now)
753 start.day = start.day + 4 - bgweekday + 7 * bginweeks
754 start.hour = 0
755 start.min = 0
756 start.sec = 0
757 start = date("*t", time(start))
758 if not done then
759 done = {}
760 end
761 done = copyTable(done, now)
762 done.day = done.day + 7 - bgweekday + 7 * bginweeks
763 done.hour = 23
764 done.min = 59
765 done.sec = 59
766 done = date("*t", time(done))
767 local sMonth
768 local dMonth
769 if start.month == done.month then
770 sMonth = date("%B", time(start))
771 dMonth = sMonth
772 else
773 sMonth = date("%b", time(start))
774 dMonth = date("%b", time(done))
775 end
776 return sMonth, start.day, dMonth, done.day, time(start) <= time(now) and time(now) <= time(done)
777 end
778  
779 function Glory:GetCurrentOrNextArathiWeekend()
780 return GetCurrentOrNextBattlegroundWeekend(4)
781 end
782  
783 function Glory:GetCurrentOrNextWarsongWeekend()
784 return GetCurrentOrNextBattlegroundWeekend(3)
785 end
786  
787 function Glory:GetCurrentOrNextAlteracWeekend()
788 return GetCurrentOrNextBattlegroundWeekend(2)
789 end
790  
791 function Glory:_TogglePVP()
792 self.permaPvP = not self.permaPvP
793 if not UnitIsPVP("player") then
794 self.permaPvP = true
795 end
796 events:TriggerEvent("Glory_UpdatePermanentPvP", self.permaPvP)
797 self.pvpTime = GetTime()
798 end
799  
800 function Glory:GetTodayHKs(person, realm)
801 if person then
802 self:argCheck(person, 2, "string")
803 if realm and realm ~= playerRealm then
804 self:argCheck(realm, 3, "string")
805 person = person .. "-" .. realm
806 end
807 return self.data.hks[person] or 0
808 else
809 return self.data.todayHK
810 end
811 end
812  
813 function Glory:GetTodayDeaths()
814 return self.data.todayDeaths
815 end
816  
817 function Glory:GetTodayHKHonor()
818 return self.data.todayHKHonor
819 end
820  
821 function Glory:GetTodayBonusHonor()
822 return self.data.todayBonusHonor
823 end
824  
825 function Glory:GetTodayHonor()
826 return self.data.todayHKHonor + self.data.todayBonusHonor
827 end
828  
829 function Glory:GetBattlegroundsWins()
830 return self.data.wsgWin + self.data.abWin + self.data.avWin
831 end
832  
833 function Glory:GetWarsongGulchWins()
834 return self.data.wsgWin
835 end
836  
837 function Glory:GetArathiBasinWins()
838 return self.data.abWin
839 end
840  
841 function Glory:GetAlteracValleyWins()
842 return self.data.avWin
843 end
844  
845 function Glory:GetBattlegroundsLosses()
846 return self.data.wsgLoss + self.data.abLoss + self.data.avLoss
847 end
848  
849 function Glory:GetWarsongGulchLosses()
850 return self.data.wsgLoss
851 end
852  
853 function Glory:GetArathiBasinLosses()
854 return self.data.abLoss
855 end
856  
857 function Glory:GetAlteracValleyLosses()
858 return self.data.avLoss
859 end
860  
861 function Glory:ResetBGScores()
862 self.data.wsgWin = 0
863 self.data.wsgLoss = 0
864 self.data.abWin = 0
865 self.data.abLoss = 0
866 self.data.avWin = 0
867 self.data.avLoss = 0
868 events:TriggerEvent("Glory_BGResetScores")
869 end
870  
871 function Glory:IsPermanentPvP()
872 return self.permaPvP
873 end
874  
875 function Glory:GetPvPCooldown()
876 if self:IsInBattlegrounds() or self.permaPvP then
877 return 300
878 end
879 local t = self.pvpTime - GetTime() + 300
880 if t < 0 or not UnitIsPVP("player") then
881 return 0
882 else
883 return t
884 end
885 end
886  
887 function Glory:GetRankLimitInfo()
888 local level = UnitLevel("player")
889 if level < 10 then
890 return NONE, 0
891 elseif level <= 32 then
892 return GetPVPRankInfo(7)
893 elseif level <= 37 then
894 return GetPVPRankInfo(8)
895 elseif level <= 40 then
896 return GetPVPRankInfo(9)
897 elseif level <= 43 then
898 return GetPVPRankInfo(10)
899 elseif level <= 45 then
900 return GetPVPRankInfo(11)
901 elseif level <= 47 then
902 return GetPVPRankInfo(12)
903 elseif level <= 50 then
904 return GetPVPRankInfo(13)
905 elseif level <= 52 then
906 return GetPVPRankInfo(14)
907 elseif level <= 54 then
908 return GetPVPRankInfo(15)
909 elseif level <= 56 then
910 return GetPVPRankInfo(16)
911 elseif level <= 58 then
912 return GetPVPRankInfo(17)
913 else
914 return GetPVPRankInfo(18)
915 end
916 end
917  
918 function Glory:GetRatingLimit()
919 local level = UnitLevel("player")
920 if level < 10 then
921 return 0
922 elseif level <= 29 then
923 return 6500
924 elseif level <= 35 then
925 return 7150 + (level - 30) * 975
926 elseif level <= 39 then
927 return 13325 + (level - 36) * 1300
928 elseif level <= 43 then
929 return 18850 + (level - 40) * 1625
930 elseif level <= 52 then
931 return 26000 + (level - 44) * 2275
932 elseif level <= 59 then
933 return 46800 + (level - 53) * 2600
934 else
935 return 65000
936 end
937 end
938  
939 function Glory:GetStanding(name)
940 name = name or playerName
941 self:argCheck(name, 2, "string")
942 for i=1, GetNumBattlefieldScores() do
943 if name == GetBattlefieldScore(i) then
944 return i
945 end
946 end
947 end
948  
949 function Glory:GetKillingBlows(name)
950 name = name or playerName
951 self:argCheck(name, 2, "string")
952 for i=1, GetNumBattlefieldScores() do
953 local unit, killingBlows = GetBattlefieldScore(i)
954 if unit == name then
955 return killingBlows
956 end
957 end
958 end
959  
960 function Glory:GetHonorableKills(name)
961 name = name or playerName
962 self:argCheck(name, 2, "string")
963 for i=1, GetNumBattlefieldScores() do
964 local unit, _, honorableKills = GetBattlefieldScore(i)
965 if unit == name then
966 return honorableKills
967 end
968 end
969 end
970  
971 function Glory:GetDeaths(name)
972 name = name or playerName
973 self:argCheck(name, 2, "string")
974 for i=1, GetNumBattlefieldScores() do
975 local unit, _, _, deaths = GetBattlefieldScore(i)
976 if unit == name then
977 return deaths
978 end
979 end
980 end
981  
982 function Glory:GetBonusHonor(name)
983 name = name or playerName
984 self:argCheck(name, 2, "string")
985 for i=1, GetNumBattlefieldScores() do
986 local unit, _, _, _, bonusHonor = GetBattlefieldScore(i)
987 if unit == name then
988 return bonusHonor
989 end
990 end
991 end
992  
993 function Glory:GetActiveBattlefieldZone()
994 for i = 1, MAX_BATTLEFIELD_QUEUES do
995 local status, mapName = GetBattlefieldStatus(i)
996 if status == "active" then
997 return mapName
998 end
999 end
1000 end
1001  
1002 function Glory:GetActiveBattlefieldUniqueID()
1003 for i = 1, MAX_BATTLEFIELD_QUEUES do
1004 local status, mapName, instanceID = GetBattlefieldStatus(i)
1005 if status == "active" then
1006 return mapName .. " " .. instanceID
1007 end
1008 end
1009 end
1010  
1011 local function queuedBattlefieldIndicesIter(_, position)
1012 position = position + 1
1013 while position <= MAX_BATTLEFIELD_QUEUES do
1014 local status, name = GetBattlefieldStatus(position)
1015 if status == "queued" then
1016 return position, name
1017 end
1018 position = position + 1
1019 end
1020 return nil
1021 end
1022 function Glory:IterateQueuedBattlefieldZones()
1023 return queuedBattlefieldIndicesIter, nil, 0
1024 end
1025  
1026 local function GetHolder(self, node)
1027 local poi = self:NodeToPOI(node)
1028 if self:IsUncontrolled(node) then
1029 return BGObjectiveDescriptions.UNCONTROLLED
1030 end
1031 if poi and not self:IsDestroyed(poi) then
1032 _, description = GetMapLandmarkInfo(poi)
1033 if string.find(description, PATTERN_OBJECTIVE_HOLDER) then
1034 local _, _, faction = string.find(description, PATTERN_OBJECTIVE_HOLDER)
1035 return faction
1036 end
1037 end
1038 end
1039  
1040 function Glory:IsBattlefieldObjective(node)
1041 self:argCheck(node, 2, "string", "number")
1042 local poi = self:NodeToPOI(node)
1043 if poi and (GetHolder(self, node) or self:IsInConflict(node) or self:IsDestroyed(node)) then
1044 return true
1045 end
1046 return false
1047 end
1048  
1049 function Glory:IsInConflict(node)
1050 self:argCheck(node, 2, "string", "number")
1051 local poi = self:NodeToPOI(node)
1052 if poi then
1053 local _, description = GetMapLandmarkInfo(poi)
1054 if description == BGObjectiveDescriptions.IN_CONFLICT then
1055 return true
1056 end
1057 end
1058 return false
1059 end
1060  
1061 function Glory:IsAllianceControlled(node)
1062 self:argCheck(node, 2, "string", "number")
1063 local poi = self:NodeToPOI(node)
1064 if poi then
1065 local _, description = GetMapLandmarkInfo(poi)
1066 if description == BGObjectiveDescriptions.ALLIANCE_CONTROLLED then
1067 return true
1068 end
1069 end
1070 return false
1071 end
1072  
1073 function Glory:IsHordeControlled(node)
1074 self:argCheck(node, 2, "string", "number")
1075 local poi = self:NodeToPOI(node)
1076 if poi then
1077 local _, description = GetMapLandmarkInfo(poi)
1078 if description == BGObjectiveDescriptions.HORDE_CONTROLLED then
1079 return true
1080 end
1081 end
1082 return false
1083 end
1084  
1085 if isHorde then
1086 Glory.IsFriendlyControlled = Glory.IsHordeControlled
1087 Glory.IsHostileControlled = Glory.IsAllianceControlled
1088 else
1089 Glory.IsFriendlyControlled = Glory.IsAllianceControlled
1090 Glory.IsHostileControlled = Glory.IsHordeControlled
1091 end
1092  
1093 function Glory:IsUncontrolled(node)
1094 self:argCheck(node, 2, "string", "number")
1095 local poi = self:NodeToPOI(node)
1096 if poi then
1097 local _, description = GetMapLandmarkInfo(poi)
1098 if description == BGObjectiveDescriptions.UNCONTROLLED then
1099 return true
1100 end
1101 end
1102 return
1103 end
1104  
1105 function Glory:IsDestroyed(node)
1106 self:argCheck(node, 2, "string", "number")
1107 local poi = self:NodeToPOI(node)
1108 if poi then
1109 local _, description = GetMapLandmarkInfo(poi)
1110 if description == BGObjectiveDescriptions.DESTROYED then
1111 return true
1112 end
1113 end
1114 return
1115 end
1116  
1117 function Glory:GetTimeAttacked(node)
1118 self:argCheck(node, 2, "string", "number")
1119 return self.battlefieldObjectiveStatus[node].timeAttacked
1120 end
1121  
1122 function Glory:GetTimeToCapture(node)
1123 self:argCheck(node, 2, "string", "number")
1124 local t = BattlefieldZoneObjectiveTimes[self:GetActiveBattlefieldZone()] or 0
1125 return self.battlefieldObjectiveStatus and self.battlefieldObjectiveStatus[node] and self.battlefieldObjectiveStatus[node].timeAttacked and t - GetTime() + self.battlefieldObjectiveStatus[node].timeAttacked
1126 end
1127  
1128 function Glory:GetName(node)
1129 self:argCheck(node, 2, "string", "number")
1130 return self.battlefieldObjectiveStatus[node].name
1131 end
1132  
1133 function Glory:GetDefender(node)
1134 self:argCheck(node, 2, "string", "number")
1135 return self.battlefieldObjectiveStatus[node].defender
1136 end
1137  
1138 function Glory:GetAttacker(node)
1139 self:argCheck(node, 2, "string", "number")
1140 return self.battlefieldObjectiveStatus[node].attacker
1141 end
1142  
1143 local function objectiveNodesIter(t, position)
1144 local k = next(t, position)
1145 while k ~= nil and type(k) ~= "number" do
1146 k = next(t, position)
1147 end
1148 return k
1149 end
1150  
1151 function Glory:IterateObjectiveNodes()
1152 return objectiveNodesIter, self.battlefieldObjectiveStatus, nil
1153 end
1154  
1155 local function sortedObjectiveNodesIter(t, position)
1156 position = position + 1
1157 if position <= table.getn(t) then
1158 return position, t[position]
1159 else
1160 t = del(t)
1161 return nil
1162 end
1163 end
1164 local mySort
1165 function Glory:IterateSortedObjectiveNodes()
1166 local t = new()
1167 for poi in pairs(self.battlefieldObjectiveStatus) do
1168 if type(poi) == "number" then
1169 table.insert(t, poi)
1170 end
1171 end
1172 if not mySort then
1173 mySort = function(a, b)
1174 return self.battlefieldObjectiveStatus[a].ypos and self.battlefieldObjectiveStatus[b].ypos and self.battlefieldObjectiveStatus[a].ypos < self.battlefieldObjectiveStatus[b].ypos
1175 end
1176 end
1177 table.sort(t, mySort)
1178 return sortedObjectiveNodesIter, t, 0
1179 end
1180  
1181 function events:ClearBattlefieldObjectives()
1182 for i = 1, table.getn(Glory.battlefieldObjectiveStatus) do
1183 local o = Glory.battlefieldObjectiveStatus[i]
1184 if Glory.battlefieldObjectiveStatus[o.node] == o then
1185 Glory.battlefieldObjectiveStatus[o.node] = nil
1186 end
1187 Glory.battlefieldObjectiveStatus[i] = del(o)
1188 end
1189 for k in pairs(Glory.battlefieldObjectiveStatus) do
1190 Glory.battlefieldObjectiveStatus[k] = del(Glory.battlefieldObjectiveStatus[k])
1191 k = nil
1192 end
1193 end
1194  
1195 function events:InitializeBattlefieldObjectives()
1196 events:ClearBattlefieldObjectives()
1197 SetMapToCurrentZone()
1198 local numPOIS = GetNumMapLandmarks()
1199 for i=1, numPOIS do
1200 if Glory:IsBattlefieldObjective(i) then
1201 local node, _, _, _, y = GetMapLandmarkInfo(i)
1202 Glory.battlefieldObjectiveStatus[i] = {
1203 name = node,
1204 ypos = y,
1205 defender = GetHolder(Glory, i),
1206 inConflict = Glory:IsInConflict(i),
1207 isDestroyed = Glory:IsDestroyed(i),
1208 }
1209 Glory.battlefieldObjectiveStatus[node] = Glory.battlefieldObjectiveStatus[i]
1210 end
1211 end
1212 end
1213  
1214 function events:OnObjectiveClaimed(node, faction)
1215 local poi = Glory:NodeToPOI(node)
1216 if poi then
1217 if not next(Glory.battlefieldObjectiveStatus) then
1218 events:InitializeBattlefieldObjectives()
1219 end
1220 local n = Glory.battlefieldObjectiveStatus[poi]
1221 if n then
1222 n.attacker = faction
1223 n.inConflict = true
1224 n.timeAttacked = GetTime()
1225 end
1226 end
1227 end
1228  
1229 function events:OnObjectiveCaptured(node, faction)
1230 local poi = Glory:NodeToPOI(node)
1231 if poi then
1232 if not next(Glory.battlefieldObjectiveStatus) then
1233 events:InitializeBattlefieldObjectives()
1234 end
1235 local n = Glory.battlefieldObjectiveStatus[poi]
1236 if n then
1237 n.defender = GetHolder(Glory, node) or faction
1238 n.attacker = nil
1239 n.inConflict = nil
1240 n.timeAttacked = nil
1241 end
1242 end
1243 end
1244  
1245 function events:OnObjectiveAttacked(node)
1246 local poi = Glory:NodeToPOI(node)
1247 if poi then
1248 if not next(Glory.battlefieldObjectiveStatus) then
1249 events:InitializeBattlefieldObjectives()
1250 end
1251 local n = Glory.battlefieldObjectiveStatus[poi]
1252 if n then
1253 if n.defender == FACTION_ALLIANCE then
1254 n.attacker = FACTION_HORDE
1255 else
1256 n.attacker = FACTION_ALLIANCE
1257 end
1258 n.inConflict = true
1259 n.timeAttacked = GetTime()
1260 end
1261 end
1262 end
1263  
1264 function events:OnObjectiveDefended(node)
1265 local poi = Glory:NodeToPOI(node)
1266 if poi then
1267 if not next(Glory.battlefieldObjectiveStatus) then
1268 events:InitializeBattlefieldObjectives()
1269 end
1270 local n = Glory.battlefieldObjectiveStatus[poi]
1271 if n then
1272 n.attacker = nil
1273 n.inConflict = nil
1274 n.timeAttacked = nil
1275 end
1276 end
1277 end
1278  
1279 function events:OnObjectiveDestroyed(node)
1280 local poi = Glory:NodeToPOI(node)
1281 if poi then
1282 if not next(Glory.battlefieldObjectiveStatus) then
1283 events:InitializeBattlefieldObjectives()
1284 end
1285 local n = Glory.battlefieldObjectiveStatus[poi]
1286 if n then
1287 n.isDestroyed = true
1288 n.defender = nil
1289 n.attacker = nil
1290 n.inConflict = nil
1291 n.timeAttacked = nil
1292 end
1293 end
1294 end
1295  
1296 function Glory:GetAllianceFlagCarrier()
1297 return self.allianceFC
1298 end
1299  
1300 function Glory:GetHordeFlagCarrier()
1301 return self.hordeFC
1302 end
1303  
1304 function Glory:TargetAllianceFlagCarrier()
1305 if self.allianceFC then
1306 TargetByName(self.allianceFC)
1307 end
1308 end
1309  
1310 function Glory:TargetHordeFlagCarrier()
1311 if self.hordeFC then
1312 TargetByName(self.hordeFC)
1313 end
1314 end
1315  
1316 if isHorde then
1317 Glory.GetFriendlyFlagCarrier = Glory.GetHordeFlagCarrier
1318 Glory.GetHostileFlagCarrier = Glory.GetAllianceFlagCarrier
1319 Glory.TargetFriendlyFlagCarrier = Glory.TargetHordeFlagCarrier
1320 Glory.TargetHostileFlagCarrier = Glory.TargetAllianceFlagCarrier
1321 else
1322 Glory.GetFriendlyFlagCarrier = Glory.GetAllianceFlagCarrier
1323 Glory.GetHostileFlagCarrier = Glory.GetHordeFlagCarrier
1324 Glory.TargetFriendlyFlagCarrier = Glory.TargetAllianceFlagCarrier
1325 Glory.TargetHostileFlagCarrier = Glory.TargetHordeFlagCarrier
1326 end
1327  
1328 function Glory:GetFlagCarrier(faction)
1329 self:argCheck(faction, 2, "string", "number")
1330 if faction == FACTION_ALLIANCE or faction == "Alliance" or faction == 1 then
1331 return self.allianceFC
1332 else
1333 return self.hordeFC
1334 end
1335 end
1336  
1337 function Glory:TargetFlagCarrier(faction)
1338 self:argCheck(faction, 2, "string", "number")
1339 if faction == FACTION_ALLIANCE or faction == "Alliance" or faction == 1 then
1340 if self.allianceFC then
1341 TargetByName(self.allianceFC)
1342 end
1343 elseif self.hordeFC then
1344 TargetByName(self.hordeFC)
1345 end
1346 end
1347  
1348 function Glory:GetNumAllianceBases()
1349 local _, s = GetWorldStateUIInfo(1)
1350 if s then
1351 local _, _, bases = string.find(s, PATTERN_GWSUII_BASES)
1352 return tonumber(bases)
1353 end
1354 end
1355  
1356 function Glory:GetNumHordeBases()
1357 local _, s = GetWorldStateUIInfo(2)
1358 if s then
1359 local _, _, bases = string.find(s, PATTERN_GWSUII_BASES)
1360 return tonumber(bases)
1361 end
1362 end
1363  
1364 if isHorde then
1365 Glory.GetNumFriendlyBases = Glory.GetNumHordeBases
1366 Glory.GetNumHostileBases = Glory.GetNumAllianceBases
1367 else
1368 Glory.GetNumFriendlyBases = Glory.GetNumAllianceBases
1369 Glory.GetNumHostileBases = Glory.GetNumHordeBases
1370 end
1371  
1372 function Glory:GetNumBases(team)
1373 self:argCheck(team, 2, "string", "number")
1374 if team == FACTION_ALLIANCE or team == "Alliance" or team == 1 then
1375 return self:GetNumAllianceBases()
1376 else
1377 return self:GetNumHordeBases()
1378 end
1379 end
1380  
1381 function Glory:GetNumAllianceResources()
1382 local _, s = GetWorldStateUIInfo(1)
1383 if s then
1384 local _, _, resources = string.find(s, PATTERN_GWSUII_RESOURCES)
1385 return tonumber(resources)
1386 end
1387 end
1388  
1389 function Glory:GetNumHordeResources()
1390 local _, s = GetWorldStateUIInfo(2)
1391 if s then
1392 local _, _, resources = string.find(s, PATTERN_GWSUII_RESOURCES)
1393 return tonumber(resources)
1394 end
1395 end
1396  
1397 if isHorde then
1398 Glory.GetNumFriendlyResources = Glory.GetNumHordeResources
1399 Glory.GetNumHostileResources = Glory.GetNumAllianceResources
1400 else
1401 Glory.GetNumFriendlyResources = Glory.GetNumAllianceResources
1402 Glory.GetNumHostileResources = Glory.GetNumHordeResources
1403 end
1404  
1405 function Glory:GetNumTeamResources(team)
1406 self:argCheck(team, 2, "string", "number")
1407 if team == FACTION_ALLIANCE or team == "Alliance" or team == 1 then
1408 return self:GetNumAllianceResources()
1409 else
1410 return self:GetNumHordeResources()
1411 end
1412 end
1413  
1414 function Glory:GetAllianceTTV()
1415 return self.aResourceTTV - GetTime() + self.aLastUpdate
1416 end
1417  
1418 function Glory:GetHordeTTV()
1419 return self.hResourceTTV - GetTime() + self.hLastUpdate
1420 end
1421  
1422 function Glory:GetTeamTTV(team)
1423 self:argCheck(team, 2, "string", "number")
1424 if team == FACTION_ALLIANCE or team == "Alliance" or team == 1 then
1425 return self:GetAllianceTTV()
1426 else
1427 return self:GetHordeTTV()
1428 end
1429 end
1430  
1431 if isHorde then
1432 Glory.GetFriendlyTTV = Glory.GetHordeTTV
1433 Glory.GetHostileTTV = Glory.GetAllianceTTV
1434 else
1435 Glory.GetFriendlyTTV = Glory.GetAllianceTTV
1436 Glory.GetHostileTTV = Glory.GetHordeTTV
1437 end
1438  
1439 function Glory:GetAllianceScoreString()
1440 local _, s = GetWorldStateUIInfo(1)
1441 if s then
1442 local _, _, scoreString = string.find(s, PATTERN_GWSUII_SCORE)
1443 return scoreString
1444 end
1445 end
1446  
1447 function Glory:GetHordeScoreString()
1448 local _, s = GetWorldStateUIInfo(2)
1449 if s then
1450 local _, _, scoreString = string.find(s, PATTERN_GWSUII_SCORE)
1451 return scoreString
1452 end
1453 end
1454  
1455 if isHorde then
1456 Glory.GetFriendlyScoreString = Glory.GetHordeScoreString
1457 Glory.GetHostileScoreString = Glory.GetAllianceScoreString
1458 else
1459 Glory.GetFriendlyScoreString = Glory.GetAllianceScoreString
1460 Glory.GetHostileScoreString = Glory.GetHordeScoreString
1461 end
1462  
1463 function Glory:GetTeamScoreString(team)
1464 if team == FACTION_ALLIANCE or team == "Alliance" or team == 1 then
1465 return self:GetAllianceScoreString()
1466 else
1467 return self:GetHordeScoreString()
1468 end
1469 end
1470  
1471 function Glory:GetNumAlliancePlayers()
1472 local numPlayers = 0
1473 for i = 1, GetNumBattlefieldScores() do
1474 local _, _, _, _, _, faction = GetBattlefieldScore(i)
1475 if faction == 1 then
1476 numPlayers = numPlayers + 1
1477 end
1478 end
1479 return numPlayers
1480 end
1481  
1482 function Glory:GetNumHordePlayers()
1483 local numPlayers = 0
1484 for i = 1, GetNumBattlefieldScores() do
1485 local _, _, _, _, _, faction = GetBattlefieldScore(i)
1486 if faction == 0 then
1487 numPlayers = numPlayers + 1
1488 end
1489 end
1490 return numPlayers
1491 end
1492  
1493 if isHorde then
1494 Glory.GetNumFriendlyPlayers = Glory.GetNumHordePlayers
1495 Glory.GetNumHostilePlayers = Glory.GetNumAlliancePlayers
1496 else
1497 Glory.GetNumFriendlyPlayers = Glory.GetNumAlliancePlayers
1498 Glory.GetNumHostilePlayers = Glory.GetNumHordePlayers
1499 end
1500  
1501 function Glory:GetNumPlayers(team)
1502 self:argCheck(team, 2, "string", "number")
1503 if team == FACTION_ALLIANCE or team == "Alliance" or team == 1 then
1504 return self:GetNumAlliancePlayers()
1505 else
1506 return self:GetNumHordePlayers()
1507 end
1508 end
1509  
1510 function Glory:SafeNodeToPOI(node)
1511 if type(node) == "number" and node > 0 and node <= GetNumMapLandmarks() then
1512 return node
1513 elseif type(node) == "string" then
1514 for i = 1, GetNumMapLandmarks() do
1515 if string.lower(node) == string.lower(GetMapLandmarkInfo(i)) then
1516 return i
1517 end
1518 end
1519 elseif type(node) ~= "number" then
1520 self:error("Bad argument #2 to `NodeToPOI' (string or number expected, got %s)", tostring(type(node)))
1521 else
1522 self:error("Bad argument #2 to `NodeToPOI' (out of bounds: [1, %d] expected, got %d)", GetNumMapLandmarks(), node)
1523 end
1524 end
1525  
1526 function Glory:NodeToPOI(node)
1527 if type(node) == "number" then return node end
1528 if type(node) == "string" then
1529 for i = 1, GetNumMapLandmarks() do
1530 if string.lower(node) == string.lower(GetMapLandmarkInfo(i)) then return i end
1531 end
1532 end
1533 end
1534  
1535 function Glory:GetBGAcronym(bgName)
1536 self:argCheck(bgName, 2, "string")
1537 return BGAcronyms[bgName] or bgName
1538 end
1539  
1540 function Glory:GetFactionColor(faction)
1541 self:argCheck(faction, 2, "string", "number", "nil")
1542 if faction then
1543 if faction == "Alliance" or faction == FACTION_ALLIANCE or faction == 1 then
1544 faction = "ALLIANCE"
1545 elseif faction == "Horde" or faction == FACTION_HORDE or faction == 0 or faction == 2 then
1546 faction = "HORDE"
1547 end
1548 local cti = ChatTypeInfo["BG_SYSTEM_" .. faction]
1549 if cti then
1550 return cti.r, cti.g, cti.b
1551 end
1552 end
1553 return 0.7, 0.7, 0.7
1554 end
1555  
1556 function Glory:GetFactionHexColor(faction)
1557 local r, g, b = self:GetFactionColor(faction)
1558 return string.format("%02X%02X%02X", 255*r, 255*g, 255*b)
1559 end
1560  
1561 local function activate(self, oldLib, oldDeactivate)
1562 Glory = self
1563 if oldLib then
1564 self.registry = oldLib.registry
1565 else
1566 self.registry = {}
1567 end
1568 events:CancelAllScheduledEvents()
1569 events:UnregisterAllEvents()
1570 events:RegisterEvent("ADDON_LOADED")
1571 events:RegisterEvent("VARIABLES_LOADED")
1572 events:RegisterEvent("PLAYER_LOGOUT")
1573 events:RegisterEvent("CHAT_MSG_COMBAT_HONOR_GAIN")
1574 events:RegisterEvent("CHAT_MSG_BG_SYSTEM_NEUTRAL")
1575 events:RegisterEvent("CHAT_MSG_BG_SYSTEM_HORDE")
1576 events:RegisterEvent("CHAT_MSG_MONSTER_YELL")
1577 events:RegisterEvent("CHAT_MSG_BG_SYSTEM_ALLIANCE")
1578 events:RegisterEvent("CHAT_MSG_COMBAT_FACTION_CHANGE")
1579 events:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE")
1580 events:RegisterEvent("CHAT_MSG_COMBAT_HOSTILEPLAYER_HITS")
1581 events:RegisterEvent("UPDATE_WORLD_STATES")
1582 events:RegisterEvent("PLAYER_ENTERING_WORLD")
1583 events:RegisterEvent("UNIT_PVP_UPDATE")
1584 events:RegisterEvent("PLAYER_DEAD")
1585 events:RegisterEvent("UPDATE_BATTLEFIELD_SCORE")
1586 events:VARIABLES_LOADED()
1587 events:ScheduleRepeatingEvent(function()
1588 if self:IsInBattlegrounds() then
1589 RequestBattlefieldScoreData()
1590 end
1591 end, 15)
1592  
1593 if not oldLib then
1594 local old_TogglePVP = TogglePVP
1595 function TogglePVP()
1596 AceLibrary(MAJOR_VERSION):_TogglePVP()
1597  
1598 old_TogglePVP()
1599 end
1600 end
1601  
1602 self.battlefieldObjectiveStatus = new()
1603 self.pvpTime = 0
1604 self.currentBonusHonor = 0
1605 self.lastHostileTime = 0
1606 self.aLastResources = 0
1607 self.hLastResources = 0
1608 self.aLastBases = 0
1609 self.hLastBases = 0
1610 self.aLastUpdate = 0
1611 self.hLastUpdate = 0
1612 self.aResourceTTV = 0
1613 self.hResourceTTV = 0
1614  
1615 SLASH_TARFLAG1 = "/tarflag"
1616 SLASH_TARFLAG2 = "/tflag"
1617 SlashCmdList.TARFLAG = function()
1618 self:TargetHostileFlagCarrier()
1619 end
1620  
1621 if oldDeactivate then
1622 oldDeactivate(oldLib)
1623 end
1624 end
1625  
1626 local function external(self, major, instance)
1627 if major == "AceConsole-2.0" then
1628 local print = print
1629 if DEFAULT_CHAT_FRAME then
1630 function print(key, value)
1631 DEFAULT_CHAT_FRAME:AddMessage("|cffffff7f" .. key .. ": [|r" .. value .. "|cffffff7f]|r")
1632 end
1633 end
1634 instance.RegisterChatCommand(self, { "/glory", "/glorylib" }, {
1635 name = MAJOR_VERSION .. "." .. string.gsub(MINOR_VERSION, ".-(%d+).*", "%1"),
1636 desc = "A library for PvP and Battlegrounds.",
1637 type = "group",
1638 args = {
1639 bg = {
1640 name = "Battlegrounds",
1641 desc = "Show battlegrounds information",
1642 type = "execute",
1643 func = function()
1644 print("BG Score", self:GetBattlegroundsWins() .. "-" .. self:GetBattlegroundsLosses())
1645 print("WSG Score", self:GetWarsongGulchWins() .. "-" .. self:GetWarsongGulchLosses())
1646 print("AB Score", self:GetArathiBasinWins() .. "-" .. self:GetArathiBasinLosses())
1647 print("AV Score", self:GetAlteracValleyWins() .. "-" .. self:GetAlteracValleyLosses())
1648 if self:IsInBattlegrounds() then
1649 print("Current", self:GetActiveBattlegroundUniqueID())
1650 print("Standing", self:GetStanding())
1651 print("Killing Blows", self:GetKillingBlows())
1652 print("Honorable Kills", self:GetHonorableKills())
1653 print("Deaths", self:GetDeaths())
1654 print("Bonus Honor", self:GetBonusHonor())
1655 if self:IsInWarsongGulch() then
1656 print("Friendly FC", (self:GetFriendlyFlagCarrier() or NONE))
1657 print("Hostile FC", (self:GetHostileFlagCarrier() or NONE))
1658 else
1659 print("Friendly Bases", self:GetNumFriendlyBases())
1660 print("Hostile Bases", self:GetNumHostileBases())
1661 print("Friendly Resources", self:GetNumFriendlyResources())
1662 print("Hostile Resources", self:GetNumHostileResources())
1663 end
1664 print("Friendly Players", self:GetNumFriendlyPlayers())
1665 print("Hostile Players", self:GetNumHostilePlayers())
1666 end
1667 end
1668 },
1669 honor = {
1670 name = "Honor",
1671 desc = "Show honor information",
1672 type = "execute",
1673 func = function()
1674 print("Today's HKs", self:GetTodayHKs())
1675 print("Today's Deaths", self:GetTodayDeaths())
1676 print("Today's HK Honor", self:GetTodayHKHonor())
1677 print("Today's Bonus Honor", self:GetTodayBonusHonor())
1678 print("Today's Honor", self:GetTodayHonor())
1679 local s
1680 if self:IsPermanentPvP() then
1681 s = "Flagged"
1682 elseif self:IsInBattlegrounds() then
1683 s = "Battlegrounds"
1684 else
1685 local t = self:GetPvPCooldown()
1686 if t == 0 then
1687 s = "None"
1688 else
1689 local min = floor(t / 60)
1690 local sec = floor(mod(t, 60))
1691 s = format("%d:%02d", min, sec)
1692 end
1693 end
1694 print("PvP Cooldown", s)
1695 print("Rank Limit", string.format("%s (%d)", self:GetRankLimitInfo()))
1696 print("Rating Limit", self:GetRatingLimit())
1697 end
1698 }
1699 }
1700 }, "GLORY")
1701 end
1702 end
1703  
1704 AceLibrary:Register(Glory, MAJOR_VERSION, MINOR_VERSION, activate, nil, external)