2017-06-29 23:17:25 +02:00
LevelUpAnnouncer = LibStub ( " AceAddon-3.0 " ) : NewAddon ( " LevelUpAnnouncer " , " AceConsole-3.0 " , " AceEvent-3.0 " )
2017-07-03 06:58:55 +02:00
local version = GetAddOnMetadata ( " LevelUpAnnouncer " , " Version " )
local author = GetAddOnMetadata ( " LevelUpAnnouncer " , " Author " )
2017-06-29 23:17:25 +02:00
local defaults = {
profile = {
playSound = true ,
chatAnnounce = true ,
chatPercAnnounce = false ,
reversePerc = false ,
2020-08-12 06:10:02 +02:00
replaceWithEmote = true ,
2017-06-29 23:17:25 +02:00
dingFormat = " DING! LEVEL {LEVEL}! " ,
percFormat = " {PERCENTAGE}% INTO LEVEL {LEVEL}! " ,
reversePercFormat = " {PERCENTAGE}% UNTIL LEVEL {NEXTLEVEL}! " ,
messageModeString = " YELL " ,
soundID = 24297
}
}
local options = {
type = " group " ,
args = {
LUPHeader = {
2017-07-03 06:58:55 +02:00
name = " Level Up Announcer - by " .. author .. " - version " .. version ,
2017-06-29 23:17:25 +02:00
type = " header "
} ,
LUPToggles = {
order = 1 ,
name = " Toggles " ,
type = " group " ,
args = {
playSound = {
order = 1 ,
name = " Enable Sound " ,
desc = " Play a sound each time you level up " ,
type = " toggle " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.playSound = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.playSound end
2017-06-29 23:17:25 +02:00
} ,
chatAnnounce = {
order = 2 ,
name = " Chat Announce " ,
desc = " Announces in chat when you level up " ,
type = " toggle " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.chatAnnounce = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.chatAnnounce end
2017-06-29 23:17:25 +02:00
} ,
chatPercAnnounce = {
order = 3 ,
name = " Percentage Announce " ,
desc = " Announces in chat when you pass a percentage barrier (25%, 50% and 75%) " ,
type = " toggle " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.chatPercAnnounce = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.chatPercAnnounce end
2017-06-29 23:17:25 +02:00
} ,
reversePerc = {
order = 4 ,
name = " Reverse Percentages " ,
desc = " Reverses the percentages in percentage announce mode (announces 75% when at 25%, 50% at 50% and 25% at 75%) " ,
type = " toggle " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.reversePerc = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.reversePerc end
2020-08-12 06:10:02 +02:00
} ,
replaceWithEmote = {
order = 5 ,
name = " Smart Message Mode " ,
desc = " Replace \" Message Mode \" with EMOTE if using SAY or YELL when not in an instance " ,
type = " toggle " ,
set = function ( _ , value ) LevelUpAnnouncer.db . profile.replaceWithEmote = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.replaceWithEmote end
2017-06-29 23:17:25 +02:00
}
}
} ,
LUPFormats = {
order = 2 ,
name = " Formats " ,
desc = " Formats for annoucing to chat " ,
type = " group " ,
args = {
dingFormat = {
order = 1 ,
name = " Level Up Announce Format " ,
desc = " The format used when you level up " ,
type = " input " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.dingFormat = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.dingFormat end
2017-06-29 23:17:25 +02:00
} ,
percFormat = {
order = 2 ,
name = " Percentage Announce Format " ,
desc = " The format used when you pass a percentage barrier (25%, 50% and 75%) " ,
type = " input " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.percFormat = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.percFormat end
2017-06-29 23:17:25 +02:00
} ,
reversePercFormat = {
order = 3 ,
name = " Reversed Percentage Announce Format " ,
desc = " The format used when you pass a percentage barrier in reverse mode (75%, 50% and 25%) " ,
type = " input " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.reversePercFormat = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.reversePercFormat end
2017-06-29 23:17:25 +02:00
} ,
placeholderHelp = {
order = 4 ,
name = " Available placeholders: {PERCENTAGE}, {LEVEL} and {NEXTLEVEL} " ,
type = " description "
}
}
} ,
LUPMisc = {
order = 3 ,
name = " Misc " ,
type = " group " ,
args = {
soundID = {
order = 1 ,
name = " Sound ID " ,
desc = " The ID of the sound played when you level up (only applicable when sound is enabled) " ,
type = " input " ,
pattern = " (%d) " ,
usage = " Only numbers are allowed " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.soundID = tonumber ( value ) end ,
get = function ( ) return tostring ( LevelUpAnnouncer.db . profile.soundID ) end
2017-06-29 23:17:25 +02:00
} ,
messageMode = {
order = 2 ,
name = " Message Mode " ,
2020-08-12 06:10:02 +02:00
desc = " Modes: \n SAY (Only works in instances) \n YELL(Only works in instances) \n PARTY \n INSTANCE_CHAT \n RAID \n RAID_WARNING \n GUILD \n OFFICER \n EMOTE \n CHANNEL.X (So for channel 1 type: \" CHANNEL.1 \" , only works in instances) " ,
2017-06-29 23:17:25 +02:00
type = " input " ,
2017-07-03 06:58:55 +02:00
set = function ( _ , value ) LevelUpAnnouncer.db . profile.messageModeString = value end ,
get = function ( ) return LevelUpAnnouncer.db . profile.messageModeString end
2017-06-29 23:17:25 +02:00
} ,
testSound = {
order = 3 ,
name = " Test Sound " ,
desc = " Click this to test the selected sound " ,
type = " execute " ,
2017-10-13 19:01:26 +02:00
func = function ( ) PlaySound ( LevelUpAnnouncer.db . profile.soundID ) end
2017-06-29 23:17:25 +02:00
}
}
}
}
}
local split = function ( inputstr , sep )
if sep == nil then
sep = " %s "
end
local t = { }
local i = 1
for str in string.gmatch ( inputstr , " ([^ " .. sep .. " ]+) " ) do
t [ i ] = str
i = i + 1
end
return t
end
local sendToChat = function ( message )
local str = LevelUpAnnouncer.db . profile.messageModeString
local t = split ( str , " . " )
local channel
if # t > 1 then channel = t [ 2 ] end
2020-08-12 06:10:02 +02:00
local mode = string.upper ( t [ 1 ] )
local replaceWithEmote = LevelUpAnnouncer.db . profile.replaceWithEmote
-- SendChatMessage is hardware event protected for modes "SAY", "YELL" and "CHANNEL" when player is "outdoors"
if not IsInInstance ( ) and ( mode == " SAY " or mode == " YELL " or mode == " CHANNEL " ) then
if replaceWithEmote and mode ~= " CHANNEL " then
mode = " EMOTE "
else
return
end
end
2017-06-29 23:17:25 +02:00
SendChatMessage ( message , mode , nil , channel )
end
local replaceFormats = function ( strFormat , perc , level )
strFormat = strFormat : gsub ( " ({PERCENTAGE}) " , tostring ( perc ) )
strFormat = strFormat : gsub ( " ({LEVEL}) " , tostring ( level ) )
strFormat = strFormat : gsub ( " ({NEXTLEVEL}) " , tostring ( level + 1 ) )
return strFormat
end
function LevelUpAnnouncer : OnInitialize ( )
self.db = LibStub ( " AceDB-3.0 " ) : New ( " LevelUpAnnouncerDB " , defaults , true )
self.profiles = LibStub ( " AceDBOptions-3.0 " ) : GetOptionsTable ( self.db )
LibStub ( " AceConfig-3.0 " ) : RegisterOptionsTable ( " LevelUpAnnouncer " , options )
self.optionsFrame = LibStub ( " AceConfigDialog-3.0 " ) : AddToBlizOptions ( " LevelUpAnnouncer " , " LevelUpAnnouncer " )
LibStub ( " AceConfig-3.0 " ) : RegisterOptionsTable ( " LevelUpAnnouncerProfiles " , self.profiles )
self.profilesFrame = LibStub ( " AceConfigDialog-3.0 " ) : AddToBlizOptions ( " LevelUpAnnouncerProfiles " , " Profiles " , " LevelUpAnnouncer " )
LevelUpAnnouncer : RegisterEvent ( " PLAYER_LOGIN " )
LevelUpAnnouncer : RegisterEvent ( " PLAYER_LEVEL_UP " )
LevelUpAnnouncer : RegisterEvent ( " PLAYER_XP_UPDATE " )
2017-07-03 06:58:55 +02:00
local command = function ( ) LibStub ( " AceConfigDialog-3.0 " ) : Open ( " LevelUpAnnouncer " ) end
2017-06-29 23:17:25 +02:00
LevelUpAnnouncer : RegisterChatCommand ( " levelup " , command )
LevelUpAnnouncer : RegisterChatCommand ( " lvlup " , command )
end
function LevelUpAnnouncer : PLAYER_LOGIN ( )
self.xpPct = UnitXP ( " player " ) / UnitXPMax ( " player " )
self.tmpPercIndex = math.floor ( self.xpPct * 4 )
self.curLevel = UnitLevel ( " player " )
2017-07-03 06:58:55 +02:00
print ( " |cff0066FFLevel Up Announcer|r, by |cff0066FF " .. author .. " |r, version |cff0066FF " .. version .. " |r loaded, / " .. " |cff0066FFlevelup|r for settings. " )
2017-06-29 23:17:25 +02:00
end
function LevelUpAnnouncer : PLAYER_LEVEL_UP ( )
2017-06-29 23:18:45 +02:00
self.tmpPercIndex = 0
self.curLevel = self.curLevel + 1
if self.db . profile.playSound then
2017-10-13 19:01:26 +02:00
PlaySound ( self.db . profile.soundID )
2017-06-29 23:18:45 +02:00
end
if self.db . profile.chatAnnounce then
2017-06-29 23:17:25 +02:00
sendToChat ( replaceFormats ( self.db . profile.dingFormat , 0 , self.curLevel ) )
2017-06-29 23:18:45 +02:00
end
2017-06-29 23:17:25 +02:00
end
function LevelUpAnnouncer : PLAYER_XP_UPDATE ( )
2017-06-29 23:18:45 +02:00
self.xpPct = UnitXP ( " player " ) / UnitXPMax ( " player " )
2017-06-29 23:17:25 +02:00
if not self.db . profile.chatPercAnnounce then return end
local stc = function ( p ) return sendToChat ( replaceFormats ( self.db . profile.percFormat , p , self.curLevel ) ) end
if self.db . profile.reversePerc then stc = function ( p ) return sendToChat ( replaceFormats ( self.db . profile.reversePercFormat , 100 - p , self.curLevel ) ) end end
if self.xpPct >= 0.25 and self.tmpPercIndex < 1 then
stc ( 25 )
2017-10-31 18:51:52 +01:00
end
if self.xpPct >= 0.5 and self.tmpPercIndex < 2 then
2017-06-29 23:17:25 +02:00
stc ( 50 )
2017-10-31 18:51:52 +01:00
end
if self.xpPct >= 0.75 and self.tmpPercIndex < 3 then
2017-06-29 23:17:25 +02:00
stc ( 75 )
end
2017-06-29 23:18:45 +02:00
self.tmpPercIndex = math.floor ( self.xpPct * 4 )
end