Skip to content

Commit

Permalink
Update 1.0.1 - Added option to sell insurances with discounts for som…
Browse files Browse the repository at this point in the history
…e determined jobs.
  • Loading branch information
Muhaddil committed Oct 15, 2024
1 parent 9f0a578 commit d6e39e2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
68 changes: 68 additions & 0 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 8 additions & 6 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d6e39e2

Please sign in to comment.