Files
md-drugs/server/functions.lua
T

531 lines
18 KiB
Lua
Raw Normal View History

2025-01-29 20:26:39 -08:00
local QBCore, ESX, qbx = {}, {}, nil
2024-05-02 18:34:49 -07:00
local notify = Config.Notify -- qb or ox
local inventory = Config.Inventory -- qb or ox
2025-01-29 20:26:39 -08:00
local invname = ''
------------------------------------------ logging stuff
2024-07-16 14:14:48 -07:00
local logs = false
local logapi = GetConvar("fivemerrLogs", "")
local endpoint = 'https://api.fivemerr.com/v1/logs'
local headers = {
2025-01-27 23:56:13 -08:00
['Authorization'] = logapi,
['Content-Type'] = 'application/json',
}
if Config.Framework == 'qb' then
QBCore = exports['qb-core']:GetCoreObject()
elseif Config.Framework == 'esx' then
ESX = exports['es_extended']:getSharedObject()
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
qbx = exports.qbx_core
2025-01-27 23:56:13 -08:00
end
2024-07-16 14:14:48 -07:00
2025-01-29 20:26:39 -08:00
2024-07-16 14:14:48 -07:00
CreateThread(function()
if logs then
print'^2 Logs Enabled for md-drugs'
if logapi == 'insert string here' then
print'^1 homie you gotta set your api on line 4'
else
print'^2 API Key Looks Good, Dont Trust Me Though, Im Not Smart'
end
else
print'^1 logs disabled for md-drugs'
end
end)
2024-07-16 14:32:04 -07:00
function Log(message, type)
2025-01-29 20:26:39 -08:00
if logs == false then return end
local buffer = { level = "info", message = message, resource = GetCurrentResourceName(),
metadata = { drugs = type, playerid = source}
2024-07-16 14:14:48 -07:00
}
2025-01-29 20:26:39 -08:00
SetTimeout(500, function()
2024-07-16 14:14:48 -07:00
PerformHttpRequest(endpoint, function(status, _, _, response)
if status ~= 200 then
if type(response) == 'string' then
response = json.decode(response) or response
end
end
end, 'POST', json.encode(buffer), headers)
buffer = nil
2025-01-29 20:26:39 -08:00
end)
2024-07-16 14:14:48 -07:00
end
2024-11-01 00:54:21 -07:00
------------------------------------ Player Stuff functions
2025-01-27 23:56:13 -08:00
function getPlayer(source)
2024-11-01 00:54:21 -07:00
local src = source
2025-01-27 23:56:13 -08:00
if Config.Framework == 'qb' then
local Player = QBCore.Functions.GetPlayer(src)
return Player
elseif Config.Framework == 'esx' then
local Player = ESX.GetPlayerFromId(src)
return Player
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
local Player = qbx:GetPlayer(source)
return Player
2025-01-27 23:56:13 -08:00
end
2024-11-01 00:54:21 -07:00
end
2025-01-28 20:15:32 -08:00
function getPlayerByCid(cid)
if Config.Framework == 'qb' then
local Player = QBCore.Functions.GetPlayerByCitizenId(cid)
return Player
elseif Config.Framework == 'esx' then
local Player = ESX.GetPlayerFromIdentifier(cid)
return Player
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
local Player = qbx:GetPlayerByCitizenId(cid)
return Player
2025-01-28 20:15:32 -08:00
end
end
2025-01-27 23:56:13 -08:00
function GetName(source)
local src = source
if Config.Framework == 'qb' then
local Player = getPlayer(src)
return Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
elseif Config.Framework == 'esx' then
local Player = getPlayer(src)
return Player.getName()
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
local Player = getPlayer(src)
return Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
2025-01-27 23:56:13 -08:00
end
2024-11-01 00:54:21 -07:00
end
2025-01-29 20:26:39 -08:00
function getCid(source)
2025-01-27 23:56:13 -08:00
local src = source
if Config.Framework == 'qb' then
local Player = getPlayer(src)
return Player.PlayerData.citizenid
elseif Config.Framework == 'esx' then
local Player = getPlayer(src)
return Player.getIdentifier()
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
local Player = getPlayer(src)
return Player.PlayerData.citizenid
2025-01-27 23:56:13 -08:00
end
end
2025-01-29 20:26:39 -08:00
2024-11-01 00:54:21 -07:00
function GetCoords(source)
local ped = GetPlayerPed(source)
local playerCoords = GetEntityCoords(ped)
return playerCoords
end
function dist(source, coords)
local playerPed = GetPlayerPed(source)
local pcoords = GetEntityCoords(playerPed)
local dist = #(pcoords - coords)
2025-01-29 20:26:39 -08:00
return dist
2024-11-01 00:54:21 -07:00
end
function CheckDist(source, coords)
local playerPed = GetPlayerPed(source)
local pcoords = GetEntityCoords(playerPed)
local ok
if #(pcoords - coords) < 4.0 then
return ok
2025-01-29 20:26:39 -08:00
else
2024-11-01 00:54:21 -07:00
DropPlayer(source, 'md-drugs: You Were A Total Of ' .. #(pcoords - coords) .. ' Too Far Away To Trigger This Event')
return false
end
end
function Notifys(source, text, type)
2024-07-21 21:45:06 -07:00
local src = source
2024-05-02 18:34:49 -07:00
if notify == 'qb' then
2024-07-21 21:45:06 -07:00
TriggerClientEvent("QBCore:Notify", src, text, type)
2024-05-02 18:34:49 -07:00
elseif notify == 'ox' then
2024-07-21 21:45:06 -07:00
lib.notify(src, { title = text, type = type})
2024-05-02 18:34:49 -07:00
elseif notify == 'okok' then
2024-07-21 21:45:06 -07:00
TriggerClientEvent('okokNotify:Alert', src, '', text, 4000, type, false)
2024-05-02 18:34:49 -07:00
else
print"^1 Look At The Config For Proper Alert Options"
end
end
2025-01-29 20:26:39 -08:00
function GetLabels(item)
if inventory == 'qb' then
return QBCore.Shared.Items[item].label
elseif inventory == 'ox' then
2024-10-01 17:41:24 -07:00
local items = exports.ox_inventory:Items()
return items[item].label
end
end
function CUI(item, useFunction)
2025-01-27 23:56:13 -08:00
if Config.Framework == 'qb' then
QBCore.Functions.CreateUseableItem(item, useFunction)
elseif Config.Framework == 'esx' then
ESX.RegisterUsableItem(item, useFunction)
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
qbx:CreateUseableItem(item, useFunction)
2025-01-27 23:56:13 -08:00
end
end
function Itemcheck(source, item, amount)
if inventory == 'qb' then
2024-11-01 00:54:21 -07:00
local Player = getPlayer(source)
local itemchecks = Player.Functions.GetItemByName(item)
if itemchecks and itemchecks.amount >= amount then
return true
else
Notifys(source, 'You Need ' .. amount .. ' Of ' .. GetLabels(item) .. ' To Do this', 'error')
return false
end
elseif inventory == 'ox' then
2025-01-27 23:56:13 -08:00
local items = exports.ox_inventory:GetItem(source, item, nil, false)
2024-10-01 17:41:24 -07:00
if items.count >= amount then
2025-01-27 23:56:13 -08:00
return true
else
Notifys(source, 'You Need ' .. amount .. ' Of ' .. GetLabels(item) .. ' To Do This', 'error')
return false
end
end
end
2024-10-01 17:41:24 -07:00
2025-01-27 23:56:13 -08:00
function hasItem(source, item, amount)
if inventory == 'qb' then
local Player = getPlayer(source)
local itemchecks = Player.Functions.GetItemByName(item)
if itemchecks and itemchecks.amount >= amount then
return true
else
return false
end
elseif inventory == 'ox' then
local items = exports.ox_inventory:GetItem(source, item, nil, false)
if items == nil then return false end
if items.count >= amount then
return true
else
return false
end
end
end
function getItemCount(source, item)
if inventory == 'qb' then
local Player = getPlayer(source)
local itemchecks = Player.Functions.GetItemByName(item)
if itemchecks then
return itemchecks.amount
else
return false
end
elseif inventory == 'ox' then
local items = exports.ox_inventory:GetItem(source, item, nil, false)
if items == nil then
print('You are missing ' .. item)
return 0
end
if items.count >= 1 then
return items.count
else
return 0
end
end
end
function RemoveItem(source, item, amount)
if inventory == 'qb' then
2024-11-01 00:54:21 -07:00
local Player = getPlayer(source)
if Player.Functions.RemoveItem(item, amount) then
TriggerClientEvent(invname ..":client:ItemBox", source, QBCore.Shared.Items[item], "remove", amount)
return true
2025-01-29 20:26:39 -08:00
else
Notifys(source, 'You Need ' .. amount .. ' Of ' .. GetLabels(item) .. ' To Do This', 'error')
end
elseif inventory == 'ox' then
if exports.ox_inventory:RemoveItem(source, item, amount) then
return true
else
Notifys(source, 'You Need ' .. amount .. ' Of ' .. GetLabels(item) .. ' To Do This', 'error')
end
2024-05-02 18:34:49 -07:00
end
end
function AddItem(source, item, amount)
if inventory == 'qb' then
2024-11-01 00:54:21 -07:00
local Player = getPlayer(source)
2025-01-27 23:56:13 -08:00
if Player.Functions.AddItem(item, amount) then
TriggerClientEvent(invname ..":client:ItemBox", source, QBCore.Shared.Items[item], "add", amount)
return true
2025-01-27 23:56:13 -08:00
else
print('Failed To Give Item: ' .. item .. ' Check Your qb-core/shared/items.lua')
return false
end
else
if exports.ox_inventory:AddItem(source, item, amount) then
return true
else
print('Failed To Give Item: ' .. item .. ' Check Your ox_inventory/data/items.lua')
return false
end
2024-05-02 18:34:49 -07:00
end
end
2025-01-27 23:56:13 -08:00
function addMoney(source, type, amount)
if Config.Framework == 'qb' then
local Player = getPlayer(source)
if Player.Functions.AddMoney(type, amount) then
return true
else
return false
end
elseif Config.Framework == 'esx' then
local Player = getPlayer(source)
if Player.addMoney(amount) then
return true
else
return false
end
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
if qbx:AddMoney(source, type, amount) then
return true
else
return false
end
2025-01-27 23:56:13 -08:00
end
end
2025-01-27 23:56:13 -08:00
function removeMoney(source, type, amount)
if Config.Framework == 'qb' then
local Player = getPlayer(source)
if Player.Functions.RemoveMoney(type, amount) then
return true
else
return false
end
elseif Config.Framework == 'esx' then
2025-01-28 20:15:32 -08:00
if type == 'bank' then
print('Does Not Support Bank Funds Removing From ESX Defaulted To Cash')
end
2025-01-27 23:56:13 -08:00
local Player = getPlayer(source)
2025-01-28 20:15:32 -08:00
if Player.getMoney() >= amount then
Player.removeMoney(amount)
2025-01-27 23:56:13 -08:00
return true
2025-01-28 20:15:32 -08:00
else
return false
end
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
if qbx:RemoveMoney(source, type, amount) then
return true
else
return false
end
2025-01-27 23:56:13 -08:00
end
end
local function handleFresh(source)
local table = json.encode({
coke = 0,lsd = 0,heroin = 0,dealerrep = 0,
cornerselling = { price = QBConfig.SellLevel[1].price,rep = 0,label = QBConfig.SellLevel[1].label,level = 1}
})
MySQL.insert('INSERT INTO drugrep SET cid = ?, drugrep = ?, name = ?', {getCid(source), table, GetName(source)})
Wait(1000)
return json.decode(table)
end
function getRep(source, type)
local sql = MySQL.query.await('SELECT * FROM drugrep WHERE cid = ?', {getCid(source)})
if not sql[1] then
2025-01-27 23:56:13 -08:00
local new = handleFresh(source)
return new[type]
2024-07-21 21:45:06 -07:00
else
2024-10-30 13:03:51 -07:00
local reps = json.decode(sql[1].drugrep)
local rep = ''
if reps.coke == nil then rep = reps[1] else rep = reps end
2025-01-27 23:56:13 -08:00
return rep[type]
end
end
2025-01-27 23:56:13 -08:00
function GetAllRep(source)
local sql = MySQL.query.await('SELECT * FROM drugrep WHERE cid = ?', {getCid(source)})
if not sql[1] then
2025-01-27 23:56:13 -08:00
local new = handleFresh(source)
return new
else
2024-10-30 13:03:51 -07:00
local rep = json.decode(sql[1].drugrep)
if rep.coke == nil then return rep[1] end
return rep
end
end
2024-07-21 21:45:06 -07:00
2025-01-29 20:26:39 -08:00
function AddRep(source, type, amount)
if not Config.TierSystem then return false end
2024-10-28 14:11:49 -07:00
if not amount then amount = 1 end
2025-01-27 23:56:13 -08:00
local sql = MySQL.query.await('SELECT * FROM drugrep WHERE cid = ?', {getCid(source)})
2024-10-30 13:03:51 -07:00
local reps = json.decode(sql[1].drugrep)
local update
local rep = ''
if reps.coke == nil then rep = reps[1] else rep = reps end
2024-10-28 14:11:49 -07:00
if type == 'cornerselling' then
for k, v in pairs (QBConfig.SellLevel) do
if rep.cornerselling.level == k then
if rep.cornerselling.rep + amount >= v.maxrep then
update = json.encode({coke = rep.coke, heroin = rep.heroin, lsd = rep.lsd, dealerrep = rep.dealerrep, cornerselling = {label = v.label, price = v.price, rep = rep.cornerselling.rep + amount, level = k + 1}})
else
update = json.encode({coke = rep.coke, heroin = rep.heroin, lsd = rep.lsd, dealerrep = rep.dealerrep, cornerselling = {label = v.label, price = v.price, rep = rep.cornerselling.rep + amount, level = k}})
end
end
2025-01-27 23:56:13 -08:00
end
if update == '' then return false end
MySQL.update('UPDATE drugrep SET drugrep = ? WHERE cid = ?', {update, getCid(source)})
return true
else
rep[type] = rep[type] + amount
MySQL.update('UPDATE drugrep SET drugrep = ? WHERE cid = ?', {json.encode(rep), getCid(source)})
return true
2024-07-21 21:45:06 -07:00
end
end
2024-11-05 00:46:12 -08:00
lib.addCommand('addCornerSellingTOREP', {
help = 'RUN THIS ONCE AND DELETE',
restricted = 'group.admin'
}, function(source, args, raw)
2024-10-30 13:03:51 -07:00
local sql = MySQL.query.await('SELECT * FROM drugrep', {})
for k, v in pairs (sql) do
local new = {}
local old = json.decode(v.drugrep)
local get = old[1] or old
table.insert(new, {
2025-01-29 20:26:39 -08:00
coke = get.coke,
lsd = get.lsd,
heroin = get.heroin,
dealerrep = get.dealerrep,
cornerselling = {
price = QBConfig.SellLevel[1].price,
rep = 0,
label = QBConfig.SellLevel[1].label,
level = 1
}
})
2024-10-30 13:03:51 -07:00
local news = json.encode(new)
MySQL.query.await('UPDATE drugrep SET drugrep = ? WHERE cid = ?', {news, v.cid})
end
2024-11-05 00:46:12 -08:00
end)
2024-10-30 13:03:51 -07:00
function sortTab(tbl, type)
table.sort(tbl, function(a, b)
return a[type] < b[type]
end)
end
2024-07-21 21:45:06 -07:00
function getCops()
local amount = 0
2025-01-29 12:59:49 -08:00
if Config.Framework == 'qb' then
local players = QBCore.Functions.GetQBPlayers()
for k, v in pairs(players) do
if v.PlayerData.job.type == 'leo' and v.PlayerData.job.onduty then
amount = amount + 1
end
end
return amount
elseif Config.Framework == 'esx' then
local players = ESX.GetPlayers()
for k, v in pairs(players) do
local xPlayer = ESX.GetPlayerFromId(v)
if xPlayer.job.name == 'police' and xPlayer.job.onduty then
amount = amount + 1
end
end
return amount
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
return qbx:GetDutyCountType('leo')
end
end
2025-01-28 20:15:32 -08:00
function getNear(src)
if Config.Framework == 'qb' then
local near = {}
local players = QBCore.Functions.GetPlayers()
for k, v in pairs (players) do
local targ = QBCore.Functions.GetPlayerByCitizenId(v.PlayerData.citizenid)
local tname = GetName(v.PlayerData.source)
local ped, tped = GetPlayerPed(src), GetPlayerPed(v.PlayerData.source)
local dist = #(GetEntityCoords(ped) - GetEntityCoords(tped))
if dist < 5.0 then
table.insert(near, {label = tname, value = targ.PlayerData.citizenid})
end
end
return near
elseif Config.Framework == 'esx' then
local near = {}
local players = ESX.GetPlayers()
for k, v in pairs (players) do
local targ = ESX.GetPlayerFromId(v)
local tname = GetName(v)
local ped, tped = GetPlayerPed(src), GetPlayerPed(v)
local dist = #(GetEntityCoords(ped) - GetEntityCoords(tped))
if dist < 5.0 then
table.insert(near, {label = tname, value = getCid(v)})
end
end
return near
2025-01-29 20:26:39 -08:00
elseif Config.Framework == 'qbx' then
local near = {}
local players = qbx:GetPlayers()
for k, v in pairs (players) do
local targ = qbx:GetPlayer(v)
local tname = GetName(v)
local ped, tped = GetPlayerPed(src), GetPlayerPed(v)
local dist = #(GetEntityCoords(ped) - GetEntityCoords(tped))
if dist < 5.0 then
table.insert(near, {label = tname, value = getCid(v)})
end
end
return near
2025-01-28 20:15:32 -08:00
end
end
2025-01-29 20:26:39 -08:00
function handleCornersell(source, item, amount, price)
2025-01-27 23:56:13 -08:00
if RemoveItem(source, item, amount) then
if inventory == 'qb' then
local Player = getPlayer(source)
if QBConfig.MarkedBills == true then
Player.Functions.AddItem('markedbills',1,false, {worth = price})
return true
end
if QBConfig.CustomDirtyMoney == true then
AddItem(source, QBConfig.CustomDirtyMoneyitem,price)
return true
end
Player.Functions.AddMoney('cash', price)
elseif inventory == 'ox' then
AddItem(source, 'black_money', price)
return true
end
2025-01-29 20:26:39 -08:00
else
return false
2025-01-27 23:56:13 -08:00
end
end
2024-07-11 17:39:58 -07:00
lib.callback.register('md-drugs:server:GetCoppers', function(source, cb, args)
2025-01-29 20:26:39 -08:00
return getCops()
2024-05-02 18:34:49 -07:00
end)
lib.callback.register('md-drugs:server:GetRep', function(source, cb, args)
local rep = GetAllRep(source)
return rep
end)
2024-05-02 20:39:27 -07:00
CreateThread(function()
2025-01-29 20:26:39 -08:00
if GetResourceState('ps-inventory') == 'started' then
invname = 'ps-inventory'
elseif GetResourceState('qb-inventory') == 'started' then
invname = 'qb-inventory'
else
invname = 'inventory'
2024-05-02 18:34:49 -07:00
end
local url = "https://raw.githubusercontent.com/Mustachedom/md-drugs/main/version.txt"
2024-09-24 18:42:02 -07:00
local version = GetResourceMetadata('md-drugs', "version" )
2024-05-02 18:34:49 -07:00
PerformHttpRequest(url, function(err, text, headers)
if (text ~= nil) then
print('^2 Your Version:' .. version .. ' | Current Version:' .. text .. '' )
end
end, "GET", "", "")
2025-01-29 20:26:39 -08:00
if GetResourceState('ox_lib') ~= 'started' then
print('^1 ox_lib Is A Depndancy, Not An Optional ')
end
2024-11-25 01:21:53 -08:00
end)