forked from Simnation/Main
195 lines
6.2 KiB
Lua
195 lines
6.2 KiB
Lua
![]() |
local QBCore = exports['qb-core']:GetCoreObject()
|
||
|
|
||
|
-- Admin Permission Check
|
||
|
local function IsAdmin(source)
|
||
|
local Player = QBCore.Functions.GetPlayer(source)
|
||
|
if not Player then return false end
|
||
|
return QBCore.Functions.HasPermission(source, 'admin') or Player.PlayerData.job.name == 'admin'
|
||
|
end
|
||
|
|
||
|
-- Get all players from database
|
||
|
QBCore.Functions.CreateCallback('vehicleadmin:getPlayers', function(source, cb)
|
||
|
if not IsAdmin(source) then
|
||
|
cb(false)
|
||
|
return
|
||
|
end
|
||
|
|
||
|
MySQL.Async.fetchAll('SELECT citizenid, charinfo FROM players ORDER BY charinfo', {}, function(result)
|
||
|
local players = {}
|
||
|
for i = 1, #result do
|
||
|
local charinfo = json.decode(result[i].charinfo)
|
||
|
table.insert(players, {
|
||
|
citizenid = result[i].citizenid,
|
||
|
name = charinfo.firstname .. ' ' .. charinfo.lastname
|
||
|
})
|
||
|
end
|
||
|
cb(players)
|
||
|
end)
|
||
|
end)
|
||
|
|
||
|
-- Get vehicles for specific player
|
||
|
QBCore.Functions.CreateCallback('vehicleadmin:getPlayerVehicles', function(source, cb, citizenid)
|
||
|
if not IsAdmin(source) then
|
||
|
cb(false)
|
||
|
return
|
||
|
end
|
||
|
|
||
|
MySQL.Async.fetchAll('SELECT * FROM player_vehicles WHERE citizenid = ?', {citizenid}, function(result)
|
||
|
local vehicles = {}
|
||
|
for i = 1, #result do
|
||
|
local vehicle = result[i]
|
||
|
local vehicleData = json.decode(vehicle.mods)
|
||
|
|
||
|
table.insert(vehicles, {
|
||
|
plate = vehicle.plate,
|
||
|
vehicle = vehicle.vehicle,
|
||
|
garage = vehicle.garage,
|
||
|
state = vehicle.state,
|
||
|
fuel = vehicle.fuel,
|
||
|
engine = vehicle.engine,
|
||
|
body = vehicle.body,
|
||
|
citizenid = vehicle.citizenid,
|
||
|
depotprice = vehicle.depotprice,
|
||
|
drivingdistance = vehicle.drivingdistance
|
||
|
})
|
||
|
end
|
||
|
cb(vehicles)
|
||
|
end)
|
||
|
end)
|
||
|
|
||
|
-- Get all garages
|
||
|
QBCore.Functions.CreateCallback('vehicleadmin:getGarages', function(source, cb)
|
||
|
if not IsAdmin(source) then
|
||
|
cb(false)
|
||
|
return
|
||
|
end
|
||
|
|
||
|
local garages = {}
|
||
|
for garageName, garageData in pairs(QBCore.Shared.Garages) do
|
||
|
table.insert(garages, {
|
||
|
name = garageName,
|
||
|
label = garageData.label or garageName
|
||
|
})
|
||
|
end
|
||
|
cb(garages)
|
||
|
end)
|
||
|
|
||
|
-- Move vehicle to garage
|
||
|
RegisterNetEvent('vehicleadmin:moveToGarage', function(plate, garage)
|
||
|
local src = source
|
||
|
if not IsAdmin(src) then return end
|
||
|
|
||
|
-- Delete vehicle from world if it exists
|
||
|
local vehicles = GetAllVehicles()
|
||
|
for i = 1, #vehicles do
|
||
|
local veh = vehicles[i]
|
||
|
local vehPlate = GetVehicleNumberPlateText(veh):gsub("%s+", "")
|
||
|
if vehPlate == plate:gsub("%s+", "") then
|
||
|
DeleteEntity(veh)
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
|
||
|
-- Update database
|
||
|
MySQL.Async.execute('UPDATE player_vehicles SET state = ?, garage = ? WHERE plate = ?', {1, garage, plate}, function(affectedRows)
|
||
|
if affectedRows > 0 then
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Fahrzeugverwaltung',
|
||
|
description = 'Fahrzeug wurde in Garage ' .. garage .. ' gestellt',
|
||
|
type = 'success'
|
||
|
})
|
||
|
else
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Fahrzeugverwaltung',
|
||
|
description = 'Fehler beim Verschieben des Fahrzeugs',
|
||
|
type = 'error'
|
||
|
})
|
||
|
end
|
||
|
end)
|
||
|
end)
|
||
|
|
||
|
-- Delete vehicle from map
|
||
|
RegisterNetEvent('vehicleadmin:deleteFromMap', function(plate)
|
||
|
local src = source
|
||
|
if not IsAdmin(src) then return end
|
||
|
|
||
|
local vehicles = GetAllVehicles()
|
||
|
local deleted = false
|
||
|
|
||
|
for i = 1, #vehicles do
|
||
|
local veh = vehicles[i]
|
||
|
local vehPlate = GetVehicleNumberPlateText(veh):gsub("%s+", "")
|
||
|
if vehPlate == plate:gsub("%s+", "") then
|
||
|
DeleteEntity(veh)
|
||
|
deleted = true
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
|
||
|
if deleted then
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Fahrzeugverwaltung',
|
||
|
description = 'Fahrzeug wurde von der Map gelöscht',
|
||
|
type = 'success'
|
||
|
})
|
||
|
else
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Fahrzeugverwaltung',
|
||
|
description = 'Fahrzeug nicht auf der Map gefunden',
|
||
|
type = 'error'
|
||
|
})
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
-- Repair vehicle
|
||
|
RegisterNetEvent('vehicleadmin:repairVehicle', function(plate)
|
||
|
local src = source
|
||
|
if not IsAdmin(src) then return end
|
||
|
|
||
|
-- Update database
|
||
|
MySQL.Async.execute('UPDATE player_vehicles SET engine = ?, body = ? WHERE plate = ?', {1000, 1000, plate}, function(affectedRows)
|
||
|
-- Also repair if vehicle is on map
|
||
|
local vehicles = GetAllVehicles()
|
||
|
for i = 1, #vehicles do
|
||
|
local veh = vehicles[i]
|
||
|
local vehPlate = GetVehicleNumberPlateText(veh):gsub("%s+", "")
|
||
|
if vehPlate == plate:gsub("%s+", "") then
|
||
|
SetVehicleFixed(veh)
|
||
|
SetVehicleDeformationFixed(veh)
|
||
|
SetVehicleUndriveable(veh, false)
|
||
|
SetVehicleEngineOn(veh, true, true)
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
|
||
|
if affectedRows > 0 then
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Fahrzeugverwaltung',
|
||
|
description = 'Fahrzeug wurde repariert',
|
||
|
type = 'success'
|
||
|
})
|
||
|
else
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Fahrzeugverwaltung',
|
||
|
description = 'Fehler beim Reparieren des Fahrzeugs',
|
||
|
type = 'error'
|
||
|
})
|
||
|
end
|
||
|
end)
|
||
|
end)
|
||
|
|
||
|
-- Command to open admin menu
|
||
|
QBCore.Commands.Add('vehicleadmin', 'Öffne Fahrzeug Admin Menu', {}, false, function(source, args)
|
||
|
local src = source
|
||
|
if not IsAdmin(src) then
|
||
|
TriggerClientEvent('ox_lib:notify', src, {
|
||
|
title = 'Keine Berechtigung',
|
||
|
description = 'Du hast keine Admin-Rechte',
|
||
|
type = 'error'
|
||
|
})
|
||
|
return
|
||
|
end
|
||
|
|
||
|
TriggerClientEvent('vehicleadmin:openMenu', src)
|
||
|
end, 'admin')
|