This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPvpScribe.lua
More file actions
597 lines (522 loc) · 19.4 KB
/
PvpScribe.lua
File metadata and controls
597 lines (522 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
--[[
PvpScribe.lua -- AIE PvP statistics recorder add-on
Author: Mark Rogaski, stigg@pobox.com
$Id$
Copyright (c) 2007, 2009; Mark Rogaski.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the copyright holder nor the names of any
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]
PvpScribe = AceLibrary('AceAddon-2.0'):new('AceConsole-2.0',
'AceEvent-2.0', 'AceDB-2.0', 'AceDebug-2.0')
--
-- Addon properties
--
PvpScribe.VERSION = '0.3.00'
PvpScribe.statsRecorded = false
PvpScribe.currentBattlegroundStart = nil
PvpScribe.currentBattlegroundName = nil
PvpScribe.currentBattlegroundID = nil
PvpScribe.hashStore = {}
PvpScribe:RegisterChatCommand({ '/pvpscribe', '/pvps' }, {
type = 'group',
args = {
history = {
type = 'text',
name = 'History Size',
desc = 'Set the maximum number of event records',
usage = '<length>',
set = function(n)
PvpScribe.db.char.config.maxHistory = n
while table.maxn(PvpScribe.db.char.history) > n do
table.remove(PvpScribe.db.char.history)
end
end,
get = function()
return PvpScribe.db.char.config.maxHistory
end,
},
clear = {
type = 'execute',
name = 'Clear Stats',
desc = 'Erase statistics database',
func = function ()
StaticPopup_Show('PVPSCRIBE_CLEAR_CONFIRM')
end,
},
show = {
type = 'execute',
name = 'Show Stats',
desc = 'Show PvP statistics',
func = 'ShowStats'
},
},
})
PvpScribe:RegisterDB('PvpScribeDB', 'PvpScribeDB', 'char')
PvpScribe:RegisterDefaults('char', {
config = {
maxHistory = 256,
},
charInfo = {
name = '',
faction = '',
realm = '',
class = '',
level = '',
guild = '',
guildRank = '',
},
summary = {
count = 0,
wins = 0,
killingBlows = 0,
honorKills = 0,
deaths = 0,
honorGained = 0,
damageDone = 0,
healingDone = 0,
rankSum = 0,
rankMax = 0,
},
wgStats = {
count = 0,
flagsCaptured = 0,
flagsReturned = 0,
},
abStats = {
count = 0,
basesAssaulted = 0,
basesDefended = 0,
},
avStats = {
count = 0,
graveyardsAssaulted = 0,
graveyardsDefended = 0,
towersAssaulted = 0,
towersDefended = 0,
minesCaptured = 0,
leadersKilled = 0,
secondaryObjectives = 0,
},
signature = {
summary = '',
wgStats = '',
abStats = '',
avStats = '',
},
history = {},
})
--[[
--
-- Popup definitions
--
--]]
StaticPopupDialogs['PVPSCRIBE_CLEAR_CONFIRM'] = {
text = 'Do you want to erase all recorded PvP statistics?',
button1 = 'Yes',
button2 = 'No',
timeout = 30,
whileDead = 1,
hideOnEscape = 1,
OnAccept = function ()
PvpScribe:ClearStats()
end,
};
--[[
--
-- Event handlers
--
--]]
function PvpScribe:OnInitialize()
-- Refresh DB signatures
local char = self.db.char
local sig = char.signature
local newDb = true
for _, sec in pairs({ 'summary', 'wgStats', 'abStats', 'avStats' }) do
for k, v in pairs(char[sec]) do
if v ~= 0 then
newDb = false
end
end
end
if not newDb then
PvpScribe:StoreSignatures()
end
PvpScribe:GenerateSignatures()
self.db.char.version = PvpScribe.VERSION
end
function PvpScribe:OnEnable(first)
-- get character info
local info = self.db.char.charInfo
if first then
-- these shouldn't change during a login session
info.name = UnitName('player')
info.class = UnitClass('player')
_, info.faction = UnitFactionGroup('player')
info.realm = GetRealmName()
end
info.level = UnitLevel('player')
info.guild, info.guildRank = GetGuildInfo('player')
PvpScribe:RegisterEvent('UPDATE_BATTLEFIELD_STATUS')
PvpScribe:RegisterEvent('ZONE_CHANGED_NEW_AREA')
PvpScribe:RegisterEvent('PLAYER_LEVEL_UP')
PvpScribe:RegisterEvent('PLAYER_GUILD_UPDATE')
self:Print('enabled ... the Bookkeeper of Blood is ready.')
end
function PvpScribe:OnDisable()
self:Print('disabled.')
end
function PvpScribe:UPDATE_BATTLEFIELD_STATUS()
self:LevelDebug(3, 'received event: UPDATE_BATTLEFIELD_STATUS')
PvpScribe:ExamineBGStatus()
end
function PvpScribe:ZONE_CHANGED_NEW_AREA()
self:LevelDebug(3, 'received event: ZONE_CHANGED_NEW_AREA')
if PvpScribe.statsRecorded then
PvpScribe.statsRecorded = false
PvpScribe.currentBattlegroundStart = nil
PvpScribe.currentBattlegroundName = nil
self:LevelDebug(3, 'set currentBattlegroundName = NIL')
PvpScribe.currentBattlegroundID = nil
end
end
function PvpScribe:PLAYER_LEVEL_UP(new)
self.db.char.charInfo.level = new
end
function PvpScribe:PLAYER_GUILD_UPDATE()
local info = self.db.char.charInfo
info.guild, info.guildRank = GetGuildInfo('player')
end
function PvpScribe:ClearStats()
local char = self.db.char
char.summary = {
count = 0,
wins = 0,
killingBlows = 0,
honorKills = 0,
deaths = 0,
honorGained = 0,
damageDone = 0,
healingDone = 0,
rankSum = 0,
rankMax = 0,
}
char.wgStats = {
count = 0,
flagsCaptured = 0,
flagsReturned = 0,
}
char.abStats = {
count = 0,
basesAssaulted = 0,
basesDefended = 0,
}
char.avStats = {
count = 0,
graveyardsAssaulted = 0,
graveyardsDefended = 0,
towersAssaulted = 0,
towersDefended = 0,
minesCaptured = 0,
leadersKilled = 0,
secondaryObjectives = 0,
}
char.signature = {
summary = '',
wgStats = '',
abStats = '',
avStats = '',
}
char.history = {}
PvpScribe:GenerateSignatures()
UIErrorsFrame:AddMessage('PvP Statistics Cleared', 0, 0.5, 1.0, 1.0, 10)
end
function PvpScribe:ShowStats()
local summary = self.db.char.summary
local wgStats = self.db.char.wgStats
local abStats = self.db.char.abStats
local avStats = self.db.char.avStats
-- This is a friggin' ugly mess. But, until I start mucking about
-- with some XML for a proper display it will have to do.
local message = '|cFF00FF00PvP Statistics|r\n\n' ..
string.format('Battles: %d\n', summary.count) ..
string.format('Wins: %d\n', summary.wins) ..
string.format('Killing blows: %d\n', summary.killingBlows) ..
string.format('Honor kills: %d\n', summary.honorKills) ..
string.format('Deaths: %d\n', summary.deaths) ..
string.format('Honor gained: %d\n', summary.honorGained) ..
string.format('Damage done: %d\n', summary.damageDone) ..
string.format('Healing done: %d\n', summary.healingDone) ..
'\n|cFF00FF00Warsong Gulch Statistics|r\n\n' ..
string.format('Battles: %d\n', wgStats.count) ..
string.format('Flags captured: %d\n', wgStats.flagsCaptured) ..
string.format('Flags returned: %d\n', wgStats.flagsReturned) ..
'\n|cFF00FF00Arathi Basin Statistics|r\n\n' ..
string.format('Battles: %d\n', abStats.count) ..
string.format('Bases assaulted: %d\n', abStats.basesAssaulted) ..
string.format('Bases defended: %d\n', abStats.basesDefended) ..
'\n|cFF00FF00Alterac Valley Statistics|r\n\n' ..
string.format('Battles: %d\n', avStats.count) ..
string.format('Graveyards assaulted: %d\n',
avStats.graveyardsAssaulted) ..
string.format('Graveyards defended: %d\n',
avStats.graveyardsDefended) ..
string.format('Towers assaulted: %d\n', avStats.towersAssaulted) ..
string.format('Towers defended: %d\n', avStats.towersDefended) ..
string.format('Mines captured: %d\n', avStats.minesCaptured) ..
string.format('Leaders killed: %d\n', avStats.leadersKilled) ..
string.format('Secondary objectives: %d\n',
avStats.secondaryObjectives)
UIErrorsFrame:AddMessage(message, 0, 0.5, 1.0, 1.0, 10)
end
function PvpScribe:ExamineBGStatus()
local map = {};
if not PvpScribe.statsRecorded then
for i = 1, MAX_BATTLEFIELD_QUEUES do
map = {}
local status, mapName, instanceID = GetBattlefieldStatus(i);
-- find the current battleground instance
if (status == 'active') then
if not PvpScribe.currentBattlegroundStart then
PvpScribe.currentBattlegroundStart =
date('%Y-%m-%d %H:%M:%S')
PvpScribe.currentBattlegroundName = mapName
self:LevelDebug(3,
'set currentBattlegroundName = ' .. mapName)
PvpScribe.currentBattlegroundID = instanceID
end
local winner = GetBattlefieldWinner()
if winner then
PvpScribe:RecordBGScore(winner)
PvpScribe.statsRecorded = true
end
end
end
end
end
function PvpScribe:RecordBGScore(winner)
local n = GetNumBattlefieldScores()
local m = GetNumBattlefieldStats()
local bgRecord = {}
local bgAuxStat = {}
local bgName = PvpScribe.currentBattlegroundName
local archRecord = ''
for i = 1, n do
local name, killingBlows, honorKills, deaths, honorGained, faction,
rank, race, class, filename, damageDone, healingDone =
GetBattlefieldScore(i);
if name == self.db.char.charInfo.name then
local isWin
if winner == faction then
isWin = true
else
isWin = false
end
bgRecord = {
beginTime = PvpScribe.currentBattlegroundStart,
endTime = date('%Y-%m-%d %H:%M:%S'),
battleground = bgName,
win = isWin,
killingBlows = killingBlows,
honorKills = honorKills,
deaths = deaths,
honorGained = honorGained,
damageDone = damageDone,
healingDone = healingDone,
rank = i,
population = n,
}
-- Gather battleground-specific stats
for j = 1, m do
bgAuxStat[j] = GetBattlefieldStatData(i, j)
end
end
end
-- Stash the current DB signatures
PvpScribe:StoreSignatures()
-- Add data to the collected statistics
PvpScribe:UpdateSummaryStats(bgRecord)
-- Add battleground-specific data
PvpScribe:UpdateBattlegroundStats(bgName, bgRecord, bgAuxStat)
-- Generate the new DB signatures
PvpScribe:GenerateSignatures()
-- Build the archival record
archRecord = PvpScribe:BuildArchiveRecord(bgRecord, bgAuxStat)
self:LevelDebug(3, 'created record ' .. tostring(name))
-- Push the record to the historical data queue
local history = self.db.char.history
local histmax = self.db.char.config.maxHistory
table.insert(history, 1, archRecord)
while table.maxn(history) > histmax do
table.remove(history)
end
end
function PvpScribe:UpdateSummaryStats(rec)
local db = self.db.char.summary
db.count = db.count + 1
if rec.win then
db.wins = db.wins + 1
end
db.killingBlows = db.killingBlows + rec.killingBlows
db.honorKills = db.honorKills + rec.honorKills
db.deaths = db.deaths + rec.deaths
db.honorGained = db.honorGained + rec.honorGained
db.damageDone = db.damageDone + rec.damageDone
db.healingDone = db.healingDone + rec.healingDone
db.rankSum = db.rankSum + rec.rank
db.rankMax = db.rankMax + rec.population
end
function PvpScribe:UpdateBattlegroundStats(name, rec, aux)
if name == 'Warsong Gulch' then
local db = self.db.char.wgStats
db.count = db.count + 1
db.flagsCaptured = db.flagsCaptured + aux[1]
db.flagsReturned = db.flagsReturned + aux[2]
elseif name == 'Arathi Basin' then
local db = self.db.char.abStats
db.count = db.count + 1
db.basesAssaulted = db.basesAssaulted + aux[1]
db.basesDefended = db.basesDefended + aux[2]
elseif name == 'Alterac Valley' then
local db = self.db.char.avStats
db.count = db.count + 1
db.graveyardsAssaulted = db.graveyardsAssaulted + aux[1]
db.graveyardsDefended = db.graveyardsDefended + aux[2]
db.towersAssaulted = db.towersAssaulted + aux[3]
db.towersDefended = db.towersDefended + aux[4]
db.minesCaptured = db.minesCaptured + aux[5]
db.leadersKilled = db.leadersKilled + aux[6]
db.secondaryObjectives = db.secondaryObjectives + aux[7]
else
self:LevelDebug(2, 'battleground stats not supported for ' .. name)
end
end
function PvpScribe:BuildArchiveRecord(rec, aux)
local arch = PvpScribe:FreezeRecord(rec)
arch = arch .. '/'
arch = arch .. PvpScribe:FreezeRecord(aux)
arch = arch .. '/'
arch = arch .. PvpScribe:CheckSum(arch)
return arch
end
function PvpScribe:FreezeRecord(rec)
local temp = {}
for index, value in pairs(rec) do
local code
if index == 'count' then
code = 'ct'
elseif index == 'beginTime' then
code = 'bt'
elseif index == 'endTime' then
code = 'et'
elseif index == 'battleground' then
code = 'bg'
elseif index == 'win' then
code = 'wn'
elseif index == 'killingBlows' then
code = 'kb'
elseif index == 'honorKills' then
code = 'hk'
elseif index == 'deaths' then
code = 'de'
elseif index == 'honorGained' then
code = 'hg'
elseif index == 'damageDone' then
code = 'dd'
elseif index == 'healingDone' then
code = 'hd'
elseif index == 'rank' then
code = 'rk'
elseif index == 'population' then
code = 'po'
elseif index == 'flagsCaptured' then
code = 'fc'
elseif index == 'flagsReturned' then
code = 'fr'
elseif index == 'basesAssaulted' then
code = 'ba'
elseif index == 'basesDefended' then
code = 'bd'
elseif index == 'graveyardsAssaulted' then
code = 'ga'
elseif index == 'graveyardsDefended' then
code = 'gd'
elseif index == 'towersAssaulted' then
code = 'ta'
elseif index == 'towersDefended' then
code = 'td'
elseif index == 'minesCaptured' then
code = 'mc'
elseif index == 'leadersKilled' then
code = 'lk'
elseif index == 'secondaryObjectives' then
code = 'so'
else
self:LevelDebug(2, 'no code mapping for ' .. index)
code = ''
end
if code ~= '' then
table.insert(temp, code .. '=' .. tostring(value))
end
end
return table.concat(temp, ':')
end
function PvpScribe:StoreSignatures()
-- Record the current signature hashes for future comparison
local char = self.db.char
local sig = char.signature
for _, v in pairs({ 'summary', 'wgStats', 'abStats', 'avStats' }) do
PvpScribe.hashStore[v] =
PvpScribe:CheckSum(PvpScribe:FreezeRecord(char[v]))
if PvpScribe.hashStore[v] ~= sig[v] then
self:LevelDebug(2, 'bad signature detected for ' .. v)
end
end
end
function PvpScribe:GenerateSignatures()
-- Sign the current database values
local char = self.db.char
local sig = char.signature
for _, v in pairs({ 'summary', 'wgStats', 'abStats', 'avStats' }) do
if PvpScribe.hashStore[v] and PvpScribe.hashStore[v] ~= sig[v] then
sig[v] = ''
else
sig[v] = PvpScribe:CheckSum(PvpScribe:FreezeRecord(char[v]))
end
end
PvpScribe.hashStore = {}
end
function PvpScribe:CheckSum(str)
-- Implementation of a Fletcher 8-bit checksum, as described in
-- RFC 1146, adapted for ASCII strings.
local a = 0
local b = 0
local n = string.len(str)
for i = 1, n do
local d = string.byte(str, i)
a = (a + d) % 256
b = (b + a) % 256
end
return string.format('%02x%02x', a, b)
end