237 lines
		
	
	
		
			No EOL
		
	
	
		
			11 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			237 lines
		
	
	
		
			No EOL
		
	
	
		
			11 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| QBCore = exports['qb-core']:GetCoreObject()
 | |
| 
 | |
| QBCore.Functions.CreateCallback('mh_jobgarage:CallVehiclesInfo', function(source, cb, job)
 | |
|     local result = {}
 | |
|     MySQL.query("SELECT * FROM mh_jobgarage WHERE job = ?", {job}, function(rs)
 | |
|         if rs[1] ~= nil then
 | |
|             for k, v in pairs(rs) do
 | |
|                 table.insert(result, {plate = v.plate, key_value = v.value, name = v.name, closed = v.closed})
 | |
|             end
 | |
|         else
 | |
|             result = false
 | |
|         end
 | |
|         cb(result)
 | |
|     end)
 | |
| end)
 | |
| 
 | |
| -- mh_jobgarage:AddVehicleToJob VARIABLEN: 
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:AddVehicleToJob')
 | |
| AddEventHandler('mh_jobgarage:AddVehicleToJob', function(plate, stats)
 | |
|     local Player = QBCore.Functions.GetPlayer(source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
|     local pedjob = Player.PlayerData.job.name
 | |
| 
 | |
|     MySQL.query("SELECT * FROm vehicle_keys WHERE owner = ? AND plate = ?", {pedid, plate}, function(rs)
 | |
|         if rs ~= nil and rs[1] ~= nil then
 | |
|             if rs[1].count == stats[2] then
 | |
|                 MySQL.query("DELETE FROM vehicle_keys WHERE owner = ? AND plate = ?", {pedid, plate})
 | |
|             else
 | |
|                 MySQL.query("UPDATE vehicle_keys SET count = count - ? WHERE owner = ? AND plate = ?", {stats[2], pedid, plate})
 | |
|             end
 | |
|         end
 | |
|     end)
 | |
| 
 | |
|     MySQL.query("INSERT INTO mh_jobgarage(job, rang, value, plate, name, closed) VALUES (?, ?, ?, ?, ?, ?)", {pedjob, stats[3], stats[2], plate, stats[1], "false"})
 | |
|     MySQL.query("UPDATE player_vehicles SET citizenid = ? WHERE citizenid = ? and plate  = ?", {pedjob, pedid, plate})
 | |
|     TriggerClientEvent('mh_jobgarage:notify', source, "Schlüsselkasten", "Schlüssel Hinzugefügt!", "success")
 | |
| end)
 | |
| 
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:DeleteFromList')
 | |
| AddEventHandler('mh_jobgarage:DeleteFromList', function(plate)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(_source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
|     local veh_opt = MySQL.query("SELECT * FROM mh_jobgarage WHERE plate = ?", {plate})
 | |
| 
 | |
|     MySQL.query("SELECT * FROM vehicle_keys WHERE plate = ? and owner = ?", {plate, pedid}, function(rs)
 | |
|         if rs[1] ~= nil then
 | |
|             MySQL.query("UPDATE vehicle_keys SET count = count + ? WHERE plate = ? and owner = ?", {rs[1].count, plate, pedid},function(rowsChange)
 | |
|                 if rowsChange then
 | |
|                     TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten", "Du hast "..rs[1].count.."x Schlüssel bekommen.", "success")
 | |
|                 end
 | |
|             end)
 | |
|         else
 | |
|             MySQL.query("INSERT INTO vehicle_keys(owner, plate, count) VALUES (?, ?, ?)", {pedid, plate, rs[1].count}, function(rowsChange)
 | |
|                 if rowsChange then
 | |
|                     TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten", "Du hast "..rs[1].count.."x Schlüssel bekommen.", "success")
 | |
|                 end
 | |
|             end)
 | |
|         end
 | |
|     end)
 | |
| 
 | |
|     MySQL.query("UPDATE player_vehicles SET citizenid = ? WHERE plate = ?", {pedid, plate})
 | |
| 
 | |
|     MySQL.query("DELETE FROM mh_jobgarage WHERE plate = ?", {plate})
 | |
| end)
 | |
| 
 | |
| QBCore.Functions.CreateCallback('mh_jobgarage:lockVehicle', function(source, cb, plate)
 | |
|     MySQL.query("SELECT * FROM mh_jobgarage WHERE plate = ?", {plate}, function(rs)
 | |
|         if rs[1] ~= nil then
 | |
|             if rs[1].closed == "true" then
 | |
|                 MySQL.query("UPDATE mh_jobgarage SET closed = ? WHERE plate = ?", {'false', plate})
 | |
|                 cb(true)
 | |
|             elseif rs[1].closed == "false" then
 | |
|                 MySQL.query("UPDATE mh_jobgarage SET closed = ? WHERE plate = ?", {'true', plate})
 | |
|                 cb(true)
 | |
|             end
 | |
|         end
 | |
|     end)
 | |
|     Wait(300)
 | |
|     cb(false)
 | |
| end)
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:RemoveKeys')
 | |
| AddEventHandler('mh_jobgarage:RemoveKeys', function(plate, value)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
|     MySQL.query("SELECT * FROM mh_jobgarage WHERE plate = ?", {plate}, function(rs)
 | |
|         if rs ~= nil then
 | |
|             MySQL.query("UPDATE mh_jobgarage SET value = value - ? WHERE plate = ?", {value, plate}, function(rs)
 | |
|                 if rs ~= nil then
 | |
|                     TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten", value.."x Schlüssel für das Fahrzeug mit dem Kennzeichen: "..plate.." Entfernt.", "success")
 | |
|                     local havekey = CheckEintrag_v_key(pedid, plate)
 | |
|                     if havekey then
 | |
|                         MySQL.query("UPDATE vehicle_keys SET count = count + ? WHERE owner = ? and plate = ?", {value, pedid, plate})
 | |
|                     else
 | |
|                         MySQL.query("INSERT INTO vehicle_keys(owner, plate, count) VALUES (?, ?, ?)", {pedid, plate, value})
 | |
|                     end
 | |
|                 else
 | |
|                     TriggerClientEvent('mh_jobgarage:notify', _source, "Fehler! Fahrzeug nicht mehr da!", "error")
 | |
|                 end
 | |
|             end)
 | |
|         end
 | |
|     end)
 | |
| end)
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:AddKeys')
 | |
| AddEventHandler('mh_jobgarage:AddKeys', function(plate, value)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
|     MySQL.query("SELECT * FROM vehicle_keys WHERE owner = ? and plate = ?", {pedid, plate}, function(rs)
 | |
|         if rs then
 | |
|             if rs[1].count > value then
 | |
|                 MySQL.query("UPDATE vehicle_keys SET count = count - ? WHERE owner = ? and plate = ?", {value, pedid, plate})
 | |
|                 MySQL.query("UPDATE mh_jobgarage SET value = value + ? WHERE plate = ?", {value, plate})
 | |
|                 TriggerClientEvent('mh_jobgarage:notify', _source, "Du hast "..value.."x Schlüssel Hinzugefügt!", "success")
 | |
|             elseif rs[1].count == value then
 | |
|                 MySQL.query("DELETE FROM vehicle_keys WHERE owner = ? and plate = ?", {pedid, plate})
 | |
|                 MySQL.query("UPDATE mh_jobgarage SET value = value + ? WHERE plate = ?", {value, plate})
 | |
|                 TriggerClientEvent('mh_jobgarage:notify', _source, "Du hast "..value.."x Schlüssel Hinzugefügt!", "success")
 | |
|             else
 | |
|                 TriggerClientEvent('mh_jobgarage:notify', _source, "Du hast versucht mehr Schlüssel reinzupacken als du hast!", "error")
 | |
|             end
 | |
|         else
 | |
|             TriggerClientEvent('mh_jobgarage:notify', _source, "Du Besitzt kein Ersatzschlüssel, fertige erst welche an!", "inform")
 | |
|         end
 | |
|     end)
 | |
| end)
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:ChangeName')
 | |
| AddEventHandler('mh_jobgarage:ChangeName', function(plate, name)
 | |
|     local _source = source
 | |
|     MySQL.query("UPDATE mh_jobgarage SET name = ? WHERE plate = ?", {name, plate}, function(rs)
 | |
|         if rs then
 | |
|             TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten", "Anzeigename geändert auf '"..name.."'.", "success")
 | |
|         else
 | |
|             TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten", "Wurde das Fahrzeug grade Entfernt? bitte Prüfen und erneut versuchen...", "success")
 | |
|         end
 | |
|     end)
 | |
| end)
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:AddVehicleKey')
 | |
| AddEventHandler('mh_jobgarage:AddVehicleKey', function(veh)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(_source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
| 
 | |
|     MySQL.query("SELECT * FROM mh_jobgarage WHERE plate = ?", {veh.plate}, function(rs)
 | |
|         if rs then
 | |
|             if rs[1].value < 1 then
 | |
|                 TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten - "..veh.name, "Keine Schlüssel mehr vorhanden!", "inform")
 | |
|             else
 | |
|                 MySQL.query("SELECT * FROM vehicle_keys WHERE owner = ? and plate = ?", {pedid, veh.plate}, function(rs)
 | |
|                     print(json.encode(rs))
 | |
|                     if rs[1] ~= nil then
 | |
|                         MySQL.query("UPDATE vehicle_keys SET count = count + 1 WHERE owner = ? and plate = ?", {pedid, veh.plate})
 | |
|                     else
 | |
|                         MySQL.query("INSERT INTO vehicle_keys(owner, plate, count) VALUES (?, ?, ?)", {pedid, veh.plate, 1})
 | |
|                     end
 | |
|                     MySQL.query("UPDATE mh_jobgarage SET value = value - 1 WHERE plate = ?", {veh.plate})
 | |
|                     TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten - "..veh.name, "Du hast dir ein Schlüssel für den "..veh.name.." genommen!", "success")
 | |
|                 end)
 | |
|             end 
 | |
|         end
 | |
|     end)
 | |
| end)
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:DelVehicleKey')
 | |
| AddEventHandler('mh_jobgarage:DelVehicleKey', function(veh)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
|     print("DEL 1")
 | |
|     MySQL.query("SELECT * FROM vehicle_keys WHERE owner = ? and plate = ?", {pedid, veh.plate}, function(rs)
 | |
|         if rs then
 | |
|             print("DEL 2")
 | |
|             if rs[1].count > 1 then
 | |
|                 print("DEL 3")
 | |
|                 MySQL.query("UPDATE vehicle_keys SET count = count - 1 WHERE owner = ? AND plate = ?", {pedid, veh.plate})
 | |
|             else
 | |
|                 print("DEL 4")
 | |
|                 MySQL.query("DELETE FROM vehicle_keys WHERE owner = ? AND plate = ?", {pedid, veh.plate})
 | |
|             end
 | |
|             print("DEL 5")
 | |
|             MySQL.query("UPDATE mh_jobgarage SET value = value + 1 WHERE plate = ?", {veh.plate})
 | |
|             TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten - "..veh.name, "Du hast ein Schlüssel zurückgegeben.", "success")
 | |
|         else
 | |
|             print("DEL 6")
 | |
|             TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten - "..veh.name, "Du hast kein Schlüssel für deses Fahrzeug!", "inform")
 | |
|         end
 | |
|     end)
 | |
| end)
 | |
| 
 | |
| RegisterServerEvent('mh_jobgarage:GiveAllKeysBack')
 | |
| AddEventHandler('mh_jobgarage:GiveAllKeysBack', function(veh)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(_source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
| 
 | |
|     for k, v in pairs(veh) do
 | |
|         MySQL.query("SELECT * FROM vehicle_keys WHERE owner = ? and plate = ?", {pedid, v.plate}, function(rs)
 | |
|             if rs then
 | |
|                 MySQL.query("UPDATE mh_jobgarage SET value = value + ? WHERE plate = ?", {rs[1].count, v.plate})
 | |
|                 MySQL.query("DELETE FROM vehicle_keys WHERE owner = ? AND plate = ?", {pedid, v.plate})
 | |
|             end
 | |
|         end)
 | |
|     end
 | |
|     TriggerClientEvent('mh_jobgarage:notify', _source, "Schlüsselkasten", "Du hast sämtliche Schlüssel zurückgegeben", "inform")
 | |
| end)
 | |
| 
 | |
| QBCore.Functions.CreateCallback('mh_jobgarage:CallOwnerVehicles', function(source, cb)
 | |
|     local _source = source
 | |
|     local Player = QBCore.Functions.GetPlayer(_source)
 | |
|     local pedid = Player.PlayerData.citizenid
 | |
|     local veh = {}
 | |
|     MySQL.query("SELECT * FROM player_vehicles where citizenid = ?", {pedid}, function(rs)
 | |
|         if rs ~= nil and rs[1] ~= nil then
 | |
|             for k, v in pairs(rs) do
 | |
|                 MySQL.query("SELECT count FROM vehicle_keys WHERE owner = ? AND plate = ?", {pedid, v.plate}, function(keycount)
 | |
|                 print(json.encode(keycount))
 | |
|                     if keycount and keycount[1] then
 | |
|                         table.insert(veh, {plate = v.plate, keys = keycount[1].count})
 | |
|                     else
 | |
|                         table.insert(veh, {plate = v.plate, keys = 0})
 | |
|                     end
 | |
|                 end)
 | |
|             end
 | |
|             Wait(100)
 | |
|             cb(veh)
 | |
|         else
 | |
|             cb(false)
 | |
|         end
 | |
|     end)
 | |
| end) |