From d6e39e264d424dffc318fa64b2be552fe180e2a6 Mon Sep 17 00:00:00 2001 From: Muhaddil <151466679+Muhaddil@users.noreply.github.com> Date: Tue, 15 Oct 2024 20:29:01 +0200 Subject: [PATCH] Update 1.0.1 - Added option to sell insurances with discounts for some determined jobs. --- client.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ config.lua | 11 ++++++++ fxmanifest.lua | 2 +- server.lua | 14 ++++++----- 4 files changed, 88 insertions(+), 7 deletions(-) diff --git a/client.lua b/client.lua index 623e241..18e0b51 100644 --- a/client.lua +++ b/client.lua @@ -180,3 +180,71 @@ AddEventHandler('muhaddil_insurances:checkInsurance', function() Notify("Acceso denegado", "No tienes el trabajo adecuado para acceder a esta función.", 5000, "error") end end) + +function CanSellDiscountInsurance() + if hasUsedDiscount then + return false + end + + if Config.UseDiscounts == false then + return false + else + local playerJob = nil + if Config.FrameWork == "esx" then + playerJob = ESX.GetPlayerData().job.name + elseif Config.FrameWork == "qb" then + playerJob = QBCore.Functions.GetPlayerData().job.name + end + + for _, job in ipairs(Config.DiscountJobs) do + if playerJob == job then + return true + end + end + + return false + end +end + +function GetClosestPlayer() + local players = GetActivePlayers() + local closestDistance = -1 + local closestPlayer = -1 + local playerPed = PlayerPedId() + local playerCoords = GetEntityCoords(playerPed) + + for i = 1, #players do + local target = GetPlayerPed(players[i]) + if target ~= playerPed then + local targetCoords = GetEntityCoords(target) + local distance = #(playerCoords - targetCoords) + + if closestDistance == -1 or distance < closestDistance then + closestPlayer = players[i] + closestDistance = distance + end + end + end + + return closestPlayer, closestDistance +end + +RegisterNetEvent('muhaddil_insurances:insurance:buyDiscount') +AddEventHandler('muhaddil_insurances:insurance:buyDiscount', function(data) + if not hasUsedDiscount then + local closestPlayer, closestDistance = GetClosestPlayer() + + if closestPlayer ~= -1 and closestDistance <= 3.0 then + local targetPlayerId = GetPlayerServerId(closestPlayer) + local accountType = Config.Account + + TriggerServerEvent('muhaddil_insurances:insurance:buy', data, accountType, targetPlayerId) + hasUsedDiscount = true + Notify("Descuento aplicado", "Has vendido un seguro con descuento al jugador más cercano.", 5000, "success") + else + Notify("Error", "No hay ningún jugador cerca para aplicar el descuento.", 5000, "error") + end + else + Notify("Descuento ya utilizado", "Ya has usado el descuento para esta sesión.", 5000, "error") + end +end) diff --git a/config.lua b/config.lua index 4140946..a462e1d 100644 --- a/config.lua +++ b/config.lua @@ -13,6 +13,8 @@ Config.UseOXNotifications = true -- Enable or disable OX Notifications. If 'true Config.Account = 'money' -- Choose the account type for transactions: 'bank' to use the player's bank account or 'money' to use cash. Config.OnlyAllowedJobs = false -- Enable or disable restricted access to the insurance menu. If 'true', only specific jobs can access. If 'false', everyone can access. Config.AllowedJobs = {"ambulance", "police", "safd"} -- List of allowed jobs. Only these jobs can access the insurance menu when 'OnlyAllowedJobs' is set to true. +Config.DiscountJobs = { "ambulance" } -- List of jobs that are allowed to sell insurance at a discounted rate. +Config.UseDiscounts = true -- Setting this to true allows players (with specified jobs) to sell insurance at a discounted rate. Config.BlipLabel = 'Seguros Médicos' -- The label displayed for the blip on the map, indicating the location of medical insurance services. Config.ShowBlip = true -- Enable or disable the display of the blip on the map. If 'true', the blip will be shown; if 'false', it will be hidden. @@ -71,6 +73,15 @@ function openInsuranceMenu(insuranceData) event = 'muhaddil_insurances:insurance:buy', args = { type = "premium", duration = 30, price = 100000 } -- Arguments for the event. }) + if CanSellDiscountInsurance() then + table.insert(options, { + title = 'Vender Seguro con Descuento', + description = 'Duración: 30 días\nPrecio: $50000 (descuento)', + icon = 'circle', + event = 'muhaddil_insurances:insurance:buyDiscount', + args = { type = "premium", duration = 30, price = 50000 } + }) + end end lib.registerContext({ diff --git a/fxmanifest.lua b/fxmanifest.lua index 1074009..bcc12e3 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,7 +4,7 @@ lua54 'yes' author 'Muhaddil' description 'Simple Medical Insurance Script' -version 'v1.0.0' +version 'v1.0.1' shared_script 'config.lua' client_script 'client.lua' diff --git a/server.lua b/server.lua index cec0a60..0f7ab6b 100644 --- a/server.lua +++ b/server.lua @@ -9,7 +9,7 @@ elseif Config.FrameWork == "qb" then end RegisterServerEvent('muhaddil_insurances:insurance:buy') -AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType) +AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType, targetPlayerId) local source = source local identifier = nil local xPlayer = nil @@ -20,11 +20,13 @@ AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType) local price = data.price local expiration = os.time() + (duration * 24 * 60 * 60) + local playerId = targetPlayerId or source + if Config.FrameWork == "esx" then - xPlayer = ESX.GetPlayerFromId(source) + xPlayer = ESX.GetPlayerFromId(playerId) identifier = xPlayer.identifier elseif Config.FrameWork == "qb" then - xPlayer = QBCore.Functions.GetPlayer(source) + xPlayer = QBCore.Functions.GetPlayer(playerId) identifier = xPlayer.PlayerData.citizenid end @@ -81,15 +83,15 @@ AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType) ['@expiration'] = expiration }, function(rowsChanged) if rowsChanged > 0 then - TriggerClientEvent('muhaddil_insurances:Notify', source, 'Seguro', + TriggerClientEvent('muhaddil_insurances:Notify', playerId, 'Seguro', 'Has comprado un seguro: ' .. type .. ' por ' .. duration .. ' días', 5000, 'success') else - TriggerClientEvent('muhaddil_insurances:Notify', source, 'Seguro', + TriggerClientEvent('muhaddil_insurances:Notify', playerId, 'Seguro', 'Hubo un error al contratar el seguro', 5000, 'error') end end) else - TriggerClientEvent('muhaddil_insurances:Notify', source, 'Seguro', + TriggerClientEvent('muhaddil_insurances:Notify', playerId, 'Seguro', 'No tienes suficiente dinero para comprar este seguro', 5000, 'error') end end)