Files
md-drugs/server/functions.lua
T

134 lines
4.9 KiB
Lua
Raw Normal View History

2025-07-14 01:34:58 -07:00
------------------------------------------ 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-07-14 01:34:58 -07:00
['Authorization'] = logapi,
['Content-Type'] = 'application/json',
}
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}
})
2025-07-15 02:21:34 -07:00
MySQL.insert('INSERT INTO drugrep SET cid = ?, drugrep = ?, name = ?', {ps.getIdentifier(source), table, ps.getPlayerName(source)})
2025-07-14 01:34:58 -07:00
return json.decode(table)
end
2025-07-14 01:34:58 -07:00
function getRep(source, type)
2025-07-15 02:21:34 -07:00
local sql = MySQL.query.await('SELECT JSON_UNQUOTE(JSON_EXTRACT(drugrep, ?)) AS rep FROM drugrep WHERE cid = ?', {'$.' .. type, ps.getIdentifier(source)})
if not sql[1] then
2025-07-14 01:34:58 -07:00
local new = handleFresh(source)
return new[type]
2024-07-21 21:45:06 -07:00
else
2025-07-15 02:21:34 -07:00
return tonumber(sql[1].rep)
end
end
2025-07-14 01:34:58 -07:00
function GetAllRep(source)
2025-07-15 02:21:34 -07:00
local sql = MySQL.query.await('SELECT * FROM drugrep WHERE cid = ?', {ps.getIdentifier(source)})
if not sql[1] then
2025-07-14 01:34:58 -07: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-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
2025-07-17 02:06:17 -05:00
local sql = MySQL.query.await('SELECT * FROM drugrep WHERE cid = ?', {ps.getIdentifier(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-07-14 01:34:58 -07:00
end
if update == '' then return false end
2025-07-17 02:06:17 -05:00
MySQL.update('UPDATE drugrep SET drugrep = ? WHERE cid = ?', {update, ps.getIdentifier(source)})
2025-07-14 01:34:58 -07:00
return true
else
rep[type] = rep[type] + amount
2025-07-17 02:06:17 -05:00
MySQL.update('UPDATE drugrep SET drugrep = ? WHERE cid = ?', {json.encode(rep), ps.getIdentifier(source)})
2025-07-14 01:34:58 -07:00
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-07-14 01:34:58 -07: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
2025-07-14 01:34:58 -07:00
function handleCornersell(source, item, amount, price)
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
else
return false
2024-05-02 18:34:49 -07:00
end
2025-07-14 01:34:58 -07:00
end
lib.callback.register('md-drugs:server:GetCoppers', function(source, cb, args)
return getCops()
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)
local rep = GetAllRep(source)
return rep
end)
2025-07-17 02:06:17 -05:00
ps.versionCheck('md-drugs', 'https://raw.githubusercontent.com/Mustachedom/md-drugs/refs/heads/hushhushmotherfucker/version.txt', 'https://github.com/Mustachedom/md-drugs/')