Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra data column #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions lua/cfc_ulx_commands/timed_punishments/server/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ,
)
]] )

Expand Down Expand Up @@ -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 )
Expand All @@ -98,7 +99,7 @@ end
function Data:getPunishments( steamID64 )
local result = query( [[
SELECT
expiration, punishment
expiration, punishment, extraData
FROM
cfc_timed_punishments
WHERE
Expand All @@ -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
Expand Down
21 changes: 15 additions & 6 deletions lua/ulib/modules/cfc_timed_punishments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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
Expand All @@ -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 )
Expand Down