From 3a29c2ddc30f2cc541f03a2cac93e9b2e80fe15f Mon Sep 17 00:00:00 2001 From: isRyven Date: Fri, 15 Nov 2019 00:52:17 +0200 Subject: [PATCH] add working banner rotation script --- README.md | 16 +++- banners/banner.lua | 56 -------------- banners/banners.lua | 183 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 57 deletions(-) delete mode 100644 banners/banner.lua create mode 100644 banners/banners.lua diff --git a/README.md b/README.md index 4be183a..a163c51 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,21 @@ ## banners -* Banners managment system in lua +Banner managment system for `legacy` mod. + +Reserves next cvars to configure banners: +* `g_bannerTime` sets banner change interval (default `5000`) +* `g_bannerLocation` sets banner print location (default `top`) + Possible values: + * `top` top of the screen, banner print spot (`bp`) + * `left` popup messages (`cpm`) + * `center` center print (`cp`) + * `chat` chat print (`chat`) +* `g_bannerN` (where N is a number in range of `1` to `5`) sets banner messages + +All cvars should be filled before lua module gets initialized. + +* This script is intended for legacy `2.77+` mod. ## dynamite diff --git a/banners/banner.lua b/banners/banner.lua deleted file mode 100644 index fe6a2f1..0000000 --- a/banners/banner.lua +++ /dev/null @@ -1,56 +0,0 @@ -modname = "Banners" -version = "0.5" - --- Set Banners of you desire -banner = "..." -banner1 = "..." -banner2 = "..." -banner3 = "..." -banner4 = "..." -banner5 = "..." - --- Set time in seconds when banners string has to be executed -timer = 10 -timer1 = 15 -timer2 = 20 -timer3 = 25 -timer4 = 30 -timer5 = 35 - -------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------- -------------------------------------------!!DO NOT CHANGE BELOW!!-------------------------------------------------- - -function et_InitGame( levelTime, randomSeed, restart ) - et.RegisterModname( modname .. " " .. version ) - - local mil_sec = (levelTime*1000)%60 - - while(true) do - - if(mil_sec == timer) - then et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. banner .."^7\n" ) - elseif(mil_sec == timer1) - then et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. banner1 .."^7\n" ) - elseif(mil_sec == timer2) - then et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. banner2 .."^7\n" ) - elseif(mil_sec == timer3) - then et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. banner3 .."^7\n" ) - elseif(mil_sec == timer4) - then et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. banner4 .."^7\n" ) - elseif(mil_sec == timer5) - then et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. banner5 .."^7\n" ) - local mil_sec = 0 -- we reset it here - else - et.trap_SendConsoleCommand( et.EXEC_NOW, "cp \"" .. "NO BANNERS" .."^7\n" ) - - end - end - - return 0 -end - -function et_ShutdownGame( restart ) - et.G_Print("Shutting down: " .. modname .. "\n") -end \ No newline at end of file diff --git a/banners/banners.lua b/banners/banners.lua new file mode 100644 index 0000000..b0eda1e --- /dev/null +++ b/banners/banners.lua @@ -0,0 +1,183 @@ +--[[ + ET: Legacy + Copyright (C) 2012-2019 ET:Legacy team + This file is part of ET: Legacy - http://www.etlegacy.com + ET: Legacy is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + ET: Legacy is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with ET: Legacy. If not, see . +]]-- + +local modname = "banners" +local version = "0.1" + +-- Map Data Structure + +local MapPrototype = {} +MapPrototype.__index = MapPrototype + +function Map(tab) + local obj = { _storage = assign({}, tab) } + setmetatable(obj, MapPrototype) + return obj +end + +function MapPrototype:set(key, value) + self._storage[key] = value + return value +end + +function MapPrototype:get(key, def) + return self._storage[key] or self._storage[def] +end + +function MapPrototype:has(key) + return self._storage[key] and true or false +end + +function MapPrototype:toKeyString() + local str = "" + for k, v in pairs(self._storage) do + str = str .. "'" .. k .. "', " + end + return string.sub(str, 1, -3) +end + +-- Banner System -- + +local BannerSystemPrototype = {} +BannerSystemPrototype.__index = BannerSystemPrototype + +function BannerSystem(props) + local obj = {} + obj.nextUpdateTime = 0 + obj.nextBanner = 0 + obj.interval = props.interval + obj.command = props.command + obj.banners = {} + for _, val in ipairs(props.banners) do + if string.len(val) > 0 then + table.insert(obj.banners, val) + end + end + setmetatable(obj, BannerSystemPrototype) + return obj +end + +function BannerSystemPrototype:frame(time) + if self.nextUpdateTime > time then + return + end + if #self.banners == 0 then + return + end + self:update(time) + self:render(time) +end + +function BannerSystemPrototype:update(time) + self.nextUpdateTime = time + self.interval + self.nextBanner = 1 + (self.nextBanner % #self.banners) +end + +function BannerSystemPrototype:render(time) + local banner = self.banners[self.nextBanner] + et.trap_SendServerCommand(-1, string.format('%s \"%s\"^7', self.command, banner)) +end + +function BannerSystemPrototype:add(banner) + if (string.len(banner) > 0) then + table.insert(self.banners, banner) + end +end + +function BannerSystemPrototype:count() + return #self.banners +end + +-- Main Code -- + +local DEFAULT_TIME = 5000 +local DEFAULT_TIME_THRESHOLD = 2000 +local DEFAULT_LOCATION = "top" +local bannerSystem = nil -- BannerSystem instance global (well, local) + +function et_InitGame(levelTime, randomSeed, restart) + et.RegisterModname(modname .. " " .. version) + + local locationMapping = Map { + top = "bp", + left = "cpm", + center = "cpm", + chat = "chat" + } + + local g_bannerTime = tonumber(et.trap_Cvar_Get("g_bannerTime")) or DEFAULT_TIME + local g_bannerLocation = string.lower(et.trap_Cvar_Get("g_bannerLocation")) or DEFAULT_LOCATION + + if (g_bannerTime < DEFAULT_TIME_THRESHOLD) then + et.G_Print( + string.format( + "^3%s.lua: Warning! You cannot set banner time lower than %ims, forcing to %ims.\n", + modname, DEFAULT_TIME_THRESHOLD, DEFAULT_TIME)) + g_bannerTime = DEFAULT_TIME + end + + if not locationMapping:has(g_bannerLocation) then + et.G_Print( + string.format( + "^3%s.lua: Warning! Invalid location '%s', forcing to '%s'; valid locations: %s.\n", + modname, g_bannerLocation, DEFAULT_LOCATION, locationMapping:toKeyString())) + g_bannerLocation = DEFAULT_LOCATION + end + + bannerSystem = BannerSystem { + interval = g_bannerTime, + command = locationMapping:get(g_bannerLocation), + banners = { + et.trap_Cvar_Get("g_banner1"), + et.trap_Cvar_Get("g_banner2"), + et.trap_Cvar_Get("g_banner3"), + et.trap_Cvar_Get("g_banner4"), + et.trap_Cvar_Get("g_banner5") + } + } + + if bannerSystem:count() > 0 then + et.G_Print( + string.format( + "^2%s.lua: Initialized banner system (%is, '%s'); showing %i banners.\n", + modname, g_bannerTime / 1000, g_bannerLocation, bannerSystem:count())) + else + et.G_Print(string.format("^3%s.lua: Warning! No banners were set.\n", modname)) + bannerSystem = nil + end +end + +function et_RunFrame(levelTime) + if bannerSystem then + bannerSystem:frame(levelTime) + end +end + +function et_ShutdownGame( restart ) + et.G_Print("Shutting down: " .. modname .. "\n") +end + +-- Utils -- + +function assign(tab1, ...) + local tables = { ... } + for _, tab in pairs(tables) do + for k, v in pairs(tab) do + tab1[k] = v + end + end + return tab1 +end