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

349 lines
10 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

if Config.HouseScript ~= 'ps-housing' then
return
end
local QBCore = exports['qb-core']:GetCoreObject()
RPC.Register('codem-phone:home:getKeyHolders', function(source, keyholders)
if not keyholders then
return {
success = false,
message = "No keyholders provided"
}
end
local KeyHoldersName = {}
for _, citizenid in ipairs(keyholders) do
local Player = QBCore.Functions.GetPlayerByCitizenId(citizenid) or QBCore.Functions.GetOfflinePlayerByCitizenId(citizenid)
if Player then
local PlayerData = Player.PlayerData
local name = PlayerData.charinfo.firstname .. " " .. PlayerData.charinfo.lastname
KeyHoldersName[#KeyHoldersName + 1] = {
citizenid = citizenid,
name = name,
phoneNumber = GetPhoneNumberByIdentifier(citizenid)
}
end
end
return {
success = true,
keyholders = KeyHoldersName
}
end)
RPC.Register('codem-phone:home:getHouse', function(source)
local src = source
local identifier = Core.Functions.GetIdentifier(src)
if not identifier then
return {
success = false,
message = "Player not found"
}
end
local ownedHomes = {}
local properties = exports['ps-housing']:GetProperties()
if properties then
for property_id, property in pairs(properties) do
local propertyData = property.propertyData
if propertyData and propertyData.owner == identifier then
local keys = {}
local has_access = propertyData.has_access or {}
for _, citizenid in ipairs(has_access) do
keys[#keys + 1] = citizenid
end
local door_data = propertyData.door_data or {}
local enterCoords = { x = 0, y = 0 }
local isLocked = false
if door_data.x then
enterCoords = { x = door_data.x or 0, y = door_data.y or 0 }
isLocked = door_data.locked or false
elseif door_data[1] then
enterCoords = { x = door_data[1].x or 0, y = door_data[1].y or 0 }
isLocked = door_data[1].locked or false
end
ownedHomes[#ownedHomes + 1] = {
id = property_id,
property_id = property_id,
citizenid = propertyData.owner,
coords = { enter = enterCoords },
house = property_id,
keyholders = keys,
label = propertyData.street or propertyData.apartment or ("Property " .. property_id),
name = propertyData.street or propertyData.apartment or ("Property " .. property_id),
owned = true,
tier = propertyData.shell or "default",
locked = isLocked,
}
end
end
end
return {
success = true,
houses = ownedHomes
}
end)
RPC.Register('codem-phone:home:addkeyholderhouse', function(source, data)
local src = source
local identifier = Core.Functions.GetIdentifier(src)
print("[codem-phone] addkeyholderhouse called")
print("[codem-phone] data:", json.encode(data))
print("[codem-phone] identifier:", identifier)
if not identifier then
print("[codem-phone] ERROR: Player not found")
return {
success = false,
message = "Player not found"
}
end
local targetServerID = tonumber(data.targetID)
local houseID = data.houseID
print("[codem-phone] targetServerID:", targetServerID)
print("[codem-phone] houseID:", houseID)
if not targetServerID or not houseID then
print("[codem-phone] ERROR: Invalid data - targetServerID:", targetServerID, "houseID:", houseID)
return {
success = false,
message = "Invalid data provided"
}
end
-- Server ID'den citizenid al
local TargetPlayer = QBCore.Functions.GetPlayer(targetServerID)
if not TargetPlayer then
print("[codem-phone] ERROR: Player not online - serverID:", targetServerID)
return {
success = false,
message = "Player not online"
}
end
print("[codem-phone] TargetPlayer found:", TargetPlayer.PlayerData.citizenid)
local targetCitizenID = TargetPlayer.PlayerData.citizenid
-- Kendine anahtar vermeyi engelle
if targetCitizenID == identifier then
return {
success = false,
message = "You cannot give yourself a key"
}
end
local properties = exports['ps-housing']:GetProperties()
if not properties then
return {
success = false,
message = "Properties not found"
}
end
local property = properties[tostring(houseID)]
if not property then
return {
success = false,
message = "Property not found"
}
end
local propertyData = property.propertyData
if propertyData.owner ~= identifier then
return {
success = false,
message = "You are not the owner"
}
end
local has_access = propertyData.has_access or {}
-- Zaten erişimi var mı kontrol et
for _, cid in ipairs(has_access) do
if cid == targetCitizenID then
return {
success = false,
message = "This person already has access"
}
end
end
-- Erişim ekle
has_access[#has_access + 1] = targetCitizenID
propertyData.has_access = has_access
-- Database güncelle
MySQL.update("UPDATE properties SET has_access = @has_access WHERE property_id = @property_id", {
["@has_access"] = json.encode(has_access),
["@property_id"] = houseID
})
-- Client'lara bildir
TriggerClientEvent("ps-housing:client:updateProperty", -1, "UpdateHas_access", houseID, has_access)
return {
success = true
}
end)
RPC.Register('codem-phone:home:removekeyholderhouse', function(source, data)
local src = source
local identifier = Core.Functions.GetIdentifier(src)
if not identifier then
return {
success = false,
message = "Player not found"
}
end
local targetCitizenID = data.targetCitizenId
local houseID = data.houseID
if not targetCitizenID or not houseID then
return {
success = false,
message = "Invalid data provided"
}
end
local properties = exports['ps-housing']:GetProperties()
if not properties then
return {
success = false,
message = "Properties not found"
}
end
local property = properties[tostring(houseID)]
if not property then
return {
success = false,
message = "Property not found"
}
end
local propertyData = property.propertyData
if propertyData.owner ~= identifier then
return {
success = false,
message = "You are not the owner"
}
end
local has_access = propertyData.has_access or {}
local found = false
-- Erişimi kaldır
for i = #has_access, 1, -1 do
if has_access[i] == targetCitizenID then
table.remove(has_access, i)
found = true
break
end
end
if not found then
return {
success = false,
message = "This person does not have access"
}
end
propertyData.has_access = has_access
-- Database güncelle
MySQL.update("UPDATE properties SET has_access = @has_access WHERE property_id = @property_id", {
["@has_access"] = json.encode(has_access),
["@property_id"] = houseID
})
-- Client'lara bildir
TriggerClientEvent("ps-housing:client:updateProperty", -1, "UpdateHas_access", houseID, has_access)
return {
success = true
}
end)
RPC.Register('codem-phone:home:toggleLock', function(source, data)
local src = source
local identifier = Core.Functions.GetIdentifier(src)
if not identifier then
return {
success = false,
message = "Player not found"
}
end
local houseID = data.houseID
if not houseID then
return {
success = false,
message = "Invalid house ID"
}
end
local properties = exports['ps-housing']:GetProperties()
if not properties then
return {
success = false,
message = "Properties not found"
}
end
local property = properties[tostring(houseID)]
if not property then
return {
success = false,
message = "Property not found"
}
end
local propertyData = property.propertyData
if propertyData.owner ~= identifier then
return {
success = false,
message = "You are not the owner"
}
end
local door_data = propertyData.door_data or {}
local currentLocked = false
if door_data.locked ~= nil then
currentLocked = door_data.locked
elseif door_data[1] and door_data[1].locked ~= nil then
currentLocked = door_data[1].locked
end
local newLocked = not currentLocked
if door_data.x then
door_data.locked = newLocked
elseif door_data[1] then
door_data[1].locked = newLocked
end
MySQL.update("UPDATE properties SET door_data = @door WHERE property_id = @property_id", {
["@door"] = json.encode(door_data),
["@property_id"] = houseID
})
TriggerClientEvent("ps-housing:client:updateProperty", -1, "UpdateDoor", houseID, door_data, propertyData.street, propertyData.region)
return {
success = true,
locked = newLocked
}
end)