• 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!

Cleanhouses.lua Talkactions

ugurdrahs

New Member
Joined
May 6, 2010
Messages
555
Reaction score
2
Location
Netherland
Clean Non-Active Houses

safelist values are the id of certain houses you don't want to check.

days number of days a player has to be inactive to clear the house (10 days).

Lua Code:
PHP:
    --Adam 2008 Edited by Teh Maverick to work in 0.2+
     
    --Config
    local safelist = {1,2}
    local days = 10*3600*24
     
    --Leave these
    local ownerr = 0
    local hid = 0
    local pid = 0
    local lastlogin = 0
    local cleared = 0
    local name = ""
    local house = nil
    local player = nil
    local playerID = nil
    local cur = nil
    local cur2 = nil
     
    function onSay(cid, words, param)
    if getPlayerGroupId(cid) > 2 then
        pdelete = "Inactive Players With Houses:\n\n"
        t=os.date('*t')
    	dofile("./config.lua")
    	env = assert(luasql.mysql())
    	con = assert(env:connect(mysqlDatabase, mysqlUser, mysqlPass, mysqlHost, mysqlPort))
    	local houseID = assert(con:execute("SELECT `owner` FROM `houses`  WHERE `owner` > 0"))
    	local houseCount = numRows(houseID)
    	cur = assert(con:execute("SELECT `owner`, `id` FROM `houses` WHERE `owner` > 0"))
    	house  = cur:fetch({}, "a")
    	cur:close()
     
      if(houseCount >= 1) then
    	for owner, id in rows(con, "SELECT `owner`, `id` FROM `houses` WHERE `owner` > 0 ORDER BY `id` DESC;") do
            ownerr = tonumber(owner)
            hid = tonumber(id)
    		playerID = assert(con:execute("SELECT `id` FROM `players` WHERE `id` = '"..ownerr.."'  "))
    		cur2 = assert(con:execute("SELECT `id`,`name`,`lastlogin` FROM `players` WHERE `id` = '"..ownerr.."'  "))
    		player = cur2:fetch({}, "a")
    		cur2:close()
     
                if(numRows(playerID) >= 1) then
                    lastlogin = player.lastlogin
                    pid = player.id
                    name = player.name
                    time=os.time(t) - lastlogin
                    offline = time - days
     
                    if offline >= 0 then
                        pdelete = pdelete.."House #"..hid.." owned by "..name.."\n"
                        if isInArray(safelist,hid) == FALSE then                    
                            setHouseOwner(hid, 0)
    						cleared=cleared+1
                        end
                    end
    				player = nil
                end
        end
    	  if cleared < 1 then
    		pdelete = "No players have been inactive.\n"
    	  end
         doShowTextDialog(cid, 5958, pdelete.."\n"..cleared.." Houses cleared.")
      else
         doPlayerSendCancel(cid, "No one owns any houses")
    	 print('No one owns any houses')
      end
      house = nil
      con:close()
      env:close()
    end
    return TRUE
    end
Add this to your talkactions.xml:
Lua Code:

<talkaction words="/cleanhouses" script="houseclean.lua"/>





Will this one works for 0.4???
 
Last edited:
Not working


Arrow Automagically clean unused houses, multi-world support!

versions

Code:


v.1.03 - Added 'onlyNonPremium' configurable. (26.04.2010)

Removed modlib, since the script is run once anyway we don't need it loaded at all times.
It shouldn't now stop execution when a house is nameless.

1.02 - now uses only one sql query, removed multiworld configurable since it's not needed anymore (18.03.2010)
1.01 - added multiworld support, optimized query. (17.03.2010)
1.0 - initial release (17.02.2010)

This mod will clean houses of inactive players every time your server starts and move their items to their depots.

Mind it may freeze for a long time if it was to clean many heavily stuffed houses. Moving items to depots is kinda slow. However running this mod every day will lessen the load and keep the houses free for active players.

Tested on TFS 0.4 DEV, although it should work flawlessly with 0.3.6 and possibly with 0.3.5 as well.

MOD VERSION

Code:
PHP:
    <?xml version="1.0" encoding="UTF-8"?>
    <mod name="Cleanhouses" version="1.03" author="nsanee" contact="otland.net" enabled="yes">
        <description>
            v.1.03 - Added 'onlyNonPremium' configurable.
                     Removed modlib, since the script is run once anyway we don't need it loaded at all times.
                     It shouldn't now stop execution when a house is nameless.
    				
    		v.1.02 - now uses only one sql query, removed multiworld configurable  since it's not needed anymore
            v.1.01 - small fixes, optimized query + multiworld support.
           
    		
            This mod will clean houses of inactive players and move their items to the depot.
            
            config explained:
                
                days - If the player hasn't logged in for this number of days his house will be freed.
                log - true/false, whether to enable logging of which houses have been cleaned.
                file - path to the log file, where logs will be stored. Ignored if 'log' set to false
                onlyNonPremium - if set to 'true', the script will clean only the houses of players who don't have any pacc days left.
    			
    		other notes:
    			DO NOT remove doSaveServer() at the end, otherwise if your server happens to crash before the nearest server save you will regret it =)
        </description>

        <globalevent name="cleanhouses" type="start" event="buffer"><![CDATA[
    	
    	[PHP]
            local config = { 
                days = 14,
                log = true,
                file = getDataDir() .. "/logs/cleanhouses.txt",
    	    onlyNonPremium = true
            }
    		
    		
    		
            local ns_query =[[ SELECT houses.owner, houses.id as hid, houses.name as house_name ,players.name FROM houses
                LEFT JOIN players ON players.id=houses.owner
                LEFT JOIN accounts ON players.account_id=accounts.id
                WHERE players.lastlogin < (UNIX_TIMESTAMP() - ]] ..config.days.. [[*24*60*60)
                ]] ..(config.onlyNonPremium and ' AND accounts.premdays=0 ' or '')..[[
                AND	players.world_id =]] .. getConfigValue("worldId")
    		
            local house = db.getResult(ns_query)
            local logs = " :: Houses cleaned:\n\n"
            if house:getID() ~= -1 then
                repeat
                    logs = logs .. house:getDataString('house_name') ..", owned by " .. house:getDataString('name') .. "\n"
                    setHouseOwner(house:getDataInt('hid'), 0)
                until not house:next()
                house:free()
            else
                logs = logs .. "There were no houses to clean."
            end
            if config.log then
                doWriteLogFile(config.file, logs)
            end
            addEvent(doSaveServer, 1000)
    		
        ]]></globalevent>
    </mod>
Just save it as cleanhouses.xml in your TFS/mods directory.

NON-MOD VERSION (OUTDATED)

globalevents/globalevents.xml

Code:

<globalevent name="cleanhouses" type="start" event="script" value="cleanhouses.lua"/>

globalevents/scripts/cleanhouses.lua

Code:
PHP:
    local config = { 
        days = 14,
        log = true,
        file = getDataDir() .. "/logs/cleanhouses.txt"
    }    
    local ns_query =[[ SELECT houses.owner,houses.id,players.name FROM houses
                    LEFT JOIN players ON players.id=houses.owner
                    WHERE players.lastlogin < (UNIX_TIMESTAMP() - ]] ..config.days.. [[*24*60*60)
                    AND
                    players.world_id =]] .. getConfigValue("worldId")

    function onStartup(_time)

        local house = db.getResult(ns_query)
        local logs = " :: Houses cleaned:\n\n"
        if house:getID() ~= -1 then
            repeat
                logs = logs .. getHouseInfo(house:getDataInt('id')).name ..", owned by " .. house:getDataString('name') .. "\n"
                setHouseOwner(house:getDataInt('id'), 0)
            until not house:next()
            house:free()
        else
            logs = logs .. "There were no houses to clean."
        end
        if config.log then
            doWriteLogFile(config.file, logs)
        end
        addEvent(doSaveServer, 1000)
    end
 
Back
Top