From 956fe5f183e2a58a2b21c982bb321668734344b9 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Sat, 9 Nov 2019 17:25:41 +0100 Subject: [PATCH] Minor code cleanup --- client/main.lua | 192 +++++++++++++++++++++-------------------------- client/utils.lua | 4 +- config.lua | 24 +++--- server/main.lua | 142 ++++++++++++++++------------------- 4 files changed, 164 insertions(+), 198 deletions(-) diff --git a/client/main.lua b/client/main.lua index 81ba51a1..a6d2a2b8 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,29 +1,17 @@ -local Keys = { - ["ESC"] = 322, ["F1"] = 288, ["F2"] = 289, ["F3"] = 170, ["F5"] = 166, ["F6"] = 167, ["F7"] = 168, ["F8"] = 169, ["F9"] = 56, ["F10"] = 57, - ["~"] = 243, ["1"] = 157, ["2"] = 158, ["3"] = 160, ["4"] = 164, ["5"] = 165, ["6"] = 159, ["7"] = 161, ["8"] = 162, ["9"] = 163, ["-"] = 84, ["="] = 83, ["BACKSPACE"] = 177, - ["TAB"] = 37, ["Q"] = 44, ["W"] = 32, ["E"] = 38, ["R"] = 45, ["T"] = 245, ["Y"] = 246, ["U"] = 303, ["P"] = 199, ["["] = 39, ["]"] = 40, ["ENTER"] = 18, - ["CAPS"] = 137, ["A"] = 34, ["S"] = 8, ["D"] = 9, ["F"] = 23, ["G"] = 47, ["H"] = 74, ["K"] = 311, ["L"] = 182, - ["LEFTSHIFT"] = 21, ["Z"] = 20, ["X"] = 73, ["C"] = 26, ["V"] = 0, ["B"] = 29, ["N"] = 249, ["M"] = 244, [","] = 82, ["."] = 81, - ["LEFTCTRL"] = 36, ["LEFTALT"] = 19, ["SPACE"] = 22, ["RIGHTCTRL"] = 70, - ["HOME"] = 213, ["PAGEUP"] = 10, ["PAGEDOWN"] = 11, ["DELETE"] = 178, - ["LEFT"] = 174, ["RIGHT"] = 175, ["TOP"] = 27, ["DOWN"] = 173, - ["NENTER"] = 201, ["N4"] = 108, ["N5"] = 60, ["N6"] = 107, ["N+"] = 96, ["N-"] = 97, ["N7"] = 117, ["N8"] = 61, ["N9"] = 118 -} - local HasAlreadyEnteredMarker = false -local LastZone = nil -local CurrentAction = nil +local LastZone +local CurrentAction local CurrentActionMsg = '' local CurrentActionData = {} local IsInShopMenu = false local Categories = {} local Vehicles = {} local LastVehicles = {} -local CurrentVehicleData = nil +local CurrentVehicleData -ESX = nil +ESX = nil -Citizen.CreateThread(function () +Citizen.CreateThread(function() while ESX == nil do TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) Citizen.Wait(0) @@ -31,11 +19,11 @@ Citizen.CreateThread(function () Citizen.Wait(10000) - ESX.TriggerServerCallback('esx_vehicleshop:getCategories', function (categories) + ESX.TriggerServerCallback('esx_vehicleshop:getCategories', function(categories) Categories = categories end) - ESX.TriggerServerCallback('esx_vehicleshop:getVehicles', function (vehicles) + ESX.TriggerServerCallback('esx_vehicleshop:getVehicles', function(vehicles) Vehicles = vehicles end) @@ -74,12 +62,12 @@ AddEventHandler('esx:playerLoaded', function(xPlayer) end) RegisterNetEvent('esx_vehicleshop:sendCategories') -AddEventHandler('esx_vehicleshop:sendCategories', function (categories) +AddEventHandler('esx_vehicleshop:sendCategories', function(categories) Categories = categories end) RegisterNetEvent('esx_vehicleshop:sendVehicles') -AddEventHandler('esx_vehicleshop:sendVehicles', function (vehicles) +AddEventHandler('esx_vehicleshop:sendVehicles', function(vehicles) Vehicles = vehicles end) @@ -93,9 +81,10 @@ function DeleteShopInsideVehicles() end function ReturnVehicleProvider() - ESX.TriggerServerCallback('esx_vehicleshop:getCommercialVehicles', function (vehicles) + ESX.TriggerServerCallback('esx_vehicleshop:getCommercialVehicles', function(vehicles) local elements = {} local returnPrice + for i=1, #vehicles, 1 do returnPrice = ESX.Math.Round(vehicles[i].price * 0.75) @@ -109,13 +98,13 @@ function ReturnVehicleProvider() title = _U('return_provider_menu'), align = 'top-left', elements = elements - }, function (data, menu) + }, function(data, menu) TriggerServerEvent('esx_vehicleshop:returnProvider', data.current.value) Citizen.Wait(300) menu.close() ReturnVehicleProvider() - end, function (data, menu) + end, function(data, menu) menu.close() end) end) @@ -124,7 +113,7 @@ end function StartShopRestriction() Citizen.CreateThread(function() while IsInShopMenu do - Citizen.Wait(1) + Citizen.Wait(0) DisableControlAction(0, 75, true) -- Disable exit vehicle DisableControlAction(27, 75, true) -- Disable exit vehicle @@ -142,7 +131,7 @@ function OpenShopMenu() FreezeEntityPosition(playerPed, true) SetEntityVisible(playerPed, false) - SetEntityCoords(playerPed, Config.Zones.ShopInside.Pos.x, Config.Zones.ShopInside.Pos.y, Config.Zones.ShopInside.Pos.z) + SetEntityCoords(playerPed, Config.Zones.ShopInside.Pos) local vehiclesByCategory = {} local elements = {} @@ -189,7 +178,7 @@ function OpenShopMenu() title = _U('car_dealer'), align = 'top-left', elements = elements - }, function (data, menu) + }, function(data, menu) local vehicleData = vehiclesByCategory[data.current.name][data.current.value + 1] ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop_confirm', { @@ -198,8 +187,7 @@ function OpenShopMenu() elements = { {label = _U('no'), value = 'no'}, {label = _U('yes'), value = 'yes'} - } - }, function(data2, menu2) + }}, function(data2, menu2) if data2.current.value == 'yes' then if Config.EnablePlayerManagement then ESX.TriggerServerCallback('esx_vehicleshop:buyVehicleSociety', function(hasEnoughMoney) @@ -216,7 +204,7 @@ function OpenShopMenu() FreezeEntityPosition(playerPed, false) SetEntityVisible(playerPed, true) - SetEntityCoords(playerPed, Config.Zones.ShopEntering.Pos.x, Config.Zones.ShopEntering.Pos.y, Config.Zones.ShopEntering.Pos.z) + SetEntityCoords(playerPed, Config.Zones.ShopEntering.Pos) menu2.close() menu.close() @@ -236,7 +224,7 @@ function OpenShopMenu() elements = { {label = _U('staff_type'), value = 'personnal'}, {label = _U('society_type'), value = 'society'} - }}, function (data3, menu3) + }}, function(data3, menu3) if data3.current.value == 'personnal' then @@ -249,7 +237,7 @@ function OpenShopMenu() menu.close() DeleteShopInsideVehicles() - ESX.Game.SpawnVehicle(vehicleData.model, Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading, function (vehicle) + ESX.Game.SpawnVehicle(vehicleData.model, Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading, function(vehicle) TaskWarpPedIntoVehicle(playerPed, vehicle, -1) local newPlate = GeneratePlate() @@ -273,7 +261,7 @@ function OpenShopMenu() elseif data3.current.value == 'society' then - ESX.TriggerServerCallback('esx_vehicleshop:buyVehicleSociety', function (hasEnoughMoney) + ESX.TriggerServerCallback('esx_vehicleshop:buyVehicleSociety', function(hasEnoughMoney) if hasEnoughMoney then IsInShopMenu = false @@ -283,7 +271,7 @@ function OpenShopMenu() DeleteShopInsideVehicles() - ESX.Game.SpawnVehicle(vehicleData.model, Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading, function (vehicle) + ESX.Game.SpawnVehicle(vehicleData.model, Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading, function(vehicle) TaskWarpPedIntoVehicle(playerPed, vehicle, -1) local newPlate = GeneratePlate() @@ -302,18 +290,18 @@ function OpenShopMenu() end, playerData.job.name, vehicleData.model) end - end, function (data3, menu3) + end, function(data3, menu3) menu3.close() end) else - ESX.TriggerServerCallback('esx_vehicleshop:buyVehicle', function (hasEnoughMoney) + ESX.TriggerServerCallback('esx_vehicleshop:buyVehicle', function(hasEnoughMoney) if hasEnoughMoney then IsInShopMenu = false menu2.close() menu.close() DeleteShopInsideVehicles() - ESX.Game.SpawnVehicle(vehicleData.model, Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading, function (vehicle) + ESX.Game.SpawnVehicle(vehicleData.model, Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading, function(vehicle) TaskWarpPedIntoVehicle(playerPed, vehicle, -1) local newPlate = GeneratePlate() @@ -337,10 +325,10 @@ function OpenShopMenu() end end end - end, function (data2, menu2) + end, function(data2, menu2) menu2.close() end) - end, function (data, menu) + end, function(data, menu) menu.close() DeleteShopInsideVehicles() local playerPed = PlayerPedId() @@ -351,17 +339,17 @@ function OpenShopMenu() FreezeEntityPosition(playerPed, false) SetEntityVisible(playerPed, true) - SetEntityCoords(playerPed, Config.Zones.ShopEntering.Pos.x, Config.Zones.ShopEntering.Pos.y, Config.Zones.ShopEntering.Pos.z) + SetEntityCoords(playerPed, Config.Zones.ShopEntering.Pos) IsInShopMenu = false - end, function (data, menu) + end, function(data, menu) local vehicleData = vehiclesByCategory[data.current.name][data.current.value + 1] local playerPed = PlayerPedId() DeleteShopInsideVehicles() WaitForVehicleToLoad(vehicleData.model) - ESX.Game.SpawnLocalVehicle(vehicleData.model, Config.Zones.ShopInside.Pos, Config.Zones.ShopInside.Heading, function (vehicle) + ESX.Game.SpawnLocalVehicle(vehicleData.model, Config.Zones.ShopInside.Pos, Config.Zones.ShopInside.Heading, function(vehicle) table.insert(LastVehicles, vehicle) TaskWarpPedIntoVehicle(playerPed, vehicle, -1) FreezeEntityPosition(vehicle, true) @@ -372,13 +360,12 @@ function OpenShopMenu() DeleteShopInsideVehicles() WaitForVehicleToLoad(firstVehicleData.model) - ESX.Game.SpawnLocalVehicle(firstVehicleData.model, Config.Zones.ShopInside.Pos, Config.Zones.ShopInside.Heading, function (vehicle) + ESX.Game.SpawnLocalVehicle(firstVehicleData.model, Config.Zones.ShopInside.Pos, Config.Zones.ShopInside.Heading, function(vehicle) table.insert(LastVehicles, vehicle) TaskWarpPedIntoVehicle(playerPed, vehicle, -1) FreezeEntityPosition(vehicle, true) SetModelAsNoLongerNeeded(firstVehicleData.model) end) - end function WaitForVehicleToLoad(modelHash) @@ -392,7 +379,7 @@ function WaitForVehicleToLoad(modelHash) EndTextCommandBusyString(4) while not HasModelLoaded(modelHash) do - Citizen.Wait(1) + Citizen.Wait(0) DisableAllControlActions(0) end @@ -418,8 +405,7 @@ function OpenResellerMenu() {label = _U('set_vehicle_owner_sell_society'), value = 'set_vehicle_owner_sell_society'}, {label = _U('deposit_stock'), value = 'put_stock'}, {label = _U('take_stock'), value = 'get_stock'} - } - }, function (data, menu) + }}, function(data, menu) local action = data.current.value if action == 'buy_vehicle' then @@ -444,7 +430,7 @@ function OpenResellerMenu() ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'set_vehicle_owner_sell_amount', { title = _U('invoice_amount') - }, function (data2, menu2) + }, function(data2, menu2) local amount = tonumber(data2.value) if amount == nil then @@ -459,7 +445,7 @@ function OpenResellerMenu() TriggerServerEvent('esx_billing:sendBill', GetPlayerServerId(closestPlayer), 'society_cardealer', _U('car_dealer'), tonumber(data2.value)) end end - end, function (data2, menu2) + end, function(data2, menu2) menu2.close() end) @@ -496,7 +482,7 @@ function OpenResellerMenu() if closestPlayer == -1 or closestDistance > 3.0 then ESX.ShowNotification(_U('no_players')) else - ESX.TriggerServerCallback('esx:getOtherPlayerData', function (xPlayer) + ESX.TriggerServerCallback('esx:getOtherPlayerData', function(xPlayer) local newPlate = GeneratePlate() local vehicleProps = ESX.Game.GetVehicleProperties(LastVehicles[#LastVehicles]) @@ -520,7 +506,7 @@ function OpenResellerMenu() ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'set_vehicle_owner_rent_amount', { title = _U('rental_amount') - }, function (data2, menu2) + }, function(data2, menu2) local amount = tonumber(data2.value) if amount == nil then @@ -545,14 +531,13 @@ function OpenResellerMenu() end ESX.ShowNotification(_U('vehicle_set_rented', vehicleProps.plate, GetPlayerName(closestPlayer))) - TriggerServerEvent('esx_vehicleshop:setVehicleForAllPlayers', vehicleProps, Config.Zones.ShopInside.Pos.x, Config.Zones.ShopInside.Pos.y, Config.Zones.ShopInside.Pos.z, 5.0) end end - end, function (data2, menu2) + end, function(data2, menu2) menu2.close() end) end - end, function (data, menu) + end, function(data, menu) menu.close() CurrentAction = 'reseller_menu' @@ -562,7 +547,7 @@ function OpenResellerMenu() end function OpenPopVehicleMenu() - ESX.TriggerServerCallback('esx_vehicleshop:getCommercialVehicles', function (vehicles) + ESX.TriggerServerCallback('esx_vehicleshop:getCommercialVehicles', function(vehicles) local elements = {} for i=1, #vehicles, 1 do @@ -576,12 +561,12 @@ function OpenPopVehicleMenu() title = _U('vehicle_dealer'), align = 'top-left', elements = elements - }, function (data, menu) + }, function(data, menu) local model = data.current.value DeleteShopInsideVehicles() - ESX.Game.SpawnVehicle(model, Config.Zones.ShopInside.Pos, Config.Zones.ShopInside.Heading, function (vehicle) + ESX.Game.SpawnVehicle(model, Config.Zones.ShopInside.Pos, Config.Zones.ShopInside.Heading, function(vehicle) table.insert(LastVehicles, vehicle) for i=1, #Vehicles, 1 do @@ -591,14 +576,14 @@ function OpenPopVehicleMenu() end end end) - end, function (data, menu) + end, function(data, menu) menu.close() end) end) end function OpenRentedVehiclesMenu() - ESX.TriggerServerCallback('esx_vehicleshop:getRentedVehicles', function (vehicles) + ESX.TriggerServerCallback('esx_vehicleshop:getRentedVehicles', function(vehicles) local elements = {} for i=1, #vehicles, 1 do @@ -612,7 +597,7 @@ function OpenRentedVehiclesMenu() title = _U('rent_vehicle'), align = 'top-left', elements = elements - }, nil, function (data, menu) + }, nil, function(data, menu) menu.close() end) end) @@ -627,7 +612,7 @@ function OpenBossActionsMenu() elements = { {label = _U('boss_actions'), value = 'boss_actions'}, {label = _U('boss_sold'), value = 'sold_vehicles'} - }}, function (data, menu) + }}, function(data, menu) if data.current.value == 'boss_actions' then TriggerEvent('esx_society:openBossMenu', 'cardealer', function(data2, menu2) menu2.close() @@ -661,7 +646,7 @@ function OpenBossActionsMenu() end) end - end, function (data, menu) + end, function(data, menu) menu.close() CurrentAction = 'boss_actions_menu' @@ -671,7 +656,7 @@ function OpenBossActionsMenu() end function OpenGetStocksMenu() - ESX.TriggerServerCallback('esx_vehicleshop:getStockItems', function (items) + ESX.TriggerServerCallback('esx_vehicleshop:getStockItems', function(items) local elements = {} for i=1, #items, 1 do @@ -685,12 +670,12 @@ function OpenGetStocksMenu() title = _U('dealership_stock'), align = 'top-left', elements = elements - }, function (data, menu) + }, function(data, menu) local itemName = data.current.value ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'stocks_menu_get_item_count', { title = _U('amount') - }, function (data2, menu2) + }, function(data2, menu2) local count = tonumber(data2.value) if count == nil then @@ -701,18 +686,17 @@ function OpenGetStocksMenu() menu.close() OpenGetStocksMenu() end - end, function (data2, menu2) + end, function(data2, menu2) menu2.close() end) - - end, function (data, menu) + end, function(data, menu) menu.close() end) end) end function OpenPutStocksMenu() - ESX.TriggerServerCallback('esx_vehicleshop:getPlayerInventory', function (inventory) + ESX.TriggerServerCallback('esx_vehicleshop:getPlayerInventory', function(inventory) local elements = {} for i=1, #inventory.items, 1 do @@ -731,12 +715,12 @@ function OpenPutStocksMenu() title = _U('inventory'), align = 'top-left', elements = elements - }, function (data, menu) + }, function(data, menu) local itemName = data.current.value ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'stocks_menu_put_item_count', { title = _U('amount') - }, function (data2, menu2) + }, function(data2, menu2) local count = tonumber(data2.value) if count == nil then @@ -747,17 +731,17 @@ function OpenPutStocksMenu() menu.close() OpenPutStocksMenu() end - end, function (data2, menu2) + end, function(data2, menu2) menu2.close() end) - end, function (data, menu) + end, function(data, menu) menu.close() end) end) end RegisterNetEvent('esx:setJob') -AddEventHandler('esx:setJob', function (job) +AddEventHandler('esx:setJob', function(job) ESX.PlayerData.job = job if Config.EnablePlayerManagement then @@ -774,7 +758,7 @@ AddEventHandler('esx:setJob', function (job) end end) -AddEventHandler('esx_vehicleshop:hasEnteredMarker', function (zone) +AddEventHandler('esx_vehicleshop:hasEnteredMarker', function(zone) if zone == 'ShopEntering' then if Config.EnablePlayerManagement then @@ -845,7 +829,7 @@ AddEventHandler('esx_vehicleshop:hasEnteredMarker', function (zone) end end) -AddEventHandler('esx_vehicleshop:hasExitedMarker', function (zone) +AddEventHandler('esx_vehicleshop:hasExitedMarker', function(zone) if not IsInShopMenu then ESX.UI.Menu.CloseAll() end @@ -863,14 +847,14 @@ AddEventHandler('onResourceStop', function(resource) FreezeEntityPosition(playerPed, false) SetEntityVisible(playerPed, true) - SetEntityCoords(playerPed, Config.Zones.ShopEntering.Pos.x, Config.Zones.ShopEntering.Pos.y, Config.Zones.ShopEntering.Pos.z) + SetEntityCoords(playerPed, Config.Zones.ShopEntering.Pos) end end end) if Config.EnablePlayerManagement then RegisterNetEvent('esx_phone:loaded') - AddEventHandler('esx_phone:loaded', function (phoneNumber, contacts) + AddEventHandler('esx_phone:loaded', function(phoneNumber, contacts) local specialContact = { name = _U('dealership'), number = 'cardealer', @@ -883,7 +867,7 @@ end -- Create Blips Citizen.CreateThread(function() - local blip = AddBlipForCoord(Config.Zones.ShopEntering.Pos.x, Config.Zones.ShopEntering.Pos.y, Config.Zones.ShopEntering.Pos.z) + local blip = AddBlipForCoord(Config.Zones.ShopEntering.Pos) SetBlipSprite (blip, 326) SetBlipDisplay(blip, 4) @@ -895,40 +879,32 @@ Citizen.CreateThread(function() EndTextCommandSetBlipName(blip) end) --- Display markers +-- Enter / Exit marker events & Draw Markers Citizen.CreateThread(function() while true do Citizen.Wait(0) - - local coords = GetEntityCoords(PlayerPedId()) + local playerCoords = GetEntityCoords(PlayerPedId()) + local isInMarker, letSleep, currentZone = false, true for k,v in pairs(Config.Zones) do - if(v.Type ~= -1 and GetDistanceBetweenCoords(coords, v.Pos.x, v.Pos.y, v.Pos.z, true) < Config.DrawDistance) then - DrawMarker(v.Type, v.Pos.x, v.Pos.y, v.Pos.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Size.x, v.Size.y, v.Size.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 100, false, true, 2, false, false, false, false) - end - end - end -end) + local distance = #(playerCoords - v.Pos) --- Enter / Exit marker events -Citizen.CreateThread(function () - while true do - Citizen.Wait(0) + if distance < Config.DrawDistance then + letSleep = false - local coords = GetEntityCoords(PlayerPedId()) - local isInMarker = false - local currentZone = nil + if v.Type ~= -1 then + DrawMarker(v.Type, v.Pos, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Size.x, v.Size.y, v.Size.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 100, false, true, 2, false, nil, nil, false) + end - for k,v in pairs(Config.Zones) do - if(GetDistanceBetweenCoords(coords, v.Pos.x, v.Pos.y, v.Pos.z, true) < v.Size.x) then - isInMarker = true - currentZone = k + if distance < v.Size.x then + isInMarker, currentZone = true, k + end end end if (isInMarker and not HasAlreadyEnteredMarker) or (isInMarker and LastZone ~= currentZone) then - HasAlreadyEnteredMarker = true - LastZone = currentZone + HasAlreadyEnteredMarker, LastZone = true, currentZone + LastZone = currentZone TriggerEvent('esx_vehicleshop:hasEnteredMarker', currentZone) end @@ -936,20 +912,22 @@ Citizen.CreateThread(function () HasAlreadyEnteredMarker = false TriggerEvent('esx_vehicleshop:hasExitedMarker', LastZone) end + + if letSleep then + Citizen.Wait(500) + end end end) -- Key controls Citizen.CreateThread(function() while true do - Citizen.Wait(10) + Citizen.Wait(0) - if CurrentAction == nil then - Citizen.Wait(500) - else + if CurrentAction then ESX.ShowHelpNotification(CurrentActionMsg) - if IsControlJustReleased(0, Keys['E']) then + if IsControlJustReleased(0, 38) then if CurrentAction == 'shop_menu' then if Config.LicenseEnable then ESX.TriggerServerCallback('esx_license:checkLicense', function(hasDriversLicense) @@ -988,6 +966,8 @@ Citizen.CreateThread(function() CurrentAction = nil end + else + Citizen.Wait(500) end end end) diff --git a/client/utils.lua b/client/utils.lua index cfb4b114..26e586bb 100644 --- a/client/utils.lua +++ b/client/utils.lua @@ -49,7 +49,7 @@ function IsPlateTaken(plate) end function GetRandomNumber(length) - Citizen.Wait(1) + Citizen.Wait(0) math.randomseed(GetGameTimer()) if length > 0 then return GetRandomNumber(length - 1) .. NumberCharset[math.random(1, #NumberCharset)] @@ -59,7 +59,7 @@ function GetRandomNumber(length) end function GetRandomLetter(length) - Citizen.Wait(1) + Citizen.Wait(0) math.randomseed(GetGameTimer()) if length > 0 then return GetRandomLetter(length - 1) .. Charset[math.random(1, #Charset)] diff --git a/config.lua b/config.lua index bcf88181..0c87297e 100644 --- a/config.lua +++ b/config.lua @@ -19,40 +19,40 @@ Config.PlateUseSpace = true Config.Zones = { ShopEntering = { - Pos = { x = -33.777, y = -1102.021, z = 25.422 }, - Size = { x = 1.5, y = 1.5, z = 1.0 }, + Pos = vector3(-33.7, -1102.0, 25.4), + Size = {x = 1.5, y = 1.5, z = 1.0}, Type = 1 }, ShopInside = { - Pos = { x = -47.570, y = -1097.221, z = 25.422 }, - Size = { x = 1.5, y = 1.5, z = 1.0 }, + Pos = vector3(-47.5, -1097.2, 25.4), + Size = {x = 1.5, y = 1.5, z = 1.0}, Heading = -20.0, Type = -1 }, ShopOutside = { - Pos = { x = -28.637, y = -1085.691, z = 25.565 }, - Size = { x = 1.5, y = 1.5, z = 1.0 }, + Pos = vector3(-28.6, -1085.6, 25.5), + Size = {x = 1.5, y = 1.5, z = 1.0}, Heading = 330.0, Type = -1 }, BossActions = { - Pos = { x = -32.065, y = -1114.277, z = 25.422 }, - Size = { x = 1.5, y = 1.5, z = 1.0 }, + Pos = vector3(-32.0, -1114.2, 25.4), + Size = {x = 1.5, y = 1.5, z = 1.0}, Type = -1 }, GiveBackVehicle = { - Pos = { x = -18.227, y = -1078.558, z = 25.675 }, - Size = { x = 3.0, y = 3.0, z = 1.0 }, + Pos = vector3(-18.2, -1078.5, 25.6), + Size = {x = 3.0, y = 3.0, z = 1.0}, Type = (Config.EnablePlayerManagement and 1 or -1) }, ResellVehicle = { - Pos = { x = -44.630, y = -1080.738, z = 25.683 }, - Size = { x = 3.0, y = 3.0, z = 1.0 }, + Pos = vector3(-44.6, -1080.7, 25.6), + Size = {x = 3.0, y = 3.0, z = 1.0}, Type = 1 } diff --git a/server/main.lua b/server/main.lua index 2773e180..ed3182b2 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,6 +1,5 @@ -ESX = nil -local Categories = {} -local Vehicles = {} +ESX = nil +local Categories, Vehicles = {}, {} TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) @@ -46,7 +45,7 @@ MySQL.ready(function() end) RegisterServerEvent('esx_vehicleshop:setVehicleOwned') -AddEventHandler('esx_vehicleshop:setVehicleOwned', function (vehicleProps) +AddEventHandler('esx_vehicleshop:setVehicleOwned', function(vehicleProps) local _source = source local xPlayer = ESX.GetPlayerFromId(_source) @@ -55,13 +54,13 @@ AddEventHandler('esx_vehicleshop:setVehicleOwned', function (vehicleProps) ['@owner'] = xPlayer.identifier, ['@plate'] = vehicleProps.plate, ['@vehicle'] = json.encode(vehicleProps) - }, function (rowsChanged) + }, function(rowsChanged) TriggerClientEvent('esx:showNotification', _source, _U('vehicle_belongs', vehicleProps.plate)) end) end) RegisterServerEvent('esx_vehicleshop:setVehicleOwnedPlayerId') -AddEventHandler('esx_vehicleshop:setVehicleOwnedPlayerId', function (playerId, vehicleProps) +AddEventHandler('esx_vehicleshop:setVehicleOwnedPlayerId', function(playerId, vehicleProps) local xPlayer = ESX.GetPlayerFromId(playerId) MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)', @@ -69,31 +68,28 @@ AddEventHandler('esx_vehicleshop:setVehicleOwnedPlayerId', function (playerId, v ['@owner'] = xPlayer.identifier, ['@plate'] = vehicleProps.plate, ['@vehicle'] = json.encode(vehicleProps) - }, function (rowsChanged) + }, function(rowsChanged) TriggerClientEvent('esx:showNotification', playerId, _U('vehicle_belongs', vehicleProps.plate)) end) end) RegisterServerEvent('esx_vehicleshop:setVehicleOwnedSociety') -AddEventHandler('esx_vehicleshop:setVehicleOwnedSociety', function (society, vehicleProps) +AddEventHandler('esx_vehicleshop:setVehicleOwnedSociety', function(society, vehicleProps) local _source = source local xPlayer = ESX.GetPlayerFromId(_source) - MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)', - { + MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)', { ['@owner'] = 'society:' .. society, ['@plate'] = vehicleProps.plate, ['@vehicle'] = json.encode(vehicleProps), - }, function (rowsChanged) - - end) + }, function(rowsChanged) end) end) RegisterServerEvent('esx_vehicleshop:sellVehicle') -AddEventHandler('esx_vehicleshop:sellVehicle', function (vehicle) +AddEventHandler('esx_vehicleshop:sellVehicle', function(vehicle) MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', { ['@vehicle'] = vehicle - }, function (result) + }, function(result) local id = result[1].id MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', { @@ -107,34 +103,32 @@ AddEventHandler('esx_vehicleshop:addToList', function(target, model, plate) local xPlayer, xTarget = ESX.GetPlayerFromId(source), ESX.GetPlayerFromId(target) local dateNow = os.date('%Y-%m-%d %H:%M') - if xPlayer.job.name ~= 'cardealer' then + if xPlayer.job.name == 'cardealer' then + MySQL.Async.execute('INSERT INTO vehicle_sold (client, model, plate, soldby, date) VALUES (@client, @model, @plate, @soldby, @date)', { + ['@client'] = xTarget.getName(), + ['@model'] = model, + ['@plate'] = plate, + ['@soldby'] = xPlayer.getName(), + ['@date'] = dateNow + }) + else print(('esx_vehicleshop: %s attempted to add a sold vehicle to list!'):format(xPlayer.identifier)) - return end - - MySQL.Async.execute('INSERT INTO vehicle_sold (client, model, plate, soldby, date) VALUES (@client, @model, @plate, @soldby, @date)', { - ['@client'] = xTarget.getName(), - ['@model'] = model, - ['@plate'] = plate, - ['@soldby'] = xPlayer.getName(), - ['@date'] = dateNow - }) end) -ESX.RegisterServerCallback('esx_vehicleshop:getSoldVehicles', function (source, cb) - +ESX.RegisterServerCallback('esx_vehicleshop:getSoldVehicles', function(source, cb) MySQL.Async.fetchAll('SELECT * FROM vehicle_sold', {}, function(result) cb(result) end) end) RegisterServerEvent('esx_vehicleshop:rentVehicle') -AddEventHandler('esx_vehicleshop:rentVehicle', function (vehicle, plate, playerName, basePrice, rentPrice, target) +AddEventHandler('esx_vehicleshop:rentVehicle', function(vehicle, plate, playerName, basePrice, rentPrice, target) local xPlayer = ESX.GetPlayerFromId(target) MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', { ['@vehicle'] = vehicle - }, function (result) + }, function(result) local id = result[1].id local price = result[1].price local owner = xPlayer.identifier @@ -143,8 +137,7 @@ AddEventHandler('esx_vehicleshop:rentVehicle', function (vehicle, plate, playerN ['@id'] = id }) - MySQL.Async.execute('INSERT INTO rented_vehicles (vehicle, plate, player_name, base_price, rent_price, owner) VALUES (@vehicle, @plate, @player_name, @base_price, @rent_price, @owner)', - { + MySQL.Async.execute('INSERT INTO rented_vehicles (vehicle, plate, player_name, base_price, rent_price, owner) VALUES (@vehicle, @plate, @player_name, @base_price, @rent_price, @owner)', { ['@vehicle'] = vehicle, ['@plate'] = plate, ['@player_name'] = playerName, @@ -156,12 +149,12 @@ AddEventHandler('esx_vehicleshop:rentVehicle', function (vehicle, plate, playerN end) RegisterServerEvent('esx_vehicleshop:getStockItem') -AddEventHandler('esx_vehicleshop:getStockItem', function (itemName, count) +AddEventHandler('esx_vehicleshop:getStockItem', function(itemName, count) local _source = source local xPlayer = ESX.GetPlayerFromId(_source) local sourceItem = xPlayer.getInventoryItem(itemName) - TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function (inventory) + TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function(inventory) local item = inventory.getItem(itemName) -- is there enough in the society? @@ -182,11 +175,11 @@ AddEventHandler('esx_vehicleshop:getStockItem', function (itemName, count) end) RegisterServerEvent('esx_vehicleshop:putStockItems') -AddEventHandler('esx_vehicleshop:putStockItems', function (itemName, count) +AddEventHandler('esx_vehicleshop:putStockItems', function(itemName, count) local _source = source local xPlayer = ESX.GetPlayerFromId(_source) - TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function (inventory) + TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function(inventory) local item = inventory.getItem(itemName) if item.count >= 0 then @@ -199,17 +192,17 @@ AddEventHandler('esx_vehicleshop:putStockItems', function (itemName, count) end) end) -ESX.RegisterServerCallback('esx_vehicleshop:getCategories', function (source, cb) +ESX.RegisterServerCallback('esx_vehicleshop:getCategories', function(source, cb) cb(Categories) end) -ESX.RegisterServerCallback('esx_vehicleshop:getVehicles', function (source, cb) +ESX.RegisterServerCallback('esx_vehicleshop:getVehicles', function(source, cb) cb(Vehicles) end) -ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function (source, cb, vehicleModel) - local xPlayer = ESX.GetPlayerFromId(source) - local vehicleData = nil +ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function(source, cb, vehicleModel) + local xPlayer = ESX.GetPlayerFromId(source) + local vehicleData for i=1, #Vehicles, 1 do if Vehicles[i].model == vehicleModel then @@ -218,7 +211,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function (source, cb, v end end - if xPlayer.getMoney() >= vehicleData.price then + if vehicleData and xPlayer.getMoney() >= vehicleData.price then xPlayer.removeMoney(vehicleData.price) cb(true) else @@ -226,8 +219,8 @@ ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function (source, cb, v end end) -ESX.RegisterServerCallback('esx_vehicleshop:buyVehicleSociety', function (source, cb, society, vehicleModel) - local vehicleData = nil +ESX.RegisterServerCallback('esx_vehicleshop:buyVehicleSociety', function(source, cb, society, vehicleModel) + local vehicleData for i=1, #Vehicles, 1 do if Vehicles[i].model == vehicleModel then @@ -236,7 +229,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:buyVehicleSociety', function (source end end - TriggerEvent('esx_addonaccount:getSharedAccount', 'society_' .. society, function (account) + TriggerEvent('esx_addonaccount:getSharedAccount', 'society_' .. society, function(account) if account.money >= vehicleData.price then account.removeMoney(vehicleData.price) @@ -246,15 +239,14 @@ ESX.RegisterServerCallback('esx_vehicleshop:buyVehicleSociety', function (source }, function(rowsChanged) cb(true) end) - else cb(false) end end) end) -ESX.RegisterServerCallback('esx_vehicleshop:getCommercialVehicles', function (source, cb) - MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles ORDER BY vehicle ASC', {}, function (result) +ESX.RegisterServerCallback('esx_vehicleshop:getCommercialVehicles', function(source, cb) + MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles ORDER BY vehicle ASC', {}, function(result) local vehicles = {} for i=1, #result, 1 do @@ -275,10 +267,9 @@ AddEventHandler('esx_vehicleshop:returnProvider', function(vehicleModel) MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', { ['@vehicle'] = vehicleModel - }, function (result) - + }, function(result) if result[1] then - local id = result[1].id + local id = result[1].id local price = ESX.Math.Round(result[1].price * 0.75) TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account) @@ -291,21 +282,19 @@ AddEventHandler('esx_vehicleshop:returnProvider', function(vehicleModel) TriggerClientEvent('esx:showNotification', _source, _U('vehicle_sold_for', vehicleModel, ESX.Math.GroupDigits(price))) else - print(('esx_vehicleshop: %s attempted selling an invalid vehicle!'):format(GetPlayerIdentifiers(_source)[1])) end - end) end) -ESX.RegisterServerCallback('esx_vehicleshop:getRentedVehicles', function (source, cb) - MySQL.Async.fetchAll('SELECT * FROM rented_vehicles ORDER BY player_name ASC', {}, function (result) +ESX.RegisterServerCallback('esx_vehicleshop:getRentedVehicles', function(source, cb) + MySQL.Async.fetchAll('SELECT * FROM rented_vehicles ORDER BY player_name ASC', {}, function(result) local vehicles = {} for i=1, #result, 1 do table.insert(vehicles, { - name = result[i].vehicle, - plate = result[i].plate, + name = result[i].vehicle, + plate = result[i].plate, playerName = result[i].player_name }) end @@ -314,12 +303,12 @@ ESX.RegisterServerCallback('esx_vehicleshop:getRentedVehicles', function (source end) end) -ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function (source, cb, plate) +ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function(source, cb, plate) MySQL.Async.fetchAll('SELECT * FROM rented_vehicles WHERE plate = @plate', { ['@plate'] = plate - }, function (result) - if result[1] ~= nil then - local vehicle = result[1].vehicle + }, function(result) + if result[1] then + local vehicle = result[1].vehicle local basePrice = result[1].base_price MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', { @@ -339,7 +328,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function (source, end) end) -ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function (source, cb, plate, model) +ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function(source, cb, plate, model) local resellPrice = 0 -- calculate the resell price @@ -356,7 +345,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function (source, cb else MySQL.Async.fetchAll('SELECT * FROM rented_vehicles WHERE plate = @plate', { ['@plate'] = plate - }, function (result) + }, function(result) if result[1] then -- is it a rented vehicle? cb(false) -- it is, don't let the player sell it since he doesn't own it else @@ -365,7 +354,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function (source, cb MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND @plate = plate', { ['@owner'] = xPlayer.identifier, ['@plate'] = plate - }, function (result) + }, function(result) if result[1] then -- does the owner match? local vehicle = json.decode(result[1].vehicle) @@ -387,7 +376,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function (source, cb MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND @plate = plate', { ['@owner'] = 'society:' .. xPlayer.job.name, ['@plate'] = plate - }, function (result) + }, function(result) if result[1] then local vehicle = json.decode(result[1].vehicle) @@ -418,36 +407,35 @@ ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function (source, cb end end) - -ESX.RegisterServerCallback('esx_vehicleshop:getStockItems', function (source, cb) +ESX.RegisterServerCallback('esx_vehicleshop:getStockItems', function(source, cb) TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function(inventory) cb(inventory.items) end) end) -ESX.RegisterServerCallback('esx_vehicleshop:getPlayerInventory', function (source, cb) +ESX.RegisterServerCallback('esx_vehicleshop:getPlayerInventory', function(source, cb) local xPlayer = ESX.GetPlayerFromId(source) local items = xPlayer.inventory cb({items = items}) end) -ESX.RegisterServerCallback('esx_vehicleshop:isPlateTaken', function (source, cb, plate) +ESX.RegisterServerCallback('esx_vehicleshop:isPlateTaken', function(source, cb, plate) MySQL.Async.fetchAll('SELECT 1 FROM owned_vehicles WHERE plate = @plate', { ['@plate'] = plate - }, function (result) + }, function(result) cb(result[1] ~= nil) end) end) -ESX.RegisterServerCallback('esx_vehicleshop:retrieveJobVehicles', function (source, cb, type) +ESX.RegisterServerCallback('esx_vehicleshop:retrieveJobVehicles', function(source, cb, type) local xPlayer = ESX.GetPlayerFromId(source) MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND type = @type AND job = @job', { ['@owner'] = xPlayer.identifier, ['@type'] = type, ['@job'] = xPlayer.job.name - }, function (result) + }, function(result) cb(result) end) end) @@ -468,18 +456,16 @@ AddEventHandler('esx_vehicleshop:setJobVehicleState', function(plate, state) end) function PayRent(d, h, m) - MySQL.Async.fetchAll('SELECT * FROM rented_vehicles', {}, function (result) + MySQL.Async.fetchAll('SELECT * FROM rented_vehicles', {}, function(result) for i=1, #result, 1 do local xPlayer = ESX.GetPlayerFromIdentifier(result[i].owner) - -- message player if connected - if xPlayer ~= nil then + if xPlayer then -- message player if connected xPlayer.removeAccountMoney('bank', result[i].rent_price) TriggerClientEvent('esx:showNotification', xPlayer.source, _U('paid_rental', ESX.Math.GroupDigits(result[i].rent_price))) - else -- pay rent either way - MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier', - { - ['@bank'] = result[i].rent_price, + else -- pay rent by updating SQL + MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier', { + ['@bank'] = result[i].rent_price, ['@identifier'] = result[i].owner }) end