Quantcast
--[[
To change how the messages are said (e.g. language, channel), visit this page:
	http://www.wowpedia.org/API_SendChatMessage
]]

--SELECT YOUR CHANNEL. You may enter the name of the channel you want your messages to go to (for example "SAY", "EMOTE", "PARTY", "RAID", "BATTLEGROUND", "GUILD", "OFFICER", or "SMART"). "SMART" will send the message to raid if you're in a raid, party if you're in a party, or say if you're in neither.
local MESSAGE_CHANNEL = "SMART"

--SELECT YOUR LANGUAGE. This is the language that all of your your messages will be sent in. e.g. "TROLL", "TAURAHE", "DWARVEN", "DRAENEI". nil uses your default language (Orcish for Horde, Common for Alliance)
local MESSAGE_LANGUAGE = nil

--ENTER LIGHTWELL SUMMON MESSAGE. Select a channel and enter a message to be displayed when you summon your lightwell.
local SUMMON_MESSAGE = "Click my Lightwell for a SURPRISE!"
local SUMMON_CHANNEL = "SMART"


--[[Add Messages:
Format:
	[#] = "Phrase",
	[#] = "Phrase",

Any occurrence of the following placeholders will be replaced as follows:
%u will be replaced by the Lightwell user's name.
%p will be replace by your name.
%c will be replaced by the number of charges remaining on the lightwell
%x will be replaced by the number of charges used on the lightwell.

Put a double dash ( -- ) at the start of a line in this table to "comment" it and stop LWB from using that phrase.
To use the \ (backslash) or | (vertical bar) characters in the phrase, you may need to use \\ or || respectively.
]]


local LWB_phrases = {
  --Start of table
  [1] = "Thank you for using the Lightwell %u!",
  [2] = "May the Light bless you %u.",

  --End of table
}

--END OF CONFIGURATION. DO NOT MODIFY ANYTHING ELSE BELOW UNLESS YOU REALLY KNOW WHAT YOU'RE DOING!



local ADDON_NAME, ns = ...

local LWB_events = {}
local LWB_frame = CreateFrame("Frame")
LWB_frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

local PLAYER_NAME = UnitName("player")
local PLAYER_GUID = UnitGUID("player")
local LIGHTWELL_SPELLID = 724
local RENEW_SPELLID = 7001

local charges_used = 0
local charges_remaining = 0

local message;

local gsub = string.gsub
local SendChatMessgae = SendChatMessage


local function resetCharges()
	charges_remaining = 10
	charges_used = 0
	for i = 7, NUM_GLYPH_SLOTS do
 		local enabled, glyphType, glyphTooltipIndex, glyphSpellID, icon = GetGlyphSocketInfo(i);
 		if ( enabled ) then
   			if glyphSpellID == 55673 then
				charges_remaining = 15
			end
		end
	end
end


function LWB_events:COMBAT_LOG_EVENT_UNFILTERED(timestamp, event, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, spellId, spellName, spellSchool, ...)
	if not PLAYER_GUID then
		PLAYER_GUID = UnitGUID("player")
	end

	if event == "SPELL_SUMMON" and sourceGUID == PLAYER_GUID and spellId == LIGHTWELL_SPELLID then
		resetCharges()
		SendChatMessage(SUMMON_MESSAGE, SUMMON_CHANNEL == "SMART" and ((GetNumRaidMembers() > 0 and RAID) or (GetNumPartyMembers() > 0 and PARTY) or (SAY)) or SUMMON_CHANNEL, MESSAGE_LANGUAGE, nil)

	elseif event == "SPELL_AURA_APPLIED" and sourceGUID == PLAYER_GUID and spellId == RENEW_SPELLID then
		charges_remaining = charges_remaining - 1
		charges_used = charges_used + 1
		message = nil --reset the message

		repeat --keep trying to pick a random message until we get one
			message = LWB_phrases[random(#LWB_phrases)] or nil
		until message

		message = gsub(message, "(%%[pucx])", function(arg)
			if arg == "%p" then
				return PLAYER_NAME
			elseif	arg == "%u" then
				return destName
			elseif arg == "%c" then
				return charges_remaining
			elseif arg == "%x" then
				return charges_used
			end
		end)

	SendChatMessage(message, MESSAGE_CHANNEL == "SMART" and ((GetNumRaidMembers() > 0 and RAID) or (GetNumPartyMembers() > 0 and PARTY) or (SAY)) or MESSAGE_CHANNEL, MESSAGE_LANGUAGE, (MESSAGE_CHANNEL == "WHISPER" and destName) or nil) --Say the message. DO NOT CHANGE THE message ARGUMENT.
	end
end


LWB_frame:SetScript("OnEvent", function(self, event, ...)
	LWB_events[event](self, ...)
end)