Files
md-drugs/server/functions.lua
T

136 lines
4.2 KiB
Lua
Raw Normal View History

local timeOutPlayers = {}
2026-01-16 02:57:26 -07:00
local playerRep = {}
2026-01-16 02:57:26 -07:00
function checkDistance(src, coords, dist)
local pcoords = GetEntityCoords(GetPlayerPed(src))
coords = vector3(coords.x, coords.y, coords.z)
if #(pcoords - coords) > dist then
return false
end
return true
end
local function timeOutThread(src)
2026-01-16 02:57:26 -07:00
local id = Bridge.Framework.GetPlayerIdentifier(src)
CreateThread(function()
2026-01-16 02:57:26 -07:00
timeOutPlayers[id] = true
Wait(3000)
2026-01-16 02:57:26 -07:00
timeOutPlayers[id] = nil
end)
end
function timeOut(src, event)
2026-01-16 02:57:26 -07:00
if timeOutPlayers[src] then
local first, last = Bridge.Framework.GetPlayerName(src)
Bridge.Prints.Warn(Bridge.Language.Locale('Catches.onCooldownWarn', first .. ' ' .. last, event))
return true
end
timeOutThread(src)
return false
end
2026-01-16 02:57:26 -07:00
function craft(source, recipe)
local need, have, oops = 0,0,{}
for item, amount in pairs (recipe.take) do
2025-08-09 03:10:00 -06:00
need = need + 1
2026-01-16 02:57:26 -07:00
local count = Bridge.Inventory.GetItemCount(source, item)
if count >= amount then
2025-08-09 03:10:00 -06:00
have = have + 1
else
2026-01-16 02:57:26 -07:00
table.insert(oops, 'You Need ' .. amount .. ' X ' .. Bridge.Inventory.GetItemInfo(item).label)
2025-08-09 03:10:00 -06:00
end
end
2026-01-16 02:57:26 -07:00
if have ~= need then
for index, text in pairs (oops) do
Bridge.Notify.SendNotify(source, text, 'error')
end
return false
2025-08-09 03:10:00 -06:00
end
2026-01-16 02:57:26 -07:00
for item, amount in pairs (recipe.take) do
if not Bridge.Inventory.RemoveItem(source, item, amount) then
return false
end
end
for item, amount in pairs (recipe.give) do
Bridge.Inventory.AddItem(source, item, amount)
end
return true
2025-08-09 03:10:00 -06:00
end
2025-07-14 01:34:58 -07:00
local function handleFresh(source)
2026-01-16 02:57:26 -07:00
local id = Bridge.Framework.GetPlayerIdentifier(source)
local player = {
coke = 0,
lsd = 0,
heroin = 0,
dealerrep = 0,
cornerselling = {
price = QBConfig.SellLevel[1].price,
rep = 0,
label = QBConfig.SellLevel[1].label,
level = 1
}
}
playerRep[id] = player
local first, last = Bridge.Framework.GetPlayerName(source)
MySQL.insert('INSERT INTO drugrep SET cid = ?, drugrep = ?, name = ?', {id, player, first .. ' ' .. last})
end
local function loadRep(source)
local id = Bridge.Framework.GetPlayerIdentifier(source)
local hasRep = MySQL.query.await('SELECT * FROM drugrep WHERE cid = ?', {id})
if hasRep and not hasRep[1] then
handleFresh(source)
end
if hasRep and hasRep[1] then
playerRep[id] = json.decode(hasRep[1].drugrep)
end
2025-07-14 01:34:58 -07:00
end
2025-07-14 01:34:58 -07:00
function getRep(source, type)
2026-01-16 02:57:26 -07:00
local id = Bridge.Framework.GetPlayerIdentifier(source)
if not playerRep[id] then
loadRep(source)
end
2026-01-16 02:57:26 -07:00
return playerRep[id][type]
end
2025-07-14 01:34:58 -07:00
function GetAllRep(source)
2026-01-16 02:57:26 -07:00
local id = Bridge.Framework.GetPlayerIdentifier(source)
if not playerRep[id] then
loadRep(source)
end
2026-01-16 02:57:26 -07:00
return playerRep[id]
end
2024-07-21 21:45:06 -07:00
2025-07-14 01:34:58 -07: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
2026-01-16 02:57:26 -07:00
local id = Bridge.Framework.GetPlayerIdentifier(source)
if not playerRep[id] then
loadRep(source)
end
2024-10-28 14:11:49 -07:00
if type == 'cornerselling' then
2026-01-16 02:57:26 -07:00
local rep = playerRep[id].cornerselling
local cornerData = QBConfig.SellLevel[rep.level]
if rep.rep + amount >= cornerData.maxrep then
rep.level = rep.level + 1
rep.label = QBConfig.SellLevel[rep.level].label
rep.price = QBConfig.SellLevel[rep.level].price
2025-07-14 01:34:58 -07:00
end
2026-01-16 02:57:26 -07:00
rep.rep = rep.rep + amount
2025-07-14 01:34:58 -07:00
else
2026-01-16 02:57:26 -07:00
playerRep[id][type] = playerRep[id][type] + amount
2024-07-21 21:45:06 -07:00
end
2026-01-16 02:57:26 -07:00
MySQL.query.await('UPDATE drugrep SET drugrep = ? WHERE cid = ?', {json.encode(playerRep[id]), id})
2024-07-21 21:45:06 -07:00
end
2026-01-16 02:57:26 -07:00
-- TODO: Callbacks
2025-08-09 03:10:00 -06:00
ps.registerCallback('md-drugs:server:GetCoppers', function(source, cb, args)
2026-01-16 02:57:26 -07:00
return Bridge.Framework.GetPlayersByJob('police')
2024-05-02 18:34:49 -07:00
end)
2025-07-17 02:06:17 -05:00
ps.registerCallback('md-drugs:server:GetRep', function(source, cb, args)
2025-08-09 03:10:00 -06:00
local rep = GetAllRep(source)
return rep
end)
2026-01-16 02:57:26 -07:00
--TODO: fix versioncheck
Bridge.Version.VersionChecker('Mustachedom/md-drugs/', false)