Skip to content

Commit

Permalink
Comprehensive comments detailing what everything does, planned for th…
Browse files Browse the repository at this point in the history
…e future for everything
  • Loading branch information
actuallykane committed Jan 7, 2019
1 parent 4c1f53c commit a6ad5a7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions server/player/login.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,39 @@
-- NO TOUCHY, IF SOMETHING IS WRONG CONTACT KANERSPS! --
-- NO TOUCHY, IF SOMETHING IS WRONG CONTACT KANERSPS! --

-- Loads the user when called, only ever needs to get called once
function LoadUser(identifier, source, new, licenseNotRequired)
local Source = source
db.retrieveUser(identifier, function(user)
if user.license or licenseNotRequired then
-- Creates the player class for OOP imitation and then sets a var to say which idType was used (This isn't relevant anymore)
Users[source] = CreatePlayer(source, user.permission_level, user.money, user.bank, user.identifier, user.license, user.group, user.roles or "")
Users[Source].setSessionVar('idType', 'identifier')

-- Tells other resources that a player has loaded
TriggerEvent('es:playerLoaded', Source, Users[Source])

log('User (' .. identifier .. ') loaded')

-- Sets a decorator on the client if enabled, allows some cool stuff on the client see: https://runtime.fivem.net/doc/natives/#_0xA06C969B02A97298
if(settings.defaultSettings.enableRankDecorators ~= "false")then
TriggerClientEvent('es:setPlayerDecorator', Source, 'rank', Users[Source]:getPermissions())
end


-- Sets the money "icon" on the client. This is UTF8
TriggerClientEvent('es:setMoneyIcon', Source,settings.defaultSettings.moneyIcon)

-- Sends the command suggestions to the client, this creates a neat autocomplete
for k,v in pairs(commandSuggestions) do
TriggerClientEvent('chat:addSuggestion', Source, settings.defaultSettings.commandDelimeter .. k, v.help, v.params)
end

-- If a player connected that was never on the server before then this will be triggered for other resources
if new then
TriggerEvent('es:newPlayerLoaded', Source, Users[Source])
end
else
-- Irrelevant
local license

for k,v in ipairs(GetPlayerIdentifiers(Source))do
Expand All @@ -48,14 +56,17 @@ function LoadUser(identifier, source, new, licenseNotRequired)
end)
end

-- Exported function, same as es:getPlayerFromId
function getPlayerFromId(id)
return Users[id]
end

-- Returns all EssentialMode user objects
AddEventHandler('es:getPlayers', function(cb)
cb(Users)
end)

-- This gets called whenever a user spawns for the first time in the server, it basically loads the player
function registerUser(identifier, source)
local Source = source
db.doesUserExist(identifier, function(exists)
Expand All @@ -69,6 +80,7 @@ function registerUser(identifier, source)
end)
end

-- Allow other resources to set raw data on a player instead of using helper functions, these aren't really used often.
AddEventHandler("es:setPlayerData", function(user, k, v, cb)
if(Users[user])then
if(Users[user].get(k))then
Expand All @@ -95,12 +107,14 @@ AddEventHandler("es:setPlayerData", function(user, k, v, cb)
end
end)

-- Same as above just easier was we know the ID already now.
AddEventHandler("es:setPlayerDataId", function(user, k, v, cb)
db.updateUser(user, {[k] = v}, function(d)
cb("Player data edited.", true)
end)
end)

-- Returns the user if all checks completed, if the first if check fails then you're in a bit of trouble
AddEventHandler("es:getPlayerFromId", function(user, cb)
if(Users)then
if(Users[user])then
Expand All @@ -113,13 +127,14 @@ AddEventHandler("es:getPlayerFromId", function(user, cb)
end
end)

-- Same as above but uses the DB to get a user instead of memory.
AddEventHandler("es:getPlayerFromIdentifier", function(identifier, cb)
db.retrieveUser(identifier, function(user)
cb(user)
end)
end)

-- Function to update player money every 60 seconds.
-- Function to save player money to the database every 60 seconds.
local function savePlayerMoney()
SetTimeout(60000, function()
Citizen.CreateThread(function()
Expand Down

0 comments on commit a6ad5a7

Please sign in to comment.