18.02.2014, 12:07
What is this tutorial? This is a tutorial to count online Hours:Minutes of every player.
What will we use? Enums, Timers, INI Functions
Defining DCMD
Don't forget under OnDialogResponse, DIALOG_REGISTER
and OnPlayerDisconnect.
Now we're done with the Enum and saving, lets create a timer every 60 seconds, we'll use OnGameModeInIt
Now here's the trick, creating a public for counting minutes.
Defining the command.
Calling the command.
What is a timer? https://sampwiki.blast.hk/wiki/SetTimer.
I hope this helps you creating an online hours counter for your server.
Thank You,
Vanter
What will we use? Enums, Timers, INI Functions
Here we go
It's pretty simple actually, first we add the minutes and hours to player enum.PHP Code:
#define PATH "/Users/%s.ini" //Defining path for User, edit to fit your own.
enum pInfo //Enum for the player
{
pHours,
pMinutes
}
new PlayerInfo[MAX_PLAYERS][pInfo];
//Loading user data.
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
INI_Int("Hours",PlayerInfo[playerid][pHours]);
INI_Int("Minutes",PlayerInfo[playerid][pMinutes]);
return 1;
}
stock UserPath(playerid) //defining userpath for needed on INI_OPEN
{
new string[128],playername[MAX_PLAYER_NAME];
GetPlayerName(playerid,playername,sizeof(playername));
format(string,sizeof(string),PATH,playername);
return string;
}
PHP Code:
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
PHP Code:
new INI:File = INI_Open(UserPath(playerid)); //Opening file
INI_WriteInt(File,"Hours",0); //Writing Hours = 0
INI_WriteInt(File,"Minutes",0); //Writing Minutes = 0
INI_Close(File);
PlayerInfo[playerid][pHours] =0; //making sure
PlayerInfo[playerid][pMinutes] =0; //making sure
PHP Code:
public OnPlayerDisconnect(playerid, reason) //for saving on disconnect
{
new INI:File = INI_Open(UserPath(playerid)); //Opening file
INI_WriteInt(File,"Hours",PlayerInfo[playerid][pHours]); //Saving Hours
INI_WriteInt(File,"Minutes",PlayerInfo[playerid][pMinutes]); //Saving Minutes
INI_Close(File); //Closing file
return 1;
}
PHP Code:
public OnGameModeInit()
{
SetTimer("TimeOnServer",60000,1);
return 1;
}
PHP Code:
forward TimeOnServer(); //Defining the public
public TimeOnServer() //calling the public for the timer
{
for(new i=0; i<MAX_PLAYERS; i++) //Looping through players
{
if(IsPlayerConnected(i)) //For me I rather use IsSpawned, but for most people who might not have IsSpawned Variable, use IsPlayerConnected
{
PlayerInfo[i][pMinutes] ++; //Increasing pMinutes every 60 seconds.
if(PlayerInfo[i][pMinutes] >= 60) //Checking if minutes reached 60. (one hour)
{
PlayerInfo[i][pMinutes] =0; //Setting a new hour.
PlayerInfo[i][pHours] ++; //Increasing online hour +1.
}
}
}
return 1;
}
PHP Code:
public OnPlayerCommandText(playerid, cmdtext[]) //Whenever someone types a command, this public is called.
{
dcmd(online,6,cmdtext); //defining the command
return 1;
}
PHP Code:
dcmd_online(playerid,params[]) //The command
{
new string[128]; //Creating a string
new ID; //ID of the target
new tname[MAX_PLAYER_NAME]; //Target name
if(sscanf(params,"u",ID)) //if player types (/online) only.
{
SendClientMessage(playerid,0xFF0000AA,"(ERROR) /online (Player Name/ID)");
return 1;
}
if(!IsPlayerConnected(ID)) //if the player name/ID you entered is not online.
{
format(string,sizeof(string),"[ERROR] The player ID you entered is not connected to the server.");
SendClientMessage(playerid,0xFF0000AA,string);
return 1;
}
GetPlayerName(ID,tname,sizeof(tname)); //Getting player name of target.
format(string,sizeof(string),"[ONLINE] Player %s(%d) has been playing for %d Hours and %d minutes.",tname,ID, PlayerInfo[ID][pHours], PlayerInfo[ID][pMinutes]); //Formatting the string
SendClientMessage(playerid,0xFFFFFFFF,string); //Sending it to the player who typed the command
return 1;
}
I hope this helps you creating an online hours counter for your server.
Thank You,
Vanter