• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua [TFS 1.2] Database Clean Player, "lastLogin"

Caduceus

Unknown Member
Joined
May 10, 2010
Messages
321
Solutions
2
Reaction score
24
I am trying to make a database cleaner, with log file that tells playerName - level - lastLogin. However, if the player has never logged in, display "Never" instead of "Wed Dec 31 1969 16:00:00". How would I go about this?

Code:
local config = {
    log = true,
    file = "data/logs/cleanplayers.txt"
}

local function doWriteLogFile(file, text)
    local f = io.open(file, "a+")
    if not f then
        return false
    end

    f:write("[" .. os.date("%m/%d/%Y %H:%M:%S") .. "] " .. text .. "\n")
    f:close()
    return true
end

local cleanup = {
        [1] = {level = 11, time = 10 * 24 * 60 * 60},
        [2] = {level = 20, time = 20 * 24 * 60 * 60},
        [3] = {level = 50, time = 30 * 24 * 60 * 60},
        [4] = {level = 100, time = 60 * 24 * 60 * 60},
        [5] = {level = 130, time = 90 * 24 * 60 * 60},
        [6] = {level = 200, time = 120 * 24 * 60 * 60},
        [7] = {level = 300, time = 180 * 24 * 60 * 60}
    }

function onStartup()
local logs = "Players Deleted:\n"
    for i = 7, #cleanup do
        resultId = db.storeQuery("SELECT `id`,`name`,`level`,`lastlogin` FROM `players` WHERE `level` <= ".. cleanup[i].level .." AND `name` NOT IN('Rook Sample', 'Sorcerer Sample', 'Druid Sample', 'Paladin Sample', 'Knight Sample', 'Templar Sample') AND `group_id` < 2 AND `lastlogin` < UNIX_TIMESTAMP() - ".. cleanup[i].time ..";")
    if resultId == false then
        logs = string.format("%sThere were no players to delete.\n", logs)
    else
        repeat
            local player = result.getString(resultId, "id")
            local playerName = result.getString(resultId, "name")
            local playerLevel = result.getString(resultId, "level")
            local neverLogin = "Never", result.getString(resultId, "lastlogin")
            local login = string.format(os.date("%a %b %d %Y %X", result.getString(resultId, "lastlogin")))
           
            if neverLogin == 0 then
                logs = string.format("%s".. playerName .." - ".. playerLevel .." - " .. neverLogin .."\n", logs)
                db.query("DELETE FROM `players` WHERE `id` = '" .. player .. "';")
            end
            if player ~= nil then
                logs = string.format("%s".. playerName .." - ".. playerLevel .." - " .. login .."\n", logs)
                db.query("DELETE FROM `players` WHERE `id` = '" .. player .. "';")
            end
           
        until not result.next(resultId)
        result.free(resultId)
    end

    if config.log then
        doWriteLogFile(config.file, logs)
    end

    return true
    end
end
 
Last edited:
Solution
Try with this:
Code:
            repeat
                local id = result.getNumber(resultId, "id")
                local name = result.getString(resultId, "name")
                local level = result.getNumber(resultId, "level")
                local lastLogin = result.getNumber(resultId, "lastlogin")

                logs = string.format("%s%s - %d - %s\n", logs, name, level, lastLogin ~= 0 and os.date("%a %b %d %Y %X", lastLogin) or "Never")
                db.query("DELETE FROM `players` WHERE `id` = " .. id)

            until not result.next(resultId)
            result.free(resultId)
Try change:
Code:
local neverLogin = "Never", result.getString(resultId, "lastlogin")

To:
Code:
local nLogin = result.getNumber(resultId, "lastlogin")
local neverLogin = (nLogin == 0 and "Never" or nLogin)

* Great! *

:D
 
Last edited:
Try change:
Code:
local neverLogin = "Never", result.getString(resultId, "lastlogin")

To:
Code:
local nLogin = result.getString(resultId, "lastlogin")
local neverLogin = (nLogin == 0 and "Never" or nLogin)

This still displays "Wed Dec 31 1969 16:00:00" on lastLogin.
 
This is easily resolved by replacing result.getString with result.getNumber (for id, level, and lastlogin). These columns are stored as integers, and shouldn't not be fetched with result.getString as it converts the integer value to a string, which may result into unexpected behavior.
 
This is easily resolved by replacing result.getString with result.getNumber (for id, level, and lastlogin). These columns are stored as integers, and shouldn't not be fetched with result.getString as it converts the integer value to a string, which may result into unexpected behavior.

As far as the deleting of players from the database, this works fine. However, my log file places both "playerName - level - Wed Dec 31 1969 16:00:00" & "playerName - level - Never" per each player.

Try:
Code:
local nLogin = result.getNumber(resultId, "lastlogin")
local neverLogin = (nLogin == 0 and "Never" or nLogin)

As for this line, what is this supposed to do?
local neverLogin = (nLogin == 0 and "Never" or nLogin)

Code:
repeat
            local player = result.getNumber(resultId, "id")
            local playerName = result.getString(resultId, "name")
            local playerLevel = result.getNumber(resultId, "level")
            local nLogin = result.getNumber(resultId, "lastlogin")
            local neverLogin = "Never"
            local login = string.format(os.date("%a %b %d %Y %X", result.getNumber(resultId, "lastlogin")))
     
             if nLogin == 0 then
                logs = string.format("%s".. playerName .." - ".. playerLevel .." - " .. neverLogin .."\n", logs)
                db.query("DELETE FROM `players` WHERE `id` = '" .. player .. "';")
            end
            if player ~= nil then
                logs = string.format("%s".. playerName .." - ".. playerLevel .." - " .. login .."\n", logs)
                db.query("DELETE FROM `players` WHERE `id` = '" .. player .. "';")
            end
     
        until not result.next(resultId)
        result.free(resultId)
    end
 
Last edited:
Try with this:
Code:
            repeat
                local id = result.getNumber(resultId, "id")
                local name = result.getString(resultId, "name")
                local level = result.getNumber(resultId, "level")
                local lastLogin = result.getNumber(resultId, "lastlogin")

                logs = string.format("%s%s - %d - %s\n", logs, name, level, lastLogin ~= 0 and os.date("%a %b %d %Y %X", lastLogin) or "Never")
                db.query("DELETE FROM `players` WHERE `id` = " .. id)

            until not result.next(resultId)
            result.free(resultId)
 
Solution
Try with this:
Code:
            repeat
                local id = result.getNumber(resultId, "id")
                local name = result.getString(resultId, "name")
                local level = result.getNumber(resultId, "level")
                local lastLogin = result.getNumber(resultId, "lastlogin")

                logs = string.format("%s%s - %d - %s\n", logs, name, level, lastLogin ~= 0 and os.date("%a %b %d %Y %X", lastLogin) or "Never")
                db.query("DELETE FROM `players` WHERE `id` = " .. id)

            until not result.next(resultId)
            result.free(resultId)
Thank you. Works great.
 
Back
Top