Zadrot Damage Meter

18.08.2020 - Zadrot

Zadrot Damage Meter

Последнее время прёт на аддоны для Вова :)
Вот ещё один

Zadrot Damage Meter - Маленький дамаг метр, у которого даже нет своего окна.
Количество дамага и хила показываются во всплывающей подсказке, которую, кстати, можно закрепить правой кнопкой.

Зачем этот дпс метр, если есть другие?

Что ещё забыл?
Позицию можно сбросить командой /zdm, а передвигать левой кнопкой мыши
Работает в Classic и Shadowlands pre-patch (правда особо не тестировал)
Это первая версия (или вторая), пожалуйста сообщайте о багах :)

  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
local damageTable = {}
local showTable = {}
local totaldamage = 0
local healTable = {}
local showTableHeal = {}
local totalHealing = 0
local colorTable = {}
local pets = {}
local reflects = {}
local combat = false
local bg = false
local playerName = UnitName("player")
colorTable[playerName] = RAID_CLASS_COLORS[select(2, UnitClass("player"))]
damageTable[playerName] = 0
healTable[playerName] = 0
startTime = 0
endTime = 0

local function GetGroup()
	local groupType
	local count = GetNumGroupMembers()
	if IsInRaid() then groupType = "raid"
	elseif IsInGroup() then groupType = "party"
	end
	return groupType, count
end

local function ClearTable()
	local namesTable = {}
	local count = GetNumGroupMembers()
	if count > 0 then
		for i = 1, count do
			local unitName = GetRaidRosterInfo(i)
			if unitName then namesTable[unitName] = true end
		end
	end
	namesTable[playerName] = true
	for key in pairs(damageTable) do
		if namesTable[key] then damageTable[key] = 0
		else damageTable[key] = nil
		end
	end
	for key in pairs(healTable) do
		if namesTable[key] then healTable[key] = 0
		else healTable[key] = nil
		end
	end
end

local function GetName(name, realm)
	if realm and realm ~= "" then return name .. "-" .. realm
	else return name
	end
end

local function Okr(number)
	if number then
		if number > 999999999 then return ("%02.3fB"):format(number / 1000000000)
		elseif number > 999999 then return ("%02.2fM"):format(number / 1000000)
		elseif number > 9999 then return ("%02.1fK"):format(number / 1000)
		end
		return math.floor(number)
	end
end

local function FindOwner(guid, table)
	local scan = CreateFrame("GameTooltip", "ZadrotScanTip", nil, "GameTooltipTemplate")
	scan:SetOwner(WorldFrame, "ANCHOR_NONE")
	scan:SetHyperlink("unit:" .. guid or "")
	local ownerText = ZadrotScanTipTextLeft2:GetText()
	if not ownerText then return end
	for key in pairs(table) do
		local sex = UnitSex(key)
		local justName = key:gsub("%-.*", "")
		for i = 1, GetNumDeclensionSets(justName, sex) do
			local declension = DeclineName(justName, sex, i)
			if ownerText:match(declension) then
				pets[guid] = key
				return key
			end
		end
	end
end

local function Color(unit)
	if colorTable[unit] then return colorTable[unit]
	else return {r = 0.75, g = 0.75, b = 0.75}
	end
end

local f = CreateFrame("FRAME", "ZadrotDamageMeter", Minimap)
f:SetPoint("BOTTOM")
f:SetSize(80, 20)
f:SetMovable(true)
f:EnableMouse(true)
f:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
f:RegisterEvent("ENCOUNTER_END")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("ZONE_CHANGED_NEW_AREA")

local text = f:CreateFontString(nil, "OVERLAY")
text:SetPoint("CENTER")
text:SetFont("Fonts\\FRIZQT__.TTF", 20, "OUTLINE")
text:SetText(" ")

local tip = CreateFrame("GameTooltip", "ZadrotDPSTip", UIParent, "GameTooltipTemplate")
tip:SetScript("OnShow", function(self)
	self:SetBackdrop({bgFile = 130937, edgeFile = 130937, tile = false, tileEdge = false, tileSize = 16, edgeSize = 4, insets = {left = 4, right = 4, top = 4, bottom = 4}})
	self:SetBackdropColor(0, 0, 0, 0.5)
	self:SetBackdropBorderColor(0, 0, 0, 0.5)
end)

local toggle = false
f:SetScript("OnMouseDown", function(self, button)
	if button == "LeftButton" and not self.isMoving then
		self:StartMoving()
		self.isMoving = true
	elseif button == "RightButton" then
		if toggle then toggle = false
		else toggle = true
		end
	end
end)

f:SetScript("OnMouseUp", function(self, button)
	if button == "LeftButton" and self.isMoving then
		self:StopMovingOrSizing();
		self.isMoving = false;
		f:SetUserPlaced(true)
	end
end)

local function Line(show, table, total, elapsed, textures, percents)
	if total > 0 then
		for key, value in pairs(show) do
			local damage = table[value] or 0
			if damage > 0 then
				ZadrotDPSTip:AddDoubleLine(string.format("%s. %s", key, value),
				string.format("%s (%s)", Okr(damage), Okr(damage / elapsed)), 1, 1, 1, 1, 1, 1)
				local currentLine = ZadrotDPSTip:NumLines()
				percents[currentLine] = damage / total
				if not textures[currentLine] then
					local tex = ZadrotDPSTip:CreateTexture(nil, "BORDER", ZadrotDPSTip)
					tex:SetTexture(130937)
					tex:SetAlpha(0.5)
					tex:SetPoint("TOPLEFT", _G["ZadrotDPSTipTextLeft" .. currentLine], "TOPLEFT", 0, 0)
					tex:SetPoint("BOTTOMRIGHT", _G["ZadrotDPSTipTextRight" .. currentLine], "BOTTOMRIGHT", 0, 0)
					textures[currentLine] = tex
				else textures[currentLine]:Show()
				end
				local color = Color(value)
				textures[currentLine]:SetColorTexture(color.r, color.g, color.b)
			end
		end
	end
end

local fTextureWidth = 0
local function CalculateTextures(firstLine, textures, percents)
	if firstLine then
		for line, percent in pairs(percents) do
			if line == firstLine then
				textures[line]:SetPoint("BOTTOMRIGHT", _G["ZadrotDPSTipTextRight" .. line], "BOTTOMRIGHT")
			else
				textures[line]:SetPoint("BOTTOMRIGHT", _G["ZadrotDPSTipTextRight" .. line], "BOTTOMRIGHT",
				-fTextureWidth * (1 - percent / percents[firstLine]), 0)
			end
		end
	end
end

local texturesAll = {}
local fTexture
local function UpdateTip()
	if ZadrotDPSTip:IsShown() then
		if combat then endTime = time() end
		local elapsed = math.max(endTime - startTime, 1)
		ZadrotDPSTip:ClearLines()
		for _, texture in pairs(texturesAll) do texture:Hide() end

		ZadrotDPSTip:AddDoubleLine("Zadrot |cFFFF9999Damage|r", " ")
		local firstDamageLine = ZadrotDPSTip:NumLines() + 1
		local percentsDamage = {}	
		Line(showTable, damageTable, totaldamage, elapsed, texturesAll, percentsDamage)

		local firstHealingLine
		local percentsHealing = {}
		if totalHealing > 0 then
			ZadrotDPSTip:AddLine(" ")
			ZadrotDPSTip:AddLine("|cFFCCEECCHealing|r")
			firstHealingLine = ZadrotDPSTip:NumLines() + 1
		end
		Line(showTableHeal, healTable, totalHealing, elapsed, texturesAll, percentsHealing)

		if not fTexture then
			fTexture = ZadrotDPSTip:CreateTexture(nil, "BORDER", ZadrotDPSTip)
			fTexture:SetPoint("TOPLEFT", ZadrotDPSTipTextLeft1, "TOPLEFT")
			fTexture:SetPoint("BOTTOMRIGHT", ZadrotDPSTipTextRight1, "BOTTOMRIGHT")
			fTexture:Hide()
		end
		ZadrotDPSTip:Show()
		fTextureWidth = fTexture:GetWidth()
		CalculateTextures(firstDamageLine, texturesAll, percentsDamage)
		CalculateTextures(firstHealingLine, texturesAll, percentsHealing)
	end
end

f:SetScript("OnEnter", function(self)
	ZadrotDPSTip:SetOwner(self)
	ZadrotDPSTip:AddLine(" ")
	ZadrotDPSTip:Show()
	UpdateTip()
end)
f:SetScript("OnLeave", function()
	if not toggle then
		ZadrotDPSTip:Hide()
	end
end)

local function CheckGroup()
	local groupType, count = GetGroup()
	if count > 0 then
		for i = 1, count do
			local unit = groupType .. i
			local name, server = UnitName(unit)
			local unitName = GetName(name, server)
			if not unitName then break end
			if not damageTable[unitName] then damageTable[unitName] = 0 end
			if not healTable[unitName] then healTable[unitName] = 0 end
			if not colorTable[unitName] then colorTable[unitName] = RAID_CLASS_COLORS[select(2, UnitClass(unit))] end
			local petGUID = UnitGUID(unit .. "pet")
			if petGUID and not pets[petGUID] then pets[petGUID] = unitName end
		end
	end
	local petGUID = UnitGUID("playerpet")
	if petGUID and not pets[petGUID] then pets[petGUID] = playerName end
end

local function IsInCombat()
	local groupType, count = GetGroup()
	if count > 0 then
		for i = 1, count do
			local unit = groupType .. i
			if UnitAffectingCombat(unit) or UnitAffectingCombat(unit .. "pet") then return true end
		end
	end
	if UnitAffectingCombat("player") or UnitAffectingCombat("playerpet") then return true end
end

C_Timer.NewTicker(2, function()
	CheckGroup()
	if not bg and combat and not IsInCombat() then
		C_Timer.After(1.5, function()
			if not bg and combat and not IsInCombat() then
				combat = false
				endTime = time()
			end
		end)
	end

	showTable = {}
	totaldamage = 0
	for key, value in pairs(damageTable) do
		table.insert(showTable, key)
		totaldamage = totaldamage + value
	end
	table.sort(showTable, function(a, b) return damageTable[a] > damageTable[b] end)
	local damagePos = 1
	if totaldamage > 0 then
		for key, value in pairs(showTable) do
			if value == playerName then
				damagePos = key
				break
			end
		end
	end

	totalHealing = 0
	showTableHeal = {}
	for key, value in pairs(healTable) do
		table.insert(showTableHeal, key)
		totalHealing = totalHealing + value
	end
	table.sort(showTableHeal, function(a, b) return healTable[a] > healTable[b] end)
	local healPos = 1
	if totalHealing > 0 then
		for key, value in pairs(showTableHeal) do
			if value == playerName then
				healPos = key
				break
			end
		end
	end

	text:SetText(string.format("|cFFFF9999%s|r - |cFFCCEECC%s|r", damagePos, healPos))
	UpdateTip()
end)

local FILTER_RAID = bit.bor(COMBATLOG_OBJECT_TYPE_MASK, COMBATLOG_OBJECT_CONTROL_MASK, COMBATLOG_OBJECT_REACTION_FRIENDLY,
COMBATLOG_OBJECT_AFFILIATION_MINE, COMBATLOG_OBJECT_AFFILIATION_PARTY, COMBATLOG_OBJECT_AFFILIATION_RAID)
local FILTER_PLAYERS = bit.bor(COMBATLOG_OBJECT_TYPE_PLAYER, COMBATLOG_OBJECT_CONTROL_PLAYER,
COMBATLOG_OBJECT_REACTION_MASK,COMBATLOG_OBJECT_AFFILIATION_MASK)
local FILTER_PETS = bit.bor(COMBATLOG_OBJECT_TYPE_OBJECT, COMBATLOG_OBJECT_TYPE_GUARDIAN, COMBATLOG_OBJECT_TYPE_PET,
COMBATLOG_OBJECT_CONTROL_MASK, COMBATLOG_OBJECT_REACTION_MASK, COMBATLOG_OBJECT_AFFILIATION_MASK)
local FILTER_ENEMY = bit.bor(COMBATLOG_OBJECT_TYPE_MASK, COMBATLOG_OBJECT_CONTROL_MASK,COMBATLOG_OBJECT_REACTION_NEUTRAL,
COMBATLOG_OBJECT_REACTION_HOSTILE, COMBATLOG_OBJECT_AFFILIATION_OUTSIDER)

f:SetScript("OnEvent", function(self, event)
	if event == "PLAYER_ENTERING_WORLD" then
		if not bg and UnitInBattleground("player") then bg = true
		elseif bg and not UnitInBattleground("player") then bg = false
		end
	elseif event == "ZONE_CHANGED_NEW_AREA" or event == "ENCOUNTER_END" then pets = {}
	elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local timestamp, logEvent, _,  sourceGUID, sourceName, sourceFlags, _, destGUID, destName, destFlags, _,
		arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11 = CombatLogGetCurrentEventInfo()
		local fromEnemy
		if sourceName and sourceGUID and not CombatLog_Object_IsA(sourceFlags, COMBATLOG_OBJECT_NONE) then
			fromEnemy = CombatLog_Object_IsA(sourceFlags, FILTER_ENEMY)
		end
		if fromEnemy then
			if arg4 == "REFLECT" then
				if damageTable[destName] then
					reflects[timestamp] = {}
					reflects[timestamp]["igrok"] = destName
					reflects[timestamp]["vrag"] = sourceGUID
					reflects[timestamp]["spell"] = arg1
				end
			end
			if logEvent == "SPELL_DAMAGE" and sourceGUID == destGUID then
				for key, value in pairs(reflects) do
					if (timestamp - key) < 5 then
						if value["spell"] == arg1 and value["vrag"] == sourceGUID then
							if combat and damageTable[value["igrok"]] then
								damageTable[value["igrok"]] = damageTable[value["igrok"]] + arg4
							end
							reflects[key] = nil
							return
						end
					else reflects[key] = nil
					end
				end
			end
		end

		if logEvent == "SPELL_ABSORBED" then
			local healer
			local amount
			if arg11 then
				if pets[arg4] then healer = pets[arg4]
				else healer = arg5
				end
				amount = arg11
			else
				if pets[arg1] then healer = pets[arg1]
				else healer = arg2
				end
				amount = arg8
			end
			if combat and healTable[healer] then healTable[healer] = healTable[healer] + amount end
			return
		end

		local fromRaid
		local fromPets
		if sourceName and sourceGUID and not CombatLog_Object_IsA(sourceFlags, COMBATLOG_OBJECT_NONE) then
			fromRaid = CombatLog_Object_IsA(sourceFlags, FILTER_RAID)
			fromPets = CombatLog_Object_IsA(sourceFlags, FILTER_PETS)
		end

		if not fromRaid then return	end
		if fromPets and pets[sourceGUID] then sourceName = pets[sourceGUID] end
		if logEvent == "SPELL_SUMMON" then pets[destGUID] = sourceName return end	

		if logEvent == "SPELL_HEAL" or logEvent == "SPELL_PERIODIC_HEAL" then
			local amount = arg4 - arg5 + arg6
			if fromPets and not healTable[sourceName] then
				local owner = FindOwner(sourceGUID, healTable)
				if owner then sourceName = owner end
			end
			if combat and healTable[sourceName] then healTable[sourceName] = healTable[sourceName] + amount end
			return
		end
		
		local toEnemy
		if destName and destGUID and not CombatLog_Object_IsA(destFlags, COMBATLOG_OBJECT_NONE) then
			toEnemy = CombatLog_Object_IsA(destFlags, FILTER_ENEMY)
		end
		if not toEnemy then return end

		local amount
		if logEvent == "SWING_DAMAGE" then
			amount = arg1 + (arg6 or 0)
		elseif logEvent == "SWING_MISSED" and arg1 == "ABSORB" then
			amount = arg3
		elseif logEvent == "SPELL_DAMAGE" or logEvent == "SPELL_PERIODIC_DAMAGE" or logEvent == "SPELL_BUILDING_DAMAGE" or
		logEvent == "RANGE_DAMAGE" or logEvent == "SPELL_EXTRA_ATTACKS" or logEvent == "DAMAGE_SHIELD" or logEvent == "DAMAGE_SPLIT" then
			amount = arg4 + (arg9 or 0)
		elseif arg4 == "ABSORB" and (logEvent == "SPELL_MISSED" or logEvent == "RANGE_MISSED" or logEvent == "SPELL_PERIODIC_MISSED" or
		logEvent == "SPELL_BUILDING_MISSED" or logEvent == "DAMAGE_SHIELD_MISSED") then
			amount = arg6
		end
		if not amount then return end

		if fromPets and not damageTable[sourceName] then
			local owner = FindOwner(sourceGUID, damageTable)
			if owner then sourceName = owner end
		end

		if not combat then
			ClearTable()
			combat = true
			startTime = time()
		end

		if damageTable[sourceName] then damageTable[sourceName] = damageTable[sourceName] + amount end
	end
end)

SLASH_ZADROTDAMAGEMETER1 = "/zdm"
function SlashCmdList.ZADROTDAMAGEMETER(arg)
	f:ClearAllPoints()
	f:SetPoint("BOTTOM")
	print(RESET_POSITION .. " Zadrot DamageMeter")
end