Scripts/resources/[hp]/hp_gv/server/server.lua

178 lines
5.7 KiB
Lua
Raw Normal View History

2024-12-29 19:48:41 +00:00
QBCore = exports['qb-core']:GetCoreObject()
math.randomseed(os.time())
2024-12-30 10:15:34 +00:00
GlobalState.ActiveVan = -1
GlobalState.ActiveItems = {}
GlobalState.LastVan = os.time()
GlobalState.totalVans = #Config.Gunvans.vehicles
2024-12-29 19:48:41 +00:00
2024-12-30 10:15:34 +00:00
-- Fordi laver du ActiveItems = Items, så laver den en REFERENCE istedet for at KOPIERE array'et over....
-- deepcopy(orig)
-- - Type: **Local function**
-- - Creates a deep copy of a table
-- - **Returns** a new table with the same values as the original
--
-- **Parameters**
-- - **orig** = The table to copy
2024-12-29 19:48:41 +00:00
local function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else
copy = orig
end
return copy
end
2024-12-30 10:15:34 +00:00
-- Func: **spawnVan()**
-- - Type: **Local function**
-- - Spawns the van and sets the active items
2024-12-29 19:48:41 +00:00
local function spawnVan()
2024-12-30 10:15:34 +00:00
if GlobalState.ActiveVan ~= -1 or Config.Debug then
GlobalState.LastVan = os.time()
2024-12-29 19:48:41 +00:00
if Config.perVanItems then
2024-12-30 10:15:34 +00:00
GlobalState.ActiveItems = deepcopy(Config.Gunvans.vehicles[GlobalState.ActiveVan].items)
2024-12-29 19:48:41 +00:00
else
2024-12-30 10:15:34 +00:00
GlobalState.ActiveItems = deepcopy(Config.VanItems)
2024-12-29 19:48:41 +00:00
end
2024-12-30 10:15:34 +00:00
local time = os.date("%X - %d/%m/%Y", GlobalState.LastVan)
2024-12-29 19:48:41 +00:00
2024-12-30 10:15:34 +00:00
local x = Config.Gunvans.vehicles[GlobalState.ActiveVan].coords.x
local y = Config.Gunvans.vehicles[GlobalState.ActiveVan].coords.y
local z = Config.Gunvans.vehicles[GlobalState.ActiveVan].coords.z
print("At "..time.." a van spawned at Coordinates: " .. x .. " " .. y .. " " .. z)
TriggerClientEvent('_hp_gv:client:_spawnVan', -1, GlobalState.ActiveVan)
2024-12-29 19:48:41 +00:00
end
end
2024-12-30 10:15:34 +00:00
-- Func: **deleteVan()**
-- - Type: **Local function**
-- - Trigger a countdown to delete the van after a certain amount of time
2024-12-29 19:48:41 +00:00
local function deleteVan()
Citizen.CreateThread(function()
Citizen.Wait(Config.duration * 60000)
2024-12-30 10:15:34 +00:00
GlobalState.ActiveVan = -1
GlobalState.ActiveItems = {}
2024-12-29 19:48:41 +00:00
TriggerClientEvent('_hp_gv:client:_destroyVan', -1)
startCountdown()
end)
end
2024-12-30 10:15:34 +00:00
local function triggerVanSpawn()
GlobalState.ActiveVan = math.random(1, GlobalState.totalVans)
spawnVan()
deleteVan()
end
2024-12-29 19:48:41 +00:00
2024-12-30 10:15:34 +00:00
-- Func: **startCountdown()**
-- - Type: **Local function**
-- - Trigger a countdown to spawn a new van after a certain amount of time
2024-12-29 19:48:41 +00:00
function startCountdown()
Citizen.CreateThread(function()
2024-12-30 10:15:34 +00:00
Wait(10000)
if Config.SpawnDuringNight then
if GetClockHours() > 20 and GetClockHours() < 5 then
Citizen.Wait(Config.minutesBetweenVans * 60000)
triggerVanSpawn()
end
else
Citizen.Wait(Config.minutesBetweenVans * 60000)
triggerVanSpawn()
end
2024-12-29 19:48:41 +00:00
end)
end
2024-12-30 10:15:34 +00:00
-- startCountdown()
-- GiveItem(src, item)
--
-- src = Player source
--
-- item = item index
2024-12-29 19:48:41 +00:00
local function GiveItem(src, item)
2024-12-30 10:15:34 +00:00
if GlobalState.ActiveItems[item].iAmount <= 0 then
return Config.NotifyEmpty(src, GlobalState.ActiveItems[item].label)
2024-12-29 19:48:41 +00:00
end
local Player = QBCore.Functions.GetPlayer(src)
2024-12-30 10:15:34 +00:00
if Player.Functions.GetMoney('cash') < GlobalState.ActiveItems[item].price then
return Config.NotifyNoMoney(src, GlobalState.ActiveItems[item].label)
2024-12-29 19:48:41 +00:00
end
2024-12-30 10:15:34 +00:00
Player.Functions.RemoveMoney('cash', GlobalState.ActiveItems[item].price, 'Gun Van')
2024-12-29 19:48:41 +00:00
2024-12-30 10:15:34 +00:00
Config.InventoryExport(src, GlobalState.ActiveItems[item].item, 1)
if GlobalState.ActiveItems[item].weaponType == "pistol" then
Config.InventoryExport(src, "pistol_ammo", GlobalState.ActiveItems[item].aAmount)
2024-12-29 19:48:41 +00:00
end
2024-12-30 10:15:34 +00:00
if GlobalState.ActiveItems[item].triggerCops then
2024-12-29 19:48:41 +00:00
Config.EmergencyTrigger()
end
2024-12-30 10:15:34 +00:00
GlobalState.ActiveItems[item].iAmount = GlobalState.ActiveItems[item].iAmount - 1
Config.Notify(src, GlobalState.ActiveItems[item].label)
2024-12-29 19:48:41 +00:00
end
-- #### Debug stuff
if Config.Debug then
QBCore.Commands.Add('gv', '', {}, false, function(source, args)
TriggerClientEvent('_hp_gv:client:_destroyVan', -1)
2024-12-30 10:15:34 +00:00
GlobalState.ActiveVan = math.random(1, GlobalState.totalVans)
2024-12-29 19:48:41 +00:00
spawnVan()
end, Config.Permission)
QBCore.Commands.Add('dgv', '', {}, false, function(source, args)
2024-12-30 10:15:34 +00:00
GlobalState.ActiveVan = -1
GlobalState.ActiveItems = {}
2024-12-29 19:48:41 +00:00
TriggerClientEvent('_hp_gv:client:_destroyVan', -1)
end, Config.Permission)
end
-- #### Server: Give Item
RegisterNetEvent('_hp_gv:server:_giveItem')
AddEventHandler('_hp_gv:server:_giveItem', function(item)
local src = source
local playerPed = GetPlayerPed(src)
local Player = QBCore.Functions.GetPlayer(source)
local playerCoords = GetEntityCoords(playerPed)
2024-12-30 10:15:34 +00:00
local targetCoords = Config.Gunvans.vehicles[GlobalState.ActiveVan].coords
2024-12-29 19:48:41 +00:00
if #(playerCoords - targetCoords) > 5.5 then
Config.LogHandler(Player.PlayerData.citizenid .. " " .. Config.Lang.OutsideRange)
Config.LogHandler("Distance: " .. #(playerCoords - targetCoords) .. " - Max distance: 5.5")
if #(playerCoords - targetCoords) > 10 and Config.DropPlayer then
Config.LogHandler("GunVan | " .. Player.PlayerData.citizenid .. " " .. Config.Lang.OutsideRangeKick)
DropPlayer(src, Config.Lang.OutsideRangeKickPlayer)
end
return
end
2024-12-30 10:15:34 +00:00
if GlobalState.ActiveVan ~= -1 or Config.Debug then
2024-12-29 19:48:41 +00:00
GiveItem(src, item)
else
Config.LogHandler("GunVan | " .. Player.PlayerData.citizenid .. " " .. Config.Lang.OutsideBuyHours)
2024-12-30 10:15:34 +00:00
Config.LogHandler("GunVan | " .. Config.Lang.TimeUntilNext .. " " .. Config.minutesBetweenVans * 60 - (os.time() - GlobalState.LastVan) .. " " .. Config.Lang.Seconds)
2024-12-29 19:48:41 +00:00
return
end
end)