From 348cc46354466c119bf5ce14b0a336c49aac1c00 Mon Sep 17 00:00:00 2001 From: Brandon Sturgeon Date: Sun, 1 May 2022 15:59:27 -0700 Subject: [PATCH] Add extra data column --- .../timed_punishments/server/data.lua | 29 ++++++++++++------- lua/ulib/modules/cfc_timed_punishments.lua | 21 ++++++++++---- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lua/cfc_ulx_commands/timed_punishments/server/data.lua b/lua/cfc_ulx_commands/timed_punishments/server/data.lua index 864ace74..83a32970 100644 --- a/lua/cfc_ulx_commands/timed_punishments/server/data.lua +++ b/lua/cfc_ulx_commands/timed_punishments/server/data.lua @@ -26,11 +26,12 @@ function Data:setupTables() query( [[ CREATE TABLE IF NOT EXISTS cfc_timed_punishments( id INTEGER PRIMARY KEY, - steamid64 TEXT NOT NULL, - expiration INTEGER NOT NULL, - issuer TEXT NOT NULL, - punishment TEXT NOT NULL, - reason TEXT + steamid64 TEXT NOT NULL , + expiration INTEGER NOT NULL , + issuer TEXT NOT NULL , + punishment TEXT NOT NULL , + reason TEXT , + extraData TEXT , ) ]] ) @@ -75,13 +76,13 @@ function Data:removeExpired() ]], now ) end -function Data:createPunishment( punishment, steamID64, expiration, issuer, reason ) +function Data:createPunishment( punishment, steamID64, expiration, issuer, reason, extraData ) query( [[ INSERT OR REPLACE INTO - cfc_timed_punishments (steamid64, expiration, issuer, punishment, reason ) + cfc_timed_punishments (steamid64, expiration, issuer, punishment, reason, extraData ) VALUES (%s, %s, %s, %s, %s) - ]], steamID64, expiration, issuer, punishment, reason ) + ]], steamID64, expiration, issuer, punishment, reason, extraData ) end function Data:removePunishment( punishment, steamID64 ) @@ -98,7 +99,7 @@ end function Data:getPunishments( steamID64 ) local result = query( [[ SELECT - expiration, punishment + expiration, punishment, extraData FROM cfc_timed_punishments WHERE @@ -110,7 +111,15 @@ function Data:getPunishments( steamID64 ) if result == false then return ErrorNoHaltWithStack( steamID64 ) end for _, p in ipairs( result ) do - punishments[p.punishment] = tonumber( p.expiration ) + local extraData = p.extraData + if extraData then + extraData = util.JSONToTable( extraData ) + end + + punishments[p.punishment] = { + expiration = tonumber( p.expiration ), + extraData = extraData + } end return punishments diff --git a/lua/ulib/modules/cfc_timed_punishments.lua b/lua/ulib/modules/cfc_timed_punishments.lua index 4e9d4a78..9cd7b78b 100644 --- a/lua/ulib/modules/cfc_timed_punishments.lua +++ b/lua/ulib/modules/cfc_timed_punishments.lua @@ -26,13 +26,13 @@ function TP.Register( punishment, enable, disable ) } end -function TP.Punish( steamID64, punishment, expiration, issuer, reason ) - Data:createPunishment( punishment, steamID64, expiration, issuer, reason ) +function TP.Punish( steamID64, punishment, expiration, issuer, reason, extraData ) + Data:createPunishment( punishment, steamID64, expiration, issuer, reason, extraData ) local ply = player.GetBySteamID64( steamID64 ) if not IsValid( ply ) then return end - Punishments[punishment].enable( ply ) + Punishments[punishment].enable( ply, extraData ) end function TP.Unpunish( steamID64, punishment ) @@ -50,8 +50,15 @@ hook.Add( "PlayerInitialSpawn", "CFC_TimedPunishments_Check", function( ply ) local steamID64 = ply:SteamID64() local punishments = Data:getPunishments( steamID64 ) - for punishment, expiration in pairs( punishments or none ) do - Punishments[punishment].enable( ply ) + local now = os.time() + + for punishment, fields in pairs( punishments or none ) do + local expiration = fields.expiration + + if expiration == 0 or expiration > now then + local extraData = fields.extraData + Punishments[punishment].enable( ply, extraData ) + end end ply.TimedPunishments = punishments @@ -65,7 +72,9 @@ hook.Add( "Initialize", "CFC_TimedPunishments_Init", function() local steamID64 = ply:SteamID64() local punishments = ply.TimedPunishments or none - for punishment, expiration in pairs( punishments ) do + for punishment, fields in pairs( punishments ) do + local expiration = fields.expiration + if expiration > 0 and expiration <= now then ply.TimedPunishments[punishment] = nil TP.Unpunish( steamID64, punishment )