Scripts/resources/[hp]/hp_gv/server/server.lua
2024-12-30 11:15:34 +01:00

178 lines
5.7 KiB
Lua

QBCore = exports['qb-core']:GetCoreObject()
math.randomseed(os.time())
GlobalState.ActiveVan = -1
GlobalState.ActiveItems = {}
GlobalState.LastVan = os.time()
GlobalState.totalVans = #Config.Gunvans.vehicles
-- 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
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
-- Func: **spawnVan()**
-- - Type: **Local function**
-- - Spawns the van and sets the active items
local function spawnVan()
if GlobalState.ActiveVan ~= -1 or Config.Debug then
GlobalState.LastVan = os.time()
if Config.perVanItems then
GlobalState.ActiveItems = deepcopy(Config.Gunvans.vehicles[GlobalState.ActiveVan].items)
else
GlobalState.ActiveItems = deepcopy(Config.VanItems)
end
local time = os.date("%X - %d/%m/%Y", GlobalState.LastVan)
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)
end
end
-- Func: **deleteVan()**
-- - Type: **Local function**
-- - Trigger a countdown to delete the van after a certain amount of time
local function deleteVan()
Citizen.CreateThread(function()
Citizen.Wait(Config.duration * 60000)
GlobalState.ActiveVan = -1
GlobalState.ActiveItems = {}
TriggerClientEvent('_hp_gv:client:_destroyVan', -1)
startCountdown()
end)
end
local function triggerVanSpawn()
GlobalState.ActiveVan = math.random(1, GlobalState.totalVans)
spawnVan()
deleteVan()
end
-- Func: **startCountdown()**
-- - Type: **Local function**
-- - Trigger a countdown to spawn a new van after a certain amount of time
function startCountdown()
Citizen.CreateThread(function()
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
end)
end
-- startCountdown()
-- GiveItem(src, item)
--
-- src = Player source
--
-- item = item index
local function GiveItem(src, item)
if GlobalState.ActiveItems[item].iAmount <= 0 then
return Config.NotifyEmpty(src, GlobalState.ActiveItems[item].label)
end
local Player = QBCore.Functions.GetPlayer(src)
if Player.Functions.GetMoney('cash') < GlobalState.ActiveItems[item].price then
return Config.NotifyNoMoney(src, GlobalState.ActiveItems[item].label)
end
Player.Functions.RemoveMoney('cash', GlobalState.ActiveItems[item].price, 'Gun Van')
Config.InventoryExport(src, GlobalState.ActiveItems[item].item, 1)
if GlobalState.ActiveItems[item].weaponType == "pistol" then
Config.InventoryExport(src, "pistol_ammo", GlobalState.ActiveItems[item].aAmount)
end
if GlobalState.ActiveItems[item].triggerCops then
Config.EmergencyTrigger()
end
GlobalState.ActiveItems[item].iAmount = GlobalState.ActiveItems[item].iAmount - 1
Config.Notify(src, GlobalState.ActiveItems[item].label)
end
-- #### Debug stuff
if Config.Debug then
QBCore.Commands.Add('gv', '', {}, false, function(source, args)
TriggerClientEvent('_hp_gv:client:_destroyVan', -1)
GlobalState.ActiveVan = math.random(1, GlobalState.totalVans)
spawnVan()
end, Config.Permission)
QBCore.Commands.Add('dgv', '', {}, false, function(source, args)
GlobalState.ActiveVan = -1
GlobalState.ActiveItems = {}
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)
local targetCoords = Config.Gunvans.vehicles[GlobalState.ActiveVan].coords
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
if GlobalState.ActiveVan ~= -1 or Config.Debug then
GiveItem(src, item)
else
Config.LogHandler("GunVan | " .. Player.PlayerData.citizenid .. " " .. Config.Lang.OutsideBuyHours)
Config.LogHandler("GunVan | " .. Config.Lang.TimeUntilNext .. " " .. Config.minutesBetweenVans * 60 - (os.time() - GlobalState.LastVan) .. " " .. Config.Lang.Seconds)
return
end
end)