ESX = exports['es_extended']:getSharedObject() -- ============================================================ -- CONFIG -- ============================================================ local Config = { webhookUrl = 'https://discord.com/api/webhooks/1493707634061082754/JPWbzATVb1zoP5Ixj1NDSBRRPnYimxV7A4KRSshUutQ6mIEEW-Ud0_YiGM-2BA-iKcqY', -- Discord webhook URL rewardMoney = 100000, -- $100,000 rewardItem = 'exp3', -- Itemul reward rewardItemCount = 3, -- 3x iteme minLevel = 25 -- Nivel minim pentru cod personal } -- ============================================================ -- HELPER: Trimite webhook Discord -- ============================================================ local function sendWebhook(title, description, color) local embed = { { title = title, description = description, color = color or 5814783, footer = { text = 'LEVEL UP - Referal System' }, timestamp = os.date('!%Y-%m-%dT%H:%M:%SZ') } } PerformHttpRequest(Config.webhookUrl, function(err, text, headers) end, 'POST', json.encode({ username = 'LEVEL UP Referal', embeds = embed }), { ['Content-Type'] = 'application/json' } ) end -- ============================================================ -- HELPER: Obtine nivelul unui jucator din esx_npc_xp -- ============================================================ local function getPlayerLevel(identifier, cb) MySQL.single('SELECT level FROM esx_npc_xp WHERE identifier = ?', { identifier }, function(row) if row then cb(row.level or 1) else cb(1) end end) end -- ============================================================ -- HELPER: Genereaza cod random -- ============================================================ local charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' local function generateCode(length) math.randomseed(os.time() + math.random(1, 99999)) local code = '' for i = 1, length do local idx = math.random(1, #charset) code = code .. charset:sub(idx, idx) end return code end -- ============================================================ -- DATABASE: Creaza tabelele la pornire -- ============================================================ MySQL.ready(function() MySQL.update([[ CREATE TABLE IF NOT EXISTS `lvl_referals` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `owner_identifier` VARCHAR(100) NOT NULL, `owner_name` VARCHAR(100) NOT NULL, `referal_code` VARCHAR(100) NOT NULL UNIQUE, `used_by_identifier` VARCHAR(100) DEFAULT NULL, `used_by_name` VARCHAR(100) DEFAULT NULL, `used` TINYINT(1) DEFAULT 0, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `used_at` TIMESTAMP NULL DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ]]) MySQL.update([[ CREATE TABLE IF NOT EXISTS `lvl_referal_pending` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `identifier` VARCHAR(100) NOT NULL, `money` INT NOT NULL DEFAULT 0, `item` VARCHAR(50) NOT NULL, `item_count` INT NOT NULL DEFAULT 0, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ]]) print('^2[LEVEL UP]^0 Referal system loaded successfully!') end) -- ============================================================ -- CALLBACK: Obtine datele de referal ale jucatorului -- Toti pot accesa /referal -- Sub lvl 25: mode = "new" (poate doar introduce codul unui veteran) -- Lvl 25+: mode = "veteran" (are cod personal + poate introduce cod) -- ============================================================ ESX.RegisterServerCallback('lvl-referal:getMyCode', function(source, cb) local xPlayer = ESX.GetPlayerFromId(source) if not xPlayer then return cb(nil) end local identifier = xPlayer.getIdentifier() local playerName = xPlayer.getName() getPlayerLevel(identifier, function(level) if level < Config.minLevel then -- Jucator nou — nu are cod personal, dar poate folosi codul unui veteran -- Verifica daca a folosit deja un cod MySQL.single('SELECT id FROM lvl_referals WHERE used_by_identifier = ?', { identifier }, function(alreadyUsed) cb({ mode = 'new', level = level, minLevel = Config.minLevel, alreadyUsed = alreadyUsed ~= nil }) end) else -- Veteran lvl 25+ — are cod personal random MySQL.single('SELECT referal_code, used, used_by_name FROM lvl_referals WHERE owner_identifier = ?', { identifier }, function(row) if row then cb({ mode = 'veteran', level = level, code = row.referal_code, used = row.used == 1, usedByName = row.used_by_name }) else -- Genereaza cod random nou local code = generateCode(8) MySQL.insert('INSERT INTO lvl_referals (owner_identifier, owner_name, referal_code) VALUES (?, ?, ?)', { identifier, playerName, code }, function() cb({ mode = 'veteran', level = level, code = code, used = false, usedByName = nil }) end) end end) end end) end) -- ============================================================ -- EVENT: Foloseste un cod de referal -- ============================================================ RegisterNetEvent('lvl-referal:useCode') AddEventHandler('lvl-referal:useCode', function(inputCode) local src = source local xPlayer = ESX.GetPlayerFromId(src) if not xPlayer then return end local playerIdentifier = xPlayer.getIdentifier() local playerName = xPlayer.getName() -- Validare input if not inputCode or type(inputCode) ~= 'string' or #inputCode < 1 then xPlayer.showNotification('~r~Codul introdus nu este valid!', 'error') return end inputCode = inputCode:gsub('^%s+', ''):gsub('%s+$', ''):upper() -- trim + uppercase -- Verifica daca jucatorul nu a folosit deja un cod MySQL.single('SELECT id FROM lvl_referals WHERE used_by_identifier = ?', { playerIdentifier }, function(alreadyUsed) if alreadyUsed then xPlayer.showNotification('~r~Ai folosit deja un cod de referal!', 'error') return end -- Verifica daca nu e propriul cod MySQL.single('SELECT id FROM lvl_referals WHERE owner_identifier = ? AND referal_code = ?', { playerIdentifier, inputCode }, function(ownCode) if ownCode then xPlayer.showNotification('~r~Nu poti folosi propriul cod de referal!', 'error') return end -- Cauta codul in baza de date MySQL.single('SELECT * FROM lvl_referals WHERE referal_code = ?', { inputCode }, function(refRow) if not refRow then xPlayer.showNotification('~r~Codul de referal nu exista! Verifica codul.', 'error') return end if refRow.used == 1 then xPlayer.showNotification('~r~Acest cod de referal a fost deja folosit!', 'error') return end -- Marcam codul ca folosit MySQL.update('UPDATE lvl_referals SET used = 1, used_by_identifier = ?, used_by_name = ?, used_at = NOW() WHERE id = ?', { playerIdentifier, playerName, refRow.id }, function(affectedRows) if affectedRows == 0 then xPlayer.showNotification('~r~A aparut o eroare! Incearca din nou.', 'error') return end -- REWARD: Jucatorul care a folosit codul (jucatorul nou) xPlayer.addAccountMoney('money', Config.rewardMoney, 'Referal reward - a folosit codul') xPlayer.addInventoryItem(Config.rewardItem, Config.rewardItemCount) xPlayer.showNotification('~g~Ai primit $' .. Config.rewardMoney .. ' si ' .. Config.rewardItemCount .. 'x ' .. Config.rewardItem .. '!', 'success') -- REWARD: Veteranul care detine codul (daca e online) local ownerXPlayer = ESX.GetPlayerFromIdentifier(refRow.owner_identifier) if ownerXPlayer then ownerXPlayer.addAccountMoney('money', Config.rewardMoney, 'Referal reward - a adus jucator nou') ownerXPlayer.addInventoryItem(Config.rewardItem, Config.rewardItemCount) ownerXPlayer.showNotification('~g~Jucatorul ' .. playerName .. ' ti-a folosit codul! Ai primit $' .. Config.rewardMoney .. ' si ' .. Config.rewardItemCount .. 'x ' .. Config.rewardItem .. '!', 'success') else MySQL.insert([[ INSERT INTO lvl_referal_pending (identifier, money, item, item_count) VALUES (?, ?, ?, ?) ]], { refRow.owner_identifier, Config.rewardMoney, Config.rewardItem, Config.rewardItemCount }) end -- Trimite webhook pe Discord sendWebhook( '🎉 Cod de Referal Folosit!', '🔴 **VETERAN (primeste banii reali):**\n' .. '> Nume: __**`' .. refRow.owner_name .. '`**__\n' .. '> ID: `' .. refRow.owner_identifier .. '`\n\n' .. '**L-a adus pe serverul nostru pe:**\n' .. '> Nume: `' .. playerName .. '`\n' .. '> ID: `' .. playerIdentifier .. '`\n\n' .. '**Cod folosit:** `' .. inputCode .. '`\n' .. '**Reward:** $' .. Config.rewardMoney .. ' + ' .. Config.rewardItemCount .. 'x ' .. Config.rewardItem, 3066993 -- Verde ) -- Inchide UI TriggerClientEvent('lvl-referal:closeUI', src) end) end) end) end) end) -- ============================================================ -- EVENT: Daca ownerul era offline, ii dam rewardul la connect -- ============================================================ RegisterNetEvent('esx:playerLoaded') AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) local identifier = xPlayer.getIdentifier() MySQL.query('SELECT * FROM lvl_referal_pending WHERE identifier = ?', { identifier }, function(rows) if rows and #rows > 0 then for _, row in ipairs(rows) do xPlayer.addAccountMoney('money', row.money, 'Referal reward pending') xPlayer.addInventoryItem(row.item, row.item_count) xPlayer.showNotification('~g~Ai primit reward-ul de referal: $' .. row.money .. ' si ' .. row.item_count .. 'x ' .. row.item .. '!', 'success') end MySQL.update('DELETE FROM lvl_referal_pending WHERE identifier = ?', { identifier }) end end) end) -- ============================================================ -- ADMIN: /resetreferal [nume] — reseteaza codul unui veteran -- ============================================================ RegisterCommand('resetreferal', function(source, args) local xPlayer = ESX.GetPlayerFromId(source) if not xPlayer or xPlayer.getGroup() ~= 'admin' then TriggerClientEvent('esx:showNotification', source, '~r~Nu ai permisiunea!') return end if not args[1] then TriggerClientEvent('esx:showNotification', source, '~r~Folosire: /resetreferal [nume veteran]') return end local name = table.concat(args, ' ') MySQL.update('UPDATE lvl_referals SET used = 0, used_by_identifier = NULL, used_by_name = NULL, used_at = NULL WHERE referal_code = ?', { name }, function(affectedRows) if affectedRows > 0 then TriggerClientEvent('esx:showNotification', source, '~g~Codul de referal al lui ' .. name .. ' a fost resetat!') else TriggerClientEvent('esx:showNotification', source, '~r~Nu am gasit codul de referal: ' .. name) end end) end, false) -- ============================================================ -- ADMIN: /resetreferalplayer [nume] — reseteaza un jucator care a folosit un cod -- ============================================================ RegisterCommand('resetreferalplayer', function(source, args) local xPlayer = ESX.GetPlayerFromId(source) if not xPlayer or xPlayer.getGroup() ~= 'admin' then TriggerClientEvent('esx:showNotification', source, '~r~Nu ai permisiunea!') return end if not args[1] then TriggerClientEvent('esx:showNotification', source, '~r~Folosire: /resetreferalplayer [nume jucator]') return end local name = table.concat(args, ' ') MySQL.update('UPDATE lvl_referals SET used = 0, used_by_identifier = NULL, used_by_name = NULL, used_at = NULL WHERE used_by_name = ?', { name }, function(affectedRows) if affectedRows > 0 then TriggerClientEvent('esx:showNotification', source, '~g~Referalul folosit de ' .. name .. ' a fost resetat! Poate folosi alt cod.') else TriggerClientEvent('esx:showNotification', source, '~r~Nu am gasit niciun referal folosit de: ' .. name) end end) end, false)