Skip to content

Commit

Permalink
refactor(server): organize storage functions into a module instead of…
Browse files Browse the repository at this point in the history
… global functions

* refactor: organize storage functions into a module instead of global functions

* fixed missing deletePlayer references

* store table of functions in a var to propagate types

* small rename
  • Loading branch information
Manason committed Feb 17, 2024
1 parent 637ee0f commit 9e17883
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 55 deletions.
2 changes: 0 additions & 2 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ client_scripts {

server_scripts {
'@oxmysql/lib/MySQL.lua',
'server/storage/players.lua',
'server/storage/groups.lua',
'server/main.lua',
'server/groups.lua',
'server/functions.lua',
Expand Down
5 changes: 3 additions & 2 deletions server/character.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local config = require 'config.server'
local logger = require 'modules.logger'
local storage = require 'server.storage.main'

---@param license2 string
---@param license? string
Expand All @@ -25,7 +26,7 @@ end

lib.callback.register('qbx_core:server:getCharacters', function(source)
local license2, license = GetPlayerIdentifierByType(source, 'license2'), GetPlayerIdentifierByType(source, 'license')
local chars = FetchAllPlayerEntities(license2, license)
local chars = storage.fetchAllPlayerEntities(license2, license)
local allowedAmount = getAllowedAmountOfCharacters(license2, license)
local sortedChars = {}
for i = 1, #chars do
Expand All @@ -36,7 +37,7 @@ lib.callback.register('qbx_core:server:getCharacters', function(source)
end)

lib.callback.register('qbx_core:server:getPreviewPedData', function(_, citizenId)
local ped = FetchPlayerSkin(citizenId)
local ped = storage.fetchPlayerSkin(citizenId)
if not ped then return end

return ped.skin, ped.model and joaat(ped.model)
Expand Down
7 changes: 4 additions & 3 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local serverConfig = require 'config.server'.server
local positionConfig = require 'config.shared'.notifyPosition
local logger = require 'modules.logger'
local loggingConfig = require 'config.server'.logging
local storage = require 'server.storage.main'

-- Getters
-- Get your player first and then trigger a function on them
Expand Down Expand Up @@ -325,7 +326,7 @@ exports('ToggleOptin', ToggleOptin)
---@return string? playerMessage
function IsPlayerBanned(source)
local plicense = GetPlayerIdentifierByType(source --[[@as string]], 'license2') or GetPlayerIdentifierByType(source --[[@as string]], 'license')
local result = FetchBanEntity({
local result = storage.fetchBan({
license = plicense
})
if not result then return false end
Expand All @@ -334,7 +335,7 @@ function IsPlayerBanned(source)
return true, 'You have been banned from the server:\n' .. result.reason .. '\nYour ban expires ' .. timeTable.day .. '/' .. timeTable.month .. '/' .. timeTable.year .. ' ' .. timeTable.hour .. ':' .. timeTable.min .. '\n'
else
CreateThread(function()
DeleteBanEntity({
storage.deleteBan({
license = plicense
})
end)
Expand Down Expand Up @@ -391,7 +392,7 @@ exports('GetCoreVersion', GetCoreVersion)
local function ExploitBan(playerId, origin)
local name = GetPlayerName(playerId)
CreateThread(function()
InsertBanEntity({
storage.insertBan({
name = name,
license = GetPlayerIdentifierByType(playerId --[[@as string]], 'license2') or GetPlayerIdentifierByType(playerId --[[@as string]], 'license'),
discordId = GetPlayerIdentifierByType(playerId --[[@as string]], 'discord'),
Expand Down
28 changes: 15 additions & 13 deletions server/groups.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local storage = require 'server.storage.main'

---@type table<string, Job>
local jobs = {}
---@type table<string, Gang>
Expand All @@ -7,7 +9,7 @@ local gangs = {}
---@param newJobs table<string, Job>
function CreateJobs(newJobs)
for jobName, job in pairs(newJobs) do
UpsertJob(jobName, job)
storage.upsertJob(jobName, job)
jobs[jobName] = job
TriggerEvent('qbx_core:server:onJobUpdate', jobName, job)
TriggerClientEvent('qbx_core:client:onJobUpdate', -1, jobName, job)
Expand All @@ -29,7 +31,7 @@ function RemoveJob(jobName)
return false, "job_not_exists"
end

DeleteJobEntity(jobName)
storage.deleteJobEntity(jobName)
jobs[jobName] = nil
TriggerEvent('qbx_core:server:onJobUpdate', jobName, nil)
TriggerClientEvent('qbx_core:client:onJobUpdate', -1, jobName, nil)
Expand All @@ -42,7 +44,7 @@ exports('RemoveJob', RemoveJob)
---@param newGangs table<string, Gang>
function CreateGangs(newGangs)
for gangName, gang in pairs(newGangs) do
UpsertGang(gangName, gang)
storage.upsertGang(gangName, gang)
gangs[gangName] = gang
TriggerEvent('qbx_core:server:onGangUpdate', gangName, gang)
TriggerClientEvent('qbx_core:client:onGangUpdate', -1, gangName, gang)
Expand All @@ -64,7 +66,7 @@ function RemoveGang(gangName)
return false, "gang_not_exists"
end

DeleteGangEntity(gangName)
storage.deleteGangEntity(gangName)
gangs[gangName] = nil

TriggerEvent('qbx_core:server:onGangUpdate', gangName, nil)
Expand Down Expand Up @@ -115,7 +117,7 @@ end)
---@param name string
---@param data JobData
local function upsertJobData(name, data)
UpsertJobEntity(name, data)
storage.upsertJobEntity(name, data)
if jobs[name] then
jobs[name].defaultDuty = data.defaultDuty
jobs[name].label = data.label
Expand All @@ -139,7 +141,7 @@ exports('UpsertJobData', upsertJobData)
---@param name string
---@param data GangData
local function upsertGangData(name, data)
UpsertGangEntity(name, data)
storage.upsertGangEntity(name, data)
if gangs[name] then
gangs[name].label = data.label
else
Expand All @@ -162,7 +164,7 @@ local function upsertJobGrade(name, grade, data)
lib.print.error("Job must exist to edit grades. Not found:", name)
return
end
UpsertJobGradeEntity(name, grade, data)
storage.upsertJobGradeEntity(name, grade, data)
jobs[name].grades[grade] = data
TriggerEvent('qbx_core:server:onJobUpdate', name, jobs[name])
TriggerClientEvent('qbx_core:client:onJobUpdate', -1, name, jobs[name])
Expand All @@ -178,7 +180,7 @@ local function upsertGangGrade(name, grade, data)
lib.print.error("Gang must exist to edit grades. Not found:", name)
return
end
UpsertGangGradeEntity(name, grade, data)
storage.upsertGangGradeEntity(name, grade, data)
gangs[name].grades[grade] = data
TriggerEvent('qbx_core:server:onGangUpdate', name, gangs[name])
TriggerClientEvent('qbx_core:client:onGangUpdate', -1, name, gangs[name])
Expand All @@ -193,7 +195,7 @@ local function removeJobGrade(name, grade)
lib.print.error("Job must exist to edit grades. Not found:", name)
return
end
DeleteJobGradeEntity(name, grade)
storage.deleteJobGradeEntity(name, grade)
jobs[name].grades[grade] = nil
TriggerEvent('qbx_core:server:onJobUpdate', name, jobs[name])
TriggerClientEvent('qbx_core:client:onJobUpdate', -1, name, jobs[name])
Expand All @@ -208,7 +210,7 @@ local function removeGangGrade(name, grade)
lib.print.error("Gang must exist to edit grades. Not found:", name)
return
end
DeleteGangGradeEntity(name, grade)
storage.deleteGangGradeEntity(name, grade)
gangs[name].grades[grade] = nil
TriggerEvent('qbx_core:server:onGangUpdate', name, gangs[name])
TriggerClientEvent('qbx_core:client:onGangUpdate', -1, name, gangs[name])
Expand All @@ -217,21 +219,21 @@ end
exports('RemoveGangGrade', removeGangGrade)

local function loadGroups()
local fetchedJobs, fetchedGangs = FetchGroups()
local fetchedJobs, fetchedGangs = storage.fetchGroups()
local configJobs = require 'shared.jobs'
local configGangs = require 'shared.gangs'

for name, job in pairs(configJobs) do
if not fetchedJobs[name] then
jobs[name] = job
UpsertJob(name, job)
storage.upsertJob(name, job)
end
end

for name, gang in pairs(configGangs) do
if not fetchedGangs[name] then
gangs[name] = gang
UpsertGang(name, gang)
storage.upsertGang(name, gang)
end
end

Expand Down
19 changes: 10 additions & 9 deletions server/player.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local config = require 'config.server'
local defaultSpawn = require 'config.shared'.defaultSpawn
local logger = require 'modules.logger'
local storage = require 'server.storage.main'

---@class PlayerData : PlayerEntity
---@field source? Source present if player is online
Expand Down Expand Up @@ -28,7 +29,7 @@ exports('Login', Login)
function LoginV2(source, citizenid, newData)
if citizenid then
local license, license2 = GetPlayerIdentifierByType(source --[[@as string]], 'license'), GetPlayerIdentifierByType(source --[[@as string]], 'license2')
local playerData = FetchPlayerEntity(citizenid)
local playerData = storage.fetchPlayerEntity(citizenid)
if playerData and (license2 == playerData.license or license == playerData.license) then
return CheckPlayerData(source, playerData)
else
Expand All @@ -53,7 +54,7 @@ end
---@return Player? player if found in storage
function GetOfflinePlayer(citizenid)
if not citizenid then return end
local playerData = FetchPlayerEntity(citizenid)
local playerData = storage.fetchPlayerEntity(citizenid)
if not playerData then return end
return CheckPlayerData(nil, playerData)
end
Expand Down Expand Up @@ -564,7 +565,7 @@ function Save(source)
playerData.metadata.armor = GetPedArmour(ped)

CreateThread(function()
UpsertPlayerEntity({
storage.upsertPlayerEntity({
playerEntity = playerData,
position = pcoords,
})
Expand All @@ -583,7 +584,7 @@ function SaveOffline(playerData)
end

CreateThread(function()
UpsertPlayerEntity({
storage.upsertPlayerEntity({
playerEntity = playerData,
position = playerData.position.xyz
})
Expand All @@ -598,10 +599,10 @@ exports('SaveOffline', SaveOffline)
---@param citizenid string
function DeleteCharacter(source, citizenid)
local license, license2 = GetPlayerIdentifierByType(source --[[@as string]], 'license'), GetPlayerIdentifierByType(source --[[@as string]], 'license2')
local result = FetchPlayerEntity(citizenid).license
local result = storage.fetchPlayerEntity(citizenid).license
if license == result or license2 == result then
CreateThread(function()
local success = DeletePlayerEntity(citizenid)
local success = storage.deletePlayer(citizenid)
if success then
logger.log({
source = 'qbx_core',
Expand All @@ -627,15 +628,15 @@ end

---@param citizenid string
function ForceDeleteCharacter(citizenid)
local result = FetchPlayerEntity(citizenid).license
local result = storage.fetchPlayerEntity(citizenid).license
if result then
local player = GetPlayerByCitizenId(citizenid)
if player then
DropPlayer(player.PlayerData.source --[[@as string]], "An admin deleted the character which you are currently using")
end

CreateThread(function()
local success = DeletePlayerEntity(citizenid)
local success = storage.deletePlayer(citizenid)
if success then
logger.log({
source = 'qbx_core',
Expand All @@ -659,7 +660,7 @@ function GenerateUniqueIdentifier(type)
local table = config.player.identifierTypes[type]
repeat
uniqueId = table.valueFunction()
isUnique = FetchIsUnique(type, uniqueId)
isUnique = storage.fetchIsUnique(type, uniqueId)
until isUnique
return uniqueId
end
Expand Down
36 changes: 25 additions & 11 deletions server/storage/groups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ end

---@return table<string, Job>
---@return table<string, Gang>
function FetchGroups()
local function fetchGroups()
local jobs = {}
local gangs = {}

Expand Down Expand Up @@ -108,31 +108,31 @@ end

---@param name string
---@param data JobData
function UpsertJobEntity(name, data)
local function upsertJobEntity(name, data)
upsertGroupEntity(name, GroupType.JOB, data)
end

---@param job string
---@param grade integer
---@param data JobGradeData
function UpsertJobGradeEntity(job, grade, data)
local function upsertJobGradeEntity(job, grade, data)
upsertGradeEntity(job, GroupType.JOB, grade, data)
end

---@param name string
function DeleteJobEntity(name)
local function deleteJobEntity(name)
deleteGroupEntity(name, GroupType.JOB)
end

---@param name string
---@param grade integer
function DeleteJobGradeEntity(name, grade)
local function deleteJobGradeEntity(name, grade)
deleteGradeEntity(name, GroupType.JOB, grade)
end

---@param name string
---@param job Job
function UpsertJob(name, job)
local function upsertJob(name, job)
local jobEntityData = {
defaultDuty = job.defaultDuty,
label = job.label,
Expand Down Expand Up @@ -172,31 +172,31 @@ end

---@param name string
---@param data GangData
function UpsertGangEntity(name, data)
local function upsertGangEntity(name, data)
upsertGroupEntity(name, GroupType.GANG, data)
end

---@param gang string
---@param grade integer
---@param data GangGradeData
function UpsertGangGradeEntity(gang, grade, data)
local function upsertGangGradeEntity(gang, grade, data)
upsertGradeEntity(gang, GroupType.GANG, grade, data)
end

---@param name string
function DeleteGangEntity(name)
local function deleteGangEntity(name)
deleteGroupEntity(name, GroupType.GANG)
end

---@param name string
---@param grade integer
function DeleteGangGradeEntity(name, grade)
local function deleteGangGradeEntity(name, grade)
deleteGradeEntity(name, GroupType.GANG, grade)
end

---@param name string
---@param gang Gang
function UpsertGang(name, gang)
local function upsertGang(name, gang)
local gangEntityData = {
label = gang.label
}
Expand Down Expand Up @@ -226,3 +226,17 @@ function UpsertGang(name, gang)

MySQL.transaction.await(queries)
end

return {
fetchGroups = fetchGroups,
upsertJobEntity = upsertJobEntity,
upsertJobGradeEntity = upsertJobGradeEntity,
deleteJobEntity = deleteJobEntity,
deleteJobGradeEntity = deleteJobGradeEntity,
upsertJob = upsertJob,
upsertGangEntity = upsertGangEntity,
upsertGangGradeEntity = upsertGangGradeEntity,
deleteGangEntity = deleteGangEntity,
deleteGangGradeEntity = deleteGangGradeEntity,
upsertGang = upsertGang,
}
Loading

0 comments on commit 9e17883

Please sign in to comment.