• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

One house per account - simple LUA, does not require DB modifications

guiismiti

Well-Known Member
Joined
May 19, 2014
Messages
315
Solutions
3
Reaction score
68
Hello!

I wrote this today for my server, and I remember seeing some unanswered threads in OTLand asking for it.

What it does is, it checks for other characters in the same account and then checks the houses table for an owner with that id.

There are two advantages over using an extra attribute in 'accounts' in your DB:
  1. you don't need to make an extra modification in your DB;
  2. there are too many ways a player can loose a house, and you would need to think of all of them in order to change the house key in 'accounts' for every hypothesis.
The function returns true if another character in the acc already has a house, and returns false otherwise.

If you are afraid of spoofing with !buyhouse, use a player storage with some kind of cooldown.

Place this in the end of the file lib/miscellaneous/050-functions.lua
Lua:
function checkAccHouses(player)
    -- returns true if there is another character that already owns a house in the same account
    local playerAccId = player:getAccountId()
    local resultId = db.storeQuery('SELECT `id` FROM `players` WHERE `account_id` = ' .. playerAccId)
    
    if resultId ~= false then
        repeat
            local playerId = result.getNumber(resultId, 'id')
            local ownerId = db.storeQuery('SELECT `id` FROM `houses` WHERE `owner` = ' .. playerId)
            
            if ownerId ~= false then
                return true
            end
        until not result.next(resultId)
        
        result.free(resultId)
    end
    
    return false
end
If you have a !buyhouse talkaction, add this check:
Lua:
if checkAccHouses(player) then
    player:sendCancelMessage("You already own a house with this account.")
    return false
end
If you have a !sellhouse talkaction, add this check:
Lua:
if checkAccHouses(tradePartner) then
    player:sendCancelMessage("The buyer already owns a house with his account.")
    return false
end
If your server has other means of getting a house in game, don't forget to add the check to them.
 
Someone requested an adjustment for VIP players to have a higher house limit in their account, so here it is.

NOTE #1: I used the function "player:isVip()" as an example, you are gonna have to use your own method to check if the player is VIP or not.
NOTE #2: I didn't test it but it hasn't been heavily modified, so it should work.
Lua:
function checkAccHouses(player)
    -- returns true if the account hits the limit for houses per acc
    local playerAccId = player:getAccountId()
    local resultId = db.storeQuery('SELECT `id` FROM `players` WHERE `account_id` = ' .. playerAccId)
    local houseLimit = 1
    local houseCount = 0
   
    -- here you can check if the player has a VIP status and set houseLimit to a higher value
    -- if player:isVip() then
    --        houseLimit = 3
    -- end
   
    if resultId ~= false then
        repeat
            local playerId = result.getNumber(resultId, 'id')
            local ownerId = db.storeQuery('SELECT `id` FROM `houses` WHERE `owner` = ' .. playerId)
           
            if ownerId ~= false then
                houseCount = houseCount + 1
               
                if houseCount >= houseLimit then
                    return true
                end
            end
        until not result.next(resultId)
       
        result.free(resultId)
    end
   
    return false
end
 
Last edited:
I really like this. Will definitely play around with it and see where it'll go :D
 
Back
Top