Skip to content

Commit

Permalink
Update 0.7.7 - Added new damageMultipliers config for each vehicle cl…
Browse files Browse the repository at this point in the history
…ass and organized serverside code.
  • Loading branch information
Muhaddil committed Nov 9, 2024
1 parent f050d9f commit 6d4b39d
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 132 deletions.
20 changes: 15 additions & 5 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local originalTractionCurveMin = nil
local originalLowSpeedTractionLossMult = nil
local lastVehicle = nil
local lastVehicle2 = nil
local damageMultiplier = Config.damageMultiplier
-- local damageMultiplier = Config.damageMultiplier
local checkInterval2 = Config.CheckIntervalEngineDamage
local previousSpeed = 0
local speedLimitActive = false
Expand Down Expand Up @@ -48,7 +48,7 @@ function SendNotification(msgtitle, msg, time, type)
title = msgtitle,
description = msg,
showDuration = true,
type = type2,
type = type,
style = {
backgroundColor = 'rgba(0, 0, 0, 0.75)',
color = 'rgba(255, 255, 255, 1)',
Expand Down Expand Up @@ -194,7 +194,6 @@ Citizen.CreateThread(function()
local plate = GetVehicleNumberPlateText(vehicle)
local currentCoords = GetEntityCoords(playerPed)

-- Verificar si el vehículo no tiene dueño
if not isVehicleOwned(plate) then
DebugPrint('Vehículo no es de nadie, no se actualizarán los kilómetros')
goto continueLoop
Expand Down Expand Up @@ -259,7 +258,7 @@ Citizen.CreateThread(function()
local currentTime = GetGameTimer()
local lastBreakdownTime = vehicleCooldown[plate] or 0

if currentTime - lastBreakdownTime >= Config.BreakdownCooldown then
if (currentTime - lastBreakdownTime) >= Config.BreakdownCooldown then
if math.random() < breakdownChance then
TriggerEvent('realistic-vehicle:breakdown', vehicle)
vehicleCooldown[plate] = currentTime
Expand Down Expand Up @@ -465,6 +464,16 @@ Citizen.CreateThread(function()
local currentSpeed = GetEntitySpeed(vehicle) * 3.6
local speedDifference = previousSpeed - currentSpeed

local vehicleClass = GetVehicleClass(vehicle)
local damageMultiplier = Config.damageMultiplier

if Config.ClassDamageMultipliers[vehicleClass] then
damageMultiplier = Config.ClassDamageMultipliers[vehicleClass].damageMultiplier
DebugPrint("Multiplicador de daño para la clase " .. vehicleClass .. ": " .. damageMultiplier)
else
DebugPrint("Clase de vehículo no encontrada, usando multiplicador por defecto.")
end

if speedDifference > 50 then
local damageAmount = speedDifference * damageMultiplier / 2
DebugPrint('Daño al motor: ' .. damageAmount)
Expand All @@ -473,6 +482,7 @@ Citizen.CreateThread(function()

previousSpeed = currentSpeed
end

Citizen.Wait(checkInterval2)
end
end)
Expand All @@ -497,7 +507,7 @@ if Config.preventVehicleFlip then
if vehicle ~= 0 and GetPedInVehicleSeat(vehicle, -1) == playerPed then
local roll = GetEntityRoll(vehicle)

if (roll > 75.0 or roll < -75.0) and GetEntitySpeed(vehicle) < 2 then
if (roll > 75.0 or roll < -75.0) and GetEntitySpeed(vehicle) < 10 then
DisableControlAction(2, 59, true)
DisableControlAction(2, 60, true)
waitTime = 10
Expand Down
40 changes: 32 additions & 8 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Config = {}

-- Debugging mode
Config.DebugMode = false
Config.DebugMode = true
Config.ShowNotifications = true

-- Optimal configuration for debugging
Expand All @@ -14,16 +14,40 @@ Config.KilometerMultiplier = 1.0 -- Multiplier for vehicle mileage accumu
Config.BaseBreakdownChance = 0.01 -- Base failure probability per 1000 km
Config.MaxBreakdownChance = 0.5 -- Maximum probability of failure
Config.BreakdownCooldown = 10800000 -- Cooldown in milliseconds (e.g. 10800000 ms = 3 hours)
Config.SpeedToDamageRatio = 1.0 -- Does nothing | Useless
Config.preventVehicleFlip = true -- Disable flipping overturned cars
Config.damageMultiplier = 5 -- Damage multiplier applied to the engine in each crash
Config.ApplyDamageAll = true -- If the ApplyEngineDamage function applies the damage to the engine only or to engine/petroltank(if Config.ApplyDamagePetrol true)/body.
Config.AutoRunSQL = true -- If the script should run the SQL file automatically
Config.AutoVersionChecker = true -- If the script should search for the latest version and warn you in the console if founds one
Config.FrameWork = "esx" -- Only compatible with esx or qb (for the moment)
Config.UseOXNotifications = true -- If the script uses the ox_libs notifications or framework ones
Config.damageMultiplier = 5 -- Damage multiplier (general) applied to the engine in each crash
Config.ApplyDamageAll = true -- If the ApplyEngineDamage function applies the damage to the engine only or to engine/petroltank (if Config.ApplyDamagePetrol true)/body.
Config.ApplyDamagePetrol = false -- If the ApplyEngineDamage function applies the damage to petroltank, it can cause lots of fires.
Config.CheckIntervalEngineDamage = 2000 -- Cooldown in milliseconds
Config.AutoRunSQL = true
Config.AutoVersionChecker = true
Config.FrameWork = "esx" -- Only compatible with esx or qb (for the moment)
Config.UseOXNotifications = true
Config.ClassDamageMultipliers = { -- Damage multiplier (specific for each vehicle class) applied to the engine in each crash
[0] = { damageMultiplier = 4.5 }, -- Compacts
[1] = { damageMultiplier = 5.0 }, -- Sedans
[2] = { damageMultiplier = 5.5 }, -- SUVs
[3] = { damageMultiplier = 4.8 }, -- Coupes
[4] = { damageMultiplier = 5.5 }, -- Muscle
[5] = { damageMultiplier = 5.3 }, -- Sports Classics
[6] = { damageMultiplier = 6.0 }, -- Sports
[7] = { damageMultiplier = 6.5 }, -- Super
[8] = { damageMultiplier = 3.0 }, -- Motorcycles
[9] = { damageMultiplier = 5.2 }, -- Off-road
[10] = { damageMultiplier = 7.0 }, -- Industrial
[11] = { damageMultiplier = 5.5 }, -- Utility
[12] = { damageMultiplier = 5.0 }, -- Vans
[13] = { damageMultiplier = 2.5 }, -- Cycles
[14] = { damageMultiplier = 4.0 }, -- Boats
[15] = { damageMultiplier = 8.0 }, -- Helicopters
[16] = { damageMultiplier = 9.0 }, -- Planes
[17] = { damageMultiplier = 5.0 }, -- Service
[18] = { damageMultiplier = 5.0 }, -- Emergency
[19] = { damageMultiplier = 6.0 }, -- Military
[20] = { damageMultiplier = 7.5 }, -- Commercial
[21] = { damageMultiplier = 10.0 }, -- Trains
[22] = { damageMultiplier = 5.5 }, -- Open Wheel
}

-- Setting to use mileages systems (config your own external system if you have one in server.lua line 34)
Config.MileageSystem = 'default' -- default / jg-vehiclemileage / other
Expand Down
4 changes: 2 additions & 2 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ lua54 'yes'

author 'Muhaddil'
description 'Mileage-based vehicle breakdown system for ESX&QBCore'
version 'v0.7.61-beta'
version 'v0.7.7-beta'

shared_script 'config.lua'
client_script 'client.lua'
server_script {
'@mysql-async/lib/MySQL.lua',
'server.lua'
'server/*'
}

shared_script '@ox_lib/init.lua'
Expand Down
3 changes: 1 addition & 2 deletions nui/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
meterElement.style.right = "";
meterElement.style.transform = "";

// Asegurarnos de que el elemento esté siempre dentro de los márgenes de la pantalla
const padding = "5%";

switch (position) {
Expand Down Expand Up @@ -93,7 +92,7 @@
}
});

let currentSound; // Variable para almacenar el sonido actual
let currentSound;

window.addEventListener("message", (event) => {
const data = event.data;
Expand Down
115 changes: 0 additions & 115 deletions server.lua → server/externallogic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,6 @@ local currentVersion = GetResourceMetadata(GetCurrentResourceName(), 'version')
local resourceName = 'Muhaddil/FiveM-MilageVehicleFailure'
local githubApiUrl = 'https://api.github.com/repos/' .. resourceName .. '/releases/latest'

if Config.FrameWork == "esx" then
ESX = exports['es_extended']:getSharedObject()
ESX.VehiclesTable = "owned_vehicles"
elseif Config.FrameWork == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
QBCore.VehiclesTable = "player_vehicles"
end

if Config.FrameWork == "esx" then
ESX.RegisterServerCallback('realistic-vehicle:fetchKilometers', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT kilometers FROM vehicle_kilometers WHERE plate = @plate', {
['@plate'] = plate
}, function(kilometers)
cb(kilometers)
end)
end)

ESX.RegisterServerCallback('realistic-vehicle:fetchKilometersFromDB', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT adv_stats FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = plate
}, function(adv_stats)
if adv_stats then
local stats = json.decode(adv_stats)
if stats and stats.mileage then
cb(stats.mileage)
else
cb(0)
end
else
cb(0)
end
end)
end)
elseif Config.FrameWork == "qb" then
QBCore.Functions.CreateCallback('realistic-vehicle:fetchKilometers', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT kilometers FROM vehicle_kilometers WHERE plate = @plate', {
['@plate'] = plate
}, function(kilometers)
cb(kilometers)
end)
end)

QBCore.Functions.CreateCallback('realistic-vehicle:fetchKilometersFromDB', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT adv_stats FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = plate
}, function(adv_stats)
if adv_stats then
local stats = json.decode(adv_stats)
if stats and stats.mileage then
cb(stats.mileage)
else
cb(0)
end
else
cb(0)
end
end)
end)
end

RegisterServerEvent('realistic-vehicle:updateKilometers')
AddEventHandler('realistic-vehicle:updateKilometers', function(plate, kilometers)
MySQL.Async.execute(
'INSERT INTO vehicle_kilometers (plate, kilometers) VALUES (@plate, @kilometers) ON DUPLICATE KEY UPDATE kilometers = @kilometers',
{
['@plate'] = plate,
['@kilometers'] = kilometers
})
end)

lib.callback.register("realistic-vehicle:get-mileage-JG", function(_, plate)
local distance, unit = exports["jg-vehiclemileage"]:GetMileage(plate)
return { mileage = distance, unit = unit }
end)

lib.callback.register("realistic-vehicle:isVehOwned", function(_, plate)
local isOwned = false

if Config.FrameWork == "esx" then
local result = MySQL.Sync.fetchAll("SELECT * FROM " .. ESX.VehiclesTable .. " WHERE plate = @plate", {
['@plate'] = plate
})

isOwned = #result > 0

elseif Config.FrameWork == "qb" then
local result = MySQL.Sync.fetchAll("SELECT * FROM " .. QBCore.VehiclesTable .. " WHERE plate = @plate", {
['@plate'] = plate
})

isOwned = #result > 0
end

return isOwned
end)

if Config.DebugMode then
RegisterServerEvent('realistic-vehicle:testBreakdown')
AddEventHandler('realistic-vehicle:testBreakdown', function()
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer and xPlayer.getGroup() == 'admin' then
TriggerClientEvent('realistic-vehicle:triggerTestBreakdown', source)
else
print(('%s intentó usar /testbreakdown sin permisos'):format(GetPlayerIdentifiers(source)[1]))
end
end)
end

RegisterNetEvent('vehicle:damageStatus')
AddEventHandler('vehicle:damageStatus', function()
TriggerClientEvent('chat:addMessage', source, {
args = { 'Sistema de Daño', 'El motor ha recibido ' .. engineDamage .. ' puntos de daño.' }
})
end)

if Config.AutoRunSQL then
if not pcall(function()
Expand Down
115 changes: 115 additions & 0 deletions server/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
if Config.FrameWork == "esx" then
ESX = exports['es_extended']:getSharedObject()
ESX.VehiclesTable = "owned_vehicles"
elseif Config.FrameWork == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
QBCore.VehiclesTable = "player_vehicles"
end

if Config.FrameWork == "esx" then
ESX.RegisterServerCallback('realistic-vehicle:fetchKilometers', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT kilometers FROM vehicle_kilometers WHERE plate = @plate', {
['@plate'] = plate
}, function(kilometers)
cb(kilometers)
end)
end)

ESX.RegisterServerCallback('realistic-vehicle:fetchKilometersFromDB', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT adv_stats FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = plate
}, function(adv_stats)
if adv_stats then
local stats = json.decode(adv_stats)
if stats and stats.mileage then
cb(stats.mileage)
else
cb(0)
end
else
cb(0)
end
end)
end)
elseif Config.FrameWork == "qb" then
QBCore.Functions.CreateCallback('realistic-vehicle:fetchKilometers', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT kilometers FROM vehicle_kilometers WHERE plate = @plate', {
['@plate'] = plate
}, function(kilometers)
cb(kilometers)
end)
end)

QBCore.Functions.CreateCallback('realistic-vehicle:fetchKilometersFromDB', function(source, cb, plate)
MySQL.Async.fetchScalar('SELECT adv_stats FROM owned_vehicles WHERE plate = @plate', {
['@plate'] = plate
}, function(adv_stats)
if adv_stats then
local stats = json.decode(adv_stats)
if stats and stats.mileage then
cb(stats.mileage)
else
cb(0)
end
else
cb(0)
end
end)
end)
end

RegisterServerEvent('realistic-vehicle:updateKilometers')
AddEventHandler('realistic-vehicle:updateKilometers', function(plate, kilometers)
MySQL.Async.execute(
'INSERT INTO vehicle_kilometers (plate, kilometers) VALUES (@plate, @kilometers) ON DUPLICATE KEY UPDATE kilometers = @kilometers',
{
['@plate'] = plate,
['@kilometers'] = kilometers
})
end)

lib.callback.register("realistic-vehicle:get-mileage-JG", function(_, plate)
local distance, unit = exports["jg-vehiclemileage"]:GetMileage(plate)
return { mileage = distance, unit = unit }
end)

lib.callback.register("realistic-vehicle:isVehOwned", function(_, plate)
local isOwned = false

if Config.FrameWork == "esx" then
local result = MySQL.Sync.fetchAll("SELECT * FROM " .. ESX.VehiclesTable .. " WHERE plate = @plate", {
['@plate'] = plate
})

isOwned = #result > 0

elseif Config.FrameWork == "qb" then
local result = MySQL.Sync.fetchAll("SELECT * FROM " .. QBCore.VehiclesTable .. " WHERE plate = @plate", {
['@plate'] = plate
})

isOwned = #result > 0
end

return isOwned
end)

if Config.DebugMode then
RegisterServerEvent('realistic-vehicle:testBreakdown')
AddEventHandler('realistic-vehicle:testBreakdown', function()
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer and xPlayer.getGroup() == 'admin' then
TriggerClientEvent('realistic-vehicle:triggerTestBreakdown', source)
else
print(('%s intentó usar /testbreakdown sin permisos'):format(GetPlayerIdentifiers(source)[1]))
end
end)
end

RegisterNetEvent('vehicle:damageStatus')
AddEventHandler('vehicle:damageStatus', function()
TriggerClientEvent('chat:addMessage', source, {
args = { 'Sistema de Daño', 'El motor ha recibido ' .. engineDamage .. ' puntos de daño.' }
})
end)

0 comments on commit 6d4b39d

Please sign in to comment.