From c2e321a9d8fc9830634ef0a0f57833b80cf0b0f1 Mon Sep 17 00:00:00 2001 From: ronniedude <37117448+ronniedude@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:11:51 -0500 Subject: [PATCH 1/2] added functionality for string.gsub in invitation whisper I wanted to have an invitation message that advocates for the use of inverse phrases in the /layer channel something akin to this: Inviting you to layer 1. If you are UNHAPPY with this layer, decline my invite and type in /layer channel like "layer not 1" strong.format doesn't natively allow you to use multiple %s values in the string so I created a helper function to also allow the use of string.gsub to look for the characters: {layer}. This allows you to use the layer number multiple times in a whisper. --- layering.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/layering.lua b/layering.lua index b0156f1..294bdee 100755 --- a/layering.lua +++ b/layering.lua @@ -16,6 +16,16 @@ local function isPlayerLoggingOut() return false end +local function formatWhisperMessage(template, currentLayer) + if template:find("{layer}") then + template = template:gsub("{layer}", currentLayer) + end + if template:find("%%s") then + template = string.format(template, currentLayer) + end + return template +end + function AutoLayer:pruneCache() for i, cachedPlayer in ipairs(playersInvitedRecently) do -- delete players that are over 5 minutes old @@ -347,12 +357,12 @@ function AutoLayer:ProcessSystemMessages(_, a) -- Continue with the rest of the function if the player is in the list - local finalMessage = "[AutoLayer] " .. string.format(self.db.profile.inviteWhisperTemplate, currentLayer) + local finalMessage = "[AutoLayer] " .. formatWhisperMessage(self.db.profile.inviteWhisperTemplate, currentLayer) CTL:SendChatMessage("NORMAL", characterName, finalMessage, "WHISPER", nil, characterName) end if self.db.profile.inviteWhisperReminder then - local finalMessage2 = "[AutoLayer] " .. string.format(self.db.profile.inviteWhisperTemplateReminder) + local finalMessage2 = "[AutoLayer] " .. formatWhisperMessage(self.db.profile.inviteWhisperTemplateReminder, currentLayer) CTL:SendChatMessage("NORMAL", characterName, finalMessage2, "WHISPER", nil, characterName) end end From c79a19c950371f989ea8de6d23550d37636d4fbf Mon Sep 17 00:00:00 2001 From: ronniedude <37117448+ronniedude@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:18:43 -0500 Subject: [PATCH 2/2] updates main.lua to also support {layer} gsub with ac3 config validation Goes alongside my other commit regarding using gsub for layer value string subsitution. I've seen 9 layers on Doomhowl (US) server before so 10+ layers could be possible. so the validation layer value assumes a 2 digit length for safety --- main.lua | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/main.lua b/main.lua index 5a49715..1860990 100755 --- a/main.lua +++ b/main.lua @@ -6,6 +6,24 @@ AutoLayer = LibStub("AceAddon-3.0"):NewAddon("AutoLayer", "AceConsole-3.0", "Ace AceGUI = LibStub("AceGUI-3.0") local minimap_icon = LibStub("LibDBIcon-1.0") + +--- A helper function to ensure the length of a whisper won't exceed +--- the 255 character limit but after currentLayer value substitution. +--- Assumes layer can be a 2 digit value & assumes "[Autolayer] " preceeds the +--- template value in an outgoing whisper message leaving 243 characters to customize. +local function checkTemplateLength(template) + if template:find("{layer}") then + template = template:gsub("{layer}", "99") + end + if template:find("%%s") then + template = string.format(template, "99") + end + if #template > 243 then + return "Value is too long. Maximum length is 243 characters." + end + return true +end + local options = { name = "AutoLayer", handler = AutoLayer, @@ -111,13 +129,16 @@ local options = { type = 'input', width = 'double', name = 'Whisper Template', - desc = 'Template for invite whispers. Use %s for layer number.', + desc = 'Template for invite whispers. Use {layer} for layer number.', set = function(info, val) AutoLayer.db.profile.inviteWhisperTemplate = val end, get = function(info) return AutoLayer.db.profile.inviteWhisperTemplate end, + validate = function(info, val) + return checkTemplateLength(val) + end, order = 5, }, inviteWhisperReminder = { @@ -143,6 +164,9 @@ local options = { get = function(info) return AutoLayer.db.profile.inviteWhisperTemplateReminder end, + validate = function(info, val) + return checkTemplateLength(val) + end, order = 7, }, }, @@ -208,7 +232,7 @@ local defaults = { blacklist = "wts,wtb,lfm,lfg,ashen,auto inv,autoinv,pst for,guild,raid,enchant,player,what layer,which layer, WorldBuffs", invertKeywords = "not,off,except,but,out,other than,besides,apart from", inviteWhisper = true, - inviteWhisperTemplate = "Inviting you to layer %s...", + inviteWhisperTemplate = "Inviting you to layer {layer}...", inviteWhisperReminder = true, inviteWhisperTemplateReminder = "Please Leave Party after layer switch", mutesounds = true,