ESX.RegisterServerCallback("pluto_motels:rentRoom", function(source, callback, interiorId, motelData)
local player = ESX.GetPlayerFromId(source)
local playerMoney = Config.NewESX and player.getAccount("money")["money"] or player.getMoney()
local playerBankMoney = player.getAccount("bank")["money"]
local defaultRoomData = {
["roomFinish"] = motelData["roomFinish"],
["roomLocked"] = 1,
["roomStorages"] = {},
["motelName"] = motelData["motelName"]
}
if player then
if not interiorId then return callback(false, "No room number got specified.") end
if playerMoney < motelData["motelPrice"] and playerBankMoney < motelData["motelPrice"] then return callback(false, "You don't have enough money, you need $" .. motelData["motelPrice"] - playerMoney) end
if not Config.DiscInventory then
for furnitureName, furnitureData in pairs(motelData["furniture"]) do
if furnitureData["type"] == "storage" then
defaultRoomData["roomStorages"][furnitureName] = {
["cash"] = 0,
["black_money"] = 0,
["items"] = {}
}
end
end
end
local sqlQuery = [[
INSERT
INTO
pluto_motels
(interiorId, roomOwner, roomData, latestPayment)
VALUES
(@interiorId, @roomOwner, @roomData, @latestPayment)
]]
MySQL.Async.execute(sqlQuery, {
["@interiorId"] = interiorId,
["@roomOwner"] = player["identifier"],
["@roomData"] = json.encode(defaultRoomData),
["@latestPayment"] = os.time()
}, function(rowsChanged)
if rowsChanged and rowsChanged > 0 then
if playerMoney >= motelData["motelPrice"] then
player.removeMoney(motelData["motelPrice"])
cachedData["motelRooms"][interiorId] = {
["roomOwner"] = player["identifier"],
["roomLocked"] = 1,
["roomData"] = defaultRoomData,
["roomFinish"] = defaultRoomData["roomFinish"],
["roomStorages"] = defaultRoomData["roomStorages"],
["latestPayment"] = os.time(),
["paymentTimer"] = os.difftime(os.time(), os.time()) / 3600
}
callback(true)
TriggerClientEvent("pluto_motels:syncRooms", -1, cachedData["motelRooms"])
elseif playerBankMoney >= motelData["motelPrice"] then
player.removeAccountMoney("bank", motelData["motelPrice"])
cachedData["motelRooms"][interiorId] = {
["roomOwner"] = player["identifier"],
["roomLocked"] = 1,
["roomData"] = defaultRoomData,
["roomFinish"] = defaultRoomData["roomFinish"],
["roomStorages"] = defaultRoomData["roomStorages"],
["latestPayment"] = os.time(),
["paymentTimer"] = os.difftime(os.time(), os.time()) / 3600
}
callback(true)
TriggerClientEvent("pluto_motels:syncRooms", -1, cachedData["motelRooms"])
end
else
callback(false, "Couldn't insert in db.")
end
end)
else
callback(false, "Player doesn't exist.")
end
end)