-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLibGroupCombatStats.lua
More file actions
1499 lines (1274 loc) · 56 KB
/
LibGroupCombatStats.lua
File metadata and controls
1499 lines (1274 loc) · 56 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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- SPDX-FileCopyrightText: 2025 m00nyONE
-- SPDX-License-Identifier: Artistic-2.0
--[[ doc.lua begin ]]
--- @class LibGroupCombatStats
local lib = {
name = "LibGroupCombatStats",
version = "dev",
}
local lib_debug = false
local lib_name = lib.name
local lib_version = lib.version
_G[lib_name] = lib
--[[ doc.lua end ]]
local HPS = "HPS"
local DPS = "DPS"
local ULT = "ULT"
local SKILLLINES = "SKILLLINES"
local LGB = LibGroupBroadcast
local EM = EVENT_MANAGER
local SDM = SKILLS_DATA_MANAGER
local LocalEM = ZO_CallbackObject:New()
local strmatch = string.match
local _isFirstOnPlayerActivated = true
local _sendSyncRequest = true
local _LGBHandler = {}
local _LGBProtocols = {}
local _registeredAddons = {}
local _statsShared = {
[ULT] = false,
[DPS] = false,
[HPS] = false,
[SKILLLINES] = false,
}
local _ultIdMap = {}
local _ultInternalIdMap = {}
local _skillLinesIdMap = {}
local _skillLinesInternalIdMap = {}
--- logging setup
local mainLogger
local subLoggers = {}
local LOG_LEVEL_ERROR = "E"
local LOG_LEVEL_WARNING ="W"
local LOG_LEVEL_INFO = "I"
local LOG_LEVEL_DEBUG = "D"
local LOG_LEVEL_VERBOSE = "V"
if LibDebugLogger and not IsConsoleUI() then
mainLogger = LibDebugLogger.Create(lib_name)
LOG_LEVEL_ERROR = LibDebugLogger.LOG_LEVEL_ERROR
LOG_LEVEL_WARNING = LibDebugLogger.LOG_LEVEL_WARNING
LOG_LEVEL_INFO = LibDebugLogger.LOG_LEVEL_INFO
LOG_LEVEL_DEBUG = LibDebugLogger.LOG_LEVEL_DEBUG
LOG_LEVEL_VERBOSE = LibDebugLogger.LOG_LEVEL_VERBOSE
subLoggers["broadcast"] = mainLogger:Create("broadcast")
subLoggers["encoding"] = mainLogger:Create("encoding")
subLoggers["events"] = mainLogger:Create("events")
subLoggers["debug"] = mainLogger:Create("debug")
end
--- utility functions
local function Log(category, level, ...)
if not mainLogger then return end
if category == "debug" and not lib_debug then return end
local logger = subLoggers[category] or mainLogger
if type(logger.Log)=="function" then logger:Log(level, ...) end
end
--- constants
local localPlayer = "player"
local MESSAGE_ID_ULTTYPE = 20
local MESSAGE_ID_ULTVALUE = 21
local MESSAGE_ID_DPS = 22
local MESSAGE_ID_HPS = 23
local MESSAGE_ID_SKILLLINES = 24
local MESSAGE_ID_ULTACTIVATEDSETS = 25
local PLAYER_ULT_VALUE_UPDATE_INTERVAL = 1000
local PLAYER_DPS_UPDATE_INTERVAL = 1000
local PLAYER_HPS_UPDATE_INTERVAL = 1000
local PLAYER_ULT_TYPE_SEND_ON_GROUP_CHANGE_DELAY = 1000
local PLAYER_ULT_VALUE_SEND_ON_GROUP_CHANGE_DELAY = 1000
local PLAYER_SKILLINES_SEND_ON_GROUP_CHANGE_DELAY = 1500
local PLAYER_ULT_VALUE_SEND_INTERVAL = 2000
local PLAYER_DPS_SEND_INTERVAL = 2000
local PLAYER_HPS_SEND_INTERVAL = 2000
local MAX_ULT_IDS = 0
local MAX_SKILL_LINE_IDS = 0
--[[ doc.lua begin ]]
local ULT_ACTIVATED_SET_LIST = {
{
name = "saxhleel",
link = "|H0:item:173857:364:50:0:0:0:0:0:0:0:0:0:0:0:1:0:0:1:0:0:0|h|h",
minEquipped = 3, -- we assume player has full set if he wears at least 3 items (2 can be on backbar)
},
{
name = "pillager",
link = "|H0:item:187028:364:50:0:0:0:2:0:0:0:0:0:0:0:1:0:0:1:0:0:0|h|h",
minEquipped = 3, -- we assume player has full set if he wears at least 3 items (2 can be on backbar)
},
{
name = "cryptcanon",
link = "|H0:item:194509:364:50:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h",
minEquipped = 1,
},
{
name = "MA", -- master architect
link = "|H0:item:124294:362:50:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h",
minEquipped = 3,
},
{
name = "WM", -- warmachine
link = "|H0:item:124112:362:50:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h",
minEquipped = 3,
},
}
--- export set list, so it can be used to map the ultActivatedSetID to a real set
lib.ULT_ACTIVATED_SET_LIST = ULT_ACTIVATED_SET_LIST
local SPECIAL_ULTIMATE_ABILITY_IDS = {
195031, --cryptcannon
}
--- often used variables
local PLAYER_CHARACTER_NAME = GetUnitName(localPlayer)
local PLAYER_DISPLAY_NAME = GetUnitDisplayName(localPlayer)
local PLAYER_BASE_CLASS = GetUnitClassId(localPlayer)
--- exported constants
local DAMAGE_UNKNOWN = 0
local DAMAGE_TOTAL = 1
local DAMAGE_BOSS = 2
lib.DAMAGE_UNKNOWN = DAMAGE_UNKNOWN
lib.DAMAGE_TOTAL = DAMAGE_TOTAL
lib.DAMAGE_BOSS = DAMAGE_BOSS
--- Events exposed by the library for group and player-specific combat statistics. These can be used to register for updates from the library
local EVENT_GROUP_DPS_UPDATE = "EVENT_GROUP_DPS_UPDATE" -- Event triggered when group DPS stats are updated
local EVENT_GROUP_HPS_UPDATE = "EVENT_GROUP_HPS_UPDATE" -- Event triggered when group HPS stats are updated
local EVENT_GROUP_ULT_UPDATE = "EVENT_GROUP_ULT_UPDATE" -- Event triggered when group ultimate stats are updated
local EVENT_GROUP_SKILLLINES_UPDATE = "EVENT_GROUP_SKILLLINES_UPDATE" -- Event triggered when group skill lines are updated
local EVENT_PLAYER_DPS_UPDATE = "EVENT_PLAYER_DPS_UPDATE" -- Event triggered when player DPS stats are updated
local EVENT_PLAYER_HPS_UPDATE = "EVENT_PLAYER_HPS_UPDATE" -- Event triggered when player HPS stats are updated
local EVENT_PLAYER_ULT_UPDATE = "EVENT_PLAYER_ULT_UPDATE" -- Event triggered when player ultimate stats are updated
local EVENT_PLAYER_SKILLLINES_UPDATE = "EVENT_PLAYER_SKILLLINES_UPDATE" -- Event triggered when player skill lines are updated
local EVENT_PLAYER_ULT_VALUE_UPDATE = "EVENT_PLAYER_ULT_VALUE_UPDATE" -- Event triggered when player ultimate value stats are updated
local EVENT_PLAYER_ULT_TYPE_UPDATE = "EVENT_PLAYER_ULT_TYPE_UPDATE" -- Event triggered when player ultimate type stats are updated
lib.EVENT_GROUP_DPS_UPDATE = EVENT_GROUP_DPS_UPDATE
lib.EVENT_GROUP_HPS_UPDATE = EVENT_GROUP_HPS_UPDATE
lib.EVENT_GROUP_ULT_UPDATE = EVENT_GROUP_ULT_UPDATE
lib.EVENT_GROUP_SKILLLINES_UPDATE = EVENT_GROUP_SKILLLINES_UPDATE
lib.EVENT_PLAYER_DPS_UPDATE = EVENT_PLAYER_DPS_UPDATE
lib.EVENT_PLAYER_HPS_UPDATE = EVENT_PLAYER_HPS_UPDATE
lib.EVENT_PLAYER_ULT_UPDATE = EVENT_PLAYER_ULT_UPDATE
lib.EVENT_PLAYER_SKILLLINES_UPDATE = EVENT_PLAYER_SKILLLINES_UPDATE
lib.EVENT_PLAYER_ULT_VALUE_UPDATE = EVENT_PLAYER_ULT_VALUE_UPDATE --- usually not needed
lib.EVENT_PLAYER_ULT_TYPE_UPDATE = EVENT_PLAYER_ULT_TYPE_UPDATE --- usually not needed
--[[ doc.lua end ]]
--[[ doc.lua begin ]]
--- Returns the classId which the skillLine originates from
--- @return number: classId
function lib.GetClassIdFromSkillLineId(skillLineId)
return GetSkillLineClassId(GetSkillLineIndicesFromSkillLineId(skillLineId))
end
--- Returns the icon of the class which the skillLine originates from
--- @return string: texturePath of the base class icon
function lib.GetClassIconFromSkillLineId(skillLineId)
return ZO_GetPlatformClassIcon(lib.GetClassIdFromSkillLineId(skillLineId))
end
--- Returns the collectibles class mastery icon of the class which the skillLine originates from
--- @return string: texturePath of the collectibles class mastery icon
function lib.GetFancyClassIconFromSkillLineId(skillLineId)
return GetCollectibleIcon(GetSkillLineMasteryCollectibleId(skillLineId))
end
--[[ doc.lua end ]]
--- The ObservableTable allows for firing callbacks when values are updated
local ObservableTable = {}
ObservableTable.__index = ObservableTable
-- Constructor for the ObservableTable class
-- @param onChangeCallback (function): A callback function triggered when the table is changed
-- @param fireAfterLastChangeMS (number): Delay in milliseconds to wait after the last change before firing the callback (default is 0, which triggers immediately)
-- @param initTable (table): An optional initial table to populate the observable table
-- @return (table): A new instance of ObservableTable
function ObservableTable:New(onChangeCallback, fireAfterLastChangeMS, initTable)
-- Validate that the callback is a function
assert(type(onChangeCallback) == "function", "onChangeCallback must be a function")
-- Define the internal onChange function to handle delayed callbacks
local onChange = function(instance)
-- If no delay is specified, trigger the callback immediately
if instance._fireAfterLastChangeMS == 0 then
instance._onChangeCallback(instance._data)
return
end
-- Unique update event name for this instance
local updateName = instance._eventId
-- Unregister any previous delayed callback
EM:UnregisterForUpdate(updateName)
-- Register a new delayed callback
EM:RegisterForUpdate(updateName, instance._fireAfterLastChangeMS, function()
instance._onChangeCallback(instance._data) -- Trigger the callback with the current table data
EM:UnregisterForUpdate(updateName) -- Ensure the callback is unregistered after execution
end)
end
-- Create the new instance
local instance = {
_data = initTable or {}, -- Internal data storage (backing table)
_onChange = onChange, -- Internal function to handle changes
_onChangeCallback = onChangeCallback, -- User-provided callback function
_fireAfterLastChangeMS = fireAfterLastChangeMS or 0, -- Delay in milliseconds before firing the callback
_lastUpdated = 0, -- Timestamp of the last update (in milliseconds)
_lastChanged = 0, -- Timestamp of the last update (in milliseconds)
_eventId = "" -- Unique update event name for this instance
}
-- Set the metatable for the new instance
local newObservableTable = setmetatable(instance, self)
-- create unique _eventId by getting the address of the table
newObservableTable._eventId = "ObservableTable_" .. strmatch(tostring(newObservableTable), "0%x+")
return newObservableTable
end
-- Override __index to read values from the internal data storage
-- @param key (string): The key being accessed
-- @return (any): The value associated with the key in the internal data table
function ObservableTable:__index(key)
return self._data[key]
end
-- Override __newindex to detect changes to the table
-- @param key (string): The key being modified
-- @param value (any): The new value being assigned to the key
function ObservableTable:__newindex(key, value)
-- It's important to check if the values are different otherwise every write fires the callback
local oldValue = rawget(self._data, key)
local t = GetGameTimeMilliseconds()
rawset(self, "_lastUpdated", t) -- Update the last modification attempt timestamp
if oldValue ~= value then
rawset(self, "_lastChanged", t) -- Update the last modification timestamp
rawset(self._data, key, value) -- Update the value in the internal data storage
self._onChange(self) -- Trigger the internal onChange handler
end
end
--[[ doc.lua begin ]]
--- @class ult
--- @field ultValue number raw ult points
--- @field ult1ID number id of the frontbar ultimate
--- @field ult2ID number id of the backbar ultimate
--- @field ult1Cost number cost of the ult on the frontbar
--- @field ult2Cost number cost of the ult on the backbar
--- @field ultActivatedSetID number ult activated set the player is wearing
--- @field _lastUpdated number timestamp of the last table update
--- @field _lastChanged number timestamp of the last table real change
--- @class dps
--- @field dmgType number damage type [0..2]
--- @field dps number single target dps in thousands
--- @field dmg number overall dps in thousands or overall damage in millions
--- @field _lastUpdated number timestamp of the last table update
--- @field _lastChanged number timestamp of the last table real change
--- @class hps
--- @field hps number heal per second the group is consuming
--- @field overheal number raw heal per second that is pushed out
--- @field _lastUpdated number timestamp of the last table update
--- @field _lastChanged number timestamp of the last table real change
---@class skillLines
---@field first number first skillLine
---@field second number second skillLine
---@field third number third skillLine
---@field _lastUpdated number timestamp of the last table update
---@field _lastChanged number timestamp of the last table real change
--[[ doc.lua end ]]
-- groupStats base table containing all collected data
local groupStats = {
[PLAYER_CHARACTER_NAME] = {
tag = localPlayer,
name = PLAYER_CHARACTER_NAME,
displayName = PLAYER_DISPLAY_NAME,
isPlayer = true,
isOnline = true,
ult = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_PLAYER_ULT_UPDATE, localPlayer, data)
end, 10, {
ultValue = 0,
ult1ID = 0,
ult2ID = 0,
ult1Cost = 0,
ult2Cost = 0,
ultActivatedSetID = 0,
}),
dps = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_PLAYER_DPS_UPDATE, localPlayer, data)
end, 10, {
dmgType = 0,
dmg = 0,
dps = 0,
}),
hps = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_PLAYER_HPS_UPDATE, localPlayer, data)
end, 10, {
overheal = 0,
hps = 0,
}),
skillLines = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_PLAYER_SKILLLINES_UPDATE, localPlayer, data)
end, 10, {
first = PLAYER_BASE_CLASS,
second = PLAYER_BASE_CLASS,
third = PLAYER_BASE_CLASS,
})
}
}
local playerStats = groupStats[PLAYER_CHARACTER_NAME] -- local alias for the stats of the player
--[[ doc.lua begin ]]
--- _CombatStatsObject which can be used by other addons to get data or register callbacks for events - it acts as a communication gateway between addons & the lib
--- @class CombatStatsObject
local _CombatStatsObject = {}
_CombatStatsObject.__index = _CombatStatsObject
--- Constructor for the _CombatStatsObject
--- @return CombatStatsObject A new instance of _CombatStatsObject
function _CombatStatsObject:New()
local obj = setmetatable({}, _CombatStatsObject)
return obj
end
--- Returns a list of functionalities currently enabled in the library
--- @return (string, string, string): Currently enabled functionalities ("DPS", "HPS", "ULT")
function _CombatStatsObject:GetStatsShared()
return ZO_DeepTableCopy(_statsShared)
end
--- Returns key, value of groupStats
--- @return (string, table) key value pairs of groupStats
function _CombatStatsObject:Iterate()
local key, value
return function()
key, value = next(groupStats, key)
if not key then
return nil
end
local stats = groupStats[key]
local ult = stats.ult
local dps = stats.dps
local hps = stats.hps
local skillLines = stats.skillLines
return stats.tag, {
tag = stats.tag,
name = stats.name,
displayName = stats.displayName,
isPlayer = stats.isPlayer,
ult = {
ultValue = ult.ultValue,
ult1ID = ult.ult1ID,
ult2ID = ult.ult2ID,
ult1Cost = ult.ult1Cost,
ult2Cost = ult.ult2Cost,
ultActivatedSetID = ult.ultActivatedSetID,
_lastUpdated = ult._lastUpdated,
_lastChanged = ult._lastChanged
},
dps = {
dmgType = dps.dmgType,
dps = dps.dps,
dmg = dps.dmg,
_lastUpdated = dps._lastUpdated,
_lastChanged = dps._lastChanged
},
hps = {
hps = hps.hps,
overheal = hps.overheal,
_lastUpdated = hps._lastUpdated,
_lastChanged = hps._lastChanged
},
skillLines = {
first = skillLines.first,
second = skillLines.second,
third = skillLines.third,
_lastUpdated = skillLines._lastUpdated,
_lastChanged = skillLines._lastChanged
}
}
end
end
--- metatable version of _CombatStatsObject:Iterate()
--- @return (string, table) key value pairs of groupStats
function _CombatStatsObject:__pairs()
return self:Iterate()
end
--- Returns the number of group members in "groupStats"
--- @return (number): the number of units in the group
function _CombatStatsObject:GetGroupSize()
return #groupStats
end
--- metatable version of _CombatStatsObject:GetGroupSize()
--- @return (number): the number of units in the group
function _CombatStatsObject:__len()
return self:GetGroupSize()
end
--- Retrieves a copy of the current group statistics
--- @return table A table containing group statistics (cloned from the internal state)
function _CombatStatsObject:GetGroupStats()
local result = {}
for tag, stats in self:Iterate() do
result[tag] = stats
end
return result
end
--- Retrieves statistics for a specific unit in the group
--- @param unitTag string The unitTag of the group member (e.g., "group1")
--- @return table A table containing the unit's statistics, or nil if the unit is not found
function _CombatStatsObject:GetUnitStats(unitTag)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return nil
end
local ult = unit.ult
local dps = unit.dps
local hps = unit.hps
local skillLines = unit.skillLines
local result = {
tag = unitTag,
name = unit.name,
displayName = unit.displayName,
isPlayer = unit.isPlayer,
ult = {
ultValue = ult.ultValue,
ult1ID = ult.ult1ID,
ult2ID = ult.ult2ID,
ult1Cost = ult.ult1Cost,
ult2Cost = ult.ult2Cost,
ultActivatedSetID = ult.ultActivatedSetID,
_lastUpdated = ult._lastUpdated,
_lastChanged = ult._lastChanged
},
dps = {
dmgType = dps.dmgType,
dps = dps.dps,
dmg = dps.dmg,
_lastUpdated = dps._lastUpdated,
_lastChanged = dps._lastChanged
},
hps = {
hps = hps.hps,
overheal = hps.overheal,
_lastUpdated = hps._lastUpdated,
_lastChanged = hps._lastChanged
},
skillLines = {
first = skillLines.first,
second = skillLines.second,
third = skillLines.third,
_lastUpdated = skillLines._lastUpdated,
_lastChanged = skillLines._lastChanged
}
}
return result
end
--- Retrieves DPS information for a specific unit in the group
--- @param unitTag string The unitTag of the group member
--- @return dps DPSTable The type of damage, total damage, DPS value, and the timestamp of the last update and last value update
function _CombatStatsObject:GetUnitDPS(unitTag)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return nil
end
return unit.dps
end
--- Retrieves HPS information for a specific unit in the group
--- @param unitTag string The unitTag of the group member
--- @return hps HPSTable The overhealing value, HPS value, and the timestamp of the last update and last value update
function _CombatStatsObject:GetUnitHPS(unitTag)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return nil
end
return unit.hps
end
-- TODO: hasUnitULTAbilityID
--- Retrieves ultimate information for a specific unit in the group
--- @param unitTag string The unitTag of the group member
--- @return ult ultTable The current ultimate value, ultimate 1 ID, ultimate 1 cost, ultimate 2 ID, ultimate 2 cost, and the ID for an ultActivated set, and the timestamp of the last update and last value update
function _CombatStatsObject:GetUnitULT(unitTag)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return nil
end
return unit.ult
end
--- Retrieves skillLine information for a specific unit in the group
--- @param unitTag string The unitTag of the group member
--- @return skillLines skillLinesTable The currently equipped skillLines of the unit (first, second, third) and the timestamp of the last update and last value update
function _CombatStatsObject:GetUnitSkillLines(unitTag)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return nil
end
return unit.skillLines
end
--- Checks if the group member has specific ultimates slotted
--- @param unitTag string The unitTag of the group member
--- @param listOfAbilityIDs number[] The abilityIDs that need to be checked for ( {id1, id2, id3} )
--- @return boolean hasEquipped group member has ultimate ability equipped
function _CombatStatsObject:HasUnitUltimatesSlotted(unitTag, listOfAbilityIDs)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return false
end
if not type(listOfAbilityIDs) == "table" then
Log("debug", LOG_LEVEL_WARNING, "listOfAbilityIDs is not a list of abilityIDs")
return false
end
local ult1ID = unit.ult.ult1ID
local ult2ID = unit.ult.ult2ID
for _, abilityID in ipairs(listOfAbilityIDs) do
if ult1ID == abilityID or ult2ID == abilityID then return true end
end
return false
end
--- WARNING: This will soon be changed! Thats why this function is not documented in the API - use the new LibSetDetection v4 by @ExoY94 for that when it's ready
--- Checks if the group member has a specific ultimate activated set slotted
--- @param unitTag string The unitTag of the group member
--- @param ultActivatedSetID number The ultActivatedSetID that needs to be checked for
--- @return boolean hasEquipped group member has ultActivatedSet equipped
function _CombatStatsObject:HasUnitUltActivatedSetSlotted(unitTag, ultActivatedSetID)
local characterName = GetUnitName(unitTag)
local unit = groupStats[characterName]
if not unit then
Log("debug", LOG_LEVEL_DEBUG, "unit does not exist in groupStats")
return false
end
if unit.ult.ultActivatedSetID == ultActivatedSetID then return true end
return false
end
--- Registers a callback function for a specified event
--- @param eventName string The name of the event to register for
--- @param callback function The function to be called when the event is triggered
function _CombatStatsObject:RegisterForEvent(eventName, callback)
assert(type(callback) == "function", "callback must be a function")
assert(type(eventName) == "string", "eventName must be a string")
LocalEM:RegisterCallback(eventName, callback)
Log("events", LOG_LEVEL_DEBUG, "callback for %s registered", eventName)
end
--- Unregisters a callback function for a specified event
--- @param eventName string The name of the event to unregister from
--- @param callback function The callback function to unregister
function _CombatStatsObject:UnregisterForEvent(eventName, callback)
assert(type(callback) == "function", "callback must be a function")
assert(type(eventName) == "string", "eventName must be a string")
Log("events", LOG_LEVEL_DEBUG, "callback for %s unregistered", eventName)
LocalEM:UnregisterCallback(eventName, callback)
end
--[[ doc.lua end ]]
--- group change tracking
local function OnGroupChange()
local _existingGroupCharacters = {} -- create empty table to create a list of all groupmembers after the change
local _groupSize = GetGroupSize()
for i = 1, _groupSize do
local tag = GetGroupUnitTagByIndex(i)
if IsUnitPlayer(tag) then
local isPlayer = AreUnitsEqual(tag, localPlayer)
local characterName = GetUnitName(tag)
_existingGroupCharacters[characterName] = true
if not isPlayer then
local baseClassId = GetUnitClassId(tag) or 0
groupStats[characterName] = groupStats[characterName] or {
name = characterName,
displayName = GetUnitDisplayName(tag),
isPlayer = isPlayer,
isOnline = IsUnitOnline(tag),
ult = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_GROUP_ULT_UPDATE, groupStats[characterName].tag, data)
end, 10, {
ultValue = 0,
ult1ID = 0,
ult2ID = 0,
ult1Cost = 0,
ult2Cost = 0,
ultActivatedSetID = 0,
}),
dps = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_GROUP_DPS_UPDATE, groupStats[characterName].tag, data)
end, 10, {
dmgType = 0,
dmg = 0,
dps = 0,
}),
hps = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_GROUP_HPS_UPDATE, groupStats[characterName].tag, data)
end, 10, {
overheal = 0,
hps = 0,
}),
skillLines = ObservableTable:New(function(data)
LocalEM:FireCallbacks(EVENT_GROUP_SKILLLINES_UPDATE, groupStats[characterName].tag, data)
end, 10, {
first = baseClassId,
second = baseClassId,
third = baseClassId,
}),
}
end
groupStats[characterName].tag = tag
--groupStats[characterName].isOnline = IsUnitOnline(tag)
end
end
for characterName, _ in pairs(groupStats) do
if characterName ~= PLAYER_CHARACTER_NAME then
if not _existingGroupCharacters[characterName] then
groupStats[characterName] = nil
end
end
end
end
local LC = LibCombat
local combat = {}
local LIBCOMBAT_CALLBACK_NAME = lib_name .. "_Combat"
local combatData = {
DPSOut = 0,
HPSOut = 0,
OHPSOut = 0,
dpstime = 0,
hpstime = 0,
bossFight = false,
bossDamageTotal = 0,
damageOutTotal = 0,
bossTime = 0
}
function combat.InitData()
combatData = {
DPSOut = 0,
HPSOut = 0,
OHPSOut = 0,
dpstime = 0,
hpstime = 0,
bossFight = false,
bossDamageTotal = 0,
damageOutTotal = 0,
bossTime = 0
}
end
function combat.FightRecapCallback(_, data)
combatData.DPSOut = data.DPSOut or 0
combatData.HPSOut = data.HPSOut or 0
combatData.OHPSOut = data.OHPSOut or 0
combatData.dpstime = data.dpstime or 0
combatData.hpstime = data.hpstime or 0
combatData.bossFight = data.bossFight or false
combatData.bossDamageTotal = data.bossDamageTotal or 0
combatData.damageOutTotal = data.damageOutTotal or 0
combatData.bossTime = data.bossTime or 0
end
function combat.Register()
combat.InitData()
LC:RegisterCallbackType(LIBCOMBAT_EVENT_FIGHTRECAP, combat.FightRecapCallback, LIBCOMBAT_CALLBACK_NAME)
Log("events", LOG_LEVEL_DEBUG, "registered to LibCombat")
end
function combat.Unregister()
combat.InitData()
LC:UnregisterCallbackType(LIBCOMBAT_EVENT_FIGHTRECAP, combat.FightRecapCallback, LIBCOMBAT_CALLBACK_NAME)
Log("events", LOG_LEVEL_DEBUG, "unregistered from LibCombat")
end
function combat.Reset()
combat.InitData()
end
function combat.GetData()
return combatData
end
function combat.GetCombatTime()
return zo_roundToNearest(zo_max(combatData.dpstime, combatData.hpstime), 0.1)
end
---
local function GetBaseAbilityId(rawAbilityId)
if rawAbilityId == 0 then return 0 end
local progressionData = SDM:GetProgressionDataByAbilityId(rawAbilityId)
if not progressionData then return rawAbilityId end
return progressionData.abilityId
end
--- update player values
local function updatePlayerUltValue()
playerStats.ult.ultValue = zo_max(0, zo_min(500, GetUnitPower(localPlayer, COMBAT_MECHANIC_FLAGS_ULTIMATE)))
LocalEM:FireCallbacks(EVENT_PLAYER_ULT_VALUE_UPDATE, localPlayer, playerStats.ult)
end
local function updatePlayerDps()
local dmgType = 0
local dmg = 0
local dps = 0
local data = combat.GetData()
if data.DPSOut == 0 then
dmgType, dmg, dps = DAMAGE_UNKNOWN, 0, 0
else
if data.bossFight then
dmgType, dmg, dps = DAMAGE_BOSS, zo_floor(data.bossDamageTotal / data.bossTime / 100), zo_floor(data.DPSOut / 1000)
else
dmgType, dmg, dps = DAMAGE_TOTAL, zo_floor(data.damageOutTotal / 10000), zo_floor(data.DPSOut / 1000)
end
end
playerStats.dps.dmgType = dmgType
playerStats.dps.dmg = dmg
playerStats.dps.dps = dps
end
local function updatePlayerHps()
local data = combat.GetData()
-- If no HPS data available, set values to zero
if data.HPSOut == 0 and data.OHPSOut == 0 then
playerStats.hps.overheal = 0
playerStats.hps.hps = 0
return
end
-- Otherwise calculate values from the data
playerStats.hps.overheal = zo_floor(data.OHPSOut / 1000)
playerStats.hps.hps = zo_floor(data.HPSOut / 1000)
end
local function updatePlayerSlottedUlts()
-- reset values
playerStats.ult.ult1ID = 0
playerStats.ult.ult2ID = 0
playerStats.ult.ult1Cost = 0
playerStats.ult.ult2Cost = 0
-- populate values
local rawUlt1ID = GetSlotBoundId(ACTION_BAR_ULTIMATE_SLOT_INDEX + 1, HOTBAR_CATEGORY_PRIMARY)
local rawUlt2ID = GetSlotBoundId(ACTION_BAR_ULTIMATE_SLOT_INDEX + 1, HOTBAR_CATEGORY_BACKUP)
playerStats.ult.ult1ID = GetBaseAbilityId(rawUlt1ID)
playerStats.ult.ult2ID = GetBaseAbilityId(rawUlt2ID)
playerStats.ult.ult1Cost = GetAbilityCost(playerStats.ult.ult1ID, COMBAT_MECHANIC_FLAGS_ULTIMATE, nil, localPlayer)
playerStats.ult.ult2Cost = GetAbilityCost(playerStats.ult.ult2ID, COMBAT_MECHANIC_FLAGS_ULTIMATE, nil, localPlayer)
LocalEM:FireCallbacks(EVENT_PLAYER_ULT_TYPE_UPDATE, localPlayer, playerStats.ult)
end
local function updatePlayerUltimateCost()
local ult1Cost = GetAbilityCost(playerStats.ult.ult1ID, COMBAT_MECHANIC_FLAGS_ULTIMATE, nil, localPlayer)
local ult2Cost = GetAbilityCost(playerStats.ult.ult2ID, COMBAT_MECHANIC_FLAGS_ULTIMATE, nil, localPlayer)
if playerStats.ult.ult1Cost == ult1Cost and playerStats.ult.ult2Cost == ult2Cost then return end
playerStats.ult.ult1Cost = GetAbilityCost(playerStats.ult.ult1ID, COMBAT_MECHANIC_FLAGS_ULTIMATE, nil, localPlayer)
playerStats.ult.ult2Cost = GetAbilityCost(playerStats.ult.ult2ID, COMBAT_MECHANIC_FLAGS_ULTIMATE, nil, localPlayer)
LocalEM:FireCallbacks(EVENT_PLAYER_ULT_TYPE_UPDATE, localPlayer, playerStats.ult)
end
local function updatePlayerUltActivatedSets()
-- reset values
playerStats.ult.ultActivatedSetID = 0
-- populate values
for id, set in ipairs(ULT_ACTIVATED_SET_LIST) do
local _, _, _, nonPerfectedNum, _, _, perfectedNum = GetItemLinkSetInfo(set.link, true)
local num = nonPerfectedNum + (perfectedNum or 0)
if num >= set.minEquipped then
playerStats.ult.ultActivatedSetID = id
end
end
LocalEM:FireCallbacks(EVENT_PLAYER_ULT_TYPE_UPDATE, localPlayer, playerStats.ult)
end
local function updatePlayerSkillLines()
local playerSkillLineIds = {}
for skillLineIndex = 1, GetNumSkillLines(SKILL_TYPE_CLASS) do
local _, _, active, _ = GetSkillLineDynamicInfo(SKILL_TYPE_CLASS, skillLineIndex)
if active then
local id = GetSkillLineId(SKILL_TYPE_CLASS, skillLineIndex)
table.insert(playerSkillLineIds, id)
end
end
playerStats.skillLines.first = playerSkillLineIds[1]
playerStats.skillLines.second = playerSkillLineIds[2]
playerStats.skillLines.third = playerSkillLineIds[3]
LocalEM:FireCallbacks(EVENT_PLAYER_SKILLLINES_UPDATE, localPlayer, playerStats.skillLines)
end
local function unregisterPlayerStatsUpdateFunctions()
combat.Unregister()
EM:UnregisterForUpdate(lib_name .. "_ultValueUpdate")
EM:UnregisterForUpdate(lib_name .. "_ultCostUpdate")
EM:UnregisterForUpdate(lib_name .. "_dpsUpdate")
EM:UnregisterForUpdate(lib_name .. "_hpsUpdate")
EM:UnregisterForEvent(lib_name .. "_ultTypeUpdate", EVENT_ACTION_SLOTS_ALL_HOTBARS_UPDATED)
EM:UnregisterForEvent(lib_name .. "_ultTypeUpdate", EVENT_INVENTORY_SINGLE_SLOT_UPDATE)
EM:UnregisterForEvent(lib_name .. "_SkillLinesUpdate", EVENT_SKILL_LINE_ADDED)
Log("events", LOG_LEVEL_DEBUG, "playerStatsUpdate functions unregistered")
end
local function registerPlayerStatsUpdateFunctions()
combat.Register()
EM:RegisterForUpdate(lib_name .. "_ultValueUpdate", PLAYER_ULT_VALUE_UPDATE_INTERVAL, updatePlayerUltValue)
EM:RegisterForUpdate(lib_name .. "_ultCostUpdate", PLAYER_ULT_VALUE_UPDATE_INTERVAL, updatePlayerUltimateCost)
EM:RegisterForUpdate(lib_name .. "_dpsUpdate", PLAYER_DPS_UPDATE_INTERVAL, updatePlayerDps)
EM:RegisterForUpdate(lib_name .. "_hpsUpdate", PLAYER_HPS_UPDATE_INTERVAL, updatePlayerHps)
EM:RegisterForEvent(lib_name .. "_ultTypeUpdate", EVENT_ACTION_SLOTS_ALL_HOTBARS_UPDATED, updatePlayerSlottedUlts)
EM:AddFilterForEvent(lib_name .. "_ultTypeUpdate", EVENT_ACTION_SLOTS_ALL_HOTBARS_UPDATED, REGISTER_FILTER_POWER_TYPE, COMBAT_MECHANIC_FLAGS_ULTIMATE, REGISTER_FILTER_UNIT_TAG, localPlayer)
EM:RegisterForEvent(lib_name .. "_ultTypeUpdate", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, function() updatePlayerSlottedUlts() updatePlayerUltActivatedSets() end)
EM:AddFilterForEvent(lib_name .. "_ultTypeUpdate", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, REGISTER_FILTER_BAG_ID, BAG_WORN)
EM:RegisterForEvent(lib_name .. "_SkillLinesUpdate", EVENT_SKILL_LINE_ADDED, updatePlayerSkillLines)
Log("events", LOG_LEVEL_DEBUG, "playerStatsUpdate functions registered")
end
--- send broadcast messages
local function broadcastPlayerDps(_, force)
if not IsUnitGrouped(localPlayer) then return end
if not _statsShared[DPS] then return end
if not force and GetGameTimeMilliseconds() - playerStats.dps._lastChanged > PLAYER_DPS_SEND_INTERVAL then return end
local data = {
dmgType = playerStats.dps.dmgType,
dmg = playerStats.dps.dmg,
dps = playerStats.dps.dps
}
_LGBProtocols[MESSAGE_ID_DPS]:Send(data)
end
local function broadcastPlayerHps(_, force)
if not IsUnitGrouped(localPlayer) then return end
if not _statsShared[HPS] then return end
if not force and GetGameTimeMilliseconds() - playerStats.hps._lastChanged > PLAYER_HPS_SEND_INTERVAL then return end
local data = {
overheal = playerStats.hps.overheal,
hps = playerStats.hps.hps
}
_LGBProtocols[MESSAGE_ID_HPS]:Send(data)
end
local function broadcastPlayerUltValue(_, force)
if not IsUnitGrouped(localPlayer) then return end
if not _statsShared[ULT] then return end
if not force and GetGameTimeMilliseconds() - playerStats.ult._lastChanged > PLAYER_ULT_VALUE_SEND_INTERVAL then return end
local data = {
ultValue = zo_floor(playerStats.ult.ultValue / 2)
}
_LGBProtocols[MESSAGE_ID_ULTVALUE]:Send(data)
end
local function broadcastPlayerUltType()
if not IsUnitGrouped(localPlayer) then return end
if not _statsShared[ULT] then return end
local ult1InternalId = _ultIdMap[playerStats.ult.ult1ID] or 0
local ult2InternalId = _ultIdMap[playerStats.ult.ult2ID] or 0
local data = {
ult1ID = ult1InternalId,
ult2ID = ult2InternalId,
ult1Cost = zo_floor(playerStats.ult.ult1Cost / 2),
ult2Cost = zo_floor(playerStats.ult.ult2Cost / 2 ),
ultActivatedSetID = playerStats.ult.ultActivatedSetID,
syncRequest = _sendSyncRequest
}
_sendSyncRequest = false
_LGBProtocols[MESSAGE_ID_ULTTYPE]:Send(data)
end
local function broadcastPlayerSkillLines()
if not IsUnitGrouped(localPlayer) then return end
if not _statsShared[SKILLLINES] then return end
local firstInternalId = _skillLinesIdMap[playerStats.skillLines.first] or 0
local secondInternalId = _skillLinesIdMap[playerStats.skillLines.second] or 0
local thirdInternalId = _skillLinesIdMap[playerStats.skillLines.third] or 0
local data = {
first = firstInternalId,
second = secondInternalId,
third = thirdInternalId,
--syncRequest = _sendSyncRequest
}
--_sendSyncRequest = false
_LGBProtocols[MESSAGE_ID_SKILLLINES]:Send(data)
end
--- on demand sent broadcast messages
-- this ObservableTable is created to have a callback function waiting on further changes before broadcasting to avoid sending multiple messages when swapping loadouts
local playerUltTypeObservableTable = ObservableTable:New(broadcastPlayerUltType, 2000, {
lastChange = GetGameTimeMilliseconds(),
})
local playerSkillLinesObservableTable = ObservableTable:New(broadcastPlayerSkillLines, 2000, {
lastChange = GetGameTimeMilliseconds(),
})
-- writes to playerUltTypeObservableTable to trigger the broadcastPlayerUltType
local function onPlayerUltTypeUpdate(unitTag, _)
if unitTag ~= localPlayer then return end
playerUltTypeObservableTable.lastChange = GetGameTimeMilliseconds()
end
local function onPlayerSkillLinesUpdate(unitTag, _)
if unitTag ~= localPlayer then return end
playerSkillLinesObservableTable.lastChange = GetGameTimeMilliseconds()
end
local function onSyncRequestReceived()
if not IsUnitGrouped(localPlayer) then return end
if _statsShared[ULT] then