All-Resources/vote/example_esx.lua
2026-04-14 17:41:39 +02:00

73 lines
2.8 KiB
Lua

-- Keine manuelle ESX-Initialisierung nötig, da wir die imports.lua in der fxmanifest haben!
local Config = {}
Config.GiveCash = 100
Config.GiveBank = 500
Config.GiveOffline = true
Config.Debug = 'TOP-SERVEURS'
Config.DebugCommand = true
local debug = function(msg)
if Config.Debug then print('^7[^5'..Config.Debug..'^7] '..msg..'^7') end
end
-- Test-Command (bleibt gleich)
if Config.DebugCommand then
RegisterCommand('testvote', function(source, args)
if source == 0 then
if args[1] and args[2] then
local playername = args[1]..' '..args[2]
debug('Simulated vote for: ^5'..playername)
TriggerEvent('onPlayerVote', playername)
else
print("Nutze: testvote Vorname Nachname")
end
end
end, false)
end
AddEventHandler('onPlayerVote', function (playername, date)
-- 1. Versuch: Spieler Online finden
local xPlayer = nil
local players = ESX.GetExtendedPlayers() -- ESX ist durch imports.lua bereits global verfügbar
for _, p in pairs(players) do
if string.lower(p.getName()) == string.lower(playername) then
xPlayer = p
break
end
end
if xPlayer then
if Config.GiveCash > 0 then xPlayer.addAccountMoney('money', Config.GiveCash) end
if Config.GiveBank > 0 then xPlayer.addAccountMoney('bank', Config.GiveBank) end
-- NUTZUNG VON HEX_4_HUD NOTIFY
-- Syntax: exports['hex_4_hud']:Notify(title, message, type, timeout)
TriggerClientEvent('hex_4_hud:Notify', xPlayer.source, 'VOTE', 'Danke fürs Voten! Du hast deine Belohnung erhalten.', 'success', 5000)
debug('Belohnung an Online-Spieler gesendet: '..xPlayer.getName())
elseif Config.GiveOffline then
-- 2. Versuch: Offline (Datenbank)
local fName, lName = playername:match("^(%S+)%s+(%S+)$")
if not fName or not lName then return end
exports.oxmysql:execute('SELECT identifier, accounts FROM users WHERE firstname = ? AND lastname = ?', {fName, lName}, function(result)
if result and result[1] then
local accounts = json.decode(result[1].accounts)
if accounts then
accounts.money = (accounts.money or 0) + Config.GiveCash
accounts.bank = (accounts.bank or 0) + Config.GiveBank
exports.oxmysql:execute('UPDATE users SET accounts = ? WHERE identifier = ?', {
json.encode(accounts),
result[1].identifier
})
debug('Offline-Belohnung für '..fName..' '..lName..' in DB gespeichert.')
end
end
end)
end
end)