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

feat(server): user object #598

Open
wants to merge 3 commits into
base: main
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
10 changes: 10 additions & 0 deletions qbx_core.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
CREATE TABLE IF NOT EXISTS `users` (
`userId` int UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
Manason marked this conversation as resolved.
Show resolved Hide resolved
`license` varchar(50) DEFAULT NULL,
`license2` varchar(50) DEFAULT NULL,
`fivem` varchar(20) DEFAULT NULL,
`discord` varchar(30) DEFAULT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`citizenid` varchar(50) NOT NULL,
Expand Down
27 changes: 27 additions & 0 deletions server/events.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local serverConfig = require 'config.server'.server
local loggingConfig = require 'config.server'.logging
local serverName = require 'config.shared'.serverName
local storage = require 'server.storage.main'
local logger = require 'modules.logger'
local queue = require 'server.queue'

Expand Down Expand Up @@ -53,13 +54,31 @@ AddEventHandler('playerDropped', function(reason)
QBX.Players[src] = nil
end)

---@param source Source|string
---@return table<string, string>
local function getIdentifiers(source)
local identifiers = {}

for i = 0, GetNumPlayerIdentifiers(source --[[@as string]]) - 1 do
local identifier = GetPlayerIdentifier(source --[[@as string]], i)
local prefix = identifier:match('([^:]+)')

if prefix ~= 'ip' then
identifiers[prefix] = identifier
end
end

return identifiers
end

-- Player Connecting
---@param name string
---@param _ any
---@param deferrals Deferrals
local function onPlayerConnecting(name, _, deferrals)
local src = source --[[@as string]]
local license = GetPlayerIdentifierByType(src, 'license2') or GetPlayerIdentifierByType(src, 'license')
local userId = storage.fetchUserByIdentifier(license)
deferrals.defer()

-- Mandatory wait
Expand All @@ -77,6 +96,14 @@ local function onPlayerConnecting(name, _, deferrals)
deferrals.done(locale('error.duplicate_license'))
end

if not userId then
local identifiers = getIdentifiers(src)

identifiers.username = name

storage.createUser(identifiers)
end

local databaseTime = os.clock()
local databasePromise = promise.new()

Expand Down
25 changes: 24 additions & 1 deletion server/storage/players.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
local defaultSpawn = require 'config.shared'.defaultSpawn
local characterDataTables = require 'config.server'.characterDataTables

---@param identifiers table<PlayerIdentifier, string>
---@return number?
local function createUser(identifiers)
return MySQL.insert.await('INSERT INTO users (username, license, license2, fivem, discord) VALUES (?, ?, ?, ?, ?)', {
identifiers.username,
identifiers.license,
identifiers.license2,
identifiers.fivem,
identifiers.discord,
})
end

---@param identifier string
---@return number?
local function fetchUserByIdentifier(identifier)
local idType = identifier:match('([^:]+)')
local select = ('SELECT `userId` FROM `users` WHERE `%s` = ? LIMIT 1'):format(idType)

return MySQL.scalar.await(select, { identifier })
end

---@param request InsertBanRequest
---@return boolean success
---@return ErrorResult? errorResult
Expand Down Expand Up @@ -363,6 +384,8 @@ CreateThread(function()
end)

return {
createUser = createUser,
fetchUserByIdentifier = fetchUserByIdentifier,
insertBan = insertBan,
fetchBan = fetchBan,
deleteBan = deleteBan,
Expand All @@ -379,4 +402,4 @@ return {
removePlayerFromJob = removePlayerFromJob,
removePlayerFromGang = removePlayerFromGang,
searchPlayerEntities = searchPlayerEntities,
}
}
1 change: 1 addition & 0 deletions types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

---@alias NotificationPosition 'top' | 'top-right' | 'top-left' | 'bottom' | 'bottom-right' | 'bottom-left' | 'center-right' | 'center-left'
---@alias NotificationType 'info' | 'warning' | 'success' | 'error'
---@alias PlayerIdentifier 'username' | 'license' | 'license2' | 'fivem' | 'discord'

---@class ErrorResult
---@field code string
Expand Down