Skip to content

Commit

Permalink
feat(server): add userId to players table (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm authored Nov 8, 2024
1 parent 0b7ed08 commit 6d5c98e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions qbx_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ ALTER TABLE `players`
ADD IF NOT EXISTS `last_logged_out` timestamp NULL DEFAULT NULL AFTER `last_updated`,
MODIFY COLUMN `name` varchar(255) NOT NULL COLLATE utf8mb4_unicode_ci;

ALTER TABLE `players`
ADD IF NOT EXISTS `userId` INT UNSIGNED DEFAULT NULL AFTER `id`;

CREATE TABLE IF NOT EXISTS `bans` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
Expand Down
5 changes: 5 additions & 0 deletions server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ AddEventHandler('onResourceStart', function(resource)
if resource ~= cache.resource then return end

storage.createUsersTable()

MySQL.query([[
ALTER TABLE `players`
ADD IF NOT EXISTS `userId` INT UNSIGNED DEFAULT NULL AFTER `id`;
]])
end)

-- New method for checking if logged in across all scripts (optional)
Expand Down
11 changes: 9 additions & 2 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Login(source, citizenid, newData)
return false
end

local license = GetPlayerIdentifierByType(source --[[@as string]], 'license2') or GetPlayerIdentifierByType(source --[[@as string]], 'license')
local userId = storage.fetchUserByIdentifier(license)

if citizenid then
local license, license2 = GetPlayerIdentifierByType(source --[[@as string]], 'license'), GetPlayerIdentifierByType(source --[[@as string]], 'license2')
local playerData = storage.fetchPlayerEntity(citizenid)
if playerData and (license2 == playerData.license or license == playerData.license) then
if playerData and license == playerData.license then
playerData.userId = userId

return not not CheckPlayerData(source, playerData)
else
DropPlayer(tostring(source), locale('info.exploit_dropped'))
Expand All @@ -40,6 +44,8 @@ function Login(source, citizenid, newData)
})
end
else
newData.userId = userId

local player = CheckPlayerData(source, newData)
Save(player.PlayerData.source)
return true
Expand Down Expand Up @@ -548,6 +554,7 @@ function CheckPlayerData(source, playerData)
Offline = false
end

playerData.userId = playerData.userId or nil
playerData.citizenid = playerData.citizenid or GenerateUniqueIdentifier('citizenid')
playerData.cid = playerData.charinfo?.cid or playerData.cid or 1
playerData.money = playerData.money or {}
Expand Down
8 changes: 5 additions & 3 deletions server/storage/players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local function createUser(identifiers)
end

---@param identifier string
---@return number?
---@return integer?
local function fetchUserByIdentifier(identifier)
local idType = identifier:match('([^:]+)')
local select = ('SELECT `userId` FROM `users` WHERE `%s` = ? LIMIT 1'):format(idType)
Expand Down Expand Up @@ -93,7 +93,8 @@ end

---@param request UpsertPlayerRequest
local function upsertPlayerEntity(request)
MySQL.insert.await('INSERT INTO players (citizenid, cid, license, name, money, charinfo, job, gang, position, metadata, last_logged_out) VALUES (:citizenid, :cid, :license, :name, :money, :charinfo, :job, :gang, :position, :metadata, :last_logged_out) ON DUPLICATE KEY UPDATE name = :name, money = :money, charinfo = :charinfo, job = :job, gang = :gang, position = :position, metadata = :metadata, last_logged_out = :last_logged_out', {
MySQL.insert.await('INSERT INTO players (userId, citizenid, cid, license, name, money, charinfo, job, gang, position, metadata, last_logged_out) VALUES (:userId, :citizenid, :cid, :license, :name, :money, :charinfo, :job, :gang, :position, :metadata, :last_logged_out) ON DUPLICATE KEY UPDATE userId = :userId, name = :name, money = :money, charinfo = :charinfo, job = :job, gang = :gang, position = :position, metadata = :metadata, last_logged_out = :last_logged_out', {
userId = request.playerEntity.userId,
citizenid = request.playerEntity.citizenid,
cid = request.playerEntity.charinfo.cid,
license = request.playerEntity.license,
Expand Down Expand Up @@ -146,9 +147,10 @@ end
---@return PlayerEntity?
local function fetchPlayerEntity(citizenId)
---@type PlayerEntityDatabase
local player = MySQL.single.await('SELECT citizenid, license, name, charinfo, money, job, gang, position, metadata, UNIX_TIMESTAMP(last_logged_out) AS lastLoggedOutUnix FROM players WHERE citizenid = ?', { citizenId })
local player = MySQL.single.await('SELECT userId, citizenid, license, name, charinfo, money, job, gang, position, metadata, UNIX_TIMESTAMP(last_logged_out) AS lastLoggedOutUnix FROM players WHERE citizenid = ?', { citizenId })
local charinfo = player and json.decode(player.charinfo)
return player and {
userId = player.userId,
citizenid = player.citizenid,
license = player.license,
name = player.name,
Expand Down
1 change: 1 addition & 0 deletions types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
---@field position vector3

---@class PlayerEntity
---@field userId? integer
---@field citizenid string
---@field license string
---@field name string
Expand Down

0 comments on commit 6d5c98e

Please sign in to comment.