Files
red-valley/resources/[framework]/[base]/[jobs]/mBossmenu/server/util.lua

704 lines
26 KiB
Lua
Raw Normal View History

2026-03-29 21:41:17 +03:00
function ExecuteSql(query, parameters)
local IsBusy = true
local result = nil
if Config.SQL == "oxmysql" then
if parameters then
exports.oxmysql:execute(query, parameters, function(data)
result = data
IsBusy = false
end)
else
exports.oxmysql:execute(query, function(data)
result = data
IsBusy = false
end)
end
elseif Config.SQL == "ghmattimysql" then
if parameters then
exports.ghmattimysql:execute(query, parameters, function(data)
result = data
IsBusy = false
end)
else
exports.ghmattimysql:execute(query, {}, function(data)
result = data
IsBusy = false
end)
end
elseif Config.SQL == "mysql-async" then
if parameters then
MySQL.Async.fetchAll(query, parameters, function(data)
result = data
IsBusy = false
end)
else
MySQL.Async.fetchAll(query, {}, function(data)
result = data
IsBusy = false
end)
end
end
while IsBusy do
Citizen.Wait(0)
end
return result
end
function GetPlayerRPName(source)
if Config.Framework == "oldesx" or Config.Framework == "esx" then
local xPlayer = Core.GetPlayerFromId(tonumber(source))
if xPlayer then
return xPlayer.getName()
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
local Player = Core.Functions.GetPlayer(tonumber(source))
if Player then
return Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
end
end
return GetPlayerName(source)
end
function GetPlayerInventory(source)
local data = {}
local Player = GetPlayer(source)
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
for _, v in pairs(Player.getInventory()) do
local amount = v.count or v.amount
if v and tonumber(amount) > 0 and not CheckBlacklistItem(v.name) then
local formattedData = v
formattedData.name = string.lower(v.name)
formattedData.label = v.label
formattedData.amount = amount
formattedData.image = v.image or (string.lower(v.name) .. '.png')
local metadata = v.metadata or v.info
if not metadata or next(metadata) == nil then
metadata = false
end
formattedData.metadata = metadata
table.insert(data, formattedData)
end
end
else
for _, v in pairs(Player.PlayerData.items) do
if v then
local amount = v.count or v.amount
if tonumber(amount) > 0 and not CheckBlacklistItem(v.name) then
local formattedData = v
formattedData.name = string.lower(v.name)
formattedData.label = v.label
formattedData.amount = amount
formattedData.image = v.image or (string.lower(v.name) .. '.png')
local metadata = v.metadata or v.info
if not metadata or next(metadata) == nil then
metadata = false
end
formattedData.metadata = metadata
table.insert(data, formattedData)
end
end
end
end
return data
end
function RemoveItem(src, item, amount)
local Player = GetPlayer(src)
if Player then
if Config.Inventory == "qb_inventory" then
Player.Functions.RemoveItem(item, amount)
elseif Config.Inventory == "esx_inventory" then
Player.removeInventoryItem(item, amount)
elseif Config.Inventory == "ox_inventory" then
exports.ox_inventory:RemoveItem(src, item, amount)
elseif Config.Inventory == "codem-inventory" then
Player.Functions.RemoveItem(item, amount)
elseif Config.Inventory == "qs_inventory" then
exports['qs-inventory']:RemoveItem(src, item, amount)
end
end
end
function AddItem(src, item, amount, slot, info)
local Player = GetPlayer(src)
if Player then
if Config.Inventory == "qb_inventory" then
Player.Functions.AddItem(item, amount, slot, info)
elseif Config.Inventory == "esx_inventory" then
Player.addInventoryItem(item, amount, slot, info)
elseif Config.Inventory == "ox_inventory" then
exports.ox_inventory:AddItem(src, item, amount, info)
elseif Config.Inventory == "codem-inventory" then
Player.Functions.AddItem(item, amount, slot, info)
elseif Config.Inventory == "qs_inventory" then
exports['qs-inventory']:AddItem(src, item, amount, slot or nil, info)
end
end
end
function CheckBlacklistItem(item)
for _, v in pairs(Config.BlacklistedItems) do
if v == item then
return true
end
end
return false
end
function WaitCore()
while not Core do
Wait(0)
end
end
function SetJob(src, job, grade)
local Player = GetPlayer(src)
if Player then
local newGrade = grade or 0
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
if Core.DoesJobExist(job, newGrade) then
Player.setJob(job, newGrade)
end
else
if Core.Shared.Jobs[job] and Core.Shared.Jobs[job].grades[tostring(newGrade)] then
Player.Functions.SetJob(job, newGrade)
end
end
end
end
function SetJobByIdentifier(identifier, jobb, grade)
local Player = GetPlayerFromIdentifier(identifier)
if Player then
local newGrade = grade or 0
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
Player.setJob(tostring(jobb), tonumber(newGrade))
else
if Core.Shared.Jobs[jobb] and Core.Shared.Jobs[jobb].grades[tostring(newGrade)] then
Player.Functions.SetJob(jobb, newGrade)
end
end
else
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
local newGrade = grade or 0
ExecuteSql("UPDATE users SET `job` = '" ..
jobb .. "', `job_grade` = '" .. grade .. "' WHERE `identifier` = '" .. identifier .. "'")
ExecuteSql("UPDATE `mboss_employees` SET `rankLabel` = ?, `rankLevel` = ? WHERE `identifier` = ?",
{ tostring(jobb), tonumber(newGrade), identifier })
else
local newGrade = grade or 0
local result = ExecuteSql("SELECT job FROM `players` WHERE `citizenid` = ?", { identifier })
if result then
local targetJob = json.decode(result[1].job)
targetJob.name = jobb
targetJob.payment = Core.Shared.Jobs[jobb].grades[tostring(newGrade)].payment
targetJob.grade = {
name = Core.Shared.Jobs[jobb].grades[tostring(newGrade)].name,
level = tonumber(
newGrade)
}
ExecuteSql("UPDATE `mboss_employees` SET `rankLabel` = ?, `rankLevel` = ? WHERE `identifier` = ?",
{ tostring(Core.Shared.Jobs[jobb].grades[tostring(newGrade)].name), tonumber(newGrade), identifier })
ExecuteSql("UPDATE `players` SET `job` = ? WHERE `citizenid` = ?", { json.encode(targetJob), identifier })
end
end
end
end
RegisterServerEvent("mBossmenu:setRank")
AddEventHandler("mBossmenu:setRank", function(identifier, rankLevel, name)
local src = source
local targetJob, targetJobLevel = GetJobByIdentifier(identifier)
local job, jobLevel = GetJob(src)
local targetPlayer = GetPlayerFromIdentifier(identifier)
local Player = GetPlayer(src)
local company, employee, employeeI = GetPlayerCompanyByIdentifier(identifier)
if identifier == (Player.identifier or Player.PlayerData.identifier) then
return
end
if company and CheckPlayerJob(src, targetJob) and tonumber(jobLevel) >= tonumber(targetJobLevel) then
local playerName = GetPlayerRPName(src)
SetJobByIdentifier(identifier, name, rankLevel)
CreateLog(job, playerName .. " changed " .. employee.name .. " rank to " .. rankLevel, "general",
Player.identifier)
TriggerClientEvent("mBossmenu:sendNotification", src, "Successfully changed rank")
SyncCompanyByKey(job, "employees")
end
end)
RegisterServerEvent("mBossmenu:fire")
AddEventHandler("mBossmenu:fire", function(identifier)
local src = source
local targetJob, targetJobLevel = GetJobByIdentifier(identifier)
local srcIdentifier = GetIdentifier(src)
local job, jobLevel = GetJob(src)
local targetPlayer = GetPlayerFromIdentifier(identifier)
local Player = GetPlayer(src)
local company, employee, employeeI = GetPlayerCompanyByIdentifier(identifier)
if identifier == (Player.identifier or Player.PlayerData.identifier or srcIdentifier) then
TriggerClientEvent("mBossmenu:sendNotification", src, "You can't fire yourself.")
return
end
if company and ((CheckPlayerJob(src, targetJob) and jobLevel > targetJobLevel) or CheckIsAdmin(src)) then
if not HasPermission(src, 'fireEmployee') and not CheckIsAdmin(src) then
TriggerClientEvent("mBossmenu:sendNotification", src, "You don't have permission.")
return
end
if targetPlayer then
local targetSrc = (Config.Framework == 'esx' or Config.Framework == 'oldesx') and targetPlayer.source or
targetPlayer.PlayerData.source
SetJob(targetSrc, "unemployed", 0)
else
SetJobByIdentifier(identifier, "unemployed", 0)
end
local playerName = GetPlayerRPName(src)
CreateLog(job, playerName .. " fired " .. employee.name, "general", Player.identifier)
TriggerClientEvent("mBossmenu:sendNotification", src, "You've fired " .. employee.name)
table.remove(company.employees, employeeI)
ExecuteSql("DELETE FROM `mboss_employees` WHERE `identifier` ='" .. identifier .. "'")
SyncCompany(job)
end
end)
RegisterServerEvent("mBossmenu:SelectRank")
AddEventHandler("mBossmenu:SelectRank", function(id, grade)
local src = source
local job, jobLevel = GetJob(src)
local Player = GetPlayer(src)
local targetPlayer = GetPlayer(tonumber(id))
local company = GetCompanyByName(job)
if targetPlayer and company and tonumber(company.bossrank) >= tonumber(jobLevel) and company.company == job then
if not HasPermission(src, 'recruitNewEmployee') then
TriggerClientEvent("mBossmenu:sendNotification", src, "You don't have permission.")
return
end
SetJob(tonumber(id), job, grade)
local playerName = GetPlayerRPName(src)
CreateLog(job, playerName .. " hired " .. GetPlayerRPName(tonumber(id)), "general", Player.identifier)
TriggerClientEvent("mBossmenu:sendNotification", src, "Successfully hired player")
SyncCompanyByKey(job, "employees")
end
end)
function RegisterCallback(name, cbFunc)
WaitCore()
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
Core.RegisterServerCallback(name, function(source, cb, data)
cbFunc(source, cb, data)
end)
else
Core.Functions.CreateCallback(name, function(source, cb, data)
cbFunc(source, cb, data)
end)
end
end
local Avatars = {}
local FormattedToken = "Bot " .. botToken
function DiscordRequest(method, endpoint, jsondata)
local data = nil
PerformHttpRequest("https://discordapp.com/api/" .. endpoint, function(errorCode, resultData, resultHeaders)
data = { data = resultData, code = errorCode, headers = resultHeaders }
end, method, #jsondata > 0 and json.encode(jsondata) or "",
{ ["Content-Type"] = "application/json", ["Authorization"] = FormattedToken })
while data == nil do
Citizen.Wait(0)
end
return data
end
function GetIdentifier(source)
local Player = GetPlayer(source)
if Player then
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
return Player.getIdentifier()
else
return Player.PlayerData.citizenid
end
end
end
function RemoveSocietyMoney(job, givenAmount)
if Config.Framework == "esx" or Config.Framework == "oldesx" then
if job then
local accountName = 'society_' .. job
TriggerEvent("esx_addonaccount:getSharedAccount", 'society_' .. job, function(account)
if account then
if account.money >= tonumber(givenAmount) then
local currentAmount = account.money
newAmount = currentAmount - givenAmount
account.removeMoney(givenAmount)
Wait(500)
return tonumber(newAmount) or 0
else
print('ERROR SOCIETY MONEY IS NOT ENOUGH')
return false
end
else
return false
end
end)
return newAmount or 0
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
if job then
if Config.newManagementSystem then
local accountName = job
local account_money = exports["qb-banking"]:GetAccount(accountName)
if account_money.account_balance >= givenAmount then
if givenAmount < 0 then
print('ERROR SOCIETY MONEY IS NOT ENOUGH')
return false
else
exports["qb-banking"]:RemoveMoney(job, givenAmount)
Wait(150)
local newAccountMoney = exports["qb-banking"]:GetAccount(job)
return newAccountMoney
end
return false
end
else
local accountName = job
local societyAccountMoney = ExecuteSql("SELECT * FROM `management_funds` WHERE `job_name` = '" ..
accountName .. "'")
if societyAccountMoney and societyAccountMoney[1] then
local currentAmount = societyAccountMoney[1].amount
local newAmount = currentAmount - givenAmount
if newAmount < 0 then
print('ERROR SOCIETY MONEY IS NOT ENOUGH')
return false
else
ExecuteSql("UPDATE `management_funds` SET `amount` = ? WHERE `job_name` = ?",
{ newAmount, accountName })
return newAmount
end
else
return false
end
end
end
end
end
exports('RemoveSocietyMoney', RemoveSocietyMoney)
function GetPlayerMoney(source, value)
local Player = GetPlayer(source)
if Player then
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
if value == 'bank' then
return Player.getAccount('bank').money
end
if value == 'cash' then
return Player.getMoney()
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
if value == 'bank' then
return Player.PlayerData.money['bank']
end
if value == 'cash' then
return Player.PlayerData.money['cash']
end
end
end
end
function RemoveMoney(source, type, value)
local Player = GetPlayer(source)
if Player then
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
if type == 'bank' then
Player.removeAccountMoney('bank', value)
end
if type == 'cash' then
Player.removeMoney(value)
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
if type == 'bank' then
Player.Functions.RemoveMoney('bank', value)
end
if type == 'cash' then
Player.Functions.RemoveMoney('cash', value)
end
end
end
end
function AddMoneyP(source, type, value)
local Player = GetPlayer(source)
if Player then
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
if type == 'bank' then
Player.addAccountMoney('bank', value)
end
if type == 'cash' then
Player.addMoney(value)
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
if type == 'bank' then
Player.Functions.AddMoney('bank', value)
end
if type == 'cash' then
Player.Functions.AddMoney('cash', value)
end
end
end
end
function GetSocietyMoney(job)
if Config.Framework == "esx" or Config.Framework == "oldesx" then
if job then
TriggerEvent("esx_addonaccount:getSharedAccount", 'society_' .. job, function(account)
money = tonumber(account.money)
end)
return money
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
if job then
local accountName = job
if Config.newManagementSystem then
local account_money = exports["qb-banking"]:GetAccount(accountName)
if account_money and account_money.account_balance then
return account_money.account_balance
else
if Config.CreateJobAccount then
exports["qb-banking"]:CreateJobAccount(accountName, 0)
return 0
end
end
else
local societyAccountMoney = ExecuteSql("SELECT * FROM `management_funds` WHERE `job_name` = '" ..
accountName .. "'")
if next(societyAccountMoney) then
return societyAccountMoney[1].amount
else
return false
end
end
end
end
end
exports('GetSocietyMoney', GetSocietyMoney)
function AddSocietyMoney(job, givenAmount)
if Config.Framework == "esx" or Config.Framework == "oldesx" then
if job then
local accountName = 'society_' .. job
TriggerEvent("esx_addonaccount:getSharedAccount", 'society_' .. job, function(account)
account.addMoney(givenAmount)
Wait(500)
newAmount = account.money
end)
return newAmount
end
elseif Config.Framework == 'qb' or Config.Framework == 'oldqb' then
if job then
local accountName = job
if Config.newManagementSystem then
local account_money = exports["qb-banking"]:GetAccount(accountName)
if account_money.account_balance then
if givenAmount < 0 then
print('ERROR SOCIETY MONEY IS NOT ENOUGH')
return false
else
exports["qb-banking"]:AddMoney(job, givenAmount)
end
end
else
local societyAccountMoney = ExecuteSql("SELECT * FROM `management_funds` WHERE `job_name` = '" ..
accountName .. "'")
if societyAccountMoney and societyAccountMoney[1] then
local currentAmount = societyAccountMoney[1].amount
local newAmount = currentAmount + givenAmount
if newAmount < 0 then
print('ERROR SOCIETY MONEY IS NOT ENOUGH')
return false
else
ExecuteSql("UPDATE `management_funds` SET `amount` = ? WHERE `job_name` = ?",
{ newAmount, accountName })
return newAmount
end
else
print('ERROR SOCIETY NOT FOUND')
return false
end
end
end
end
end
exports('AddSocietyMoney', AddSocietyMoney)
function removeCompanyInventory(src, item, amount)
local job = GetJob(src)
local result = ExecuteSql("SELECT `inventory` FROM `mboss_inventory` WHERE `jobname` = '" .. job .. "'")
if result then
local inventory = json.decode(result[1].inventory)
for k, v in pairs(inventory) do
if v.name == item then
v.amount = v.amount - amount
if v.amount <= 0 then
table.remove(inventory, k)
end
break
end
end
ExecuteSql("UPDATE `mboss_inventory` SET `inventory` = '" ..
json.encode(inventory) .. "' WHERE `jobname` = '" .. job .. "'")
end
end
function GetDiscordAvatar(user)
local discordId = nil
local imgURL = nil;
for _, id in ipairs(GetPlayerIdentifiers(user)) do
if string.match(id, "discord:") then
discordId = string.gsub(id, "discord:", "")
break
end
end
if discordId then
if Avatars[discordId] == nil then
local endpoint = ("users/%s"):format(discordId)
local member = DiscordRequest("GET", endpoint, {})
if member.code == 200 then
local data = json.decode(member.data)
if data ~= nil and data.avatar ~= nil then
if (data.avatar:sub(1, 1) and data.avatar:sub(2, 2) == "_") then
imgURL = "https://media.discordapp.net/avatars/" .. discordId .. "/" .. data.avatar .. ".gif";
else
imgURL = "https://media.discordapp.net/avatars/" .. discordId .. "/" .. data.avatar .. ".png"
end
end
else
return Config.DefaultImage
end
Avatars[discordId] = imgURL;
else
imgURL = Avatars[discordId];
end
end
if imgURL == nil or imgURL == false then
imgURL = Config.DefaultImage
end
return imgURL;
end
function CheckIsAdmin(source)
if source == 0 then
return false
end
if Config.Framework == 'esx' or Config.Framework == 'oldesx' then
local Player = Core.GetPlayerFromId(source)
if Player then
for _, v in pairs(Config.Admins) do
if Player.getGroup() == v then
return true
end
end
end
elseif Config.Framework == 'qb' then
for _, v in pairs(Config.Admins) do
if Core.Functions.HasPermission(source, v) or IsPlayerAceAllowed(source, 'command') then
return true
end
end
elseif Config.Framework == 'oldqb' then
for _, v in pairs(Config.Admins) do
if Core.Functions.GetPermission(source) == v then
return true
end
end
end
return false
end
function GetSteamPP(source)
local idf = nil
for k, v in pairs(GetPlayerIdentifiers(source)) do
if string.sub(v, 1, string.len("steam:")) == "steam:" then
idf = v
end
end
local avatar = "./assets/images/testpp.png"
if idf == nil then
return avatar
end
local callback = promise:new()
PerformHttpRequest('http://steamcommunity.com/profiles/' .. tonumber(GetIDFromSource('steam', idf), 16) .. '/?xml=1',
function(Error, Content, Head)
local SteamProfileSplitted = stringsplit(Content, '\n')
if SteamProfileSplitted ~= nil and next(SteamProfileSplitted) ~= nil then
for i, Line in ipairs(SteamProfileSplitted) do
if Line:find('<avatarFull>') then
callback:resolve(Line:gsub('<avatarFull><!%[CDATA%[', ''):gsub(']]></avatarFull>', ''))
for k, v in pairs(callback) do
return callback.value
end
break
end
end
end
end)
return Citizen.Await(callback)
end
-- Add Money Export
function AddMoney(job, amount)
local company = GetCompanyByName(job)
if company and company.vaultDisabled == 1 then
TriggerClientEvent("mBossmenu:sendNotification", src, "Vault is disabled.")
return
end
if amount > 0 then
AddSocietyMoney(job, amount)
local newMoney = GetPlayerMoney(src, 'bank')
local newcompanyMoney = GetSocietyMoney(job) or 0
local dataInfo = {
money = newMoney,
companyMoney = newcompanyMoney
}
local moneyLog = {
from = 'Server',
to = GetCompanyByName(job).companylabel,
identifier = 'Server',
date = os.date('%Y.%m.%d %H:%M'),
amount = amount,
type = '+',
avatar = Config.DefaultImage
}
addMoneyLogSystem(job, moneyLog)
company.vault_income = company.vault_income + amount
ExecuteSql("UPDATE mboss_general SET `vault_income` = '" ..
company.vault_income .. "' WHERE `company` = '" .. company.company .. "'")
SyncCompanyByKey(company.company, "vault_income")
print("Added money to company: " .. job .. " amount: " .. amount)
end
end
function addMoneyLogSystem(job, data)
local insertData = jobMoneyLog[job]
if not insertData then
insertData = {
companyName = job,
message = {}
}
end
table.insert(insertData.message, data)
jobMoneyLog[job] = insertData
saveJobData(insertData)
end