Skip to content

Commit

Permalink
refactor(server/storage): block non-existent groups from PlayerData (#…
Browse files Browse the repository at this point in the history
…527)

* feat(server/storage): add cleanplayergroups command

Adds cleanplayergroups command to remove invalid groups from the player_groups table to prevent issues with PlayerData loading

* fix(server/storage): prevent non-gang types in gangs table

While it's generally expected that gangs would be the only other type of data in the player_groups table this could lead to situations where a non-gang group could be errorneously pushed into the PlayerData.gangs table

* fix(server/storage): block nonexistant groups from PlayerData

This prevents invalid groups from being pushed into the PlayerData object and advises the user to check their configuration

* chore(server/storage): add comment for command

* refactor(server/storage): remove unused event

* refactor(server/storage): check for valid grades

* refactor(server/storage): extract to function

* feat(server/storage): add convar for automatic cleaning
  • Loading branch information
solareon authored Aug 5, 2024
1 parent 0a8edea commit 422f918
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions server/storage/players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,14 @@ local function fetchPlayerGroups(citizenid)
local gangs = {}
for i = 1, #groups do
local group = groups[i]
if group.type == GroupType.JOB then
local validGroup = group.type == GroupType.JOB and GetJob(group.group) or GetGang(group.group)
if not validGroup then
lib.print.warn(('Invalid group %s found in player_groups table, Does it exist in shared/%ss.lua?'):format(group.group, group.type))
elseif not validGroup[group.grade] then
lib.print.warn(('Invalid grade %s found in player_groups table for %s %s, Does it exist in shared/%ss.lua?'):format(group.grade, group.type, group.group, group.type))
elseif group.type == GroupType.JOB then
jobs[group.group] = group.grade
else
elseif group.type == GroupType.GANG then
gangs[group.group] = group.grade
end
end
Expand Down Expand Up @@ -369,13 +374,39 @@ RegisterCommand('convertjobs', function(source)
TriggerEvent('qbx_core:server:jobsconverted')
end, true)

---Removes invalid groups from the player_groups table.
local function cleanPlayerGroups()
local groups = MySQL.query.await('SELECT DISTINCT `group`, type, grade FROM player_groups')
for i = 1, #groups do
local group = groups[i]
local validGroup = group.type == GroupType.JOB and GetJob(group.group) or GetGang(group.group)
if not validGroup then
MySQL.query.await('DELETE FROM player_groups WHERE `group` = ? AND type = ?', {group.group, group.type})
lib.print.info(('Remove invalid %s %s from player_groups table'):format(group.type, group.group))
elseif not validGroup[group.grade] then
MySQL.query.await('DELETE FROM player_groups WHERE `group` = ? AND type = ? AND grade = ?', {group.group, group.type, group.grade})
lib.print.info(('Remove invalid %s %s grade %s from player_groups table'):format(group.type, group.group, group.grade))
end
end

lib.print.info('Removed invalid groups from player_groups table')
end

RegisterCommand('cleanplayergroups', function(source)
if source ~= 0 then return warn('This command can only be executed using the server console.') end
cleanPlayerGroups()
end, true)

CreateThread(function()
for _, data in pairs(characterDataTables) do
local tableName = data[1]
if not doesTableExist(tableName) then
warn(('Table \'%s\' does not exist in database, please remove it from qbx_core/config/server.lua or create the table'):format(tableName))
end
end
if GetConvar('qbx:cleanPlayerGroups', 'false') == 'true' then
cleanPlayerGroups()
end
end)

return {
Expand Down

0 comments on commit 422f918

Please sign in to comment.