norn

nStatistics v0.1

Nov 12th, 2011
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.05 KB | None | 0 0
  1. /*
  2. _________ __ __ .__ __ .__
  3. ____ / _____// |______ _/ |_|__| _______/ |_|__| ____ ______
  4. / \ \_____ \\ __\__ \\ __\ |/ ___/\ __\ |/ ___\ / ___/
  5. | | \/ \| | / __ \| | | |\___ \ | | | \ \___ \___ \
  6. |___| /_______ /|__| (____ /__| |__/____ > |__| |__|\___ >____ >
  7. \/ \/ \/ \/ \/ \/
  8.  
  9. Credits:
  10. Norn (http://forum.sa-mp.com/member.php?u=33812)
  11. Calgon - Answering timestamp related queries (http://forum.sa-mp.com/member.php?u=42627)
  12. Blacklite - Comparing Functions (http://forum.sa-mp.com/member.php?u=46788)
  13. Y_Less - Hooking Method (http://forum.sa-mp.com/member.php?u=29176)
  14. */
  15. #include <a_samp>
  16. #define NS_FILE "nstatistics.txt"
  17. forward NS_SaveData();
  18. forward NS_LoadData();
  19. forward NS_Uptime(&days, &hours, &minutes, &seconds);
  20. forward NS_AddToUptime();
  21. forward NS_ReturnData(type);
  22. forward NS_TotalUptime(&days, &hours, &minutes, &seconds);
  23. #define NS_TYPE_STARTSTAMP 0
  24. #define NS_TYPE_DEATHS 1
  25. #define NS_TYPE_CONNECTIONS 2
  26. #define NS_TYPE_WEAPONS 3
  27. #define NS_TYPE_VEHICLES 4
  28. #define NS_TYPE_PLAYERTEXT 5
  29. #define NS_TYPE_TOTALUPTIME 6
  30. #define NS_TYPE_KICKBANS 7
  31. enum _nStats {
  32. iStartStamp,
  33. iDeaths,
  34. iConnections,
  35. iWeapons,
  36. iVehicles,
  37. iPlayerText,
  38. iTotalUptime,
  39. iKickBans,
  40. }
  41. new nStatistics[_nStats], iINIT_STAMP, NS_UPTIME_TIMER;
  42. public NS_ReturnData(type) {
  43. switch(type) {
  44. case NS_TYPE_STARTSTAMP: {
  45. return nStatistics[iStartStamp];
  46. }
  47. case NS_TYPE_DEATHS: {
  48. return nStatistics[iDeaths];
  49. }
  50. case NS_TYPE_CONNECTIONS: {
  51. return nStatistics[iConnections];
  52. }
  53. case NS_TYPE_WEAPONS: {
  54. return nStatistics[iWeapons];
  55. }
  56. case NS_TYPE_VEHICLES: {
  57. return nStatistics[iVehicles];
  58. }
  59. case NS_TYPE_PLAYERTEXT: {
  60. return nStatistics[iPlayerText];
  61. }
  62. case NS_TYPE_TOTALUPTIME: {
  63. return nStatistics[iTotalUptime];
  64. }
  65. case NS_TYPE_KICKBANS: {
  66. return nStatistics[iKickBans];
  67. }
  68. }
  69. return false;
  70. }
  71. stock NS_TotalUptimeString() {
  72. new returncomparison[32] = "NULL";
  73. format(returncomparison, sizeof(returncomparison), NS_timec(nStatistics[iTotalUptime], 0));
  74. return returncomparison;
  75. }
  76. stock NS_UptimeString() {
  77. new returncomparison[32] = "NULL";
  78. format(returncomparison, sizeof(returncomparison), NS_timec(iINIT_STAMP));
  79. return returncomparison;
  80. }
  81. stock NS_TimeFromFirstUse() {
  82. new returncomparison[32] = "NULL";
  83. format(returncomparison, sizeof(returncomparison), NS_timec(nStatistics[iStartStamp]));
  84. return returncomparison;
  85. }
  86. stock NS_DateFromFirstUse(type) {
  87. new returncomparison[32] = "NULL";
  88. format(returncomparison, sizeof(returncomparison), NS_dateFromTimestamp(nStatistics[iStartStamp], type));
  89. return returncomparison;
  90. }
  91. stock NS_DateFromServerStart(type) {
  92. new returncomparison[32] = "NULL";
  93. format(returncomparison, sizeof(returncomparison), NS_dateFromTimestamp(iINIT_STAMP, type));
  94. return returncomparison;
  95. }
  96. public NS_TotalUptime(&days, &hours, &minutes, &seconds) {
  97. new timestamp = nStatistics[iTotalUptime];
  98. seconds = timestamp;
  99. if(timestamp >= 60) {
  100. minutes = timestamp / 60;
  101. }
  102. if(timestamp >= 3600) {
  103. hours = timestamp / 3600;
  104. }
  105. if(timestamp >= 86400) {
  106. days = timestamp / 86400;
  107. }
  108. return true;
  109. }
  110. public NS_Uptime(&days, &hours, &minutes, &seconds) {
  111. new timestamp = gettime() - iINIT_STAMP;
  112. seconds = timestamp;
  113. if(timestamp >= 60) {
  114. minutes = timestamp / 60;
  115. }
  116. if(timestamp >= 3600) {
  117. hours = timestamp / 3600;
  118. }
  119. if(timestamp >= 86400) {
  120. days = timestamp / 86400;
  121. }
  122. return true;
  123. }
  124. public OnGameModeInit()
  125. {
  126. iINIT_STAMP = gettime();
  127. NS_LoadData();
  128. NS_UPTIME_TIMER = SetTimer("NS_AddToUptime", 1000, true);
  129. return CallLocalFunction("ns_OnGameModeInit","");
  130. }
  131. public NS_AddToUptime() {
  132. nStatistics[iTotalUptime]++;
  133. }
  134. #if defined _ALS_OnGameModeInit
  135. #undef OnGameModeInit
  136. #else
  137. #define _ALS_OnGameModeInit
  138. #endif
  139. #define OnGameModeInit ns_OnGameModeInit
  140. forward ns_OnGameModeInit();
  141. public NS_LoadData() {
  142. new str[128];
  143. if(!fexist(NS_FILE)) {
  144. new File:stats_file = fopen(NS_FILE, io_write);
  145. nStatistics[iStartStamp] = gettime();
  146. format(str, sizeof(str), "%d:%d:%d:%d:%d:%d:%d:%d;",
  147. nStatistics[iStartStamp],
  148. nStatistics[iDeaths],
  149. nStatistics[iConnections],
  150. nStatistics[iWeapons],
  151. nStatistics[iVehicles],
  152. nStatistics[iPlayerText],
  153. nStatistics[iTotalUptime],
  154. nStatistics[iKickBans]);
  155. fwrite(stats_file, str);
  156. fclose(stats_file);
  157. }
  158. else
  159. {
  160. new File:noc_f = fopen(NS_FILE, filemode:io_read), index = 0, load_data[512];
  161. fread(noc_f, load_data);
  162. new line[64];
  163. index = 0;
  164. index = NS_token_by_delim(load_data,line,':',index);
  165. nStatistics[iStartStamp] = strval(line);
  166. index = NS_token_by_delim(load_data,line,':',index+1);
  167. nStatistics[iDeaths] = strval(line);
  168. index = NS_token_by_delim(load_data,line,':',index+1);
  169. nStatistics[iConnections] = strval(line);
  170. index = NS_token_by_delim(load_data,line,':',index+1);
  171. nStatistics[iWeapons] = strval(line);
  172. index = NS_token_by_delim(load_data,line,':',index+1);
  173. nStatistics[iVehicles] = strval(line);
  174. index = NS_token_by_delim(load_data,line,':',index+1);
  175. nStatistics[iPlayerText] = strval(line);
  176. index = NS_token_by_delim(load_data,line,':',index+1);
  177. nStatistics[iTotalUptime] = strval(line);
  178. index = NS_token_by_delim(load_data,line,';',index+1);
  179. nStatistics[iKickBans] = strval(line);
  180. fclose(noc_f);
  181. }
  182. return true;
  183. }
  184. stock NS_token_by_delim(const string[], return_str[], delim, start_index)
  185. {
  186. new x=0;
  187. while(string[start_index] != EOS && string[start_index] != delim) {
  188. return_str[x] = string[start_index];
  189. x++;
  190. start_index++;
  191. }
  192. return_str[x] = EOS;
  193. if(string[start_index] == EOS) start_index = (-1);
  194. return start_index;
  195. }
  196. public NS_SaveData() {
  197. if(fexist(NS_FILE)) {
  198. new str[128];
  199. new File:stats_file = fopen(NS_FILE, io_write);
  200. format(str, sizeof(str), "%d:%d:%d:%d:%d:%d:%d:%d;",
  201. nStatistics[iStartStamp],
  202. nStatistics[iDeaths],
  203. nStatistics[iConnections],
  204. nStatistics[iWeapons],
  205. nStatistics[iVehicles],
  206. nStatistics[iPlayerText],
  207. nStatistics[iTotalUptime],
  208. nStatistics[iKickBans]);
  209. fwrite(stats_file, str);
  210. fclose(stats_file);
  211. return true;
  212. }
  213. return false;
  214. }
  215. public ns_CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_delay) {
  216. nStatistics[iVehicles]++;
  217. return CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_delay);
  218. }
  219. #if defined _ALS_CreateVehicle
  220. #undef CreateVehicle
  221. #else
  222. #define _ALS_CreateVehicle
  223. #endif
  224. #define CreateVehicle ns_CreateVehicle
  225. forward ns_CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_delay);
  226. public OnPlayerDeath(playerid, killerid, reason)
  227. {
  228. nStatistics[iDeaths]++;
  229. return CallLocalFunction("ns_OnPlayerDeath","iii", playerid, killerid, reason);
  230. }
  231. #if defined _ALS_OnPlayerDeath
  232. #undef OnPlayerDeath
  233. #else
  234. #define _ALS_OnPlayerDeath
  235. #endif
  236. #define OnPlayerDeath ns_OnPlayerDeath
  237. forward ns_OnPlayerDeath(playerid, killerid, reason);
  238. public OnGameModeExit()
  239. {
  240. KillTimer(NS_UPTIME_TIMER);
  241. NS_SaveData();
  242. return CallLocalFunction("ns_OnGameModeExit","");
  243. }
  244. #if defined _ALS_OnGameModeExit
  245. #undef OnGameModeExit
  246. #else
  247. #define _ALS_OnGameModeExit
  248. #endif
  249. #define OnGameModeExit ns_OnGameModeExit
  250. forward ns_OnGameModeExit();
  251. public OnPlayerDisconnect(playerid, reason)
  252. {
  253. if(reason == 2) { nStatistics[iKickBans]++; }
  254. return CallLocalFunction("ns_OnPlayerDisconnect","ii", playerid, reason);
  255. }
  256. #if defined _ALS_OnPlayerDisconnect
  257. #undef OnPlayerDisconnect
  258. #else
  259. #define _ALS_OnPlayerDisconnect
  260. #endif
  261. #define OnPlayerDisconnect ns_OnPlayerDisconnect
  262. forward ns_OnPlayerDisconnect(playerid, reason);
  263. public OnPlayerConnect(playerid)
  264. {
  265. nStatistics[iConnections]++;
  266. return CallLocalFunction("ns_OnPlayerConnect","i", playerid);
  267. }
  268. #if defined _ALS_OnPlayerConnect
  269. #undef OnPlayerConnect
  270. #else
  271. #define _ALS_OnPlayerConnect
  272. #endif
  273. #define OnPlayerConnect ns_OnPlayerConnect
  274. forward ns_OnPlayerConnect(playerid);
  275. public OnPlayerText(playerid, text[])
  276. {
  277. nStatistics[iPlayerText]++;
  278. if (text[0]) return CallLocalFunction("ns_OnPlayerText","is",playerid,text);
  279. else return CallLocalFunction("ns_OnPlayerText","is",playerid,"\1\0");
  280. }
  281. #if defined _ALS_OnPlayerText
  282. #undef OnPlayerText
  283. #else
  284. #define _ALS_OnPlayerText
  285. #endif
  286. #define OnPlayerText ns_OnPlayerText
  287. forward ns_OnPlayerText(playerid, text[]);
  288. stock ns_GivePlayerWeapon(playerid, weaponid, ammo)
  289. {
  290. nStatistics[iWeapons]++;
  291. return GivePlayerWeapon(playerid, weaponid, ammo);
  292. }
  293.  
  294. #if defined _ALS_GivePlayerWeapon
  295. #undef GivePlayerWeapon
  296. #else
  297. #define _ALS_GivePlayerWeapon
  298. #endif
  299. #define GivePlayerWeapon ns_GivePlayerWeapon
  300. stock NS_timec(timestamp, compare = -1) { // BlackLite - Converted by Calgon
  301. if (compare == -1) {
  302. compare = gettime();
  303. }
  304. new
  305. n,
  306. Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
  307. returnstr[32];
  308. if (d < 60) {
  309. format(returnstr, sizeof(returnstr), "< 1 minute");
  310. return returnstr;
  311. } else if (d < 3600) { // 3600 = 1 hour
  312. n = floatround(floatdiv(d, 60.0), floatround_floor);
  313. format(returnstr, sizeof(returnstr), "minute");
  314. } else if (d < 86400) { // 86400 = 1 day
  315. n = floatround(floatdiv(d, 3600.0), floatround_floor);
  316. format(returnstr, sizeof(returnstr), "hour");
  317. } else if (d < 2592000) { // 2592000 = 1 month
  318. n = floatround(floatdiv(d, 86400.0), floatround_floor);
  319. format(returnstr, sizeof(returnstr), "day");
  320. } else if (d < 31536000) { // 31536000 = 1 year
  321. n = floatround(floatdiv(d, 2592000.0), floatround_floor);
  322. format(returnstr, sizeof(returnstr), "month");
  323. } else {
  324. n = floatround(floatdiv(d, 31536000.0), floatround_floor);
  325. format(returnstr, sizeof(returnstr), "year");
  326. }
  327. if (n == 1) {
  328. format(returnstr, sizeof(returnstr), "1 %s", returnstr);
  329. } else {
  330. format(returnstr, sizeof(returnstr), "%d %ss", n, returnstr);
  331. }
  332. return returnstr;
  333. }
  334. stock NS_dateFromTimestamp( timestamp, _form=0 )
  335. {
  336. new year=1970, day=0, month=0, hour=0, mins=0, sec=0;
  337. new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  338. new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  339. new returnstring[32];
  340.  
  341. while(timestamp>31622400){
  342. timestamp -= 31536000;
  343. if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
  344. year++;
  345. }
  346.  
  347. if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
  348. days_of_month[1] = 29;
  349. else
  350. days_of_month[1] = 28;
  351.  
  352.  
  353. while(timestamp>86400){
  354. timestamp -= 86400, day++;
  355. if(day==days_of_month[month]) day=0, month++;
  356. }
  357.  
  358. while(timestamp>60){
  359. timestamp -= 60, mins++;
  360. if( mins == 60) mins=0, hour++;
  361. }
  362.  
  363. sec=timestamp;
  364.  
  365. switch( _form ){
  366. case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
  367. case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
  368. 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);
  369.  
  370. default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
  371. }
  372.  
  373. return returnstring;
  374. }
  375.  
Advertisement
Add Comment
Please, Sign In to add comment