Skip to content

Commit

Permalink
Add tracking of when a mob was last killed
Browse files Browse the repository at this point in the history
Can be seen in a tooltip in the GUI list view, or when querying data
about a mob via slash commands.

Implements #19
  • Loading branch information
Sharparam committed May 8, 2022
1 parent bc2479a commit 653c626
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ stds.wow = {
"COMBATLOG_XPGAIN_FIRSTPERSON_GROUP",
"COMBATLOG_XPGAIN_FIRSTPERSON_RAID",
"CreateFrame",
"date",
"DEFAULT_CHAT_FRAME",
"GetServerTime",
"FauxScrollFrame_GetOffset",
"FauxScrollFrame_OnVerticalScroll",
"FauxScrollFrame_Update",
Expand Down
24 changes: 22 additions & 2 deletions KillTrack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ _G[NAME] = KT
local UnitGUID = UnitGUID
local UnitIsTapDenied = UnitIsTapDenied
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
local GetServerTime = GetServerTime

local NO_NAME = "<No Name>"

Expand Down Expand Up @@ -52,6 +53,10 @@ KT.Messages = {
Announce = "[KillTrack] Session Length: %s. Session Kills: %d. Kills Per Minute: %.2f."
}

KT.Defaults = {
DateTimeFormat = "%Y-%m-%d %H:%M:%S"
}

local ET

local KTT = KT.Tools
Expand Down Expand Up @@ -168,6 +173,9 @@ function KT.Events.ADDON_LOADED(self, ...)
if type(self.Global.TOOLTIP) ~= "boolean" then
self.Global.TOOLTIP = true
end
if type(self.Global.DATETIME_FORMAT) ~= "string" then
self.Global.DATETIME_FORMAT = self.Defaults.DateTimeFormat
end
if type(_G["KILLTRACK_CHAR"]) ~= "table" then
_G["KILLTRACK_CHAR"] = {}
end
Expand Down Expand Up @@ -397,9 +405,14 @@ end

function KT:AddKill(id, name)
name = name or NO_NAME
local current_time = GetServerTime()
self:InitMob(id, name)
self.Global.MOBS[id].Kills = self.Global.MOBS[id].Kills + 1
self.CharGlobal.MOBS[id].Kills = self.CharGlobal.MOBS[id].Kills + 1
local globalMob = self.Global.MOBS[id]
local charMob = self.CharGlobal.MOBS[id]
globalMob.Kills = self.Global.MOBS[id].Kills + 1
globalMob.LastKillAt = current_time
charMob.Kills = self.CharGlobal.MOBS[id].Kills + 1
charMob.LastKillAt = current_time
if self.Global.PRINTKILLS then
local kills = self.Global.MOBS[id].Kills
local cKills = self.CharGlobal.MOBS[id].Kills
Expand Down Expand Up @@ -533,11 +546,15 @@ function KT:PrintKills(identifier)
local name = NO_NAME
local gKills = 0
local cKills = 0
local lastKillAt = nil
if type(identifier) ~= "string" and type(identifier) ~= "number" then identifier = NO_NAME end
for k,v in pairs(self.Global.MOBS) do
if type(v) == "table" and (tostring(k) == tostring(identifier) or v.Name == identifier) then
name = v.Name
gKills = v.Kills
if type(v.LastKillAt) == "number" then
lastKillAt = KTT:FormatDateTime(v.LastKillAt)
end
if self.CharGlobal.MOBS[k] then
cKills = self.CharGlobal.MOBS[k].Kills
end
Expand All @@ -546,6 +563,9 @@ function KT:PrintKills(identifier)
end
if found then
self:Msg(("You have killed %q %d times in total, %d times on this character"):format(name, gKills, cKills))
if lastKillAt then
self:Msg(("Last killed at %s"):format(lastKillAt))
end
else
if UnitExists("target") and not UnitIsPlayer("target") then
identifier = UnitName("target")
Expand Down
20 changes: 20 additions & 0 deletions MobList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ local function CreateRow(container, previous)
KT:ShowDelete(id, name)
end)

row:SetScript("OnEnter", function(s)
local id = tonumber(s.idField:GetText())
if not id then return end
local globalData = KT.Global.MOBS[id]
if not globalData then return end
local killTimestamp = globalData.LastKillAt
if not killTimestamp then return end
local lastKillAt = KTT:FormatDateTime(killTimestamp)
local tpString = ("Last killed at %s"):format(lastKillAt)
GameTooltip:SetOwner(s, "ANCHOR_NONE")
GameTooltip:SetPoint("TOPLEFT", s, "BOTTOMLEFT")
GameTooltip:ClearLines()
GameTooltip:AddLine(tpString)
GameTooltip:Show()
end)

row:SetScript("OnLeave", function()
GameTooltip:Hide()
end)

return row
end

Expand Down
65 changes: 65 additions & 0 deletions Options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,70 @@ function Opt:Show()
function(_, checked) KT.Global.DISABLE_RAIDS = checked end)
disableRaids:SetPoint("TOPLEFT", disableDungeons, "BOTTOMLEFT", 0, -8)

local datetimeFormatDesc = self:CreateFontString(nil, "ARTWORK", "GameFontNormal")
datetimeFormatDesc:SetPoint("TOPLEFT", disableRaids, "BOTTOMLEFT", 0, -8)
datetimeFormatDesc:SetTextColor(1, 1, 1)
datetimeFormatDesc:SetText("Datetime format template (press enter to apply)")

local datetimeFormat = CreateFrame("EditBox", "KillTrackOptDateTimeFormat", panel, "InputBoxTemplate")
datetimeFormat:SetHeight(22)
datetimeFormat:SetWidth(200)
datetimeFormat:SetPoint("LEFT", datetimeFormatDesc, "RIGHT", 8, 0)
datetimeFormat:SetAutoFocus(false)
datetimeFormat:EnableMouse(true)
local datetimeFormatPreview = self:CreateFontString(nil, "ARTWORK", "GameFontNormal")
datetimeFormatPreview:SetPoint("TOPLEFT", datetimeFormat, "BOTTOMLEFT", 0, -2)
datetimeFormatPreview:SetTextColor(1, 1, 1)
datetimeFormatPreview:SetText("Preview:")
local datetimeFormatPreviewValue = self:CreateFontString(nil, "ARTWORK", "GameFontNormal")
datetimeFormatPreviewValue:SetPoint("LEFT", datetimeFormatPreview, "RIGHT", 8, 0)
datetimeFormatPreviewValue:SetTextColor(1, 1, 1)
datetimeFormatPreviewValue:SetText(KTT:FormatDateTime())

datetimeFormat:SetScript("OnEditFocusGained", function(box)
box:SetTextColor(0, 1, 0)
box:HighlightText()
end)
local function setDateTimeFormat(box, enter)
box:SetTextColor(1, 1, 1)
local value = box:GetText()
if type(value) ~= "string" then
box:SetText(KT.Global.DATETIME_FORMAT)
box:HighlightText()
return
end
local valid, errMsg = pcall(KTT.FormatDateTime, KTT, nil, value)
if not valid then
KT:Msg("Invalid format string: " .. (errMsg or "unknown error"))
box:HighlightText()
return
end
KT.Global.DATETIME_FORMAT = value
if not enter then
KT:Msg("Updated datetime format!")
end
box:ClearFocus()
box:SetText(KT.Global.DATETIME_FORMAT)
end
datetimeFormat:SetScript("OnEditFocusLost", function(box) setDateTimeFormat(box) end)
datetimeFormat:SetScript("OnEnterPressed", function(box) setDateTimeFormat(box, true) end)
datetimeFormat:SetScript("OnTextChanged", function(box)
local value = box:GetText()
if type(value) ~= "string" then return end
local valid, result = pcall(KTT.FormatDateTime, KTT, nil, value)
if valid then
datetimeFormatPreviewValue:SetText(result)
else
datetimeFormatPreviewValue:SetText("invalid format")
end
end)
local datetimeFormatReset = button("Reset", "Reset the datetime format to the default", function()
KT.Global.DATETIME_FORMAT = KT.Defaults.DateTimeFormat
datetimeFormat:SetText(KT.Global.DATETIME_FORMAT)
end)
datetimeFormatReset:SetWidth(80)
datetimeFormatReset:SetPoint("LEFT", datetimeFormat, "RIGHT", 5, 0)

local function init()
printKills:SetChecked(KT.Global.PRINT)
tooltipControl:SetChecked(KT.Global.TOOLTIP)
Expand All @@ -180,6 +244,7 @@ function Opt:Show()
minimap:SetChecked(not KT.Global.BROKER.MINIMAP.hide)
disableDungeons:SetChecked(KT.Global.DISABLE_DUNGEONS)
disableRaids:SetChecked(KT.Global.DISABLE_RAIDS)
datetimeFormat:SetText(KT.Global.DATETIME_FORMAT)
end

init()
Expand Down
10 changes: 10 additions & 0 deletions Tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ function KTT:TableLength(table)
return count
end

--------------------
-- DATETIME TOOLS --
--------------------

function KTT:FormatDateTime(timestamp, format)
timestamp = timestamp or GetServerTime()
format = format or KT.Global.DATETIME_FORMAT or KT.Defaults.DateTimeFormat
return date(format, timestamp)
end

-----------------
-- OTHER TOOLS --
-----------------
Expand Down

0 comments on commit 653c626

Please sign in to comment.