Jump to content

The last player login


Dekonpriv

Recommended Posts

 

I'm looking for some mod that can return me the date of the last player login, but I can't find it. How would it be possible to save the date of some login? Example: person ID 10 logs in today: March 25, 2023.

and this is stored when I mentalize "/lastlogin 10" it will return the last time the person logged in. I need a light

Link to comment
On 26/03/2023 at 00:48, Dekonpriv said:

 

I'm looking for some mod that can return me the date of the last player login, but I can't find it. How would it be possible to save the date of some login? Example: person ID 10 logs in today: March 25, 2023.

and this is stored when I mentalize "/lastlogin 10" it will return the last time the person logged in. I need a light

since you mentioned that you would use IDs for the players, perhaps you are using (My)SQL for your saves

use dbExec to insert or update a database column, dbQuery and dbPoll to get the results, use os.date() to retrieve the exact date and time (it's using servers time)

or..

use getPlayerAccount, setAccountData and os.date() to set an accounts last login data

then use getPlayerFromName, getPlayerAccount and getAccountData for /lastlogin

  • Like 1
Link to comment
addEventHandler ("onPlayerLogin", root, function(_, acc)
	setAccountData(acc, "lastlogin:timestamp", getRealTime().timestamp)
end)

addEventHandler ("onResourceStart", resourceRoot, function()
	for i, v in ipairs (getAccounts()) do
		if not getAccountData (v, "lastlogin:timestamp") then
			setAccountData(acc, "lastlogin:timestamp", getRealTime().timestamp)
		end
	end
end)

-------------------------

addEventHandler ("onPlayerLogin", root, function(_, acc)
	local timestamp = getAccountData(acc, "lastlogin:timestamp")
	
	local time = getRealTime(timestamp)
	local day = time.monthday
	local month = time.month + 1
	local year = time.year + 1900

	local id = getElementData(source, "your ID data") or "N/A"
	outputDebugString ("person ID "..id.." logs in "..day.." "..month..", "..year)
end)

 

Link to comment
On 31/03/2023 at 16:02, AngelAlpha said:
addEventHandler ("onPlayerLogin", root, function(_, acc)
	setAccountData(acc, "lastlogin:timestamp", getRealTime().timestamp)
end)

addEventHandler ("onResourceStart", resourceRoot, function()
	for i, v in ipairs (getAccounts()) do
		if not getAccountData (v, "lastlogin:timestamp") then
			setAccountData(acc, "lastlogin:timestamp", getRealTime().timestamp)
		end
	end
end)

-------------------------

addEventHandler ("onPlayerLogin", root, function(_, acc)
	local timestamp = getAccountData(acc, "lastlogin:timestamp")
	
	local time = getRealTime(timestamp)
	local day = time.monthday
	local month = time.month + 1
	local year = time.year + 1900

	local id = getElementData(source, "your ID data") or "N/A"
	outputDebugString ("person ID "..id.." logs in "..day.." "..month..", "..year)
end)

 

Bro what lol

-- locate ur internal db
db = dbConnect("sqlite", "internal.db")

dbExec(db, "CREATE TABLE IF NOT EXISTS player_login_data (playerID INTEGER PRIMARY KEY, lastLoginDate TEXT)")


function savePlayerLoginDate(playerID)
    local currentDate = os.date("%Y-%m-%d")
    dbExec(db, "INSERT OR REPLACE INTO player_login_data (playerID, lastLoginDate) VALUES (?, ?)", playerID, currentDate)
end

function getLastLoginDate(playerID, callback)
    dbQuery(
        function(qh)
            local result = dbPoll(qh, 0)
            if result and #result > 0 then
                callback(result[1]["lastLoginDate"])
            else
                callback(false)
            end
        end,
        db, "SELECT lastLoginDate FROM player_login_data WHERE playerID=?", playerID
    )
end

function onLastLoginCommandHandler(playerSource, command, playerID)
    if not playerID then
        outputChatBox("Usage: /lastlogin [playerID]", playerSource)
        return
    end

    getLastLoginDate(tonumber(playerID), function(lastLoginDate)
        if lastLoginDate then
            outputChatBox("Player ID " .. playerID .. " last logged in on: " .. lastLoginDate, playerSource)
        else
            outputChatBox("No login data found for player ID " .. playerID, playerSource)
        end
    end)
end
addCommandHandler("lastlogin", onLastLoginCommandHandler)

addEventHandler("onPlayerLogin", root, function()
    local playerID = getElementData(source, "account:id")
    savePlayerLoginDate(playerID)
end)

Add the script to your mtaserver.conf

Link to comment
  • 2 weeks later...
On 25/03/2023 at 17:48, Dekonpriv said:

 

I'm looking for some mod that can return me the date of the last player login, but I can't find it. How would it be possible to save the date of some login? Example: person ID 10 logs in today: March 25, 2023.

and this is stored when I mentalize "/lastlogin 10" it will return the last time the person logged in. I need a light

function onPlayerLogin()
    local playerID = getPlayerID(source)
    local currentDate = getDateString() -- get the current date in string format
    
    -- save the login information to the database
    dbExec("INSERT INTO player_logins (player_id, last_login_date) VALUES (?, ?)", playerID, currentDate)
end

function onCommandLastLogin(player, cmd, targetPlayerID)
    if not targetPlayerID then
        outputChatBox("Usage: /lastlogin <playerID>")
        return
    end
    
    -- retrieve the last login date from the database
    local result = dbQuery("SELECT last_login_date FROM player_logins WHERE player_id=?", targetPlayerID)
    local row = dbPoll(result, -1)[1]
    
    if not row then
        outputChatBox("Player not found or has not logged in before.")
    else
        local lastLoginDate = row.last_login_date
        outputChatBox("Player "..targetPlayerID.." last logged in on "..lastLoginDate)
    end
end

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...