Skip to content

Commit

Permalink
Update 1.0.8 - Enhanced player selection when selling insurances by d…
Browse files Browse the repository at this point in the history
…isplaying full names.

- Added Discord webhook logging for insurance purchases.
  • Loading branch information
Muhaddil committed Dec 22, 2024
1 parent 8fd79cf commit c4cc552
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
13 changes: 11 additions & 2 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,23 @@ RegisterNetEvent('muhaddil_insurances:insurance:customPrice', function()
end

local playerOptions = {}

-- Iteramos por los jugadores cercanos
for _, player in ipairs(nearbyPlayers) do
local serverId = GetPlayerServerId(player.id)

-- Llamada para obtener el nombre del jugador
local playerNameData = lib.callback.await('getPlayerNameInGame', serverId)
local playerName = playerNameData.firstname .. " " .. playerNameData.lastname

-- Añadimos la opción con el nombre completo
table.insert(playerOptions, {
value = serverId,
label = locale('select_nearby_player_label') .. ': ' .. serverId
label = locale('select_nearby_player_label') .. ': ' .. playerName .. ' (' ..serverId.. ')'
})
end

-- Mostramos el diálogo para seleccionar un jugador
local selectPlayer = lib.inputDialog(locale('select_nearby_player'), {
{type = 'select', label = locale('select_nearby_player_label'), options = playerOptions, required = true}
})
Expand All @@ -363,7 +372,7 @@ RegisterNetEvent('muhaddil_insurances:insurance:customPrice', function()
local price = tonumber(input[3])

if not insuranceType or duration <= 0 or price <= 0 then
print(locale('invalid_data'))
print(locale('invalid_data'))
return
end

Expand Down
4 changes: 4 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Config = {}

Config.webHook = 'https://discord.com/api/webhooks/1308527212382785597/WZJe9WyQUb-lxf0-db1kNY_yCnE7TiO18yzrHgEG1wYfEC2RxAhun1XxaXxSKEnHKFwF'
Config.webHookName = 'Logs muhaddil-machines' -- Name of the WebHook
Config.webHookLogo = 'https://github.com/Muhaddil/RSSWikiPageCreator/blob/main/public/assets/other/MuhaddilOG.png?raw=true' -- Logo of the WebHook bot

Config.Locations = {
["insurances"] = {
vector4(296.4421, -591.3871, 43.2757, 65.5415), -- Coordinates for the insurance location. You can add several possitions.
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.7'
version 'v1.0.8'

shared_script 'config.lua'
client_script 'client.lua'
Expand Down
71 changes: 71 additions & 0 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ elseif Config.FrameWork == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
end

local function discordWebHookSender(name, message, color)
local connect = {
{
["color"] = color,
["title"] = "**" .. name .. "**",
["description"] = message,
["footer"] = {
["text"] = os.date("%Y-%m-%d %H:%M:%S"),
},
}
}
PerformHttpRequest(Config.webHook, function(err, text, headers) end, 'POST',
json.encode({ username = Config.webHookName, avatar_url = Config.webHookLogo, embeds = connect }),
{ ['Content-Type'] = 'application/json' })
end

RegisterServerEvent('muhaddil_insurances:insurance:buy')
AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType, targetPlayerId)
local source = source
local identifier = nil
local xPlayer = nil
local hasEnoughMoney = false
local playerName = "Desconocido"
local currentMoney = 0
local type = data.type
local duration = data.duration
Expand All @@ -25,9 +42,11 @@ AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType,
if Config.FrameWork == "esx" then
xPlayer = ESX.GetPlayerFromId(playerId)
identifier = xPlayer.identifier
playerName = xPlayer.getName() or xPlayer.getIdentifier()
elseif Config.FrameWork == "qb" then
xPlayer = QBCore.Functions.GetPlayer(playerId)
identifier = xPlayer.PlayerData.citizenid
playerName = (xPlayer.PlayerData.charinfo.firstname and xPlayer.PlayerData.charinfo.lastname) and xPlayer.PlayerData.charinfo.firstname .. " " .. xPlayer.PlayerData.charinfo.lastname or xPlayer.PlayerData.citizenid
end

-- Money checking
Expand Down Expand Up @@ -85,6 +104,7 @@ AddEventHandler('muhaddil_insurances:insurance:buy', function(data, accountType,
if rowsChanged > 0 then
TriggerClientEvent('muhaddil_insurances:Notify', playerId, 'Seguro',
'Has comprado un seguro: ' .. type .. ' por ' .. duration .. ' días', 5000, 'success')
discordWebHookSender("Compra de Seguro", "El jugador **" ..playerName .. "** (ID: " ..playerId ..") ha comprado un seguro de tipo **" .. type .. "** por **" .. duration .. "** días. Precio: $" .. price, 3066993)
else
TriggerClientEvent('muhaddil_insurances:Notify', playerId, 'Seguro',
'Hubo un error al contratar el seguro', 5000, 'error')
Expand Down Expand Up @@ -418,3 +438,54 @@ RegisterNetEvent('muhaddil_insurance:checkInsuranceExport', function()

TriggerClientEvent('muhaddil_insurance:insuranceResult', playerId, hasInsurance)
end)

lib.callback.register('getPlayerNameInGame', function(targetPlayerServerId)
local playerData = {}

if Config.FrameWork == "esx" then
local xPlayer = ESX.GetPlayerFromId(targetPlayerServerId)
if not xPlayer then
return { firstname = "Desconocido", lastname = "" }
end

while not xPlayer.identifier do
Citizen.Wait(100)
end

local result = MySQL.Sync.fetchAll('SELECT firstname, lastname FROM `users` WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
})

if result[1] and result[1].firstname and result[1].lastname then
playerData.firstname = result[1].firstname
playerData.lastname = result[1].lastname
else
playerData.firstname = "Unknown"
playerData.lastname = ""
end

elseif Config.FrameWork == "qb" then
local player = QBCore.Functions.GetPlayer(targetPlayerServerId)
if not player then
return { firstname = "Desconocido", lastname = "" }
end

while not player.PlayerData.citizenid do
Citizen.Wait(100)
end

local result = MySQL.Sync.fetchAll('SELECT firstname, lastname FROM `players` WHERE citizenid = @citizenid', {
['@citizenid'] = player.PlayerData.citizenid
})

if result[1] and result[1].firstname and result[1].lastname then
playerData.firstname = result[1].firstname
playerData.lastname = result[1].lastname
else
playerData.firstname = "Unknown"
playerData.lastname = ""
end
end

return playerData
end)

0 comments on commit c4cc552

Please sign in to comment.