Connected Time
#1

How I can make to check players connected time not afk time for example if:
Player X play like 20 minutes and he goes afk with /sleep for 40 at payday to receive 0.20 played time and one RP.
If Player X play around 15 minutes and he quit and enter again at 59 to get that 0.15 played time.
If Player X play 45 minutes and go AFK (just esc) to get 0.45 played time.
Reply
#2

pawn Код:
//Out side all the call backs//
new PlayedTimer[MAX_PLAYERS];//Creating a variable, it is used to kill the timer.
new Time[MAX_PLAYERS] = 0;
forward IncreaseTime(playerid);//forwarding IncreaseTime(playerid).

//Under OnPlayerConnect and /back cmd
//Make sure to check the name of the player at OnPlayerConnect or it will set his time according to his id.
PlayerTimer[playerid] = SetTimerEx("IncreaseTime", 1000 * 60, 1, "i", playerid);//Making a one minute timer at OnPlayerConnect.

//In /sleep and Under OnPlayerDisconnect
KillTimer(PlayerTimer[playerid]);//Killing the Timer

public IncreaseTime(playerid)
{
    Time[playerid]++;//Increasing Time by 1 every minute.
}

//At Payday//
new Prize[MAX_PLAYERS];
Prize[playerid] = Time/100;
GivePlayerMoney....
Reply
#3

Check if OnPlayerUpdate isn't called by doing so:
pawn Код:
public OnPlayerUpdate(playerid)
{
    Update[playerid] = GetTickCount();
}
And in your timer to increase the players playtime:
pawn Код:
if(GetTickCount() > (Update[i] + 500)) return 1;
Thereby you can detect if the player is AFK.
Reply
#4

And this will make any lag?

@Calvin, I have no code atm, so I have to take it from deadpool, could you help me a little to "advance" with it please, cause I am not that good, this will require some rows in DB?
Reply
#5

Use De4dpOol's code to increase the playtime, and then you can use mine to check if he's AFK.
If you want to save his time, you need to save using either SQL or files, if you don't know how to do so, i would recommend looking up some tutorials.
Reply
#6

For payday code and /stats code I need a little more help if you could help me.
//At Payday//
new Prize[MAX_PLAYERS];
Prize[playerid] = Time/100;
GivePlayerMoney....
Idk if this would work.
Reply
#7

No you have to set a repeating timer of an hour i guess, and then he told you could do something like that in that timer.
Reply
#8

Timers are inaccurate to track this.
A timer that runs every second might run every 1.2 seconds or something like that.

You could store the unix timestamp upon loggin in (OnPlayerConnect), you can use gettime() for that.
Store that integer in the player's account.

When you need to check the connected time, use gettime() again and substract the stored value from it.
Then you have the exact time in seconds, which can easily be converted into hours, minutes and seconds.
pawn Код:
// Somewhere on top of your script
new PlayerTime[MAX_PLAYERS];

// OnPlayerConnect
public OnPlayerConnect(playerid)
{
    PlayerTime[playerid] = gettime();
    return 1;
}

// Helper function
ConnectedTime(playerid)
{
    return (gettime() - PlayerTime[playerid]);
}

// Usage anywhere
new Msg[128];
format(Msg, sizeof(Msg), "Player %i has been connected for %i seconds", playerid, ConnectedTime(playerid));
SendClientMessageToAll(-1, Msg);
Reply
#9

Still can't figure it out on PAYDAY, what to put to set his played time.
Reply
#10

pawn Код:
//Out side all the call backs//
new PlayedTimer[MAX_PLAYERS];//Creating a variable, it is used to kill the timer.
new Time[MAX_PLAYERS] = 0;
forward IncreaseTime(playerid);//forwarding IncreaseTime(playerid).
forward Payday();

public IncreaseTime(playerid)
{
    Time[playerid]++;//Increasing Time by 1 every minute.
}

public Payday()
{
    for(new i = 0, i < MAX_PLAYERS; i++)
    {
        GivePlayerMoney(i, Time[i]/100);//if he played for 20 minute. He will get 0.20$,
        Time[i] = 0;
    }
}

//under OnGameModeInit//
SetTimer("Payday", 1000*60*60, 1); //Setting a timer of 1 hour

//Under OnPlayerConnect and your /back cmd (you have it right?)//
PlayerTimer[playerid] = SetTimerEx("IncreaseTime", 1000 * 60, 1, "i", playerid);//Making a one minute timer.

//In /sleep and Under OnPlayerDisconnect//
KillTimer(PlayerTimer[playerid]);//Killing the Timer
The above code should work fine (not tested). As it is not saving player Time, when the player player connects his time will be set according to his id.
Example: Player ID 1 left and another player joined with ID 1 his time will be same as previous player.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)