there's my working SQL version(with additional commands).
sm_rank = Shows your approximate time in the server in minutes and your exact time in seconds.
sm_rankme = (root only) Modify your rank. I did it so only the one who use the command can use it on himself only. That way regular admins cannot abuse this command through rcon.
sm_rsrank = (root only) Reset all ranks.
sm_maxrank = The maximum rank(in seconds) that anyone can reach(default: 1000000).
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <clientprefs>
#include <scp>
#define VERSION "0.5"
new TotalTime[MAXPLAYERS+1];
new iTeam[MAXPLAYERS+1];
new bool:bCountSpec;
new bool:bCountCT;
new bool:bCountT;
static Handle:Conrank;
#define MAXTAGS 40
enum Tags
{
String:Tag[32],
String:Color[10],
PlayTimeNeeded
}
new TagHandler[MAXTAGS+1][Tags];
new TagCount;
new Handle:CountSpecs = INVALID_HANDLE;
new Handle:AllowCT = INVALID_HANDLE;
new Handle:AllowT = INVALID_HANDLE;
new Handle:db = INVALID_HANDLE;
public Plugin:myinfo =
{
name = "Play Time Ranking",
author = "Mitch. Edited by Ben-Yaa yey :D",
description = "Play Tag Ranks",
version = VERSION,
url = "http://snbx.info/"
}
public OnPluginStart()
{
CreateTimer(1.0, CheckTime, _, TIMER_REPEAT);
CreateTimer(60.0, UpdateTime, _, TIMER_REPEAT);
LoadConfig();
CountSpecs = CreateConVar("sm_playtime_countspec", "0", "Addtime if the players are in spec?");
AllowT = CreateConVar("sm_playtime_count2", "1", "Addtime if the players are in Terrorist/Red?");
AllowCT = CreateConVar("sm_playtime_count3", "1", "Addtime if the players are in Counter-Terrorist/blue?");
RegConsoleCmd("sm_rank", MYRANK);
AutoExecConfig(true, "playtime");
bCountSpec = GetConVarBool(CountSpecs);
bCountT = GetConVarBool(AllowT);
bCountCT = GetConVarBool(AllowCT);
HookConVarChange(CountSpecs, CvarUpdated);
HookConVarChange(AllowT, CvarUpdated);
HookConVarChange(AllowCT, CvarUpdated);
RegAdminCmd("sm_rankme", Command_RANKME, ADMFLAG_ROOT);
RegAdminCmd("sm_rsrank", Command_RESETRANK, ADMFLAG_ROOT);
HookEvent("player_team", Event_Team);
HookEvent("player_connect", Event_Connect);
Conrank = CreateConVar( "sm_maxrank", "1000000", "Maximum rank(in seconds)");
CreateConVar("playtime_version", VERSION, "Tag Ranking Version", FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_CHEAT);
new String:sqlError[64];
db = SQL_Connect("playtime", true, sqlError, sizeof(sqlError));
if (db == INVALID_HANDLE)
{
LogError("[ PlayTime ] couldn't connect to the Database!! Error: %s", sqlError);
return Plugin_Handled;
}
new String:query[200];
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS `RANKS`(`STEAM_ID` VARCHAR(30) NOT NULL PRIMARY KEY, `PLAY_TIME` VARCHAR(50) NOT NULL)");
SQL_FastQuery(db, query);
for(new client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client))
{
iTeam[client] = GetClientTeam(client);
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
Format(query, sizeof(query), "SELECT `PLAY_TIME` FROM `RANKS` WHERE `STEAM_ID` = '%s'", steamid);
SQL_TQuery(db, NewPlayerTime, query, client);
}
else
{
iTeam[client] = 0;
}
}
return Plugin_Handled;
}
public Action:Event_Connect(Handle:event, const String:name[], bool:dontBroadcast)
{
new String:sqlError[64];
db = SQL_Connect("playtime", true, sqlError, sizeof(sqlError));
new client = GetClientOfUserId(GetEventInt(event, "userid"));
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
new String:query[200];
Format(query, sizeof(query), "SELECT `PLAY_TIME` FROM `RANKS` WHERE `STEAM_ID` = '%s'", steamid);
SQL_TQuery(db, NewPlayerTime, query, client);
}
public NewPlayerTime(Handle:owner, Handle:hndl, const String:error[], any:client)
{
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
if (!SQL_FetchRow(hndl))
{
new String:query[200];
Format(query, sizeof(query), "INSERT INTO `RANKS` VALUES('%s', 0)", steamid);
SQL_FastQuery(db, query);
TotalTime[client] = 0;
}
else
{
new String:sha2[64];
SQL_FetchString(hndl, 0, sha2, sizeof(sha2));
TotalTime[client] = StringToInt(sha2);
}
}
public Action:Command_RANKME(client, args)
{
new String:Arg[128];
GetCmdArg(1, Arg, sizeof(Arg));
TotalTime[client] = StringToInt(Arg);
}
public Action:MYRANK(client, args)
{
PrintToChat(client, "\x03Your time(in seconds): \x05%d", TotalTime[client]);
PrintToChat(client, "\x03Your approximate time(in minutes): \x05%d", TotalTime[client]/60);
}
public Action:Command_RESETRANK(client, args)
{
new String:sqlError[64];
db = SQL_Connect("playtime", true, sqlError, sizeof(sqlError));
if (db == INVALID_HANDLE)
{
LogError("[ PlayTime ] couldn't connect to the Database!! Error: %s", sqlError);
return;
}
else
{
new String:query[200];
Format(query, sizeof(query), "UPDATE `RANKS` SET `PLAY_TIME` = 0");
SQL_FastQuery(db, query);
for(new i = 1; i <= MaxClients; i++)
{
new String:steamid[64];
GetClientAuthString(i, steamid, sizeof(steamid));
TotalTime[i] = 0;
}
}
}
public Action:CheckTime(Handle:timer)
{
for(new client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client))
{
if(((iTeam[client] > 2) && bCountSpec) || ((iTeam[client] == 2) && bCountT) || ((iTeam[client] == 3) && bCountCT))
{
if(TotalTime[client] < GetConVarInt(Conrank))
TotalTime[client]++;
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
new String:query[200];
Format(query, sizeof(query), "SELECT `PLAY_TIME` FROM `RANKS` WHERE `STEAM_ID` = '%s'", steamid);
SQL_TQuery(db, SetPlayerTime, query, client);
}
}
}
return Plugin_Continue;
}
public Action:UpdateTime(Handle:timer)
{
for(new client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client))
{
new String:sqlError[64];
db = SQL_Connect("playtime", true, sqlError, sizeof(sqlError));
new String:steamid[64];
new String:query[200];
GetClientAuthString(client, steamid, sizeof(steamid));
Format(query, sizeof(query), "SELECT `PLAY_TIME` FROM `RANKS` WHERE `STEAM_ID` = '%s'", steamid);
SQL_TQuery(db, UpdatePlayerTime, query, client);
}
}
return Plugin_Continue;
}
public UpdatePlayerTime(Handle:owner, Handle:hndl, const String:error[], any:client)
{
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
new String:query[200];
if (!SQL_FetchRow(hndl))
{
Format(query, sizeof(query), "INSERT INTO `RANKS` VALUES('%s', 0)", steamid);
SQL_FastQuery(db, query);
TotalTime[client] = 0;
}
else
{
Format(query, sizeof(query), "UPDATE `RANKS` SET `PLAY_TIME` = '%s' WHERE `STEAM_ID` = '%s'", TotalTime[client], steamid);
SQL_FastQuery(db, query);
}
}
public SetPlayerTime(Handle:owner, Handle:hndl, const String:error[], any:client)
{
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
new String:query[200];
if (!SQL_FetchRow(hndl))
{
Format(query, sizeof(query), "INSERT INTO `RANKS` VALUES('%s', 0)", steamid);
SQL_FastQuery(db, query);
TotalTime[client] = 0;
}
}
public OnClientDisconnect(client)
{
new String:steamid[64];
GetClientAuthString(client, steamid, sizeof(steamid));
new String:query[200];
new String:sqlError[64];
db = SQL_Connect("playtime", true, sqlError, sizeof(sqlError));
Format(query, sizeof(query), "UPDATE `RANKS` SET `PLAY_TIME` = '%i' WHERE `STEAM_ID` = '%s'", TotalTime[client], steamid);
SQL_FastQuery(db, query);
}
public Action:OnChatMessage(&author, Handle:recipients, String:name[], String:message[]) {
new TagNum = -1;
if(TagCount > 0)
{
for(new X = 0; X < TagCount; X++)
{
if(((TagHandler[X][PlayTimeNeeded])*60) <= TotalTime[author])
{
TagNum = X;
break;
}
}
}
if(TagNum == -1)
{
return Plugin_Continue;
}
//This is pretty much Dr.McKay's Customchat color code, just replaced variables.
if(strlen(TagHandler[TagNum][Tag]) > 0)
{
if(StrEqual(TagHandler[TagNum][Color], "T", false))
{
Format(name, MAXLENGTH_NAME, "\x03%s %s", TagHandler[TagNum][Tag], name);
}
else if(StrEqual(TagHandler[TagNum][Color], "G", false))
{
Format(name, MAXLENGTH_NAME, "\x04%s \x03%s", TagHandler[TagNum][Tag], name);
}
else if(StrEqual(TagHandler[TagNum][Color], "O", false))
{
Format(name, MAXLENGTH_NAME, "\x05%s \x03%s", TagHandler[TagNum][Tag], name);
}
else if(strlen(TagHandler[TagNum][Color]) == 6)
{
Format(name, MAXLENGTH_NAME, "\x07%s%s \x03%s", TagHandler[TagNum][Color], TagHandler[TagNum][Tag], name);
}
else if(strlen(TagHandler[TagNum][Color]) == 8)
{
Format(name, MAXLENGTH_NAME, "\x08%s%s \x03%s", TagHandler[TagNum][Color], TagHandler[TagNum][Tag], name);
}
else
{
Format(name, MAXLENGTH_NAME, "\x01%s \x03%s", TagHandler[TagNum][Tag], name);
}
}
return Plugin_Changed;
}
public OnPluginEnd()
{
for(new client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client))
{
OnClientDisconnect(client);
}
}
}
public Action:Event_Team(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
iTeam[client] = GetEventInt(event, "team"); //GetClientTeam(client);
}
public CvarUpdated(Handle:convar, const String:oldValue[], const String:newValue[])
{
if(convar == CountSpecs)
{
bCountSpec = GetConVarBool(CountSpecs);
}
else if(convar == AllowT)
{
bCountT = GetConVarBool(AllowT);
}
else if(convar == AllowCT)
{
bCountCT = GetConVarBool(AllowCT);
}
}
LoadConfig() {
for(new X = 0; X < MAXTAGS; X++)
{
strcopy(TagHandler[X][Tag], 32, "");
strcopy(TagHandler[X][Color], 10, "");
TagHandler[X][PlayTimeNeeded] = 0;
}
new Handle:kvs = CreateKeyValues("TagConfig");
decl String:sPaths[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sPaths, sizeof(sPaths),"configs/ranktime.cfg");
if(FileToKeyValues(kvs, sPaths))
{
if (KvGotoFirstSubKey(kvs))
{
TagCount = 0;
do
{
KvGetSectionName(kvs, TagHandler[TagCount][Tag], 32);
KvGetString(kvs, "color", TagHandler[TagCount][Color], 10);
TagHandler[TagCount][PlayTimeNeeded] = KvGetNum(kvs, "playtime", 0);
ReplaceString(TagHandler[TagCount][Color], 32, "#", "");
//PrintToServer("Tag: %s\n Color: %s\n Time: %i", TagHandler[TagCount][Tag], TagHandler[TagCount][Color], TagHandler[TagCount][PlayTimeNeeded]);
TagCount++;
} while (KvGotoNextKey(kvs));
}
}
CloseHandle(kvs);
}