PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <nvault>
#include <nvault_util>
#include <fakemeta>
#define PLUGIN "Time Played"
#define VERSION "0.1"
#define AUTHOR "[LF] | Dr.Freeman"
new g_msgSayText;
new TotalPlayedTime[33];
static const Green[] = "^x04"
static const Team[] = "^x03"
new cv_prune
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR );
register_clcmd("say /pt", "Time")
register_clcmd("say /pttop15", "show_top15")
register_concmd("amx_playedtime", "admin_showptime", ADMIN_KICK," <#Player Name> - Details about playedtime.");
g_msgSayText = get_user_msgid("SayText")
register_forward(FM_ClientUserInfoChanged, "ClientUserInfoChanged")
cv_prune = register_cvar("amx_pt_prune","30")
}
public plugin_cfg(){
new vault = nvault_open("Played_Time")
new prune_time=(get_systime()-(get_pcvar_num(cv_prune)*86400))
nvault_prune(vault,0, prune_time)
nvault_close(vault)
}
public Time(id)
{
static ctime[64], timep;
timep = get_user_time(id, 1) / 60;
get_time("%H:%M:%S", ctime, 63);
ChatColor(id, "%s[Asia_Game] Current time played: %s[%d] %sminute%s.",Green, Team, timep, Green, timep == 1 ? "" : "s");
ChatColor(id, "%s[Asia_Game] Total time played: %s[%d] %sminute%s.", Green, Team, timep+TotalPlayedTime[id], Green, timep+TotalPlayedTime[id] == 1 ? "" : "s");
}
public admin_showptime(id,level,cid)
{
if(!cmd_access(id, level, cid, 2))
return PLUGIN_HANDLED;
static arg[32];
read_argv(1, arg, 31);
new player = cmd_target(id, arg, 2);
if(!player)
return PLUGIN_HANDLED;
static name[32];
get_user_name(player, name, 31);
static timep, ctime[64];
timep = get_user_time(player, 1) / 60;
get_time("%H:%M:%S", ctime, 63);
console_print(id, "-----------------------(#PlayedTime#)-----------------------");
console_print(id, "[Asia_Game]%s Current played time %d minute%s.",name, timep, timep == 1 ? "" : "s");
console_print(id, "[Asia_Game]%s's Total played time: %d minute%s.",name, timep+TotalPlayedTime[player], timep == 1 ? "" : "s"); // new
console_print(id, "-----------------------------------------------------------------");
return PLUGIN_HANDLED;
}
public show_top15(id){
new pos,key[64],data[64],timestamp
new vault = nvault_util_open("Played_Time")
new count = nvault_util_count(vault)
new Array:stats,Trie:names
stats = ArrayCreate(20)
names = TrieCreate()
new temp[20],knum[5]
for (new i=1;i<=count;i++ ){
pos = nvault_util_read(vault,pos,key,64,data,64,timestamp)
formatex(temp,19,"%d %d",i,str_to_num(data))
ArrayPushString(stats,temp)
replace_all(key,64,"PLAYEDTIME","")
formatex(knum,4,"%d",i)
TrieSetString(names,knum,key)
}
nvault_util_close(vault)
ArraySort(stats,"sorter")
new motd[1501],iLen;
iLen = format(motd, sizeof motd - 1,"<body bgcolor=#000000><font color=#98f5ff><pre>");
iLen += format(motd[iLen], (sizeof motd - 1) - iLen,"%s %-22.22s %3s^n", "#", "Name", "Time in Minutes")
for(new x = 0; x < 15; x++){
new rank,name[32],srank[5]
new pt = getter(stats,x,rank)
formatex(srank,4,"%d",rank)
TrieGetString(names,srank,name,31)
iLen += format(motd[iLen], (sizeof motd - 1) - iLen,"%d %-22.22s %d^n", x + 1, name, pt);
}
iLen += format(motd[iLen], (sizeof motd - 1) - iLen,"</body></font></pre>")
ArrayDestroy(stats)
TrieDestroy(names)
/*
static Sort[33][2];
new players[32],num,count,index;
get_players(players,num);
for(new i = 0; i < num; i++){
index = players[i];
Sort[count][0] = index;
Sort[count][1] = TotalPlayedTime[index];
count++;
}
SortCustom2D(Sort,count,"compare_xp");
new motd[1501],iLen;
iLen = format(motd, sizeof motd - 1,"<body bgcolor=#000000><font color=#98f5ff><pre>");
iLen += format(motd[iLen], (sizeof motd - 1) - iLen,"%s %-22.22s %3s^n", "#", "Name", "Time in Minutes");
new y = clamp(count,0,10);
new name[32],kindex;
for(new x = 0; x < y; x++){
kindex = Sort[x][0];
get_user_name(kindex,name,sizeof name - 1);
iLen += format(motd[iLen], (sizeof motd - 1) - iLen,"%d %-22.22s %d^n", x + 1, name, Sort[x][1]);
}
iLen += format(motd[iLen], (sizeof motd - 1) - iLen,"</body></font></pre>");*/
show_motd(id,motd, "XP Top 10");
}
public sorter(Array:array,item1,item2){
new res1=getter(array,item1)
new res2=getter(array,item2)
if(res1<res2)
return 1
else if(res1>res2)
return -1
return 0
}
getter(Array:array,item,&rank=0){
new outa[32],temp[2][10],val
ArrayGetString(array,item,outa,31)
parse(outa,temp[0],9,temp[1],9)
rank = str_to_num(temp[0])
val = str_to_num(temp[1])
return val
}
public ClientUserInfoChanged(id)
{
static const name[] = "name"
static szOldName[32], szNewName[32]
pev(id, pev_netname, szOldName, charsmax(szOldName))
if( szOldName[0] )
{
get_user_info(id, name, szNewName, charsmax(szNewName))
if( !equal(szOldName, szNewName) )
{
TotalPlayedTime[id] = 0
set_task(0.5, "ChangeTime", id)
return FMRES_HANDLED
}
}
return FMRES_IGNORED
}
public ChangeTime(id)
{
TotalPlayedTime[id] = LoadTime(id)
}
public client_disconnect(id)
{
TotalPlayedTime[id] = TotalPlayedTime[id] + (get_user_time(id)/60);
SaveTime(id, TotalPlayedTime[id]);
}
public client_putinserver(id)
{
TotalPlayedTime[id] = LoadTime(id);
}
public LoadTime( id )
{
new valut = nvault_open("Played_Time")
new szName[33];
new vaultkey[64], vaultdata[64];
get_user_name(id, szName, 32);
format(vaultkey, 63, "PLAYEDTIME%s", szName);
nvault_get(valut, vaultkey, vaultdata, 63);
nvault_close(valut);
return str_to_num(vaultdata);
}
public SaveTime(id,PlayedTime)
{
new valut = nvault_open("Played_Time")
if(valut == INVALID_HANDLE)
set_fail_state("nValut returned invalid handle")
new szName[33];
new vaultkey[64], vaultdata[64];
get_user_name(id, szName, 32)
format(vaultkey, 63, "PLAYEDTIME%s", szName);
format(vaultdata, 63, "%d", PlayedTime);
nvault_set(valut, vaultkey, vaultdata);
nvault_close(valut);
}
stock ChatColor(const id, const input[], any:...)
{
new count = 1, players[32]
static msg[191]
vformat(msg, 190, input, 3)
replace_all(msg, 190, "!g", "^4")
replace_all(msg, 190, "!y", "^1")
replace_all(msg, 190, "!t", "^3")
if (id) players[0] = id; else get_players(players, count, "ch")
{
for (new i = 0; i < count; i++)
{
if (is_user_connected(players[i]))
{
message_begin(MSG_ONE_UNRELIABLE,g_msgSayText,_, players[i])
write_byte(players[i]);
write_string(msg);
message_end();
}
}
}
}