Advertisement
Jochemd

Player Average Calculator.inc

Nov 6th, 2012
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.27 KB | None | 0 0
  1. #include <a_samp>
  2. #include <Dini>
  3.  
  4. #define GetServerAveragePlayers() j_a_GlobalInfo[CurrentAverage]
  5.  
  6. #define TIME_USED 1
  7. /* TIME_USED is the interval for refreshing the average
  8.     - Use '1' for each 10 seconds
  9.     - Use '2' for each minute
  10.     - Use '3' for each hour
  11.    
  12.     WARNING: When changing TIME_USED, you must reset the time saved (delete "j_average.jcfg")
  13.    
  14. */
  15.  
  16.  
  17. enum j_a_gInfo
  18. {
  19.     PlayersConnected,
  20.     TotalPlayers,
  21.     TotalChecks,
  22.     Float:CurrentAverage
  23. };
  24.  
  25. new j_a_GlobalInfo[j_a_gInfo];
  26. static gj_a__HasCB[2];
  27.  
  28. #if defined FILTERSCRIPT
  29.     #define UseCallback OnFilterScriptInit
  30. #else
  31.     #define UseCallback OnGameModeInit
  32. #endif
  33.  
  34. public UseCallback()
  35. {
  36.     #if TIME_USED == 1
  37.         SetTimer("OnAverageUpdate",10000,1);
  38.     #elseif TIME_USED == 2
  39.         SetTimer("OnAverageUpdate",60000,1);
  40.     #elseif TIME_USED == 3
  41.         SetTimer("OnAverageUpdate",3600000,1);
  42.     #endif
  43.    
  44.     if(fexist("j_average.jcfg"))
  45.     {
  46.         j_a_GlobalInfo[TotalPlayers] = dini_Int("j_average.jcfg","TotalPlayers");
  47.         j_a_GlobalInfo[TotalChecks] = dini_Int("j_average.jcfg","TotalChecks");
  48.         j_a_GlobalInfo[CurrentAverage] = dini_Int("j_average.jcfg","CurrentAverage");
  49.     }
  50.     else dini_Create("j_average.jcfg");
  51.     #if defined FILTERSCRIPT
  52.         for(new i = 0; i < MAX_PLAYERS; i ++)
  53.         {
  54.             if(IsPlayerConnected(i)) j_a_GlobalInfo[OnlinePlayers] ++;
  55.         }
  56.     #endif
  57.     gj_a__HasCB[0] = funcidx("j_a__OnPlayerConnect") != -1;
  58.     gj_a__HasCB[1] = funcidx("j_a__OnPlayerDisconnect") != -1;
  59.     if(funcidx("j_a__"#UseCallback"") != -1)
  60.     {
  61.         return CallLocalFunction("j_a__"#UseCallback"", "");
  62.     }
  63.     return 1;
  64. }
  65.  
  66. #if defined FILTERSCRIPT
  67.     #if defined _ALS_OnFilterScriptInit
  68.         #undef OnFilterScriptInit
  69.     #else
  70.         #define OnFilterScriptInit
  71.     #endif
  72.     #define OnGameModeInit j_a__OnFilterScriptInit
  73.     forward j_a__OnFilterScriptInit();
  74. #else
  75.     #if defined _ALS_OnGameModeInit
  76.         #undef OnGameModeInit
  77.     #else
  78.         #define _ALS_OnGameModeInit
  79.     #endif
  80.     #define OnGameModeInit j_a__OnGameModeInit
  81.     forward j_a__OnGameModeInit();
  82. #endif
  83.  
  84. public OnPlayerConnect(playerid)
  85. {
  86.     j_a_GlobalInfo[PlayersConnected] ++;
  87.     if(gj_a__HasCB[0])
  88.     {
  89.         return CallLocalFunction("j_a__OnPlayerConnect","i",playerid);
  90.     }
  91.     return 1;
  92. }
  93.  
  94. #if defined _ALS_OnPlayerConnect
  95.     #undef OnPlayerConnect
  96. #else
  97.     #define _ALS_OnPlayerConnect
  98. #endif
  99. #define OnPlayerConnect j_a__OnPlayerConnect
  100. forward j_a__OnPlayerConnect(playerid);
  101.  
  102. public OnPlayerDisconnect(playerid,  reason)
  103. {
  104.     j_a_GlobalInfo[PlayersConnected] --;
  105.     if(gj_a__HasCB[1])
  106.     {
  107.         return CallLocalFunction("j_a__OnPlayerDisconnect","ii",playerid,reason);
  108.     }
  109.     return 1;
  110. }
  111. #if defined _ALS_OnPlayerDisconnect
  112.     #undef OnPlayerDisconnect
  113. #else
  114.     #define _ALS_OnPlayerDisconnect
  115. #endif
  116. #define OnPlayerDisconnect j_a__OnPlayerDisconnect
  117. forward j_a__OnPlayerDisconnect(playerid,  reason);
  118.  
  119. forward OnAverageUpdate();
  120. public OnAverageUpdate()
  121. {
  122.     j_a_GlobalInfo[TotalChecks] ++;
  123.     j_a_GlobalInfo[TotalPlayers] += j_a_GlobalInfo[PlayersConnected];
  124.     j_a_GlobalInfo[CurrentAverage] = floatdiv(j_a_GlobalInfo[TotalPlayers],j_a_GlobalInfo[TotalChecks]);
  125.    
  126.     dini_IntSet("j_average.jcfg","TotalPlayers",j_a_GlobalInfo[TotalPlayers]);
  127.     dini_IntSet("j_average.jcfg","TotalChecks",j_a_GlobalInfo[TotalChecks]);
  128.     dini_FloatSet("j_average.jcfg","CurrentAverage",j_a_GlobalInfo[CurrentAverage]);
  129.     return 1;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement