From 783bef24386b999d32abc56a396482da6dc10586 Mon Sep 17 00:00:00 2001 From: nocode Date: Mon, 20 Jul 2026 13:36:23 +0200 Subject: [PATCH] feat(announcement): refactor announcement system with presets and improved text handling - Add BladW namespace and module structure - Implement LimitChars() function for UTF-8 safe string truncation - Add Resolve() function to handle preset-based configuration merging - Add Broadcast() function to centralize announcement distribution logic - Update net.Receive handler to support preset system - Add configurable announcement max length support - Include announcement sound file registration - Improve text validation and default values handling --- lua/bladw_announcement/server/sv_function.lua | 65 ++++++++++++++++--- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/lua/bladw_announcement/server/sv_function.lua b/lua/bladw_announcement/server/sv_function.lua index e3b3a99..0213283 100644 --- a/lua/bladw_announcement/server/sv_function.lua +++ b/lua/bladw_announcement/server/sv_function.lua @@ -1,4 +1,19 @@ --- Ouvre le menu admin pour le joueur (réservé aux admins) +BladW = BladW or {} +BladW.Announcement = BladW.Announcement or {} + +-- Envoie le son d'annonce aux clients (téléchargement) +resource.AddFile("sound/announce.mp3") + +-- Tronque une chaîne à `maxChars` caractères (UTF-8 safe, pour les accents) +local function LimitChars(str, maxChars) + str = tostring(str or "") + local n = utf8.len(str) + if not n or n <= maxChars then return str end + local endByte = utf8.offset(str, maxChars + 1) + return endByte and string.sub(str, 1, endByte - 1) or str +end + +-- Ouvre le menu admin pour le joueur concommand.Add("bladw_amenu", function(ply, cmd) if not IsValid(ply) then return end @@ -18,18 +33,48 @@ hook.Add("PlayerSay", "BladW:AnnouncementChatCmd", function(ply, text) return "" -- masque la commande dans le chat end) +-- Résout une entrée { preset =, title =, text =, style =, duration = } +-- en données finales (le preset sert de base, title/text le surchargent) +function BladW.Announcement.Resolve(entry) + local presets = BladW.Announcement.Presets or {} + local base = (entry.preset and presets[entry.preset]) or {} + + return { + title = (entry.title and entry.title ~= "" and entry.title) or base.title or "Annonce", + text = (entry.text and entry.text ~= "" and entry.text ) or base.text or "", + style = entry.style or base.style or "", + duration = tonumber(entry.duration) or tonumber(base.duration) or 0, + } +end + +-- Diffuse une annonce à tous les joueurs. Retourne false si rien à envoyer. +function BladW.Announcement.Broadcast(entry) + local a = BladW.Announcement.Resolve(entry) + if a.text == "" then return false end + + local maxLen = tonumber((BladW.Announcement.Conf or {}).announcement_max_length) or 120 + + net.Start("bladWShowAnnouncement") + net.WriteString(LimitChars(a.title, 64)) + net.WriteString(LimitChars(a.text, maxLen)) + net.WriteString(a.style or "") + net.WriteUInt(math.Clamp(a.duration, 0, 65535), 16) + net.Broadcast() + + return true +end + -- Un admin demande l'envoi d'une annonce -> on la diffuse à tout le monde net.Receive("bladWSendAnnouncement", function(len, ply) if not IsValid(ply) or not ply:IsAdmin() then return end - local title = string.sub(net.ReadString(), 1, 64) - local text = string.sub(net.ReadString(), 1, 200) + local preset = net.ReadString() + local title = net.ReadString() + local text = net.ReadString() - if text == "" then return end - if title == "" then title = "Annonce" end - - net.Start("bladWShowAnnouncement") - net.WriteString(title) - net.WriteString(text) - net.Broadcast() + BladW.Announcement.Broadcast({ + preset = preset ~= "" and preset or nil, + title = title, + text = text, + }) end)