Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- _________ __ __ .__ __ .__
- ____ / _____// |______ _/ |_|__| _______/ |_|__| ____ ______
- / \ \_____ \\ __\__ \\ __\ |/ ___/\ __\ |/ ___\ / ___/
- | | \/ \| | / __ \| | | |\___ \ | | | \ \___ \___ \
- |___| /_______ /|__| (____ /__| |__/____ > |__| |__|\___ >____ >
- \/ \/ \/ \/ \/ \/
- Credits:
- Norn (http://forum.sa-mp.com/member.php?u=33812)
- Calgon - Answering timestamp related queries (http://forum.sa-mp.com/member.php?u=42627)
- Blacklite - Comparing Functions (http://forum.sa-mp.com/member.php?u=46788)
- Y_Less - Hooking Method (http://forum.sa-mp.com/member.php?u=29176)
- */
- #include <a_samp>
- #define NS_FILE "nstatistics.txt"
- forward NS_SaveData();
- forward NS_LoadData();
- forward NS_Uptime(&days, &hours, &minutes, &seconds);
- forward NS_AddToUptime();
- forward NS_ReturnData(type);
- forward NS_TotalUptime(&days, &hours, &minutes, &seconds);
- #define NS_TYPE_STARTSTAMP 0
- #define NS_TYPE_DEATHS 1
- #define NS_TYPE_CONNECTIONS 2
- #define NS_TYPE_WEAPONS 3
- #define NS_TYPE_VEHICLES 4
- #define NS_TYPE_PLAYERTEXT 5
- #define NS_TYPE_TOTALUPTIME 6
- #define NS_TYPE_KICKBANS 7
- enum _nStats {
- iStartStamp,
- iDeaths,
- iConnections,
- iWeapons,
- iVehicles,
- iPlayerText,
- iTotalUptime,
- iKickBans,
- }
- new nStatistics[_nStats], iINIT_STAMP, NS_UPTIME_TIMER;
- public NS_ReturnData(type) {
- switch(type) {
- case NS_TYPE_STARTSTAMP: {
- return nStatistics[iStartStamp];
- }
- case NS_TYPE_DEATHS: {
- return nStatistics[iDeaths];
- }
- case NS_TYPE_CONNECTIONS: {
- return nStatistics[iConnections];
- }
- case NS_TYPE_WEAPONS: {
- return nStatistics[iWeapons];
- }
- case NS_TYPE_VEHICLES: {
- return nStatistics[iVehicles];
- }
- case NS_TYPE_PLAYERTEXT: {
- return nStatistics[iPlayerText];
- }
- case NS_TYPE_TOTALUPTIME: {
- return nStatistics[iTotalUptime];
- }
- case NS_TYPE_KICKBANS: {
- return nStatistics[iKickBans];
- }
- }
- return false;
- }
- stock NS_TotalUptimeString() {
- new returncomparison[32] = "NULL";
- format(returncomparison, sizeof(returncomparison), NS_timec(nStatistics[iTotalUptime], 0));
- return returncomparison;
- }
- stock NS_UptimeString() {
- new returncomparison[32] = "NULL";
- format(returncomparison, sizeof(returncomparison), NS_timec(iINIT_STAMP));
- return returncomparison;
- }
- stock NS_TimeFromFirstUse() {
- new returncomparison[32] = "NULL";
- format(returncomparison, sizeof(returncomparison), NS_timec(nStatistics[iStartStamp]));
- return returncomparison;
- }
- stock NS_DateFromFirstUse(type) {
- new returncomparison[32] = "NULL";
- format(returncomparison, sizeof(returncomparison), NS_dateFromTimestamp(nStatistics[iStartStamp], type));
- return returncomparison;
- }
- stock NS_DateFromServerStart(type) {
- new returncomparison[32] = "NULL";
- format(returncomparison, sizeof(returncomparison), NS_dateFromTimestamp(iINIT_STAMP, type));
- return returncomparison;
- }
- public NS_TotalUptime(&days, &hours, &minutes, &seconds) {
- new timestamp = nStatistics[iTotalUptime];
- seconds = timestamp;
- if(timestamp >= 60) {
- minutes = timestamp / 60;
- }
- if(timestamp >= 3600) {
- hours = timestamp / 3600;
- }
- if(timestamp >= 86400) {
- days = timestamp / 86400;
- }
- return true;
- }
- public NS_Uptime(&days, &hours, &minutes, &seconds) {
- new timestamp = gettime() - iINIT_STAMP;
- seconds = timestamp;
- if(timestamp >= 60) {
- minutes = timestamp / 60;
- }
- if(timestamp >= 3600) {
- hours = timestamp / 3600;
- }
- if(timestamp >= 86400) {
- days = timestamp / 86400;
- }
- return true;
- }
- public OnGameModeInit()
- {
- iINIT_STAMP = gettime();
- NS_LoadData();
- NS_UPTIME_TIMER = SetTimer("NS_AddToUptime", 1000, true);
- return CallLocalFunction("ns_OnGameModeInit","");
- }
- public NS_AddToUptime() {
- nStatistics[iTotalUptime]++;
- }
- #if defined _ALS_OnGameModeInit
- #undef OnGameModeInit
- #else
- #define _ALS_OnGameModeInit
- #endif
- #define OnGameModeInit ns_OnGameModeInit
- forward ns_OnGameModeInit();
- public NS_LoadData() {
- new str[128];
- if(!fexist(NS_FILE)) {
- new File:stats_file = fopen(NS_FILE, io_write);
- nStatistics[iStartStamp] = gettime();
- format(str, sizeof(str), "%d:%d:%d:%d:%d:%d:%d:%d;",
- nStatistics[iStartStamp],
- nStatistics[iDeaths],
- nStatistics[iConnections],
- nStatistics[iWeapons],
- nStatistics[iVehicles],
- nStatistics[iPlayerText],
- nStatistics[iTotalUptime],
- nStatistics[iKickBans]);
- fwrite(stats_file, str);
- fclose(stats_file);
- }
- else
- {
- new File:noc_f = fopen(NS_FILE, filemode:io_read), index = 0, load_data[512];
- fread(noc_f, load_data);
- new line[64];
- index = 0;
- index = NS_token_by_delim(load_data,line,':',index);
- nStatistics[iStartStamp] = strval(line);
- index = NS_token_by_delim(load_data,line,':',index+1);
- nStatistics[iDeaths] = strval(line);
- index = NS_token_by_delim(load_data,line,':',index+1);
- nStatistics[iConnections] = strval(line);
- index = NS_token_by_delim(load_data,line,':',index+1);
- nStatistics[iWeapons] = strval(line);
- index = NS_token_by_delim(load_data,line,':',index+1);
- nStatistics[iVehicles] = strval(line);
- index = NS_token_by_delim(load_data,line,':',index+1);
- nStatistics[iPlayerText] = strval(line);
- index = NS_token_by_delim(load_data,line,':',index+1);
- nStatistics[iTotalUptime] = strval(line);
- index = NS_token_by_delim(load_data,line,';',index+1);
- nStatistics[iKickBans] = strval(line);
- fclose(noc_f);
- }
- return true;
- }
- stock NS_token_by_delim(const string[], return_str[], delim, start_index)
- {
- new x=0;
- while(string[start_index] != EOS && string[start_index] != delim) {
- return_str[x] = string[start_index];
- x++;
- start_index++;
- }
- return_str[x] = EOS;
- if(string[start_index] == EOS) start_index = (-1);
- return start_index;
- }
- public NS_SaveData() {
- if(fexist(NS_FILE)) {
- new str[128];
- new File:stats_file = fopen(NS_FILE, io_write);
- format(str, sizeof(str), "%d:%d:%d:%d:%d:%d:%d:%d;",
- nStatistics[iStartStamp],
- nStatistics[iDeaths],
- nStatistics[iConnections],
- nStatistics[iWeapons],
- nStatistics[iVehicles],
- nStatistics[iPlayerText],
- nStatistics[iTotalUptime],
- nStatistics[iKickBans]);
- fwrite(stats_file, str);
- fclose(stats_file);
- return true;
- }
- return false;
- }
- public ns_CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_delay) {
- nStatistics[iVehicles]++;
- return CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_delay);
- }
- #if defined _ALS_CreateVehicle
- #undef CreateVehicle
- #else
- #define _ALS_CreateVehicle
- #endif
- #define CreateVehicle ns_CreateVehicle
- forward ns_CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_delay);
- public OnPlayerDeath(playerid, killerid, reason)
- {
- nStatistics[iDeaths]++;
- return CallLocalFunction("ns_OnPlayerDeath","iii", playerid, killerid, reason);
- }
- #if defined _ALS_OnPlayerDeath
- #undef OnPlayerDeath
- #else
- #define _ALS_OnPlayerDeath
- #endif
- #define OnPlayerDeath ns_OnPlayerDeath
- forward ns_OnPlayerDeath(playerid, killerid, reason);
- public OnGameModeExit()
- {
- KillTimer(NS_UPTIME_TIMER);
- NS_SaveData();
- return CallLocalFunction("ns_OnGameModeExit","");
- }
- #if defined _ALS_OnGameModeExit
- #undef OnGameModeExit
- #else
- #define _ALS_OnGameModeExit
- #endif
- #define OnGameModeExit ns_OnGameModeExit
- forward ns_OnGameModeExit();
- public OnPlayerDisconnect(playerid, reason)
- {
- if(reason == 2) { nStatistics[iKickBans]++; }
- return CallLocalFunction("ns_OnPlayerDisconnect","ii", playerid, reason);
- }
- #if defined _ALS_OnPlayerDisconnect
- #undef OnPlayerDisconnect
- #else
- #define _ALS_OnPlayerDisconnect
- #endif
- #define OnPlayerDisconnect ns_OnPlayerDisconnect
- forward ns_OnPlayerDisconnect(playerid, reason);
- public OnPlayerConnect(playerid)
- {
- nStatistics[iConnections]++;
- return CallLocalFunction("ns_OnPlayerConnect","i", playerid);
- }
- #if defined _ALS_OnPlayerConnect
- #undef OnPlayerConnect
- #else
- #define _ALS_OnPlayerConnect
- #endif
- #define OnPlayerConnect ns_OnPlayerConnect
- forward ns_OnPlayerConnect(playerid);
- public OnPlayerText(playerid, text[])
- {
- nStatistics[iPlayerText]++;
- if (text[0]) return CallLocalFunction("ns_OnPlayerText","is",playerid,text);
- else return CallLocalFunction("ns_OnPlayerText","is",playerid,"\1\0");
- }
- #if defined _ALS_OnPlayerText
- #undef OnPlayerText
- #else
- #define _ALS_OnPlayerText
- #endif
- #define OnPlayerText ns_OnPlayerText
- forward ns_OnPlayerText(playerid, text[]);
- stock ns_GivePlayerWeapon(playerid, weaponid, ammo)
- {
- nStatistics[iWeapons]++;
- return GivePlayerWeapon(playerid, weaponid, ammo);
- }
- #if defined _ALS_GivePlayerWeapon
- #undef GivePlayerWeapon
- #else
- #define _ALS_GivePlayerWeapon
- #endif
- #define GivePlayerWeapon ns_GivePlayerWeapon
- stock NS_timec(timestamp, compare = -1) { // BlackLite - Converted by Calgon
- if (compare == -1) {
- compare = gettime();
- }
- new
- n,
- Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
- returnstr[32];
- if (d < 60) {
- format(returnstr, sizeof(returnstr), "< 1 minute");
- return returnstr;
- } else if (d < 3600) { // 3600 = 1 hour
- n = floatround(floatdiv(d, 60.0), floatround_floor);
- format(returnstr, sizeof(returnstr), "minute");
- } else if (d < 86400) { // 86400 = 1 day
- n = floatround(floatdiv(d, 3600.0), floatround_floor);
- format(returnstr, sizeof(returnstr), "hour");
- } else if (d < 2592000) { // 2592000 = 1 month
- n = floatround(floatdiv(d, 86400.0), floatround_floor);
- format(returnstr, sizeof(returnstr), "day");
- } else if (d < 31536000) { // 31536000 = 1 year
- n = floatround(floatdiv(d, 2592000.0), floatround_floor);
- format(returnstr, sizeof(returnstr), "month");
- } else {
- n = floatround(floatdiv(d, 31536000.0), floatround_floor);
- format(returnstr, sizeof(returnstr), "year");
- }
- if (n == 1) {
- format(returnstr, sizeof(returnstr), "1 %s", returnstr);
- } else {
- format(returnstr, sizeof(returnstr), "%d %ss", n, returnstr);
- }
- return returnstr;
- }
- stock NS_dateFromTimestamp( timestamp, _form=0 )
- {
- new year=1970, day=0, month=0, hour=0, mins=0, sec=0;
- new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
- new returnstring[32];
- while(timestamp>31622400){
- timestamp -= 31536000;
- if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
- year++;
- }
- if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
- days_of_month[1] = 29;
- else
- days_of_month[1] = 28;
- while(timestamp>86400){
- timestamp -= 86400, day++;
- if(day==days_of_month[month]) day=0, month++;
- }
- while(timestamp>60){
- timestamp -= 60, mins++;
- if( mins == 60) mins=0, hour++;
- }
- sec=timestamp;
- switch( _form ){
- case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
- case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
- case 3: format(returnstring, 31, "%d %c%c%c %d, %02d:%02d", day+1,names_of_month[month][0],names_of_month[month][1],names_of_month[month][2], year,hour,mins);
- default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
- }
- return returnstring;
- }
Advertisement
Add Comment
Please, Sign In to add comment