Server Time Tracking Issue
#1

Currently I'm trying to setup a system to track how long someone has been on a server for and below is what I've found so far

Код:
SetTimerEx("TimeOnServer", 1000, 1, "i", playerid);

forward TimeOnServer(playerid);
public TimeOnServer(playerid)
{
    Player[playerid][tSec] ++;
    if(Player[playerid][tSec]>=60)
    {
        Player[playerid][tMin]++;
        Player[playerid][tSec]=0;
    }
    if(Player[playerid][tMin]>=60)
    {
        Player[playerid][tMin]=0;
        Player[playerid][tHour]++;
    }
}
Код:
error 021: symbol already defined: "SetTimerEx"
However, I'm getting the above error, I tried looking it up but couldn't find anything, and I don't see anything wrong. tSec, tMin and tHour are already defined in my Player enum so that cannot be the issue.

Secondly, I also want this to add 1 each time the player reaches 60 minutes, how can I go about doing this through mysql? I don't know if I use a Insert Into or Update query or how to write it out to use the tHour enum.
Reply
#2

I use timer for tracking mins on the server I develop. I just add 1 to each player's time, then I save it on disconnect.

About 1'st - Create a variabled with Timer[MAX_PLAYERS] or something.
Are you sure you don't have SetTimer outside of any function/command?
Reply
#3

In my opinion, this is a bad way of doing it, with timers, I think it'll decrease server performance a lot as long as new players keep joining. I have a better way of doing it perhaps, check it out because I dunno if it works, however it should do:

pawn Код:
enum PLAYER_SPENT_TIME_STRUCT
{
    connectedAtHr, // When did this player connect to our server (hour)?
    connectedAtMin, // ~ ~ ~ minute ~ ~ ~
    connectedAtSec  // ~ ~ ~ second ~ ~ ~
}

new PlayerConnectedTime[MAX_PLAYERS][PLAYER_SPENT_TIME_STRUCT];

public OnPlayerConnect()
{
    // Update player new time
    // gettime gets the current time in hours, minutes and seconds
    // So this is when they connected
    gettime(PlayerConnectedTime[playerid][connectedAtHr], PlayerConnectedTime[playerid][connectedAtMin], PlayerConnectedTime[playerid][connectedAtSec]);
    return 1;
}

public OnPlayerDisconnect()
{
    // Now they're leaving the server
    // Let's see the difference between time now and time when they connected
    // Yes, we can do that as we've saved it in the PlayerConnectedTime variables
    new curHr, curMin, curSec;
    gettime(curHr, curMin, curSec);
    new newHrs, newMins, newSecs;
    newHrs = curHr - PlayerConnectedTime[playerid][connectedAtHr];
    newSecs = curSec - PlayerConnectedTime[playerid][connectedAtSec];
    newMins = curMin - PlayerConnectedTime[playerid][connectedAtMin];
    // Now newHrs, newMins, newSecs is the newly spent time
    // Do whatever you want with them!
    return 1;
}
Reply
#4

NetStats_GetConnectedTime

You don't need a timer dedicated to keep track of player's playing time.You can save some CPU by avoiding it
Reply
#5

Quote:
Originally Posted by [KHK]Khalid
Посмотреть сообщение
In my opinion, this is a bad way of doing it, with timers, I think it'll decrease server performance a lot as long as new players keep joining. I have a better way of doing it perhaps, check it out because I dunno if it works, however it should do:

pawn Код:
enum PLAYER_SPENT_TIME_STRUCT
{
    connectedAtHr, // When did this player connect to our server (hour)?
    connectedAtMin, // ~ ~ ~ minute ~ ~ ~
    connectedAtSec  // ~ ~ ~ second ~ ~ ~
}

new PlayerConnectedTime[MAX_PLAYERS][PLAYER_SPENT_TIME_STRUCT];

public OnPlayerConnect()
{
    // Update player new time
    // gettime gets the current time in hours, minutes and seconds
    // So this is when they connected
    gettime(PlayerConnectedTime[playerid][connectedAtHr], PlayerConnectedTime[playerid][connectedAtMin], PlayerConnectedTime[playerid][connectedAtSec]);
    return 1;
}

public OnPlayerDisconnect()
{
    // Now they're leaving the server
    // Let's see the difference between time now and time when they connected
    // Yes, we can do that as we've saved it in the PlayerConnectedTime variables
    new curHr, curMin, curSec;
    gettime(curHr, curMin, curSec);
    new newHrs, newMins, newSecs;
    newHrs = curHr - PlayerConnectedTime[playerid][connectedAtHr];
    newSecs = curSec - PlayerConnectedTime[playerid][connectedAtSec];
    newMins = curMin - PlayerConnectedTime[playerid][connectedAtMin];
    // Now newHrs, newMins, newSecs is the newly spent time
    // Do whatever you want with them!
    return 1;
}
Quote:
Originally Posted by Yashas
Посмотреть сообщение
NetStats_GetConnectedTime

You don't need a timer dedicated to keep track of player's playing time.You can save some CPU by avoiding it
So once I actually get that time, how should I store it for when they go offline, just store it into the mysql table and when they reconnect retrieve that time and continue from that?
Reply
#6

Quote:
Originally Posted by BR3TT
Посмотреть сообщение
So once I actually get that time, how should I store it for when they go offline, just store it into the mysql table and when they reconnect retrieve that time and continue from that?
Yeah, basically you'd be adding newHrs, newMins, newSecs to whatever is saved in the database for a player every time they disconnect from your server. You may want to check Yashas' method too, it's efficient, but it'd need some math calculations to get the time in Hours/Minutes/Seconds format because GetConnectedTime gives you time in milliseconds only. I prefer my method though, it looks easier, neater and it's CPU-friendly as well.
Reply
#7

Код:
new secs = NetStats_GetConnectedTime(playerid);
new mins = (secs/60)%60;
new hours = ((secs/3600)%24)/3600;
secs = secs%60;
If the player has played for 60000 seconds, the values of the variables will be as follows:

secs = 0;
mins = 40;
hours = 0.67;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)