Files
ND_Core/server/functions.lua

410 lines
19 KiB
Lua
Raw Normal View History

-- For support join my discord: https://discord.gg/Z9Mxu72zZ6
2022-09-06 11:12:25 +02:00
-- return the players selected character.
function NDCore.Functions.GetPlayer(player)
return NDCore.Players[player]
end
-- Callback to update each selected character on the server.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GetPlayers(players)
if not cb then return NDCore.Players end
cb(NDCore.Players)
end
local discordErrors = {
[400] = "improper http request",
[401] = "Discord bot token might be missing or incorrect",
[404] = "user might not be in server.",
2022-08-11 01:17:13 +02:00
[429] = "Discord bot rate limited."
}
-- Used to retrive the players discord server nickname, discord name and tag, and the roles.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GetUserDiscordInfo(discordUserId)
local data
2022-08-11 01:17:13 +02:00
local timeout = 0
PerformHttpRequest("https://discordapp.com/api/guilds/" .. server_config.guildId .. "/members/" .. discordUserId, function(errorCode, resultData, resultHeaders)
if errorCode ~= 200 then
print("Error: " .. errorCode .. " " .. discordErrors[errorCode])
end
local result = json.decode(resultData)
local roles = {}
for _, roleId in pairs(result.roles) do
roles[roleId] = roleId
end
data = {
nickname = result.nick,
discordTag = tostring(result.user.username) .. "#" .. tostring(result.user.discriminator),
roles = roles
}
end, "GET", "", {["Content-Type"] = "application/json", ["Authorization"] = "Bot " .. server_config.discordServerToken})
2022-08-11 01:17:13 +02:00
while not data do
Wait(1000)
timeout = timeout + 1
if timeout > 5 then
break
end
end
2022-08-11 01:17:13 +02:00
return data
end
-- Get player any identifier, available types: steam, license, xbl, ip, discord, live.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GetPlayerIdentifierFromType(type, player)
local identifierCount = GetNumPlayerIdentifiers(player)
for count = 0, identifierCount do
local identifier = GetPlayerIdentifier(player, count)
if identifier and string.find(identifier, type) then
return identifier
end
end
return nil
end
-- This will return the server id and ND player data of a nearby player.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GetNearbyPedToPlayer(player)
local pedCoords = GetEntityCoords(GetPlayerPed(player))
2022-07-16 00:12:02 -04:00
for targetId, targetInfo in pairs(NDCore.Players) do
local targetCoords = GetEntityCoords(GetPlayerPed(targetId))
if #(pedCoords - targetCoords) < 2.0 and targetId ~= player then
return targetId, targetInfo
end
end
end
-- update the players money on the client kinda like a refresh.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.UpdateMoney(player)
local player = tonumber(player)
2022-09-18 02:36:45 +02:00
local result = MySQL.query.await("SELECT cash, bank FROM characters WHERE character_id = ? LIMIT 1", {NDCore.Players[player].id})
if result then
local cash = result[1].cash
local bank = result[1].bank
NDCore.Players[player].cash = cash
NDCore.Players[player].bank = bank
TriggerClientEvent("ND:updateMoney", player, cash, bank)
end
end
-- Transfer money from one players bank account to another.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.TransferBank(amount, player, target)
local amount = tonumber(amount)
local player = tonumber(player)
local target = tonumber(target)
if player == target then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "You can't send money to yourself."}
})
return false
elseif GetPlayerPing(target) == 0 then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "That player does not exist."}
})
return false
elseif amount <= 0 then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "You can't send that amount."}
})
return false
2022-07-16 00:12:02 -04:00
elseif NDCore.Players[player].bank < amount then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "You don't have enough money."}
})
return false
else
2022-09-15 15:54:05 +02:00
MySQL.query.await("UPDATE characters SET bank = bank - ? WHERE character_id = ?", {amount, NDCore.Players[player].id})
2022-07-16 00:12:02 -04:00
NDCore.Functions.UpdateMoney(player)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", player, "bank", amount, "remove")
TriggerClientEvent("chat:addMessage", player, {
color = {0, 255, 0},
2022-07-16 00:12:02 -04:00
args = {"Success", "You paid " .. NDCore.Players[target].firstName .. " " .. NDCore.Players[target].lastName .. " $" .. amount .. "."}
})
2022-09-15 15:54:05 +02:00
MySQL.query.await("UPDATE characters SET bank = bank + ? WHERE character_id = ?", {amount, NDCore.Players[target].id})
NDCore.Functions.UpdateMoney(target)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", target, "bank", amount, "add")
2022-08-11 19:43:33 +02:00
TriggerClientEvent("chat:addMessage", target, {
color = {0, 255, 0},
2022-08-23 21:26:44 +02:00
args = {"Success", NDCore.Players[player].firstName .. " " .. NDCore.Players[player].lastName .. " sent you $" .. amount .. "."}
2022-08-11 19:43:33 +02:00
})
return true
end
end
-- Give cash from one players wallet to another.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GiveCash(amount, player, target)
local amount = tonumber(amount)
local player = tonumber(player)
local target = tonumber(target)
if player == target then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "You can't give money to yourself."}
})
return false
elseif GetPlayerPing(target) == 0 then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "That player does not exist."}
})
return false
elseif amount <= 0 then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "You can't give that amount."}
})
return false
2022-07-16 00:12:02 -04:00
elseif NDCore.Players[player].cash < amount then
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "You don't have enough money."}
})
return false
else
2022-09-15 15:54:05 +02:00
MySQL.query.await("UPDATE characters SET cash = cash - ? WHERE character_id = ?", {amount, NDCore.Players[player].id})
2022-07-16 00:12:02 -04:00
NDCore.Functions.UpdateMoney(player)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", player, "cash", amount, "remove")
TriggerClientEvent("chat:addMessage", player, {
color = {0, 255, 0},
2022-09-15 15:54:05 +02:00
args = {"Success", "You gave " .. NDCore.Players[target].firstName .. " " .. NDCore.Players[target].lastName .. " $" .. amount .. "."}
2022-08-11 19:43:33 +02:00
})
2022-09-15 15:54:05 +02:00
MySQL.query.await("UPDATE characters SET cash = cash + ? WHERE character_id = ?", {amount, NDCore.Players[target].id})
NDCore.Functions.UpdateMoney(target)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", target, "cash", amount, "add")
2022-08-11 19:43:33 +02:00
TriggerClientEvent("chat:addMessage", target, {
color = {0, 255, 0},
args = {"Success", " Received $" .. amount .. "."}
})
return true
end
end
-- Give money from a players wallet to a nearby player.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GiveCashToNearbyPlayer(player, amount)
local targetId = NDCore.Functions.GetNearbyPedToPlayer(player)
if targetId then
2022-07-16 00:12:02 -04:00
NDCore.Functions.GiveCash(amount, player, targetId)
return true
end
TriggerClientEvent("chat:addMessage", player, {
color = {255, 0, 0},
args = {"Error", "No players nearby."}
})
return false
end
-- withdraws money from a players bank account to their wallet/cash.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.WithdrawMoney(amount, player)
local amount = tonumber(amount)
local player = tonumber(player)
if amount <= 0 then return false end
2022-07-16 00:12:02 -04:00
if NDCore.Players[player].bank < amount then return false end
2022-09-03 07:54:09 -04:00
MySQL.query.await("UPDATE characters SET bank = bank - ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
MySQL.query.await("UPDATE characters SET cash = cash + ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
2022-07-16 00:12:02 -04:00
NDCore.Functions.UpdateMoney(player)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", player, "bank", amount, "remove")
TriggerEvent("ND:moneyChange", player, "cash", amount, "add")
return true
end
-- deposits money from a players wallet/cash to their bank account.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.DepositMoney(amount, player)
local amount = tonumber(amount)
local player = tonumber(player)
if amount <= 0 then return false end
2022-07-16 00:12:02 -04:00
if NDCore.Players[player].cash < amount then return false end
2022-09-03 07:54:09 -04:00
MySQL.query.await("UPDATE characters SET cash = cash - ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
MySQL.query.await("UPDATE characters SET bank = bank + ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
2022-07-16 00:12:02 -04:00
NDCore.Functions.UpdateMoney(player)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", player, "cash", amount, "remove")
TriggerEvent("ND:moneyChange", player, "bank", amount, "add")
return true
end
-- Deducts money from the player, "bank" or "cash" needs to be specified.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.DeductMoney(amount, player, from)
local amount = tonumber(amount)
local player = tonumber(player)
if from == "bank" then
2022-09-03 07:54:09 -04:00
MySQL.query.await("UPDATE characters SET bank = bank - ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
elseif from == "cash" then
2022-09-03 07:54:09 -04:00
MySQL.query.await("UPDATE characters SET cash = cash - ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
end
2022-07-16 00:12:02 -04:00
NDCore.Functions.UpdateMoney(player)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", player, from, amount, "remove")
end
-- Adds money from the player, "bank" or "cash" needs to be specified.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.AddMoney(amount, player, to)
local amount = tonumber(amount)
local player = tonumber(player)
if to == "bank" then
2022-09-03 07:54:09 -04:00
MySQL.query.await("UPDATE characters SET bank = bank + ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
elseif to == "cash" then
2022-09-03 07:54:09 -04:00
MySQL.query.await("UPDATE characters SET cash = cash + ? WHERE character_id = ? LIMIT 1", {amount, NDCore.Players[player].id})
end
2022-07-16 00:12:02 -04:00
NDCore.Functions.UpdateMoney(player)
2022-09-18 02:36:45 +02:00
TriggerEvent("ND:moneyChange", player, to, amount, "add")
end
2022-07-16 00:12:02 -04:00
-- Adds the players character to the NDCore.Players table, this table consists of every players selected character.
function NDCore.Functions.SetActiveCharacter(player, characterId)
if NDCore.Players[player] then
NDCore.Functions.SaveInventory(player)
end
2022-09-03 07:54:09 -04:00
local result = MySQL.query.await("SELECT * FROM characters WHERE character_id = ? LIMIT 1", {characterId})
if result then
local i = result[1]
2022-07-16 00:12:02 -04:00
NDCore.Players[player] = {
source = player,
id = characterId,
firstName = i.first_name,
lastName = i.last_name,
dob = i.dob,
gender = i.gender,
twt = i.twt,
job = i.job,
cash = i.cash,
bank = i.bank,
phoneNumber = i.phone_number,
groups = json.decode(i.groups),
lastLocation = json.decode(i.last_location),
clothing = json.decode(i.clothing),
2022-10-01 18:32:49 -04:00
inventory = json.decode(i.inventory)
}
end
2022-09-06 11:18:24 +02:00
TriggerEvent("ND:characterLoaded", NDCore.Players[player])
2022-07-16 00:12:02 -04:00
TriggerClientEvent("ND:setCharacter", player, NDCore.Players[player])
end
-- This returns all the characters the player has.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.GetPlayerCharacters(player)
local characters = {}
2022-09-07 10:37:50 +02:00
local result = MySQL.query.await("SELECT * FROM characters WHERE license = ?", {NDCore.Functions.GetPlayerIdentifierFromType("license", player)})
for i = 1, #result do
local temp = result[i]
characters[temp.character_id] = {id = temp.character_id, firstName = temp.first_name, lastName = temp.last_name, dob = temp.dob, gender = temp.gender, twt = temp.twt, job = temp.job, cash = temp.cash, bank = temp.bank, phoneNumber = temp.phone_number, groups = json.decode(temp.groups), lastLocation = json.decode(temp.last_location), clothing = json.decode(temp.clothing)}
end
return characters
end
-- Creates a new character for the player and returns all their characters to the client.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.CreateCharacter(player, firstName, lastName, dob, gender, twt, job, cash, bank)
local license = NDCore.Functions.GetPlayerIdentifierFromType("license", player)
if not cash or not bank or tonumber(cash) > config.startingCash or tonumber(bank) > config.startingBank then
cash = config.startingCash
bank = config.startingBank
end
local result = MySQL.query.await("SELECT character_id FROM characters WHERE license = ?", {license})
if result and config.characterLimit > #result then
MySQL.query.await("INSERT INTO characters (license, first_name, last_name, dob, gender, twt, job, cash, bank) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", {license, firstName, lastName, dob, gender, twt, job, cash, bank})
2022-07-16 00:12:02 -04:00
TriggerClientEvent("ND:returnCharacters", player, NDCore.Functions.GetPlayerCharacters(player))
end
2022-07-21 22:48:30 +02:00
return result
end
-- Update/edit a character info by character id.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.UpdateCharacterData(characterId, firstName, lastName, dob, gender, twt, job)
2022-09-03 07:54:09 -04:00
local result = MySQL.query.await("UPDATE characters SET first_name = ?, last_name = ?, dob = ?, gender = ?, twt = ?, job = ? WHERE character_id = ? LIMIT 1", {firstName, lastName, dob, gender, twt, job, characterId})
2022-07-21 22:48:30 +02:00
return result
end
-- Delete a character by character id.
2022-07-16 00:12:02 -04:00
function NDCore.Functions.DeleteCharacter(characterId)
2022-09-03 07:54:09 -04:00
local result = MySQL.query.await("DELETE FROM characters WHERE character_id = ? LIMIT 1", {characterId})
2022-07-21 22:48:30 +02:00
return result
end
-- Update the all the characters groups in the database.
function NDCore.Functions.UpdateGroups(player, groups)
local result = MySQL.query.await("UPDATE characters SET groups = ? WHERE character_id = ?", {json.encode(groups), NDCore.Players[player].id})
return result
end
-- Set a group to a character in the database.
2022-09-05 15:51:17 +02:00
function NDCore.Functions.SetGroup(player, group, groupName, groupLevel)
local groups = NDCore.Players[player].groups
groups[group] = {name = groupName, lvl = groupLevel}
result = MySQL.query.await("UPDATE characters SET groups = ? WHERE character_id = ?", {json.encode(groups), NDCore.Players[player].id})
return result
end
-- Remove the group form a character in the database.
function NDCore.Functions.RemoveGroup(player, group)
2022-09-05 15:51:17 +02:00
local groups = NDCore.Players[player].groups
groups[group] = nil
result = MySQL.query.await("UPDATE characters SET groups = ? WHERE character_id = ?", {json.encode(groups), NDCore.Players[player].id})
return result
end
-- Update the characters last location into the database.
function NDCore.Functions.UpdateLastLocation(characterId, location)
2022-09-03 07:54:09 -04:00
local result = MySQL.query.await("UPDATE characters SET last_location = ? WHERE character_id = ? LIMIT 1", {json.encode(location), characterId})
return result
end
-- Update the characters clothing into the database.
function NDCore.Functions.UpdateClothes(characterId, clothing)
2022-09-03 07:54:09 -04:00
local result = MySQL.query.await("UPDATE characters SET clothing = ? WHERE character_id = ? LIMIT 1", {json.encode(clothing), characterId})
return result
end
2022-09-06 11:13:24 +02:00
-- Updates the player's data
function NDCore.Functions.SetPlayerData(player, key, value)
if not key then return end
2022-09-11 17:41:46 +02:00
local character = NDCore.Players[player]
character[key] = value
if key == "cash" then
2022-09-15 15:47:04 +02:00
MySQL.query.await("UPDATE characters SET cash = ? WHERE character_id = ?", {tonumber(value), character.id})
2022-09-11 17:41:46 +02:00
elseif key == "bank" then
2022-09-15 15:47:04 +02:00
MySQL.query.await("UPDATE characters SET bank = ? WHERE character_id = ?", {tonumber(value), character.id})
2022-09-06 11:13:24 +02:00
end
TriggerClientEvent("ND:updateCharacter", player, NDCore.Players[player])
end
-- Saves player inventory to the database.
function NDCore.Functions.SaveInventory(player)
local inventory = NDCore.Players[player].inventory
MySQL.query("UPDATE `characters` SET inventory = ? WHERE character_id = ?", {json.encode(inventory), NDCore.Players[player].id})
2022-09-06 11:13:24 +02:00
end
2022-07-16 00:12:02 -04:00
function NDCore.Functions.VersionChecker(expectedResourceName, resourceName, downloadLink, rawGithubLink)
if expectedResourceName ~= resourceName then
print("^1[^4" .. expectedResourceName .. "^1] WARNING^0")
print("Change the resource name to ^4" .. expectedResourceName .. " ^0or else it won't work properly!")
StopResource(resourceName)
return
end
PerformHttpRequest(rawGithubLink, function(errorCode, resultData, resultHeaders)
local i, j = string.find(tostring(resultData), "version")
local resultData = string.sub(tostring(resultData), i, j + 12)
local resultData = string.gsub(resultData, "version \"", "")
local i, j = string.find(resultData, "\"")
local resultData = string.sub(resultData, 1, i - 1)
local githubVersion = string.gsub(resultData, "%.", "")
local fileVersion = string.gsub(GetResourceMetadata(expectedResourceName, "version", 0), "%.", "")
local githubVersion = tonumber(githubVersion)
local fileVersion = tonumber(fileVersion)
if not githubVersion and not fileVersion then
print("^1[^4" .. expectedResourceName .. "^1] WARNING^0")
print("You may not have the latest version of ^4" .. expectedResourceName .. "^0. A newer, improved version may be present at ^5" .. downloadLink .. "^0")
elseif githubVersion > fileVersion then
local oldVersion = string.sub(fileVersion, 1, 1) .. "." .. string.sub(fileVersion, 2, 2) .. "." .. string.sub(fileVersion, 3, 3)
local newVersion = string.sub(githubVersion, 1, 1) .. "." .. string.sub(githubVersion, 2, 2) .. "." .. string.sub(githubVersion, 3, 3)
print("^1[^4" .. expectedResourceName .. "^1] WARNING^0")
print("^4" .. expectedResourceName .. " ^0is outdated. Please update it from ^5" .. downloadLink .. " ^0| Current Version: ^1" .. oldVersion .. " ^0| New Version: ^2" .. newVersion .. " ^0|")
elseif githubVersion < fileVersion then
local oldVersion = string.sub(fileVersion, 1, 1) .. "." .. string.sub(fileVersion, 2, 2) .. "." .. string.sub(fileVersion, 3, 3)
local newVersion = string.sub(githubVersion, 1, 1) .. "." .. string.sub(githubVersion, 2, 2) .. "." .. string.sub(githubVersion, 3, 3)
print("^1[^4" .. expectedResourceName .. "^1] WARNING^0")
print("^4" .. expectedResourceName .. " ^0version number is higher than expected | Current Version: ^3" .. oldVersion .. " ^0| Expected Version: ^2" .. newVersion .. " ^0|")
else
local newVersion = string.sub(githubVersion, 1, 1) .. "." .. string.sub(githubVersion, 2, 2) .. "." .. string.sub(githubVersion, 3, 3)
print("^4" .. expectedResourceName .. " ^0is up to date | Current Version: ^2" .. newVersion .. " ^0|")
end
end)
end
2022-09-23 20:16:00 +02:00
NDCore.Functions.VersionChecker("ND_Core", GetCurrentResourceName(), "https://github.com/ND-Framework/ND_Framework", "https://raw.githubusercontent.com/ND-Framework/ND_Framework/main/ND_Core/fxmanifest.lua")