Quantcast

Updates to MobList, added Delete method.

F16Gaming [11-03-11 - 07:31]
Updates to MobList, added Delete method.

ADDED: KillTrack.Delete to delete specific entries.
ADDED: /kt delete command for deleting a specific entry.
CHANGED: Moved Reset and Purge methods into KillTrack main module.
Filename
Command.lua
Dialogs.lua
KillTrack.lua
MobList.lua
diff --git a/Command.lua b/Command.lua
index b04b3ce..dbf4f78 100644
--- a/Command.lua
+++ b/Command.lua
@@ -70,6 +70,7 @@ C:Register("__DEFAULT__", function(args)
 	KT:Msg("/kt lookup <name> - Display number of kills on <name>, <name> can also be NPC ID.")
 	KT:Msg("/kt print - Toggle printing kill updates to chat.")
 	KT:Msg("/kt list - Display a list of all mobs entries.")
+	KT:Msg("/kt delete <id> - Delete entry with NPC id <id>.")
 	KT:Msg("/kt purge [treshold] - Open dialog to purge entries, specifiying a treshold here is optional.")
 	KT:Msg("/kt reset - Clear the mob database.")
 	KT:Msg("/kt - Displays this help message.")
@@ -90,14 +91,32 @@ C:Register({"print", "p"}, function(args)
 	end
 end)

+C:Register({"delete", "del", "remove", "rem"}, function(args)
+	if #args <= 0 then
+		KT:Msg("Missing argument: id")
+		return
+	end
+	local id = tonumber(args[1])
+	if not id then
+		KT:Msg("Id must be a number")
+		return
+	end
+	if not KT.Global.MOBS[id] then
+		KT:Msg(("Id %d does not exist in the database."):format(id))
+		return
+	end
+	local name = KT.Global.MOBS[id].Name
+	KT:ShowDelete(id, name)
+end)
+
 C:Register({"purge"}, function(args)
 	local treshold
 	if #args >= 1 then treshold = tonumber(args[1]) end
-	KT:Purge(treshold)
+	KT:ShowPurge(treshold)
 end)

 C:Register({"reset", "r"}, function(args)
-	KT:Reset()
+	KT:ShowReset()
 end)

 C:Register({"lookup", "lo", "check"}, function(args)
diff --git a/Dialogs.lua b/Dialogs.lua
index 03b9f23..695a333 100644
--- a/Dialogs.lua
+++ b/Dialogs.lua
@@ -19,35 +19,6 @@

 local KT = KillTrack

-local Treshold
-
-local function Purge(treshold)
-	local count = 0
-	for k,v in pairs(KT.Global.MOBS) do
-		if type(v) == "table" and v.Kills < treshold then
-			KT.Global.MOBS[k] = nil
-			count = count + 1
-		end
-	end
-	for k,v in pairs(KT.CharGlobal.MOBS) do
-		if type(v) == "table" and v.Kills < treshold then
-			KT.CharGlobal.MOBS[k] = nil
-			count = count + 1
-		end
-	end
-	KT:Msg(("Purged %d entries with a kill count below %d"):format(count, treshold))
-	Treshold = nil
-	StaticPopup_Show("KILLTRACK_FINISH", tostring(count))
-end
-
-local function Reset()
-	local count = #KT.Global.MOBS + #KT.CharGlobal.MOBS
-	wipe(KT.Global.MOBS)
-	wipe(KT.CharGlobal.MOBS)
-	KT:Msg(("%d mob entries have been removed!"):format(count))
-	StaticPopup_Show("KILLTRACK_FINISH", tostring(count))
-end
-
 StaticPopupDialogs["KILLTRACK_FINISH"] = {
 	text = "%s entries removed.",
 	button1 = "Okay",
@@ -57,16 +28,29 @@ StaticPopupDialogs["KILLTRACK_FINISH"] = {
 	hideOnEscape = true
 }

+StaticPopupDialogs["KILLTRACK_DELETE"] = {
+	text = "Delete %s with ID %s?",
+	button1 = "Delete all",
+	button2 = "Character only",
+	button3 = "Cancel",
+	OnAccept = function() KT:Delete(KT.Temp.DeleteId) end,
+	OnCancel = function() KT:Delete(KT.Temp.DeleteId, true) end, -- Cancel is actually the second (button2) button.
+	showAlert = true,
+	timeout = 10,
+	whileDead = true,
+	hideOnEscape = true
+}
+
 StaticPopupDialogs["KILLTRACK_PURGE"] = {
 	text = "Remove all mob entries with their kill count below this treshold:",
 	button1 = "Purge",
 	button2 = "Cancel",
 	hasEditBox = true,
-	OnAccept = function(self, data, data2) Purge(tonumber(self.editBox:GetText())) end,
-	OnCancel = function() Treshold = nil end,
+	OnAccept = function(self, data, data2) KT:Purge(tonumber(self.editBox:GetText())) end,
+	OnCancel = function() KT.Temp.Treshold = nil end,
 	OnShow = function(self, data)
-		if tonumber(Treshold) then
-			self.editBox:SetText(tostring(Treshold))
+		if tonumber(KT.Temp.Treshold) then
+			self.editBox:SetText(tostring(KT.Temp.Treshold))
 		else
 			self.button1:Disable()
 		end
@@ -89,7 +73,7 @@ StaticPopupDialogs["KILLTRACK_RESET"] = {
 	text = "Remove all mob entries from the database? THIS CANNOT BE REVERSED.",
 	button1 = "Yes",
 	button2 = "No",
-	OnAccept = function() Reset() end,
+	OnAccept = function() KT:Reset() end,
 	showAlert = true,
 	enterClicksFirstButton = true,
 	timeout = 0,
@@ -97,13 +81,21 @@ StaticPopupDialogs["KILLTRACK_RESET"] = {
 	hideOnEscape = true
 }

-function KT:Purge(treshold)
+function KT:ShowDelete(id, name)
+	id = tonumber(id)
+	name = tostring(name)
+	if not id then error("'id' must be a number.") end
+	self.Temp.DeleteId = id
+	StaticPopup_Show("KILLTRACK_DELETE", name, id)
+end
+
+function KT:ShowPurge(treshold)
 	if tonumber(treshold) then
-		Treshold = tonumber(treshold)
+		self.Temp.Treshold = tonumber(treshold)
 	end
 	StaticPopup_Show("KILLTRACK_PURGE")
 end

-function KT:Reset()
+function KT:ShowReset()
 	StaticPopup_Show("KILLTRACK_RESET")
 end
diff --git a/KillTrack.lua b/KillTrack.lua
index 067d19e..8046fe6 100644
--- a/KillTrack.lua
+++ b/KillTrack.lua
@@ -23,6 +23,7 @@ KillTrack = {
 	Events = {},
 	Global = {},
 	CharGlobal = {},
+	Temp = {},
 	Sort = {
 		Desc = 0,
 		Asc = 1,
@@ -183,6 +184,54 @@ function KT:GetSortedMobTable(mode)
 	return t
 end

+function KT:Delete(id, charOnly)
+	id = tonumber(id)
+	if not id then error(("Expected 'id' param to be number, got %s."):format(type(id))) end
+	local found = false
+	local name
+	if self.Global.MOBS[id] then
+		name = self.Global.MOBS[id].Name
+		if not charOnly then self.Global.MOBS[id] = nil end
+		if self.CharGlobal.MOBS[id] then
+			self.CharGlobal.MOBS[id] = nil
+		end
+		found = true
+	end
+	if found then
+		self:Msg(("Deleted %q (%d) from database."):format(name, id))
+		StaticPopup_Show("KILLTRACK_FINISH", 1)
+	else
+		self:Msg(("ID: %d was not found in the database."):format(id))
+	end
+end
+
+function KT:Purge(treshold)
+	local count = 0
+	for k,v in pairs(KT.Global.MOBS) do
+		if type(v) == "table" and v.Kills < treshold then
+			self.Global.MOBS[k] = nil
+			count = count + 1
+		end
+	end
+	for k,v in pairs(KT.CharGlobal.MOBS) do
+		if type(v) == "table" and v.Kills < treshold then
+			self.CharGlobal.MOBS[k] = nil
+			count = count + 1
+		end
+	end
+	self:Msg(("Purged %d entries with a kill count below %d"):format(count, treshold))
+	self.Temp.Treshold = nil
+	StaticPopup_Show("KILLTRACK_FINISH", tostring(count))
+end
+
+function KT:Reset()
+	local count = #KT.Global.MOBS + #KT.CharGlobal.MOBS
+	wipe(self.Global.MOBS)
+	wipe(self.CharGlobal.MOBS)
+	KT:Msg(("%d mob entries have been removed!"):format(count))
+	StaticPopup_Show("KILLTRACK_FINISH", tostring(count))
+end
+
 KT.Frame = CreateFrame("Frame")

 for k,_ in pairs(KT.Events) do
diff --git a/MobList.lua b/MobList.lua
index 8aa04cd..e22a554 100644
--- a/MobList.lua
+++ b/MobList.lua
@@ -157,6 +157,11 @@ function ML:UpdateList()
 		id:SetText(tostring(v.Id))
 		id:SetColor(1, 1, 1)
 		id:SetHighlight(0.1, 0.1, 0.1)
+		id:SetUserData("NPC_ID", v.Id)
+		id:SetUserData("NPC_NAME", v.Name)
+		id:SetCallback("OnClick", function(self)
+			KT:ShowDelete(self:GetUserData("NPC_ID"), self:GetUserData("NPC_NAME"))
+		end)
 		local name = GUI:Create("Label")
 		name:SetWidth(200)
 		name:SetFont("Fonts\\FRIZQT__.TTF", 12)