#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;
}
#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
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
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;
}
public OnGameModeInit()
{
SetTimer("TimeOnServer",60000,1);
return 1;
}
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;
}
public OnPlayerCommandText(playerid, cmdtext[]) //Whenever someone types a command, this public is called.
{
dcmd(online,6,cmdtext); //defining the command
return 1;
}
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;
}
OnPlayerConnect - GetTickCount = tick_connect OnPlayerDisconnect - GetTickCount = tick_disconnect time_minutes = (tick_disconnect - tick_connect)/60*1000; //seconds time_hours = time_minutes/60; time_minutes = time_minutes%60; |