Quantcast

"/ovale ping" requests Ovale versions from the group.

Johnny C. Lam [06-04-14 - 20:24]
"/ovale ping" requests Ovale versions from the group.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1515 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
Ovale.lua
OvaleOptions.lua
diff --git a/Ovale.lua b/Ovale.lua
index fdcf6e6..aabd2f3 100644
--- a/Ovale.lua
+++ b/Ovale.lua
@@ -9,19 +9,22 @@
 --]]----------------------------------------------------------------------

 local addonName, addonTable = ...
-Ovale = LibStub("AceAddon-3.0"):NewAddon(addonTable, addonName, "AceConsole-3.0", "AceEvent-3.0")
+Ovale = LibStub("AceAddon-3.0"):NewAddon(addonTable, addonName, "AceConsole-3.0", "AceEvent-3.0", "AceSerializer-3.0", "AceTimer-3.0")

 --<private-static-properties>
 local L = LibStub("AceLocale-3.0"):GetLocale(addonName)
 local OvaleOptions = nil

 local format = string.format
+local next = next
 local pairs = pairs
 local select = select
 local tostring = tostring
 local wipe = table.wipe
 local API_GetTime = GetTime
+local API_IsInGroup = IsInGroup
 local API_RegisterAddonMessagePrefix = RegisterAddonMessagePrefix
+local API_SendAddonMessage = SendAddonMessage
 local API_UnitCanAttack = UnitCanAttack
 local API_UnitExists = UnitExists
 local API_UnitHasVehicleUI = UnitHasVehicleUI
@@ -93,6 +96,7 @@ function Ovale:OnInitialize()
 end

 function Ovale:OnEnable()
+	self:RegisterEvent("CHAT_MSG_ADDON")
 	self:RegisterEvent("PLAYER_REGEN_ENABLED")
 	self:RegisterEvent("PLAYER_REGEN_DISABLED")
 	self:RegisterEvent("PLAYER_TARGET_CHANGED")
@@ -102,12 +106,55 @@ function Ovale:OnEnable()
 end

 function Ovale:OnDisable()
+	self:UnregisterEvent("CHAT_MSG_ADDON")
 	self:UnregisterEvent("PLAYER_REGEN_ENABLED")
 	self:UnregisterEvent("PLAYER_REGEN_DISABLED")
 	self:UnregisterEvent("PLAYER_TARGET_CHANGED")
 	self.frame:Hide()
 end

+do
+	local versionReply = {}
+	local timer
+
+	function Ovale:CHAT_MSG_ADDON(event, ...)
+		local prefix, message, channel, sender = ...
+		if prefix == OVALE_MSG_PREFIX then
+			local ok, msgType, version = self:Deserialize(message)
+			if ok then
+				if msgType == "V" then
+					local msg = self:Serialize("VR", self.version)
+					local channel = API_IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and "INSTANCE_CHAT" or "RAID"
+					API_SendAddonMessage(OVALE_MSG_PREFIX, msg, channel)
+				elseif msgType == "VR" then
+					versionReply[sender] = version
+				end
+			end
+		end
+	end
+
+	function Ovale:VersionCheck()
+		if not timer then
+			wipe(versionReply)
+			local message = "V"
+			local channel = API_IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and "INSTANCE_CHAT" or "RAID"
+			API_SendAddonMessage(OVALE_MSG_PREFIX, message, channel)
+			timer = self:ScheduleTimer("PrintVersionCheck", 3)
+		end
+	end
+
+	function Ovale:PrintVersionCheck()
+		if next(versionReply) then
+			for sender, version in pairs(versionReply) do
+				self:FormatPrint(">>> %s is using Ovale %s", sender, version)
+			end
+		else
+			self:Print(">>> No other Ovale users present.")
+		end
+		timer = nil
+	end
+end
+
 --Called when the player target change
 --Used to update the visibility e.g. if the user chose
 --to hide Ovale if a friendly unit is targeted
diff --git a/OvaleOptions.lua b/OvaleOptions.lua
index d61e0bf..cae6b02 100644
--- a/OvaleOptions.lua
+++ b/OvaleOptions.lua
@@ -709,6 +709,12 @@ local self_options =
 					type = "execute",
 					func = function() Ovale:Print(Ovale.version) end,
 				},
+				ping = {
+					order = -15,
+					name = "Ping for Ovale users in group",
+					type = "execute",
+					func = function() Ovale:VersionCheck() end,
+				},
 			},
 		},
 	},