Skip to content

Commit

Permalink
feat(server): table auto creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm committed Oct 31, 2024
1 parent 675c34a commit 467773c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ end

AddEventHandler('playerConnecting', onPlayerConnecting)

AddEventHandler('onResourceStart', function(resource)
if resource ~= cache.resource then return end

storage.createUsersTable()
end)

-- New method for checking if logged in across all scripts (optional)
-- `if LocalPlayer.state.isLoggedIn then` for the client side
-- `if Player(source).state.isLoggedIn then` for the server side
Expand Down
15 changes: 15 additions & 0 deletions server/storage/players.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
local defaultSpawn = require 'config.shared'.defaultSpawn
local characterDataTables = require 'config.server'.characterDataTables

local function createUsersTable()
MySQL.query([[
CREATE TABLE IF NOT EXISTS `users` (
`userId` int UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`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;
]])
end

---@param identifiers table<PlayerIdentifier, string>
---@return number?
local function createUser(identifiers)
Expand Down Expand Up @@ -384,6 +398,7 @@ CreateThread(function()
end)

return {
createUsersTable = createUsersTable,
createUser = createUser,
fetchUserByIdentifier = fetchUserByIdentifier,
insertBan = insertBan,
Expand Down

0 comments on commit 467773c

Please sign in to comment.