Add test kick commands - debug output only, no kicking

Scott Yeskie [05-16-14 - 21:33]
Add test kick commands - debug output only, no kicking
Filename
AutoInvite.lua
AutoInvite.txt
diff --git a/AutoInvite.lua b/AutoInvite.lua
index e4f85fc..8822492 100644
--- a/AutoInvite.lua
+++ b/AutoInvite.lua
@@ -1,19 +1,14 @@
 --[[
   * AutoInvite
   * Author: Sasky
-  GetGuildId(luaindex index)
-  GetNumGuilds()
-  GetNumGuildMembers(integer guildId)
-  SetGuildMemberNote(integer guildId, luaindex memberIndex, string note)
-  GetGuildMemberInfo
-	playerStatus: 1 online, 4 offline
 ]]--

 local AutoInvite = {}
-AutoInvite.watch = ""
+AutoInvite.watch = "" -- Watch string
 AutoInvite.AddonId = "AutoInvite"
-AutoInvite.listening = false
-AutoInvite.maxSize = 24
+AutoInvite.listening = false -- True iff listening for invites
+AutoInvite.kicking = false -- True iff kicking event registered
+AutoInvite.maxSize = 24 -- Maximum group size

 --Main interaction switch
 SLASH_COMMANDS["/ai"] = function(str)
@@ -30,7 +25,7 @@ SLASH_COMMANDS["/ai"] = function(str)
 end

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

 	if string.lower(message) == AutoInvite.watch 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
+
 		from = from:gsub("%^.+", "")
 		d("Sending invite to " .. from)
 		GroupInvite(from)
@@ -51,6 +51,32 @@ AutoInvite.callback = function(eventCode, messageType, from, message)
 	--d("Checking message '" .. string.lower(message) .."' ?= '" .. AutoInvite.watch .."'")
 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
+			hasChar, charName, zone = GetGuildMemberCharacterInfo(guildId,i)
+			-- Might use zone check to NOT auto-invite people outside current zone if leader in Cyrodiil
+			if hasChar then
+				return charName:gsub("%^.+", "")
+			else
+				d("Could not find player name for " .. acctName .. ". Please manually invite.")
+				return ""
+			end
+		end
+	end
+end
+
 --Stop listening
 AutoInvite.disable = function()
 	AutoInvite.watch = ""
@@ -58,17 +84,67 @@ AutoInvite.disable = function()
 	AutoInvite.listening = false
 end

+--Call for when user runs enable command. Sets up listeners.
 AutoInvite.enable = function(str)
 	AutoInvite.watch = string.lower(str)
 	if not AutoInvite.listening then
 		--Add handler
 		EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_CHAT_MESSAGE_CHANNEL, AutoInvite.callback)
 		AutoInvite.listening = true
-	end
+    end
+
+    --First check. When option, will have to check option for disable
+    if not AutoInvite.kicking then
+        EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_GROUP_MEMBER_CONNECTED_STATUS, AutoInvite.offlineEvent)
+        EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_GROUP_MEMBER_LEFT, AutoInvite.leaveEvent)
+        AutoInvite.kicking = true
+        zo_callLater(AutoInvite.tick, 10000)
+    end

 	d("AutoInvite set on string '" .. AutoInvite.watch .. "'")
 end

+AutoInvite.kickTable = {}
+
+AutoInvite.offlineEvent = function(_, unitTag, connectStatus)
+    local unitName = GetUnitName(unitTag):gsub("%^.+", "")
+    if not connectStatus then
+        d(unitTag .. "/" .. unitName .. " has disconnected")
+        AutoInvite.kickTable[unitName] = GetTimeStamp()
+    else
+        d(unitName .. " has reconnected")
+        AutoInvite.kickTable[unitName] = nil
+    end
+end
+
+local function tf(val) if val 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
+    d("Current zone: '" .. GetUnitZone(unit) .. "'")
+    return GetUnitZone(unit) == "Cyrodiil"
+end
+
+AutoInvite.leaveEvent = function(code, unitTag, reason, wasLocal)
+    local unitName = GetUnitName(unitTag):gsub("%^.+", "")
+    AutoInvite.kickTable[unitName] = nil
+    d("Left group: " .. unitTag .. "/" .. unitName .. " left for " .. reason .. ". Local?" .. tf(wasLocal))
+end
+
+
+-- tick function: called every 15s
+AutoInvite.tick = function()
+    d("Current timestamp: " .. GetTimeStamp())
+    d("Offline players:")
+    for p,t in pairs(AutoInvite.kickTable) do
+        d("  " .. p .. " offline at " .. nn(t))
+    end
+
+    zo_callLater(AutoInvite.tick, 15000)
+end
+
+-- print command usage
 AutoInvite.help = function()
 	d("AutoInvite - command '/ai <str>'. Usage")
 	d("  '/ai foo' - autoInvite on 'foo' command'")
@@ -76,3 +152,43 @@ AutoInvite.help = function()
 	d("  '/ai' - turn off autoInvite (auto on group full)")
 	return
 end
+
+-- Debug commands
+SLASH_COMMANDS["/airk"] = function()
+    d("Registering event stage message")
+    EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_GROUP_MEMBER_CONNECTED_STATUS, AutoInvite.offlineEvent)
+    EVENT_MANAGER:RegisterForEvent(AutoInvite.AddonId, EVENT_GROUP_MEMBER_LEFT, AutoInvite.leaveEvent)
+    AutoInvite.kicking = true
+end
+
+SLASH_COMMANDS["/airt"] = AutoInvite.tick
+
+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("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)
+            --GroupKick(string unitTag)
+        else
+            d("  " .. p .. " offline for " .. now - t)
+        end
+    end
+end
+
diff --git a/AutoInvite.txt b/AutoInvite.txt
index 179971a..9dab0d2 100644
--- a/AutoInvite.txt
+++ b/AutoInvite.txt
@@ -1,6 +1,6 @@
 ## APIVersion: 100003
 ## Title: AutoInvite
-## Version: 1.0
+## Version: 1.1
 ## Author: Sasky

 AutoInvite.lua