2023-12-07 15:45:48 -08:00
QBCore = exports [ 'qb-core' ]: GetCoreObject ()
-- Callbacks
2024-09-24 15:07:41 -07:00
lib.callback . register ( 'md-drugs:server:getDealers' , function ( data )
local check = MySQL.query . await ( 'SELECT * FROM dealers' , {})
local loc = json.encode ({ x = 899.98 , y = - 2603.24 , z = 6.11 , h = 176.12 })
if not check [ 1 ] then
MySQL.insert ( 'INSERT INTO dealers SET name = ?, coords = ?, time = ?, createdby = ?' , { 'MD' , loc , 'any' , 'code Newb' })
2024-09-25 11:46:40 -07:00
Wait ( 2000 )
local check2 = MySQL.query . await ( 'SELECT * FROM dealers' , {})
return check2
2023-12-07 15:45:48 -08:00
else
2024-09-24 15:07:41 -07:00
return check
2023-12-07 15:45:48 -08:00
end
end )
2024-11-12 01:04:42 -08:00
2023-12-07 15:45:48 -08:00
2024-09-24 15:07:41 -07:00
lib.callback . register ( 'md-drugs:server:GetDeliveryItem' , function ( data )
2023-12-07 15:45:48 -08:00
local src = source
2024-11-01 00:54:21 -07:00
local Player = getPlayer ( src )
2024-09-24 15:07:41 -07:00
local item = math.random ( 1 , # QBConfig.DeliveryItems )
local amount = math.random ( 1 , 4 )
local check = MySQL.query . await ( 'SELECT * FROM deliveriesdealer WHERE cid = ?' , { Player.PlayerData . citizenid })
local locnum = math.random ( 1 , # QBConfig.DeliveryLocations )
local coord = json.encode ({
x = math.floor ( QBConfig.DeliveryLocations [ locnum ]. coords.x * 100 ) / 100 ,
y = math.floor ( QBConfig.DeliveryLocations [ locnum ]. coords.y * 100 ) / 100 ,
z = math.floor ( QBConfig.DeliveryLocations [ locnum ]. coords.z * 100 ) / 100 ,
})
if not check [ 1 ] then
MySQL.insert ( 'INSERT INTO deliveriesdealer SET cid = ?, itemdata = ?, timestart = ?, maxtime = ?, location = ?' ,
{ Player.PlayerData . citizenid , json.encode ({ item = QBConfig.DeliveryItems [ item ]. item , amount = amount }), os.time (), os.time () + ( 15 * 60 ), coord })
AddItem ( Player.PlayerData . source , QBConfig.DeliveryItems [ item ]. item , amount )
return true , QBConfig.DeliveryItems [ item ]. item , amount , coord
else
local time = os.time ()
local timeout = math.floor ( os.difftime ( time , check [ 1 ]. maxtime ) / 60 )
if timeout > 15 then MySQL.query . await ( 'DELETE FROM deliveriesdealer WHERE cid = ?' , { Player.PlayerData . citizenid }) Notifys ( src , 'You Failed To Make It In Time!' , 'error' ) return false end
Notifys ( src , 'You Already Have A Delivery To Make' , 'error' ) return false , QBConfig.DeliveryItems [ item ]. item , amount , coord
end
2023-12-07 15:45:48 -08:00
end )
2024-09-24 15:07:41 -07:00
RegisterNetEvent ( 'md-drugs:server:giveDeliveryItems' , function ( item , amount )
2023-12-07 15:45:48 -08:00
local src = source
2024-11-01 00:54:21 -07:00
local Player = getPlayer ( src )
2024-09-24 15:07:41 -07:00
local check = MySQL.query . await ( 'SELECT * FROM deliveriesdealer WHERE cid = ?' , { Player.PlayerData . citizenid })
if not check [ 1 ] then return end
local time = os.time ()
local timeout = math.floor ( os.difftime ( time , check [ 1 ]. maxtime ) / 60 )
local itemData = json.decode ( check [ 1 ]. itemdata )
if timeout >= 15 then MySQL.query . await ( 'DELETE FROM deliveriesdealer WHERE cid = ?' , { Player.PlayerData . citizenid }) Notifys ( source , 'You Failed To Make It In Time!' , 'error' ) return end
if item ~= itemData.item then return end
if amount ~= itemData.amount then return end
if RemoveItem ( src , item , amount ) then
for k , v in pairs ( QBConfig.DeliveryItems ) do
if v.item == item then
local income = math.random ( v.payout . min , v.payout . max )
Player.Functions . AddMoney ( 'cash' , income )
AddRep ( src , 'dealerrep' )
2023-12-07 15:45:48 -08:00
end
end
2024-09-24 15:07:41 -07:00
MySQL.query . await ( 'DELETE FROM deliveriesdealer WHERE cid = ?' , { Player.PlayerData . citizenid })
2023-12-07 15:45:48 -08:00
end
end )
2024-11-12 17:08:02 -08:00
lib.addCommand ( "newdealer" , {
help = "Place a dealer (Admin Only)" ,
params = {
{ name = "name" , help = "Dealer Name" , type = "string" },
{ name = "min" , help = "Minimum Time" , type = "number" },
{ name = "max" , help = "Maximum Time" , type = "number" }
},
restricted = "admin"
}, function ( source , args )
2023-12-07 15:45:48 -08:00
local ped = GetPlayerPed ( source )
local coords = GetEntityCoords ( ped )
2024-11-01 00:54:21 -07:00
local Player = getPlayer ( source )
2023-12-07 15:45:48 -08:00
if not Player then return end
2024-11-12 17:08:02 -08:00
local dealerName = args.name
2023-12-07 15:45:48 -08:00
local pos = json.encode ({ x = coords.x , y = coords.y , z = coords.z })
2024-11-12 17:08:02 -08:00
local result = MySQL.scalar . await ( 'SELECT name FROM dealers WHERE name = ?' , { dealerName })
if result then
return Notifys ( source , "Already Exists" , "error" )
end
MySQL.insert ( 'INSERT INTO dealers (name, coords, time, createdby) VALUES (?, ?, ?, ?)' , {
2024-11-22 01:35:25 -08:00
dealerName , pos , 'nil' , Player.PlayerData . citizenid
2024-11-12 17:08:02 -08:00
}, function ()
2023-12-07 15:45:48 -08:00
QBConfig.Dealers [ dealerName ] = {
2024-11-12 17:08:02 -08:00
name = dealerName ,
coords = {
x = coords.x ,
y = coords.y ,
z = coords.z ,
h = GetEntityHeading ( ped )
2023-12-07 15:45:48 -08:00
},
}
TriggerClientEvent ( 'md-drugs:client:RefreshDealers' , - 1 , QBConfig.Dealers )
end )
2024-11-12 17:08:02 -08:00
end )
lib.addCommand ( "deletedealer" , {
help = "Delete a dealer (Admin Only)" ,
params = {
{ name = "name" , help = "Dealer Name" , type = "string" }
},
restricted = 'group.admin'
}, function ( source , args )
local dealerName = args.name
local result = MySQL.scalar . await ( 'SELECT * FROM dealers WHERE name = ?' , { dealerName })
2023-12-07 15:45:48 -08:00
if result then
2024-11-12 17:08:02 -08:00
MySQL.query ( 'DELETE FROM dealers WHERE name = ?' , { dealerName })
2023-12-07 15:45:48 -08:00
QBConfig.Dealers [ dealerName ] = nil
TriggerClientEvent ( 'md-drugs:client:RefreshDealers' , - 1 , QBConfig.Dealers )
2024-11-12 17:08:02 -08:00
TriggerClientEvent ( 'QBCore:Notify' , source , "Dealer Deleted" , "success" )
2023-12-07 15:45:48 -08:00
else
TriggerClientEvent ( 'QBCore:Notify' , source , "Who You Tryna Delete" , "error" )
end
2024-11-12 17:08:02 -08:00
end )