Persist settings between loads. Properly setup init()

Sasky [06-12-14 - 05:36]
Persist settings between loads. Properly setup init()
Filename
AutoInvite.lua
AutoInvite.txt
AutoInviteUI.lua
diff --git a/AutoInvite.lua b/AutoInvite.lua
index a16cdd5..d4dc467 100644
--- a/AutoInvite.lua
+++ b/AutoInvite.lua
@@ -3,43 +3,29 @@
   * Author: Sasky
 ]]--

-AutoInvite = {}
-AutoInvite.watch = ""
-AutoInvite.AddonId = "AutoInvite"
-AutoInvite.listening = false
-AutoInvite.maxSize = 24
-
---Main interaction switch
-SLASH_COMMANDS["/ai"] = function(str)
-	if not str or str == "" or str == "help" then
-		if not AutoInvite.listening or str == "help" then
-			AutoInvite.help()
-			return
-		end
-		d("Disabling AutoInvite")
-		AutoInvite.disable()
-		return
-	end
-	AutoInvite.enable(str)
+if AutoInvite == nil then
+    AutoInvite = {}
 end
+AutoInvite.AddonId = "AutoInvite"

 --Main callback - sends invites
 AutoInvite.callback = function(_, messageType, from, message)
-	if not AutoInvite.listening then
-		d("WARN: AutoInvite not listening properly. Please reset. ('/ai help' for commands)")
+	if not AutoInvite.enabled or not AutoInvite.listening then
 		return
 	end

-	if GetGroupSize() >= AutoInvite.maxSize then
+	if GetGroupSize() >= AutoInvite.cfg.maxSize then
 		d("Group full. Disabling AutoInvite")
 		AutoInvite.disable()
 	end

-	if string.lower(message) == AutoInvite.watch and from ~= nil and from ~= "" then
+	if string.lower(message) == AutoInvite.cfg.watchStr and from ~= nil and from ~= "" then
 		if (messageType >= CHAT_CHANNEL_GUILD_1 and messageType <= CHAT_CHANNEL_OFFICER_5) then
 			from = AutoInvite.guildLookup(messageType, from)
 			if from == "" then return end
-		end
+        end
+
+        --TODO: Add friends list lookup

 		from = from:gsub("%^.+", "")
 		d("Sending invite to " .. from)
@@ -47,7 +33,7 @@ AutoInvite.callback = function(_, messageType, from, message)
 		GroupInviteByName(from)
 	end

-	--d("Checking message '" .. string.lower(message) .."' ?= '" .. AutoInvite.watch .."'")
+	--d("Checking message '" .. string.lower(message) .."' ?= '" .. AutoInvite.cfg.watchStr .."'")
 end

 AutoInvite.isCyrodiil = function(unit)
@@ -101,26 +87,64 @@ end

 --Stop listening
 AutoInvite.disable = function()
-	AutoInvite.watch = ""
+	AutoInvite.cfg.watchStr = ""
 	EVENT_MANAGER:UnregisterForEvent(AutoInvite.AddonId, EVENT_CHAT_MESSAGE_CHANNEL)
-	AutoInvite.listening = false
+	AutoInvite.enabled = false
+    AutoInvite.listening = false
 end

-AutoInvite.enable = function(str)
-	AutoInvite.watch = string.lower(str)
-	if not AutoInvite.listening then
+AutoInvite.enable = function()
+    AutoInvite.enabled = true
+	if not AutoInvite.listening and GetGroupSize() < AutoInvite.cfg.maxSize then
 		--Add handler
 		EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_CHAT_MESSAGE_CHANNEL, AutoInvite.callback)
 		AutoInvite.listening = true
 	end

-	d("AutoInvite set on string '" .. AutoInvite.watch .. "'")
+	d("AutoInvite set on string '" .. AutoInvite.cfg.watchStr .. "'")
 end

+
 AutoInvite.help = function()
-	d("AutoInvite - command '/ai <str>'. Usage")
-	d("  '/ai foo' - autoInvite on 'foo' command'")
-	d("  '/ai help' - show this menu")
-	d("  '/ai' - turn off autoInvite (auto on group full)")
-	return
+    d("AutoInvite - command '/ai <str>'. Usage")
+    d("  '/ai foo' - autoInvite on 'foo' command'")
+    d("  '/ai help' - show this menu")
+    d("  '/ai' - turn off autoInvite (auto on group full)")
+    return
 end
+
+--Main interaction switch
+SLASH_COMMANDS["/ai"] = function(str)
+    if not str or str == "" or str == "help" then
+        if not AutoInvite.listening or str == "help" then
+            AutoInvite.help()
+            return
+        end
+        d("Disabling AutoInvite")
+        AutoInvite.disable()
+        return
+    end
+    AutoInvite.cfg.watchStr = string.lower(str)
+    AutoInvite.enable()
+end
+
+AutoInvite.init = function()
+    EVENT_MANAGER:UnregisterForEvent("AutoInviteInit", EVENT_PLAYER_ACTIVATED)
+    if AutoInvite.initDone then return end
+    AutoInvite.initDone = true
+
+    local def = {
+        maxSize = 24,
+        restart = false,
+        cyrCheck = false,
+        autoKick = false,
+        kickDelay = 300,
+        watchStr = "",
+    }
+    AutoInvite.cfg = ZO_SavedVars:NewAccountWide("AutoInviteSettings", 1.0, "config", def)
+    AutoInvite.listening = false
+    AutoInvite.enabled = false
+    AutoInviteUI.init()
+end
+
+EVENT_MANAGER:RegisterForEvent("AutoInviteInit", EVENT_PLAYER_ACTIVATED, AutoInvite.init)
\ No newline at end of file
diff --git a/AutoInvite.txt b/AutoInvite.txt
index e6a897b..c6ec614 100644
--- a/AutoInvite.txt
+++ b/AutoInvite.txt
@@ -2,6 +2,7 @@
 ## Title: AutoInvite
 ## Version: 1.1.1
 ## Author: Sasky
+## SavedVariables: AutoInviteSettings
 ## OptionalDependsOn: LibAddonMenu-2.0

 lib/LibStub.lua
diff --git a/AutoInviteUI.lua b/AutoInviteUI.lua
index 796a903..a554ae4 100644
--- a/AutoInviteUI.lua
+++ b/AutoInviteUI.lua
@@ -45,8 +45,8 @@ function AutoInviteUI.create()
         type = "editbox",
         name = "Invite String",
         tooltip = "Text to check messages to auto-invite for",
-        getFunc = function() return AutoInvite.watch end,
-        setFunc = function(val) AutoInvite.watch = val end,
+        getFunc = function() return AutoInvite.cfg.watchStr end,
+        setFunc = function(val) AutoInvite.cfg.watchStr = val end,
         width = "half"
     })
     ui.text:SetWidth(260)
@@ -58,8 +58,8 @@ function AutoInviteUI.create()
         tooltip = "Maximum number of players to invite to group",
         min = 4,
         max = 24,
-        getFunc = function() return AutoInvite.maxSize end,
-        setFunc = function(val) AutoInvite.maxSize = val end,
+        getFunc = function() return AutoInvite.cfg.maxSize end,
+        setFunc = function(val) AutoInvite.cfg.maxSize = val end,
         default = 24,
         width = "half"
     })
@@ -69,10 +69,10 @@ function AutoInviteUI.create()
         type = "checkbox",
         name = "Restart",
         tooltip = "Restart AutoInvite if drop below max",
-        getFunc = function() return false end,
-        setFunc = function(val)  end,
+        getFunc = function() return AutoInvite.cfg.restart end,
+        setFunc = function(val) AutoInvite.cfg.restart = val  end,
         width = "half",
-        disabled = true
+        warning = "Experimental"
     })
     ui.restart:SetDimensions(250, 26)
     ui.restart:SetAnchor(TOPLEFT, ui.max, BOTTOMLEFT, 0, 12)
@@ -81,10 +81,10 @@ function AutoInviteUI.create()
         type = "checkbox",
         name = "Cyrodiil Check",
         tooltip = "Only invite players that are in Cyrodiil",
-        getFunc = function() return false end,
-        setFunc = function(val)  end,
+        getFunc = function() return AutoInvite.cfg.cyrCheck end,
+        setFunc = function(val) AutoInvite.cfg.cyrCheck = val end,
         width = "half",
-        disabled = true
+        warning = "Experimental"
     })
     ui.cyr:SetDimensions(250, 26)
     ui.cyr:SetAnchor(TOPLEFT, ui.restart, BOTTOMLEFT, 0, 5)
@@ -93,9 +93,10 @@ function AutoInviteUI.create()
         type = "checkbox",
         name = "Auto kick",
         tooltip = "Kick players that go offline",
-        getFunc = function() return false end,
-        setFunc = function(val)  end,
+        getFunc = function() return AutoInvite.cfg.autoKick end,
+        setFunc = function(val) AutoInvite.cfg.autoKick = val  end,
         width = "half",
+        warning = "Coming soon",
         disabled = true
     })
     ui.kick:SetDimensions(250, 26)
@@ -107,8 +108,8 @@ function AutoInviteUI.create()
         tooltip = "Number of seconds to wait before kicking an offline player",
         min = 5,
         max = 600,
-        getFunc = function() return 300 end,
-        setFunc = function(val) end,
+        getFunc = function() return AutoInvite.cfg.kickDelay end,
+        setFunc = function(val) AutoInvite.cfg.kickDelay = 300 end,
         default = 300,
         width = "half",
         disabled = true
@@ -134,13 +135,13 @@ function AutoInviteUI.init()
     if ui.created then return end
     ui.created = true
     AutoInviteUI.create()
+    ui.main:SetHidden(true)

     --Register events
     EVENT_MANAGER:RegisterForEvent('AutoInvite', EVENT_ACTION_LAYER_POPPED, AutoInviteUI.menuHide)
     EVENT_MANAGER:RegisterForEvent('AutoInvite', EVENT_ACTION_LAYER_PUSHED, AutoInviteUI.menuShow)
 end

-SLASH_COMMANDS["/aiui"] = AutoInviteUI.init
 SLASH_COMMANDS["/zzg"] = function()
     d(ZO_GroupList:IsHidden())
 end