forked from Simnation/Main
ed
This commit is contained in:
parent
305602602e
commit
99aed29a52
4 changed files with 415 additions and 0 deletions
175
resources/[carscripts]/nordi_rental/client.lua
Normal file
175
resources/[carscripts]/nordi_rental/client.lua
Normal file
|
@ -0,0 +1,175 @@
|
|||
local QBCore = exports['qb-core']:GetCoreObject()
|
||||
local spawnedNPCs = {}
|
||||
local currentRental = nil
|
||||
|
||||
-- NPCs spawnen
|
||||
CreateThread(function()
|
||||
for i = 1, #Config.RentalLocations do
|
||||
local location = Config.RentalLocations[i]
|
||||
|
||||
RequestModel(location.npc.model)
|
||||
while not HasModelLoaded(location.npc.model) do
|
||||
Wait(1)
|
||||
end
|
||||
|
||||
local npc = CreatePed(4, location.npc.model, location.npc.coords.x, location.npc.coords.y, location.npc.coords.z - 1.0, location.npc.coords.w, false, true)
|
||||
FreezeEntityPosition(npc, true)
|
||||
SetEntityInvincible(npc, true)
|
||||
SetBlockingOfNonTemporaryEvents(npc, true)
|
||||
|
||||
spawnedNPCs[location.id] = npc
|
||||
|
||||
-- QB-Target für NPC
|
||||
exports['qb-target']:AddTargetEntity(npc, {
|
||||
options = {
|
||||
{
|
||||
type = "client",
|
||||
event = "vehiclerental:client:openMenu",
|
||||
icon = "fas fa-car",
|
||||
label = "Fahrzeug mieten",
|
||||
locationId = location.id
|
||||
},
|
||||
{
|
||||
type = "client",
|
||||
event = "vehiclerental:client:returnVehicle",
|
||||
icon = "fas fa-car-side",
|
||||
label = "Fahrzeug zurückgeben",
|
||||
locationId = location.id
|
||||
}
|
||||
},
|
||||
distance = 2.0
|
||||
})
|
||||
end
|
||||
end)
|
||||
|
||||
-- Mietmenü öffnen
|
||||
RegisterNetEvent('vehiclerental:client:openMenu', function(data)
|
||||
local locationId = data.locationId
|
||||
local location = nil
|
||||
|
||||
for i = 1, #Config.RentalLocations do
|
||||
if Config.RentalLocations[i].id == locationId then
|
||||
location = Config.RentalLocations[i]
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not location then return end
|
||||
|
||||
local options = {}
|
||||
for i = 1, #location.vehicles do
|
||||
local vehicle = location.vehicles[i]
|
||||
table.insert(options, {
|
||||
title = vehicle.label,
|
||||
description = '$' .. vehicle.price .. ' pro Stunde',
|
||||
icon = 'car',
|
||||
onSelect = function()
|
||||
openRentalDialog(vehicle, location)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
lib.registerContext({
|
||||
id = 'vehicle_rental_menu',
|
||||
title = location.name,
|
||||
options = options
|
||||
})
|
||||
|
||||
lib.showContext('vehicle_rental_menu')
|
||||
end)
|
||||
|
||||
-- Mietdialog
|
||||
function openRentalDialog(vehicle, location)
|
||||
local input = lib.inputDialog('Fahrzeug mieten', {
|
||||
{
|
||||
type = 'number',
|
||||
label = 'Mietdauer (Stunden)',
|
||||
description = 'Maximale Mietdauer: ' .. Config.MaxRentalTime .. ' Stunden',
|
||||
required = true,
|
||||
min = 1,
|
||||
max = Config.MaxRentalTime
|
||||
}
|
||||
})
|
||||
|
||||
if not input or not input[1] then return end
|
||||
|
||||
local hours = tonumber(input[1])
|
||||
if not hours or hours < 1 or hours > Config.MaxRentalTime then
|
||||
QBCore.Functions.Notify('Ungültige Mietdauer!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
local totalCost = vehicle.price * hours
|
||||
local plate = GeneratePlate()
|
||||
|
||||
QBCore.Functions.TriggerCallback('vehiclerental:server:rentVehicle', function(success)
|
||||
if success then
|
||||
spawnRentalVehicle(vehicle.model, location.spawnPoint, plate)
|
||||
end
|
||||
end, {
|
||||
vehicleModel = vehicle.model,
|
||||
pricePerHour = vehicle.price,
|
||||
hours = hours,
|
||||
locationId = location.id,
|
||||
plate = plate
|
||||
})
|
||||
end)
|
||||
|
||||
-- Fahrzeug spawnen
|
||||
function spawnRentalVehicle(model, spawnPoint, plate)
|
||||
RequestModel(model)
|
||||
while not HasModelLoaded(model) do
|
||||
Wait(1)
|
||||
end
|
||||
|
||||
local vehicle = CreateVehicle(model, spawnPoint.x, spawnPoint.y, spawnPoint.z, spawnPoint.w, true, false)
|
||||
SetVehicleNumberPlateText(vehicle, plate)
|
||||
SetEntityAsMissionEntity(vehicle, true, true)
|
||||
TaskWarpPedIntoVehicle(PlayerPedId(), vehicle, -1)
|
||||
|
||||
TriggerEvent("vehiclekeys:client:SetOwner", plate)
|
||||
SetModelAsNoLongerNeeded(model)
|
||||
end)
|
||||
|
||||
-- Fahrzeug zurückgeben
|
||||
RegisterNetEvent('vehiclerental:client:returnVehicle', function(data)
|
||||
local ped = PlayerPedId()
|
||||
local vehicle = GetVehiclePedIsIn(ped, false)
|
||||
|
||||
if vehicle == 0 then
|
||||
QBCore.Functions.Notify('Du musst in einem Fahrzeug sitzen!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
local plate = GetVehicleNumberPlateText(vehicle)
|
||||
|
||||
QBCore.Functions.TriggerCallback('vehiclerental:server:returnVehicle', function(success)
|
||||
if success then
|
||||
DeleteVehicle(vehicle)
|
||||
end
|
||||
end, plate)
|
||||
end)
|
||||
|
||||
-- Kennzeichen generieren
|
||||
function GeneratePlate()
|
||||
local plate = ""
|
||||
for i = 1, 8 do
|
||||
if math.random(1, 2) == 1 then
|
||||
plate = plate .. string.char(math.random(65, 90)) -- A-Z
|
||||
else
|
||||
plate = plate .. tostring(math.random(0, 9)) -- 0-9
|
||||
end
|
||||
end
|
||||
return plate
|
||||
end
|
||||
|
||||
-- Cleanup beim Ressourcen-Stop
|
||||
AddEventHandler('onResourceStop', function(resourceName)
|
||||
if GetCurrentResourceName() ~= resourceName then return end
|
||||
|
||||
for _, npc in pairs(spawnedNPCs) do
|
||||
if DoesEntityExist(npc) then
|
||||
DeleteEntity(npc)
|
||||
end
|
||||
end
|
||||
end)
|
Loading…
Add table
Add a link
Reference in a new issue