Merge branch 'autoKick'

Sasky [06-13-14 - 06:45]
Merge branch 'autoKick'

Conflicts:
	AutoInvite.lua
	AutoInvite.txt

Rework to new config menu
Filename
.gitignore
AutoInvite.lua
AutoInvite.txt
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8c5c4f2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+AutoInvite.iml
+.idea
+out
diff --git a/AutoInvite.lua b/AutoInvite.lua
index dcf5ae4..e791252 100644
--- a/AutoInvite.lua
+++ b/AutoInvite.lua
@@ -12,6 +12,7 @@ AutoInvite.AddonId = "AutoInvite"
 --- Utility functions
 ------------------------------------------------
 local function b(v) if v then return "T" else return "F" end end
+local function nn(val) if val == nil then return "NIL" else return val end end

 AutoInvite.isCyrodiil = function(unit)
     if unit == nil then unit = "player" end
@@ -58,6 +59,30 @@ AutoInvite.guildLookup = function(channel, acctName)
     end
 end

+
+AutoInvite.kickTable = {}
+function AutoInvite.checkOffline()
+    local now = GetTimeStamp()
+    for i=1,GetGroupSize() do
+        local tag = GetGroupUnitTagByIndex(i)
+        if not IsUnitOnline(tag) then
+            AutoInvite.kickTable[GetUnitName(tag)] = now
+        end
+    end
+end
+
+--Since KickByName doesn't seem to be working
+function AutoInvite.kickByName(name)
+    for i=1,GetGroupSize() do
+        local tag = GetGroupUnitTagByIndex(i)
+        if GetUnitName(tag) == name then
+            GroupKick(tag)
+            AutoInvite.kickTable[name] = nil
+            return
+        end
+    end
+end
+
 ------------------------------------------------
 --- Event handlers
 ------------------------------------------------
@@ -71,7 +96,7 @@ AutoInvite.callback = function(_, messageType, from, message)
 		d("Group full. Disabling AutoInvite")
         AutoInvite.stopListening()
 	end
-
+
 	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)
@@ -93,6 +118,36 @@ AutoInvite.playerLeave = function()
     if AutoInvite.enabled and AutoInvite.cfg.restart and GetGroupSize() < AutoInvite.cfg.maxSize then
         AutoInvite.startListening()
     end
+
+    local unitName = GetUnitName(unitTag):gsub("%^.+", "")
+    AutoInvite.kickTable[unitName] = nil
+end
+
+AutoInvite.offlineEvent = function(_, unitTag, connectStatus)
+    local unitName = GetUnitName(unitTag):gsub("%^.+", "")
+    if connectStatus then
+        d(unitTag .. "/" .. unitName .. " has reconnected")
+        AutoInvite.kickTable[unitName] = nil
+    else
+        d(unitTag .. "/" .. unitName .. " has disconnected")
+        AutoInvite.kickTable[unitName] = GetTimeStamp()
+    end
+end
+
+-- tick function: called every 15s
+function AutoInvite.kickCheck()
+    if not AutoInvite.cfg.autoKick then return end
+    local now = GetTimeStamp()
+    --d("Check kick")
+    for p,t in pairs(AutoInvite.kickTable) do
+        local offTime = GetDiffBetweenTimeStamps(now, t)
+        if offTime > AutoInvite.cfg.kickDelay then
+            d("  KICK: " .. p .. " offline for " .. offTime)
+            AutoInvite.kickByName(p)
+        else
+            d(p .. " offline for " .. offTime .. " / " .. AutoInvite.cfg.kickDelay)
+        end
+    end
 end

 ------------------------------------------------
@@ -101,6 +156,7 @@ end
 AutoInvite.disable = function()
 	AutoInvite.enabled = false
     AutoInvite.stopListening()
+    EVENT_MANAGER:UnregisterForUpdate("AutoInviteKickCheck")
 end

 AutoInvite.stopListening = function()
@@ -109,19 +165,28 @@ AutoInvite.stopListening = function()
 end

 AutoInvite.startListening = function()
-    AutoInvite.enabled = true
+    if not AutoInvite.enabled then
+        AutoInvite.enabled = true
+        AutoInvite.checkOffline()
+        EVENT_MANAGER:RegisterForUpdate("AutoInviteKickCheck", 1000, AutoInvite.kickCheck)
+    end
+
 	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.cfg.watchStr .. "'")
+    end
+
+    --First check. When option, will have to check option for disable
+	d("AutoInvite listening on string '" .. AutoInvite.cfg.watchStr .. "'")
 end

 ------------------------------------------------
 --- Command line
 ------------------------------------------------
+
+
+-- print command usage
 AutoInvite.help = function()
     d("AutoInvite - command '/ai <str>'. Usage")
     d("  '/ai foo' - autoInvite on 'foo' command'")
@@ -168,9 +233,39 @@ AutoInvite.init = function()
     }
     AutoInvite.cfg = ZO_SavedVars:NewAccountWide("AutoInviteSettings", 1.0, "config", def)
     EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_GROUP_MEMBER_LEFT, AutoInvite.playerLeave)
+    EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_GROUP_MEMBER_CONNECTED_STATUS, AutoInvite.offlineEvent)
     AutoInvite.listening = false
     AutoInvite.enabled = false
     AutoInviteUI.init()
 end

-EVENT_MANAGER:RegisterForEvent("AutoInviteInit", EVENT_PLAYER_ACTIVATED, AutoInvite.init)
\ No newline at end of file
+EVENT_MANAGER:RegisterForEvent("AutoInviteInit", EVENT_PLAYER_ACTIVATED, AutoInvite.init)
+
+-- Debug commands
+SLASH_COMMANDS["/aik"] = function()
+    local now = GetTimeStamp()
+    --d("Current timestamp: " .. GetTimeStamp())
+    --d("Offline players:")
+    for p,t in pairs(AutoInvite.kickTable) do
+        local offTime = now - t
+        if offTime > 300 then
+            d("  KICK: " .. p .. " offline for " .. now - t)
+        else
+            --d("  " .. p .. " offline for " .. now - t)
+        end
+    end
+end
+
+SLASH_COMMANDS["/aikick"] = function()
+    local now = GetTimeStamp()
+    d("Offline players:")
+    for p,t in pairs(AutoInvite.kickTable) do
+        local offTime = now - t
+        if offTime > AutoInvite.cfg.kickDelay then
+            d("  KICK: " .. p .. " offline for " .. now - t)
+            GroupKickByName(p)
+        else
+            d("  " .. p .. " offline for " .. now - t)
+        end
+    end
+end
diff --git a/AutoInvite.txt b/AutoInvite.txt
index f4c7624..4ce71fe 100644
--- a/AutoInvite.txt
+++ b/AutoInvite.txt
@@ -21,4 +21,4 @@ lib/LibAddonMenu-2.0/controls/slider.lua
 lib/LibAddonMenu-2.0/controls/texture.lua

 AutoInviteUI.lua
-AutoInvite.lua
\ No newline at end of file
+AutoInvite.lua