2026-04-14 17:41:39 +02:00

117 lines
3.5 KiB
Lua

function HasPermission(sourcePlayer)
local jobName = GetJobName(sourcePlayer)
local jobGrade = GetJobGrade(sourcePlayer)
if PlayerIsAdmin(sourcePlayer) then return true end
for _, jobEntry in pairs(Config.CreatorJobs) do
if jobEntry.job == jobName then
if jobEntry.grade then
if not table.contains(jobEntry.grade, jobGrade) then
goto continue
end
end
return true
end
::continue::
end
return false
end
lib.callback.register("garages:hasPermission", function(sourcePlayer)
return HasPermission(sourcePlayer)
end)
lib.callback.register("garages:getCreatorData", function(sourcePlayer)
if not HasPermission(sourcePlayer) then
Error("garages:getCreatorData", "Player does not have permission to get creator data")
return nil
end
return {
jobs = GetJobsData()
}
end)
function GetGarageStreetId(streetName)
local count = MySQL.Sync.fetchAll(
"SELECT COUNT(*) as count FROM player_garages WHERE name LIKE ?",
{ "%" .. streetName .. "%" }
)
return count[1].count + 1
end
lib.callback.register("garages:createGarage", function(sourcePlayer, streetName, garageData)
if not HasPermission(sourcePlayer) then
Error("garages:createGarage", "Player does not have permission to create garage")
return false
end
local streetId = GetGarageStreetId(streetName)
local garageName = streetName:lower() .. tostring(streetId)
garageData.name = garageName
if not garageData.shell then
garageData.shell = { shell = 1 }
end
local insertId, creatorId = db.createGarage(sourcePlayer, garageData)
if not insertId then
Error("garages:createGarage", "Failed to create shutter")
return false
end
garageData.id = insertId
garageData.creator = creatorId
Config.Garages[garageName] = garageData
TriggerClientEvent("garages:createGarage", -1, garageData)
Notification(i18n.t("creator.garage_created"), "success")
return true
end)
lib.callback.register("garages:updateGarage", function(sourcePlayer, garageData)
if not HasPermission(sourcePlayer) then
Error("garages:updateGarage", "Player does not have permission to update garage")
return false
end
assert(garageData.id, "db.updateGarage data.id is nil")
local success = db.updateGarage(sourcePlayer, garageData)
if not success then return false end
for key, existingGarage in pairs(Config.Garages) do
if existingGarage.name == garageData.name then
garageData.creator = existingGarage.creator
Config.Garages[key] = garageData
break
end
end
TriggerClientEvent("garages:updateGarage", -1, garageData)
return true
end)
lib.callback.register("garages:removeGarage", function(sourcePlayer, garageId)
if not HasPermission(sourcePlayer) then
Error("garages:removeGarage", "Player does not have permission to remove garage")
return false
end
assert(garageId, "garages:removeGarage garageId is nil")
local playerIdentifier = GetPlayerIdentifier(sourcePlayer)
ClearGarage(garageId, playerIdentifier)
local success = db.removeGarage(garageId)
if not success then
Error("garages:removeGarage", "Failed to remove garage", garageId)
return false
end
TriggerClientEvent("garages:removeGarage", -1, garageId)
return true
end)