Reorganize code

Sasky [06-12-14 - 05:39]
Reorganize code
Filename
AutoInvite.lua
diff --git a/AutoInvite.lua b/AutoInvite.lua
index d4dc467..25ecd46 100644
--- a/AutoInvite.lua
+++ b/AutoInvite.lua
@@ -8,7 +8,57 @@ if AutoInvite == nil then
 end
 AutoInvite.AddonId = "AutoInvite"

---Main callback - sends invites
+------------------------------------------------
+--- Utility functions
+------------------------------------------------
+local function b(v) if v then return "T" else return "F" end end
+
+AutoInvite.isCyrodiil = function(unit)
+    if unit == nil then unit = "player" end
+    d("Current zone: '" .. GetUnitZone(unit) .. "'")
+    return GetUnitZone(unit) == "Cyrodiil"
+end
+
+AutoInvite.guildLookup = function(channel, acctName)
+    local guildId = 0
+    if channel == CHAT_CHANNEL_GUILD_1 or channel == CHAT_CHANNEL_OFFICER_1 then guildId = GetGuildId(1) end
+    if channel == CHAT_CHANNEL_GUILD_2 or channel == CHAT_CHANNEL_OFFICER_2 then guildId = GetGuildId(2) end
+    if channel == CHAT_CHANNEL_GUILD_3 or channel == CHAT_CHANNEL_OFFICER_3 then guildId = GetGuildId(3) end
+    if channel == CHAT_CHANNEL_GUILD_4 or channel == CHAT_CHANNEL_OFFICER_4 then guildId = GetGuildId(4) end
+    if channel == CHAT_CHANNEL_GUILD_5 or channel == CHAT_CHANNEL_OFFICER_5 then guildId = GetGuildId(5) end
+
+    if guildId == 0 then d("Error - couldn't invite on channel: " .. channel) end
+
+    local aName
+    for i=1,GetNumGuildMembers(guildId) do
+        aName = GetGuildMemberInfo(guildId,i)
+        if aName == acctName then
+            local hasChar, charName, zone = GetGuildMemberCharacterInfo(guildId,i)
+            -- Might use zone check to NOT auto-invite people outside current zone if leader in Cyrodiil
+
+            if not hasChar then
+                d("Could not find player name for " .. acctName .. ". Please manually invite.")
+                return ""
+            end
+
+            charName = charName:gsub("%^.+", "")
+
+            d("In Cyrodiil? " .. b(AutoInvite.isCyrodiil()) .. " / Zone: " .. zone)
+
+            if AutoInvite.isCyrodiil() and zone ~= "Cyrodiil" then
+                d("Player " .. charName .. " is not in Cyrodiil but in " .. zone)
+                d("Blocking invite to prevent crashes.")
+                return ""
+            end
+
+            return charName
+        end
+    end
+end
+
+------------------------------------------------
+--- Event handlers
+------------------------------------------------
 AutoInvite.callback = function(_, messageType, from, message)
 	if not AutoInvite.enabled or not AutoInvite.listening then
 		return
@@ -36,56 +86,9 @@ AutoInvite.callback = function(_, messageType, from, message)
 	--d("Checking message '" .. string.lower(message) .."' ?= '" .. AutoInvite.cfg.watchStr .."'")
 end

-AutoInvite.isCyrodiil = function(unit)
-    if unit == nil then unit = "player" end
-    d("Current zone: '" .. GetUnitZone(unit) .. "'")
-    return GetUnitZone(unit) == "Cyrodiil"
-end
-
-local function b(v) if v then return "T" else return "F" end end
-
-SLASH_COMMANDS["/zdb9"] = function()
-    d("In Cyrodiil? " .. b(AutoInvite.isCyrodiil()))
-end
-
-AutoInvite.guildLookup = function(channel, acctName)
-	local guildId = 0
-	if channel == CHAT_CHANNEL_GUILD_1 or channel == CHAT_CHANNEL_OFFICER_1 then guildId = GetGuildId(1) end
-	if channel == CHAT_CHANNEL_GUILD_2 or channel == CHAT_CHANNEL_OFFICER_2 then guildId = GetGuildId(2) end
-	if channel == CHAT_CHANNEL_GUILD_3 or channel == CHAT_CHANNEL_OFFICER_3 then guildId = GetGuildId(3) end
-	if channel == CHAT_CHANNEL_GUILD_4 or channel == CHAT_CHANNEL_OFFICER_4 then guildId = GetGuildId(4) end
-	if channel == CHAT_CHANNEL_GUILD_5 or channel == CHAT_CHANNEL_OFFICER_5 then guildId = GetGuildId(5) end
-
-	if guildId == 0 then d("Error - couldn't invite on channel: " .. channel) end
-
-	local aName
-	for i=1,GetNumGuildMembers(guildId) do
-		aName = GetGuildMemberInfo(guildId,i)
-		if aName == acctName then
-			local hasChar, charName, zone = GetGuildMemberCharacterInfo(guildId,i)
-			-- Might use zone check to NOT auto-invite people outside current zone if leader in Cyrodiil
-
-			if not hasChar then
-                d("Could not find player name for " .. acctName .. ". Please manually invite.")
-                return ""
-            end
-
-            charName = charName:gsub("%^.+", "")
-
-            d("In Cyrodiil? " .. b(AutoInvite.isCyrodiil()) .. " / Zone: " .. zone)
-
-            if AutoInvite.isCyrodiil() and zone ~= "Cyrodiil" then
-                d("Player " .. charName .. " is not in Cyrodiil but in " .. zone)
-                d("Blocking invite to prevent crashes.")
-                return ""
-            end
-
-            return charName
-		end
-	end
-end
-
---Stop listening
+------------------------------------------------
+--- Main interface
+------------------------------------------------
 AutoInvite.disable = function()
 	AutoInvite.cfg.watchStr = ""
 	EVENT_MANAGER:UnregisterForEvent(AutoInvite.AddonId, EVENT_CHAT_MESSAGE_CHANNEL)
@@ -104,7 +107,9 @@ AutoInvite.enable = function()
 	d("AutoInvite set on string '" .. AutoInvite.cfg.watchStr .. "'")
 end

-
+------------------------------------------------
+--- Command line
+------------------------------------------------
 AutoInvite.help = function()
     d("AutoInvite - command '/ai <str>'. Usage")
     d("  '/ai foo' - autoInvite on 'foo' command'")
@@ -128,6 +133,14 @@ SLASH_COMMANDS["/ai"] = function(str)
     AutoInvite.enable()
 end

+
+SLASH_COMMANDS["/zdb9"] = function()
+    d("In Cyrodiil? " .. b(AutoInvite.isCyrodiil()))
+end
+
+------------------------------------------------
+--- Initialization
+------------------------------------------------
 AutoInvite.init = function()
     EVENT_MANAGER:UnregisterForEvent("AutoInviteInit", EVENT_PLAYER_ACTIVATED)
     if AutoInvite.initDone then return end