Quantcast

Moved parts of code to independent files.

F16Gaming [11-01-11 - 16:58]
Moved parts of code to independent files.

ADDED: Implemented /kt purge [treshold]
ADDED: KillTrack global contains AddOn name and Version.
CHANGED: Better slash-command handling in its own file.
CHANGED: Created Tools.lua to hold all utility functions.
Filename
Command.lua
Dialogs.lua
KillTrack.lua
KillTrack.toc
Tools.lua
diff --git a/Command.lua b/Command.lua
new file mode 100644
index 0000000..7719b9a
--- /dev/null
+++ b/Command.lua
@@ -0,0 +1,119 @@
+--[[
+	* Copyright (c) 2011 by Adam Hellberg.
+	*
+	* This file is part of KillTrack.
+	*
+	* KillTrack is free software: you can redistribute it and/or modify
+	* it under the terms of the GNU General Public License as published by
+	* the Free Software Foundation, either version 3 of the License, or
+	* (at your option) any later version.
+	*
+	* KillTrack is distributed in the hope that it will be useful,
+	* but WITHOUT ANY WARRANTY; without even the implied warranty of
+	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	* GNU General Public License for more details.
+	*
+	* You should have received a copy of the GNU General Public License
+	* along with KillTrack. If not, see <http://www.gnu.org/licenses/>.
+--]]
+
+KillTrack.Command = {
+	Slash = {
+		"killtrack",
+		"kt"
+	},
+	Commands = {}
+}
+
+local KT = KillTrack
+local C = KT.Command
+local KTT = KillTrack_Tools
+
+--local CLib = ChocoboLib
+
+-- Argument #1 (command) can be either string or a table.
+function C:Register(command, func)
+	if type(command) == "string" then
+		command = {command}
+	end
+	for _,v in pairs(command) do
+		if not self:HasCommand(v) then
+			if v ~= "__DEFAULT__" then v = v:lower() end
+			self.Commands[v] = func
+		end
+	end
+end
+
+function C:GetCommand(command)
+	local cmd = self.Commands[command]
+	if cmd then return cmd else return self.Commands["__DEFAULT__"] end
+end
+
+function C:HandleCommand(command, args)
+	local cmd = self:GetCommand(command)
+	if cmd then
+		cmd(args)
+	else
+		KT:Msg(("%q is not a valid command."):format(command))
+	end
+end
+
+C:Register("__DEFAULT__", function(args)
+	KT:Msg(("%q is not a valid command."):format(tostring(msg)))
+	KT:Msg("/kt target - Display number of kills on target mob.")
+	KT:Msg("/kt <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 reset - Clear the mob database.")
+	KT:Msg("/kt - Displays this help message.")
+end)
+
+C:Register({"target", "t", "tar"}, function(args)
+	if not UnitExists("target") or UnitIsPlayer("target") then return end
+	local id = KTT:GUIDToID(UnitGUID("target"))
+	KT:PrintKills(id)
+end)
+
+C:Register({"print", "p"}, function(args)
+	KT.Global.PRINTKILLS = not KT.Global.PRINTKILLS
+	if KT.Global.PRINTKILLS then
+		KT:Msg("Announcing kill updates.")
+	else
+		KT:Msg("No longer announcing kill updates.")
+	end
+end)
+
+C:Register({"purge"}, function(args)
+	local treshold
+	if #args >= 1 then treshold = tonumber(args[1]) end
+	KT:Purge(treshold)
+end)
+
+C:Register({"reset", "r"}, function(args)
+	KT:Reset()
+end)
+
+C:Register({"lookup", "lo", "check"}, function(args)
+	if #args <= 0 then
+		KT:Msg("Missing argument: name")
+		return
+	end
+	local name = table.concat(args, " ")
+	KT:PrintKills(name)
+end)
+
+for i,v in ipairs(C.Slash) do
+	_G["SLASH_" .. KT.Name:upper() .. i] = "/" .. v
+end
+
+SlashCmdList[KT.Name:upper()] = function(msg, editBox)
+	msg = KTT:Trim(msg)
+	local args = KTT:Split(msg)
+	local cmd = args[1]
+	local t = {}
+	if #args > 1 then
+		for i=2,#args do
+			table.insert(t, args[i])
+		end
+	end
+	C:HandleCommand(cmd, t)
+end
diff --git a/Dialogs.lua b/Dialogs.lua
index dd29096..7f32624 100644
--- a/Dialogs.lua
+++ b/Dialogs.lua
@@ -19,6 +19,8 @@

 local KT = KillTrack

+local Treshold
+
 local function Purge(treshold)
 	local count = 0
 	for i,v in ipairs(KT.Global.MOBS) do
@@ -34,6 +36,7 @@ local function Purge(treshold)
 		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

@@ -60,7 +63,14 @@ StaticPopupDialogs["KILLTRACK_PURGE"] = {
 	button2 = "Cancel",
 	hasEditBox = true,
 	OnAccept = function(self, data, data2) Purge(tonumber(self.editBox:GetText())) end,
-	OnShow = function(self, data) self.button1:Disable() end,
+	OnCancel = function() Treshold = nil end,
+	OnShow = function(self, data)
+		if tonumber(Treshold) then
+			self.editBox:SetText(tostring(Treshold))
+		else
+			self.button1:Disable()
+		end
+	end,
 	EditBoxOnTextChanged = function(self, data)
 		if tonumber(self:GetText()) then
 			self:GetParent().button1:Enable()
@@ -87,7 +97,10 @@ StaticPopupDialogs["KILLTRACK_RESET"] = {
 	hideOnEscape = true
 }

-function KT:Purge()
+function KT:Purge(treshold)
+	if tonumber(treshold) then
+		Treshold = tonumber(treshold)
+	end
 	StaticPopup_Show("KILLTRACK_PURGE")
 end

diff --git a/KillTrack.lua b/KillTrack.lua
index 141f59a..5b564bc 100644
--- a/KillTrack.lua
+++ b/KillTrack.lua
@@ -18,6 +18,8 @@
 --]]

 KillTrack = {
+	Name = "KillTrack",
+	Version = GetAddOnMetadata("KillTrack", "Version"),
 	Events = {},
 	Global = {},
 	CharGlobal = {}
@@ -25,9 +27,7 @@ KillTrack = {

 local KT = KillTrack

-local function GUIDToID(guid)
-	return tonumber(guid:sub(-12, -9), 16)
-end
+local KTT = KillTrack_Tools

 function KT:OnEvent(_, event, ...)
 	if self.Events[event] then
@@ -61,7 +61,7 @@ end
 function KT.Events.COMBAT_LOG_EVENT_UNFILTERED(self, ...)
 	local event = (select(2, ...))
 	if event ~= "PARTY_KILL" then return end
-	local id = GUIDToID((select(8, ...)))
+	local id = KTT:GUIDToID((select(8, ...)))
 	local name = tostring((select(9, ...)))
 	if id == 0 then return end
 	self:AddKill(id, name)
@@ -70,7 +70,7 @@ end
 function KT.Events.UPDATE_MOUSEOVER_UNIT(self, ...)
 	if UnitIsPlayer("mouseover") then return end
 	if not UnitCanAttack("player", "mouseover") then return end
-	local id = GUIDToID(UnitGUID("mouseover"))
+	local id = KTT:GUIDToID(UnitGUID("mouseover"))
 	local gKills, cKills = self:GetKills(id)
 	GameTooltip:AddLine(("Killed %d (%d) times."):format(gKills, cKills), 1, 1, 1)
 	GameTooltip:Show()
@@ -133,36 +133,6 @@ function KT:Msg(msg)
 	DEFAULT_CHAT_FRAME:AddMessage("\124cff00FF00[KillTrack]\124r " .. msg)
 end

-SLASH_KILLTRACK1 = "/killtrack"
-SLASH_KILLTRACK2 = "/kt"
-
-SlashCmdList["KILLTRACK"] = function(msg, editBox)
-	if msg == "target" and UnitExists("target") and not UnitIsPlayer("target") then
-		local id = tonumber(UnitGUID("target"):sub(-12, -9), 16)
-		KT:PrintKills(id)
-	elseif msg == "print" then
-		KT.Global.PRINTKILLS = not KT.Global.PRINTKILLS
-		if KT.Global.PRINTKILLS then
-			KT:Msg("Announcing kill updates.")
-		else
-			KT:Msg("No longer announcing kill updates.")
-		end
-	elseif msg == "purge" then
-		KT:Msg("NYI")
-	elseif msg == "reset" then
-		KT:Reset()
-	elseif msg and msg ~= "" then
-		KT:PrintKills(msg)
-	else
-		KT:Msg(("%q is not a valid command."):format(tostring(msg)))
-		KT:Msg("/kt target - Display number of kills on target mob.")
-		KT:Msg("/kt <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 reset - Clear the mob database.")
-		KT:Msg("/kt - Displays this help message.")
-	end
-end
-
 KT.Frame = CreateFrame("Frame")

 for k,_ in pairs(KT.Events) do
diff --git a/KillTrack.toc b/KillTrack.toc
index 9b76170..1ad62bd 100644
--- a/KillTrack.toc
+++ b/KillTrack.toc
@@ -6,5 +6,7 @@
 ## SavedVariables: KILLTRACK
 ## SavedVariablesPerCharacter: KILLTRACK_CHAR

+Tools.lua
 KillTrack.lua
-Dialogs.lua
\ No newline at end of file
+Dialogs.lua
+Command.lua
diff --git a/Tools.lua b/Tools.lua
new file mode 100644
index 0000000..d5deaf7
--- /dev/null
+++ b/Tools.lua
@@ -0,0 +1,57 @@
+--[[
+	* Copyright (c) 2011 by Adam Hellberg.
+	*
+	* This file is part of KillTrack.
+	*
+	* KillTrack is free software: you can redistribute it and/or modify
+	* it under the terms of the GNU General Public License as published by
+	* the Free Software Foundation, either version 3 of the License, or
+	* (at your option) any later version.
+	*
+	* KillTrack is distributed in the hope that it will be useful,
+	* but WITHOUT ANY WARRANTY; without even the implied warranty of
+	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	* GNU General Public License for more details.
+	*
+	* You should have received a copy of the GNU General Public License
+	* along with KillTrack. If not, see <http://www.gnu.org/licenses/>.
+--]]
+
+KillTrack_Tools = {}
+
+local KTT = KillTrack_StringTools
+
+------------------
+-- STRING TOOLS --
+------------------
+
+function KTT:Trim(s)
+	return (s:gsub("^%s*(.-)%s*$", "%1"))
+end
+
+function KTT:Split(s)
+	local r = {}
+	for token in string.gmatch(s, "[^%s]+") do
+		table.insert(r, token)
+	end
+	return r
+end
+
+-----------------
+-- TABLE TOOLS --
+-----------------
+
+function KTT:InTable(tbl, val)
+	for _,v in pairs(tbl) do
+		if v == val then return true end
+	end
+	return false
+end
+
+-----------------
+-- OTHER TOOLS --
+-----------------
+
+function KTT:GUIDToID(guid)
+	return tonumber(guid:sub(-12, -9), 16)
+end