PHP Code:
#include <sourcemod>
static String:TPPath[PLATFORM_MAX_PATH];
new Handle:ClientTimer[32];
static Minutes[32];
//INFO
public Plugin:myinfo = {
name = "HowLong",
author = "hlsavior",
description = "How long have you been playing?",
url = "www.scnggames.com"
};
//ON START
public OnPluginStart()
{
CreateDirectory("addons/sourcemod/data/stats", 0);
BuildPath(Path_SM, TPPath, sizeof(TPPath), "data/stats/timeplayed.txt");
RegConsoleCmd("sm_playedtime", Command_getTime, "");
RegConsoleCmd("sm_pt", Command_getTime, "");
RegAdminCmd("sm_setpt", Command_setTime, ADMFLAG_ROOT);
RegAdminCmd("sm_setplayedtime", Command_setTime, ADMFLAG_ROOT);
}
//IN SERVER
public OnClientPutInServer(client)
{
AltTime(client, 1);
ClientTimer[client] = (CreateTimer(60.0, TimerAdd, client, TIMER_REPEAT))
}
//LEAVE SERVER
public OnClientDisconnect(client)
{
CloseHandle(ClientTimer[client]);
AltTime(client, 0)
}
//TIMER
public Action:TimerAdd(Handle:timer, any:client)
{
if(IsClientConnected(client) && IsClientInGame(client))
{
Minutes[client]++;
}
}
//COMMAND
public Action:Command_getTime(client, args)
{
if(Minutes[client] == 1 || Minutes[client] == -1)
{
PrintToChat(client, "[FZG] \x05You have played on this server for \x03%d\x05 minute!", Minutes[client])
}
else
PrintToChat(client, "[FZG] \x05You have played on this server for \x03%d\x05 minutes!", Minutes[client])
return Plugin_Handled;
}
public Action:Command_setTime(client, args)
{
if(!IsValidClient(client))
{
return Plugin_Handled;
}
if(args != 2)
{
ReplyToCommand(client, "[FZG] Usage: sm_setplayedtime <target> <amount>");
return Plugin_Handled;
}
char target_name[MAX_TARGET_LENGTH], buffer[512];
int target_list[MAXPLAYERS], target_count, g_GivePlayedTime;
bool tn_is_ml;
GetCmdArg(1, buffer, sizeof(buffer));
if((target_count = ProcessTargetString(buffer, client, target_list, MAXPLAYERS, COMMAND_FILTER_ALIVE, target_name, sizeof(target_name), tn_is_ml)) <= 0)
{
ReplyToTargetError(client, target_count);
return Plugin_Handled;
}
GetCmdArg(2, buffer, sizeof(buffer));
g_GivePlayedTime = StringToInt(buffer);
for(int i = 0; i < target_count; i++)
{
Minutes[target_list[i]] = g_GivePlayedTime;
}
if(tn_is_ml)
{
ReplyToCommand(client, "[FZG] %s's playtime set to %d", target_name, g_GivePlayedTime);
}
else
{
ReplyToCommand(client, "[FZG] %s's playtime set to %d", "_s", target_name, g_GivePlayedTime);
}
return Plugin_Handled;
}
//CHANGE
public AltTime(client, connection)
{
new Handle:DB = CreateKeyValues("TimePlayed");
FileToKeyValues(DB, TPPath);
new String:SID[32];
GetClientAuthId(client, AuthId_Steam2, SID, sizeof(SID));
if(connection == 1)
{
//Connected
if(KvJumpToKey(DB, SID, true))
{
new String:name[MAX_NAME_LENGTH], String:temp_name[MAX_NAME_LENGTH];
GetClientName(client, name, sizeof(name));
KvGetString(DB, "name", temp_name, sizeof(temp_name), "NULL");
Minutes[client] = KvGetNum(DB, "minutes", 0);
KvSetNum(DB, "minutes", Minutes[client])
}
}
else if(connection == 0)
{
//Not Connected
if(KvJumpToKey(DB, SID, true))
{
KvSetNum(DB, "minutes", Minutes[client]);
}
}
KvRewind(DB);
KeyValuesToFile(DB, TPPath);
CloseHandle(DB);
}
bool IsValidClient(client, bool bAllowBots = true, bool bAllowDead = true)
{
if(!(1 <= client <= MaxClients) || !IsClientInGame(client) || (IsFakeClient(client) && !bAllowBots) || IsClientSourceTV(client) || IsClientReplay(client) || (!bAllowDead && !IsPlayerAlive(client)))
{
return false;
}
return true;
}