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

Isolate PreferredJobModel #3266

Merged
merged 6 commits into from
Sep 28, 2024
Merged
Changes from 1 commit
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
25 changes: 19 additions & 6 deletions gamemode/modules/base/cl_jobmodels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sql.Query([[CREATE TABLE IF NOT EXISTS darkp_playermodels(
model VARCHAR(140) NOT NULL
);]])

sql.Query("ALTER TABLE darkp_playermodels ADD COLUMN IF NOT EXISTS server VARCHAR(21);")

local preferredModels = {}


Expand All @@ -14,7 +16,7 @@ function DarkRP.setPreferredJobModel(teamNr, model)
local job = RPExtraTeams[teamNr]
if not job then return end
preferredModels[job.command] = model
sql.Query(string.format([[REPLACE INTO darkp_playermodels VALUES(%s, %s);]], sql.SQLStr(job.command), sql.SQLStr(model)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another issue. After the table is migrated to have three columns, the old version of this query will fail with this error: table darkp_playermodels has 3 columns but 2 values were supplied.

Because SQLite errors are silent (the query just returns false, and you have to print sql.LastError() to get the error, job models are silently no longer saved.

This would not have a problem if the query was REPLACE INTO darkp_playermodels(jobcmd, model) VALUES(%s, %s);, but this is not something we can fix in hindsight 🤔

sql.Query(string.format([[REPLACE INTO darkp_playermodels VALUES(%s, %s, %s);]], sql.SQLStr(job.command), sql.SQLStr(model), sql.SQLStr(game.GetIPAddress())))

net.Start("DarkRP_preferredjobmodel")
net.WriteUInt(teamNr, 8)
Expand All @@ -31,7 +33,7 @@ end
--[[---------------------------------------------------------------------------
Load the preferred models
---------------------------------------------------------------------------]]
local function sendModels() -- run after the jobs have loaded
local function sendModels()
net.Start("DarkRP_preferredjobmodels")
for _, job in pairs(RPExtraTeams) do
if not preferredModels[job.command] then net.WriteBit(false) continue end
Expand All @@ -42,11 +44,22 @@ local function sendModels() -- run after the jobs have loaded
net.SendToServer()
end

do
local models = sql.Query([[SELECT jobcmd, model FROM darkp_playermodels;]])
local function jobHasModel(job, model)
return istable(job.model) and table.HasValue(job.model, model) or job.model == model
end

timer.Simple(0, function()
-- run after the jobs have loaded
local models = sql.Query([[SELECT jobcmd, model, server FROM darkp_playermodels;]])

for _, v in ipairs(models or {}) do
if v.server and v.server ~= game.GetIPAddress() then continue end
Be1zebub marked this conversation as resolved.
Show resolved Hide resolved

local job = DarkRP.getJobByCommand(v.jobcmd)
if job == nil or jobHasModel(job, v.model) == false then continue end
Be1zebub marked this conversation as resolved.
Show resolved Hide resolved

preferredModels[v.jobcmd] = v.model
end

timer.Simple(0, sendModels)
end
sendModels()
end)