88 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| local QBCore = exports['qb-core']:GetCoreObject()
 | |
| local tipJars = {}
 | |
| local savePath = GetResourcePath(GetCurrentResourceName())..'/tipjars.json'
 | |
| 
 | |
| -- Lade gespeicherte Trinkgelder
 | |
| local function LoadTipJars()
 | |
|     local file = io.open(savePath, 'r')
 | |
|     if file then
 | |
|         local contents = file:read('*a')
 | |
|         file:close()
 | |
|         local success, result = pcall(function()
 | |
|             return json.decode(contents)
 | |
|         end)
 | |
|         if success then
 | |
|             return result
 | |
|         end
 | |
|     end
 | |
|     return {}
 | |
| end
 | |
| 
 | |
| -- Speichere Trinkgelder in JSON
 | |
| local function SaveTipJars()
 | |
|     local file = io.open(savePath, 'w+')
 | |
|     if file then
 | |
|         file:write(json.encode(tipJars))
 | |
|         file:close()
 | |
|     end
 | |
| end
 | |
| 
 | |
| -- Initialisiere die Trinkgeldboxen
 | |
| CreateThread(function()
 | |
|     tipJars = LoadTipJars()
 | |
|     for job, _ in pairs(Config.TipBoxes) do
 | |
|         if not tipJars[job] then
 | |
|             tipJars[job] = 0
 | |
|         end
 | |
|     end
 | |
|     SaveTipJars()
 | |
| end)
 | |
| 
 | |
| RegisterNetEvent('tips:giveTipServer')
 | |
| AddEventHandler('tips:giveTipServer', function(job, amount)
 | |
|     local src = source
 | |
|     local Player = QBCore.Functions.GetPlayer(src)
 | |
|     
 | |
|     if not Player then return end
 | |
|     
 | |
|     -- Überprüfe ob der Betrag im erlaubten Bereich liegt
 | |
|     local jobData = Config.TipBoxes[job]
 | |
|     if amount < jobData.minTip or amount > jobData.maxTip then
 | |
|         TriggerClientEvent('QBCore:Notify', src, 'Ungültiger Betrag!', 'error')
 | |
|         return
 | |
|     end
 | |
|     
 | |
|     if Player.PlayerData.money['cash'] >= amount then
 | |
|         Player.Functions.RemoveMoney('cash', amount)
 | |
|         tipJars[job] = (tipJars[job] or 0) + amount
 | |
|         SaveTipJars()
 | |
|         TriggerClientEvent('QBCore:Notify', src, 'Du hast ' .. amount .. '$ Trinkgeld gegeben!', 'success')
 | |
|     else
 | |
|         TriggerClientEvent('QBCore:Notify', src, 'Du hast nicht genug Bargeld dabei!', 'error')
 | |
|     end
 | |
| end)
 | |
| 
 | |
| RegisterNetEvent('tips:collectTipsServer')
 | |
| AddEventHandler('tips:collectTipsServer', function(data)
 | |
|     local src = source
 | |
|     local Player = QBCore.Functions.GetPlayer(src)
 | |
|     
 | |
|     if not Player then return end
 | |
|     
 | |
|     local job = Player.PlayerData.job.name
 | |
|     
 | |
|     if tipJars[job] and tipJars[job] > 0 then
 | |
|         Player.Functions.AddMoney('cash', tipJars[job])
 | |
|         TriggerClientEvent('QBCore:Notify', src, 'Du hast ' .. tipJars[job] .. '$ Trinkgeld eingesammelt', 'success')
 | |
|         tipJars[job] = 0
 | |
|         SaveTipJars()
 | |
|     else
 | |
|         TriggerClientEvent('QBCore:Notify', src, 'Es ist kein Trinkgeld in der Box', 'error')
 | |
|     end
 | |
| end)
 | |
| 
 | |
| -- Speichere Trinkgelder beim Server-Stop
 | |
| AddEventHandler('onResourceStop', function(resourceName)
 | |
|     if (GetCurrentResourceName() ~= resourceName) then return end
 | |
|     SaveTipJars()
 | |
| end)
 | 
