feat(announcement): add access control and announcement queueing
- Add BladW.Announcement.HasAccess() permission check to menu commands and net receive - Implement announcement queueing: prevent stacking announcements while one is already displaying - Track active announcement duration with activeUntil timer - Add feedback to admin: send result status and remaining queue time after broadcast attempt - Improve chat command handling: hide command from chat for all users, only open menu if authorized - Refactor configuration and duration retrieval for clarity
This commit is contained in:
@@ -13,9 +13,9 @@ local function LimitChars(str, maxChars)
|
||||
return endByte and string.sub(str, 1, endByte - 1) or str
|
||||
end
|
||||
|
||||
-- ouvre le menu pour le joueur
|
||||
-- ouvre le menu pour le joueur (faut avoir le droit)
|
||||
concommand.Add("bladw_amenu", function(ply, cmd)
|
||||
if not IsValid(ply) then return end
|
||||
if not IsValid(ply) or not BladW.Announcement.HasAccess(ply) then return end
|
||||
|
||||
net.Start("bladWOpenAnnouncementMenu")
|
||||
net.Send(ply)
|
||||
@@ -25,12 +25,14 @@ end)
|
||||
hook.Add("PlayerSay", "BladW:AnnouncementChatCmd", function(ply, text)
|
||||
local cmd = string.lower(string.Trim(text))
|
||||
if cmd ~= "/bannonce" and cmd ~= "!bannonce" then return end
|
||||
if not IsValid(ply) then return end
|
||||
|
||||
-- que ceux qui ont le droit ouvrent le menu, mais on cache la commande pour tout le monde
|
||||
if IsValid(ply) and BladW.Announcement.HasAccess(ply) then
|
||||
net.Start("bladWOpenAnnouncementMenu")
|
||||
net.Send(ply)
|
||||
end
|
||||
|
||||
return "" -- pour pas laisser la commande dans le chat
|
||||
return "" -- commande reconnue -> on la retire du chat
|
||||
end)
|
||||
|
||||
-- reconstruit l'annonce finale : le preset sert de base, title/text passent devant
|
||||
@@ -46,12 +48,20 @@ function BladW.Announcement.Resolve(entry)
|
||||
}
|
||||
end
|
||||
|
||||
-- envoie l'annonce à tout le monde. false si y'a rien à envoyer
|
||||
-- envoie l'annonce à tout le monde. false si rien à envoyer ou si une annonce est déjà en cours
|
||||
function BladW.Announcement.Broadcast(entry)
|
||||
-- déjà une annonce à l'écran ? on n'en empile pas une autre
|
||||
if CurTime() < (BladW.Announcement.activeUntil or 0) then return false end
|
||||
|
||||
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
|
||||
local Conf = BladW.Announcement.Conf or {}
|
||||
local maxLen = tonumber(Conf.announcement_max_length) or 120
|
||||
local dur = a.duration > 0 and a.duration or (tonumber(Conf.announcement_duration) or 10)
|
||||
|
||||
-- on bloque les suivantes le temps que celle-ci s'affiche
|
||||
BladW.Announcement.activeUntil = CurTime() + dur
|
||||
|
||||
net.Start("bladWShowAnnouncement")
|
||||
net.WriteString(LimitChars(a.title, 64))
|
||||
@@ -65,15 +75,22 @@ end
|
||||
|
||||
-- un admin veut poster une annonce -> je la diffuse
|
||||
net.Receive("bladWSendAnnouncement", function(len, ply)
|
||||
if not IsValid(ply) or not ply:IsAdmin() then return end
|
||||
if not IsValid(ply) or not BladW.Announcement.HasAccess(ply) then return end
|
||||
|
||||
local preset = net.ReadString()
|
||||
local title = net.ReadString()
|
||||
local text = net.ReadString()
|
||||
|
||||
BladW.Announcement.Broadcast({
|
||||
local ok = BladW.Announcement.Broadcast({
|
||||
preset = preset ~= "" and preset or nil,
|
||||
title = title,
|
||||
text = text,
|
||||
})
|
||||
|
||||
-- je réponds à l'admin : publié -> il ferme le menu, refusé -> il garde son texte
|
||||
local left = math.Clamp(math.ceil((BladW.Announcement.activeUntil or 0) - CurTime()), 0, 255)
|
||||
net.Start("bladWAnnouncementResult")
|
||||
net.WriteBool(ok)
|
||||
net.WriteUInt(left, 8)
|
||||
net.Send(ply)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user