|
Senior Member
Join Date: Jan 2011
Location: Big snow country.. :)
|

06-14-2013
, 15:34
Add a some values to vault..
|
#1
|
Hello!
I have this plugin, which collect played time.
PHP Code:
/*Played Time with "Current(Total) played Time" on server.*/ /*Many thanks to hackziner & Deviance for show how to use "nvault".*/ /*Thanks to Avalanche for Prune function*/
#include <amxmodx> #include <amxmisc> #include <nvault> #include <nvault_util>
#define PLUGIN "Played Time" #define VERSION "1.3" #define AUTHOR "Alka"
/*Prune time:ater x time of beeing inactive,remove valutdata*/ #define PRUNE_TIME 5184000 /*2 x 30 days*/ /*Time in seconds*/
enum _:VaultDataN { VD_Key[64], VD_Value };
new TotalPlayedTime[33];
public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR ); register_concmd("amx_ptime", "admin_showptime", ADMIN_KICK," <#Player Name> - Details about playedtime."); register_clcmd("say /ptop15", "show_top15"); register_clcmd("say_team /ptop15", "show_top15"); }
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); if((containi(name, "Player") != -1) || (containi(name, "detected") != -1)) { console_print(id, "TimeStats data not available for nickname player and etc."); return PLUGIN_HANDLED } 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, "[PT]%s have been playing on the server for %d minute%s.",name, timep, timep == 1 ? "" : "s"); console_print(id, "[PT]%s's total played time on the server %d minute%s.",name, timep+TotalPlayedTime[player], timep == 1 ? "" : "s"); // new console_print(id, "[PT]Current time: %s", ctime); console_print(id, "-----------------------------------------------------------------"); return PLUGIN_HANDLED; }
public client_disconnect(id) { if(is_user_bot(id)) return PLUGIN_HANDLED new pName[33] get_user_name(id, pName, charsmax(pName)) if((containi(pName, "Player") != -1) || (containi(pName, "detected") != -1)) return PLUGIN_HANDLED TotalPlayedTime[id] = TotalPlayedTime[id] + (get_user_time(id)/60); SaveTime(id, TotalPlayedTime[id]); return PLUGIN_CONTINUE }
public client_putinserver(id) { if(is_user_bot(id)) return PLUGIN_HANDLED new pName[33] get_user_name(id, pName, charsmax(pName)) if((containi(pName, "Player") != -1) || (containi(pName, "detected") != -1)) return PLUGIN_HANDLED TotalPlayedTime[id] = LoadTime(id); return PLUGIN_CONTINUE }
public LoadTime( id ) { if(is_user_bot(id)) { return PLUGIN_HANDLED } new valut = nvault_open("Time_played") new name[33]; new vaultkey[64], vaultdata[64]; get_user_name(id, name, 32); if((containi(name, "Player") != -1) || (containi(name, "detected") != -1)) return PLUGIN_HANDLED format(vaultkey, 63, "> %s", name); nvault_get(valut, vaultkey, vaultdata, 63); nvault_close(valut); return str_to_num(vaultdata); }
public SaveTime(id,PlayedTime) { if(is_user_bot(id)) return PLUGIN_HANDLED new valut = nvault_open("Time_played") if(valut == INVALID_HANDLE) set_fail_state("nValut returned invalid handle") new name[33]; new vaultkey[64], vaultdata[64]; get_user_name(id, name, 32); if((containi(name, "Player") != -1) || (containi(name, "detected") != -1)) return PLUGIN_HANDLED format(vaultkey, 63, "> %s", name); format(vaultdata, 63, "%d", PlayedTime); nvault_set(valut, vaultkey, vaultdata); nvault_close(valut); return PLUGIN_CONTINUE }
public prune() { new valut = nvault_open("Time_played"); if(valut == INVALID_HANDLE) set_fail_state("nValut returned invalid handle"); nvault_prune(valut, 0, get_systime() - PRUNE_TIME); nvault_close(valut); }
public plugin_end() { prune() }
public show_top15(id) { // maximum number of entries to save in array const MAX_ENTRIES = 10; // open the vualt new vault = nvault_util_open("vaultname"); // create our array to hold entries and keep track of its size new Array:entries = ArrayCreate(VaultDataN); new sizeEntries; // count entries in vault and prepare variables new numEntries = nvault_util_count(vault); new data[VaultDataN], value[12], data2[VaultDataN]; // iterate through all entries for(new i = 0, pos, timestamp; i < numEntries; i++) { // grab entry data from current position pos = nvault_util_read(vault, pos, data[VD_Key], charsmax(data[VD_Key]), value, charsmax(value), timestamp); // turn value string into integer data[VD_Value] = str_to_num(value); // if this is the first entry if(sizeEntries == 0) { // go ahead and add it ArrayPushArray(entries, data); sizeEntries++; } else { // loop through other entries to see where this one should be placed (sorted from HIGH->LOW) for(timestamp = 0; timestamp <= sizeEntries; timestamp++) { // if we looped through all entries without finding a place if(timestamp == sizeEntries) { // this entry value is too low to fit before any others // if we have room at the end of the array if(sizeEntries < MAX_ENTRIES) { // add it to the end ArrayPushArray(entries, data); sizeEntries++; } // don't continue with code below break; } // grab current entry to compare it with ArrayGetArray(entries, timestamp, data2); // if this new entry should be placed before the compared entry if(data[VD_Value] >= data2[VD_Value]) { // insert before ArrayInsertArrayBefore(entries, timestamp, data); // if we aren't maxxed out if(sizeEntries < MAX_ENTRIES) { // increase entry size sizeEntries++; } else { // delete the last entry to keep the size at maximum ArrayDeleteItem(entries, sizeEntries); } break; } } } } // close the vault nvault_util_close(vault); // loop through our entries and display them server_print("Top %d entries are:", MAX_ENTRIES); for(new i = 0; i < sizeEntries; i++) { // grab current entry ArrayGetArray(entries, i, data); // truncate entry key for output data[VD_Key][20] = 0; // display data server_print("%d. %-20.20s %d", (i + 1), data[VD_Key], data[VD_Value]); } // destroy the entry array from cache ArrayDestroy(entries); }
stock bool:IsOdd(num) { return num % 2 == 0; }
public stats_custom_compare(elem1[],elem2[]) { if(elem1[1] > elem2[1]) return -1; else if(elem1[1] < elem2[1]) return 1; return 0; }
It's a working combination of:
Exolent[jNr]'s https://forums.alliedmods.net/showpo...11&postcount=6
and
Alka's https://forums.alliedmods.net/showthread.php?p=457071
What we have: a number in list, name of player and his total time in game):
PHP Code:
server_print("%d. %-20.20s %d", (i + 1), data[VD_Key], data[VD_Value]);
What i want: new format of stored data - number in list, name of player, his total time, tis time for each day of last week, last connection time. We can replace su, mo, tu and etc. with a date (8, 9, 10, 11, 12, 13, 14 of jun, for example)
PHP Code:
server_print("%d. %-20.20s %d %d %d %d %d %d %d %d %s", (i + 1), data[VD_Key], data[VD_Value], data[su], data[mo], data[tu], data[we], data[th], data[fr], data[sa], data[last]);
PHP Code:
[num][name] [total played time] [su] [mo] [tu] [we] [th] [fr] [sa] [last] [1] [user1] [6565] [66] [55] [32] [0] [10] [1] [150] [01/01/2012 @ 15:45:01]
__________________
sorry my bad english...
Last edited by alonelive; 06-14-2013 at 15:38.
|
|