All pastes #56909 Raw Edit

slugora

public text v1 · immutable
#56909 ·published 2006-05-18 17:41 UTC
rendered paste body
oRA_Core 		= {}
oRA_Core.Checks 	= {}
oRA_Core.ShortHandList 	= {}

local Checks		= oRA_Core.Checks
local ShortHandList 	= oRA_Core.ShortHandList

oRA_Core = AceAddonClass:new({
	name          		= oRA_CORE_CONST.Title,
	description   		= oRA_CORE_CONST.Desc,
	version       		= "1.1." .. string.sub("$Revision: 1894 $", 12, -3),
	releaseDate   		= string.sub("$Date: 2006-05-17 15:32:16 +0200 (on, 17 maj 2006) $", 8, 17),
	aceCompatible 		= 103,
	author        		= "Haste",
	email         		= "haste@haste3k.net",
	website		  	= "http://www.haste3k.net",
	category      		= "raid",
	db            		= AceDbClass:new("oRADB"),
	cmd           		= AceChatCmdClass:new(oRA_CORE_CONST.ChatCmd,oRA_CORE_CONST.ChatOpt),
	
	RosterInfo		= {},
	
	Register = function(self, module)
		if( not self.modules ) then self.modules = {} end
		self.modules[module.type] = module
		self[module.type] = module
		module.app = self
		module:Register()
		if( module.cmdOptions ) then
			module.cmdOptions.handler = module
			tinsert(module.cmdOptions.args or {}, 1, oRA_CORE_TOGGLE)
			tinsert(self.cmd.options, module.cmdOptions)
		end
	end,
		
	----------------------------
	--			Module Loadup			--
	----------------------------
	
	GetOpt = function(self, path, var)
		if (not var) then var = path; path = nil; end
		local profilePath = path and {self.profilePath, path} or self.profilePath;
	   
		return self.db:get(profilePath, var)
	end,
	
	SetOpt = function(self, path, var, val)
		if (not val) then val = var; var = path; path = nil; end
		local profilePath = path and {self.profilePath, path} or self.profilePath;
	
		return self.db:set(profilePath, var, val)
	end,
	
	TogOpt = function(self, path, var)
		if (not var) then var = path; path = nil; end
		local profilePath = path and {self.profilePath, path} or self.profilePath;
	
		return self.db:toggle(profilePath, var)
	end,
	
	SetModState = function(self, mod, state)
		self.db:set({self.profilePath, "modules"}, mod.type, ace.toggle(state))
	end,
	
	ModEnabled = function(self, mod)
		return ace.toggle(self.db:get({self.profilePath, "modules"}, mod.type))
	end,
		
	Initialize = function(self)
		self.metro = Metrognome:GetInstance("1")
		self.metro:Register("oRA Channel", self.CheckChannel, 10, self)
		self.metro:Register("oRA Raid", self.updateRoster, 7, self)
		self:RegisterEvent("MEETINGSTONE_CHANGED")
	end,
	
	Enable = function(self)
		self:RegisterEvent("CHAT_MSG_RAID", "raidParser")
		self:RegisterEvent("RA_SEND_VERSION","raidDistributeVersion")
		self:RegisterEvent("RA_UPDATE_AFK_OR_DND")
		
		self:AddCheck("RA_SEND_VERSION","SR")
		self:AddCheck("RA_UPDATE_AFK_OR_DND","AFK")
		self:AddCheck("RA_UPDATE_AFK_OR_DND","UNAFK")
		self:AddCheck("RA_UPDATE_AFK_OR_DND","DND")
		self:AddCheck("RA_UPDATE_AFK_OR_DND","UNDND")
		
		self:RegisterShortHand( "rabc", oRA_Core["BroadcastShortHand"] )
		self:RegisterShortHand( "rabroadcast", oRA_Core["BroadcastShortHand"] )
	end,
	
	MEETINGSTONE_CHANGED = function(self)
		self.metro:Start("oRA Channel")
	end,
	
	RAID_ROSTER_UPDATE = function(self)
		self.metro:Start("oRA Raid")
	end,
	
	RA_UPDATE_AFK_OR_DND = function(self, a1, a2, a3, a4, a5, a6, a7, a8, a9)
		local msg = self:Clean(a1)
		local nick = a2
		local member = self.RosterInfo[nick]
		
		if ( not member ) then return end
		
		if ( msg == "AFK" ) then
			member.afk = true
		elseif ( msg == "DND" ) then
			member.dnd = true
		elseif ( msg == "UNAFK" ) then
			member.afk = nil
		elseif ( msg == "UNDND" ) then
			member.dnd = nil
		end
	end,
	
	CheckChannel = function(self)
		self.metro:Stop("oRA Channel")
		
		if(not self:GetOpt("autoleave") and self:GetOpt("channel")) then
			self:JoinChannel()
			return
		end
		
		if(UnitInRaid("player") and self:GetOpt("channel")) then
			self:JoinChannel()
		elseif(self:GetOpt("channel") and not UnitInRaid("player")) then
			self:LeaveChannel()
		end
		self:UnregisterEvent("MEETINGSTONE_CHANGED")
	end,
	
	raidParser = function(self)
		local start, endd, chan = string.find(arg1, oRA_CORE_CT_CHANNEL_BROADCAST .. "(.+)")
		if(chan~=nil) then
			if(not self:GetOpt("channel")) then
				self:SetOpt("channel", chan)
				self:JoinChannel()
			elseif(self:GetOpt("channel") and self:GetOpt("channel")~=chan) then
				self:LeaveChannel()
				self:SetOpt("channel", chan)
				self:JoinChannel()
			else
				self:raidDistributeVersion()
			end
		end
	end,
	
	raidDistributeVersion = function(self)
		self:Send("V ".. oRA_CORE_CONST.RAVersion)
	end,
	
	JoinChannel = function(self)
		self:RegisterEvent("CHAT_MSG_CHANNEL", "raidParseMessage")
		self:RegisterEvent("CHAT_MSG_SYSTEM", "SystemMessageParser")
		self:RegisterEvent("RAID_ROSTER_UPDATE")
		
		JoinChannelByName(self:GetOpt("channel"))
		CT_RA_Channel=self:GetOpt("channel") 
		
		self:raidDistributeVersion()
		self:updateRoster()
		
		self:Msg("Channel set to: ".. CT_RA_Channel)
		self:TriggerEvent("RA_JOIN_CHANNEL")
	end,

	LeaveChannel = function(self)
		self:UnregisterEvent("CHAT_MSG_CHANNEL")
		self:UnregisterEvent("CHAT_MSG_SYSTEM")
		self:updateRoster()
		
		local chan = self:GetOpt("channel") or nil
		
		if (chan==nil) then return end
		
		if (self:GetOpt("autoleave")) then
			CT_RA_Channel=nil
			LeaveChannelByName(chan)
			self:Msg("Left channel: "..chan)
			self:TriggerEvent("RA_LEFT_CHANNEL")
			self:SetOpt("channel", nil)
		end
	end,	
	
	raidParseMessage = function(self)
		if self.RosterInfo[arg2] then
			local a1,a2,a3,a4,a5,a6,a7,a8,a9 = arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 
			local chans = self:GetOpt("channel")
			if ( chans and strlower(a9) == strlower(chans) and a1 and type(a1) == "string" ) then
				if ( not arg1 ) then
					return
				end
				
				local msg = self:Clean(a1)
				
				if ( string.find(msg, "#") ) then
					local arr = self:Split(msg, "#")
					for c, b in arr do
						local cmd = self:Split(b, " ")
						if Checks[cmd[1]] then
							self:TriggerEvent(Checks[cmd[1]].event, b, a2, a3, a4, a5, a6, a7, a8, a9)
						end
					end
				else
					local cmd = self:Split(msg, " ")
					if Checks[cmd[1]] then
						self:TriggerEvent(Checks[cmd[1]].event, a1, a2, a3, a4, a5, a6, a7, a8, a9)
					end
				end
			end
		end
	end,
	
	Split = function(self, msg, char)
		local arr = { };
		while (string.find(msg, char) ) do
			local iStart, iEnd = string.find(msg, char);
			tinsert(arr, strsub(msg, 1, iStart-1));
			msg = strsub(msg, iEnd+1, strlen(msg));
		end
		if ( strlen(msg) > 0 ) then
			tinsert(arr, msg);
		end
		return arr;
	end,
	
	SystemMessageParser = function(self)
		if(string.find(arg1, ERR_RAID_YOU_LEFT)) then
			self:TriggerEvent("RA_LEFT_RAID")
			self:LeaveChannel()
		elseif(string.find(arg1, oRA_CORE_MARKED_AFK_PATTERN)) then
			self:Send("AFK")
		elseif(string.find(arg1, oRA_CORE_MARKED_DND_PATTERN)) then
			self:Send("DND")
		elseif(string.find(arg1, oRA_CORE_CLEARED_AFK_PATTERN)) then
			local RememberAFK = GetCVar("autoClearAFK")  
			SetCVar("autoClearAFK", 0)       
			self:Send("UNAFK")
			SetCVar("autoClearAFK", RememberAFK) 
		elseif(string.find(arg1, oRA_CORE_CLEARED_DND_PATTERN)) then
			self:Send("UNDND")
		end
	end,
	
	IsPromoted = function(self, sMsg)
		if(IsRaidLeader() or IsRaidOfficer()) then
			return TRUE
		elseif (sMsg) then
			self:Msg(oRA_CORE_CONST.chat.RaidLeaderOnly)
		end
	end,
	
	isValidRequest = function(self, name, norank)
		if ( self.RosterInfo[name] ) then
			if (norank) then
				return TRUE
			elseif (self.RosterInfo[name]["rank"]>0) then
				return true
			end
		else
			return FALSE
		end
	end,
	
	Send = function(self, msg)
		if(not self:GetOpt("channel")) then return end
		
		msg = string.gsub(msg, "|Hitem:%d+:%d+:%d+:%d+|h(%[[^%|]+%])|h", "%1")
		msg = string.gsub(msg, "|c%w%w%w%w%w%w%w%w([^%|]+)|r", "%1")
		msg = string.gsub(msg, "s", "$")
		msg = string.gsub(msg, "S", "§")
		
		SendChatMessage(msg, "CHANNEL", nil, GetChannelName(self:GetOpt("channel")))
	end,
	
	Clean = function(self, sMsg)
		msg = string.gsub(sMsg, "%$", "s")
		msg = string.gsub(msg, "§", "S")
	
		if ( strsub(msg, strlen(msg)-7) == " ...hic!") then
			msg = strsub(msg, 1, strlen(msg)-8)
		end
		return msg
	end,

	RegisterShortHand = function(self, shorthand, func, system )
		if( shorthand ~= nil and shorthand ~= "" ) then 
			if( self:GetOpt("shorthands") ) then
				local type = "ORA_SHORTHAND_"..strupper(shorthand)
				SlashCmdList[type]  = func
				setglobal("SLASH_"..type.."1", "/"..strlower(shorthand))
			end
			if( not system ) then
				ShortHandList[shorthand] = func
			end
		end
	end,
	
	UnregisterShortHand = function( self, shorthand )
		if( shorthand ~= nil and shorthand ~= "" ) then
			for s,f in ShortHandList do
				if( s == shorthand ) then
					local type = "ORA_SHORTHAND_"..strupper(s)
					SlashCmdList[type] = nil
					setglobal("SLASH_"..type.."1", nil)
					ShortHandList[s] = nil
				end
			end
		end
	end,
	
	BroadcastShortHand = function( channel)
		oRA_Core:chatBroadcast(channel)
	end,
	
	updateRoster = function(self)
		local i, name, rank
				
		for i = 1, GetNumRaidMembers(), 1 do
			name, rank, _, _, _, _, _, _, _ = GetRaidRosterInfo(i)
			if ( not self.RosterInfo[name] ) then
				self.RosterInfo[name] = {}
			end
			self.RosterInfo[name]["rank"] = rank
			self.RosterInfo[name]["updated"] = true
		end
		
		-- Remove members not present in the raid anymore
		for name in self.RosterInfo do
			if ( self.RosterInfo[name]["updated"] == false ) then
				self.RosterInfo[name] = nil
			else
				self.RosterInfo[name]["updated"] = false
			end
		end
	end,
	
	
----------------------------
--			Chat			--
----------------------------
	
	chatShortHands = function( self ) 
		self:TogOpt("shorthands")
		self:Msg(oRA_CORE_CONST.Chat.ShortHands, ACEG_MAP_ONOFF[self:GetOpt("shorthands") or 0])
		if( self:GetOpt( "shorthands" ) ) then
			for s,f in ShortHandList do
				self:RegisterShortHand( s, f, TRUE )
			end
		else
			for s,f in ShortHandList do
				local type = "ORA_SHORTHAND_"..strupper(s)
				SlashCmdList[type] = nil
				setglobal("SLASH_"..type.."1", nil)
			end					
		end
	end,
	
	chatBroadcast = function(self, channel)
		if(not self:IsPromoted()) then return end
		if(channel=="" and not self:GetOpt("channel")) then return end 
		if(channel=="") then
			-- SendChatMessage("<CTMod> This is an automatic message sent by CT_RaidAssist. Channel changed to: "..self:GetOpt("channel"), "RAID")
			SendChatMessage( oRA_CORE_CT_CHANNEL_BROADCAST ..self:GetOpt("channel"), "RAID")
		else
			-- SendChatMessage("<CTMod> This is an automatic message sent by CT_RaidAssist. Channel changed to: "..channel, "RAID")
			SendChatMessage( oRA_CORE_CT_CHANNEL_BROADCAST ..channel, "RAID")
		end
	end,
	
	chatStatusRequest = function(self)
		if(not self:GetOpt("channel")) then return end
		
		self:Msg(oRA_CORE_CONST.Chat.SR)
		self:Send("SR")
	end,
	
	chatAutoLeave = function(self)
		self:TogOpt("autoleave")
		self:Msg(oRA_CORE_CONST.Chat.ALeave, ACEG_MAP_ONOFF[self:GetOpt("autoleave") or 0])
	end,
	
	chatForceJoin = function(self, channel)
		if (channel=="") then return end
		
		self:SetOpt("channel", channel)
		self:JoinChannel()
	end,

	-- Command Reporting Closures
	Msg = function(self, ...)
	   self.cmd:result(oRA_CORE_MSG_COLOR, unpack(arg))
	end,
	
	Result = function(self, text, val, map)
	   if( map ) then val = map[val or 0] or val end
	   self.cmd:result(oRA_CORE_MSG_COLOR, text, " ", ACEG_TEXT_NOW_SET_TO, " ",
		       format(oRA_CORE_DISPLAY_OPTION, val or ACE_CMD_REPORT_NO_VAL)
		       )
	end,
	
	TogMsg = function(self, var, text)
	   local val = self:TogOpt(var)
	   self:Result(text, val, ACEG_MAP_ONOFF)
	   return val
	end,
	
	Error = function(self, ...)
		self.cmd:result(ORA_CORE_MSG_COLOR, format(unpack(arg)))
	end,
	
	
	-- Command Handlers  
	
	Report = function(self)
	   if( self.modules ) then
	      local rpt, mod, _ = {}
	      for _, mod in self.modules do
		 tinsert(rpt, {text=mod.name, val=ace.toggle(mod.disabled), map=ACEG_MAP_ENABLED})
	      end
	      self.cmd:report(oRA_CORE_RPT_HDR_MODULES, rpt)
	   else
	      self:Msg(oRA_CORE_TEXT_NO_MODULES)
	   end
	end,

	AddCheck = function(self, e, r)
		if self:DupeCheck(e,r) then return end
		Checks[r] = {
			["event"] = e
		}
	end,
	
	RemoveCheck = function(self, r)
		if Checks[k] then
			Checks[k] = nil
		end
	end,
	
	DupeCheck = function(self, e,r)
	       for k,v in Checks do
			if v.event == e and k == r then
				return TRUE
			end
	       end
	end,
})

----------------------------------
--			Load this bitch up!			--
----------------------------------
oRA_Core:RegisterForLoad()