sharivan

mostactive_zmbr.sp

Oct 28th, 2017 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 21.81 KB | None | 0 0
  1. /*  SM Most Active
  2.  *
  3.  *  Copyright (C) 2017 Francisco 'Franc1sco' García
  4.  *  Contributor: shanapu
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by the Free
  8.  * Software Foundation, either version 3 of the License, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along with
  16.  * this program. If not, see http://www.gnu.org/licenses/.
  17.  */
  18.  
  19. #pragma semicolon 1
  20. #pragma newdecls required
  21.  
  22. #include <sourcemod>
  23. #include <sdktools>
  24. #include <mostactive>
  25.  
  26. #define IDAYS 26
  27.  
  28. #define VERSION "2.6"
  29.  
  30. int g_iPlayTimeSpec[MAXPLAYERS+1] = 0;
  31. int g_iPlayTimeT[MAXPLAYERS+1] = 0;
  32. int g_iPlayTimeCT[MAXPLAYERS+1] = 0;
  33.  
  34. bool g_bChecked[MAXPLAYERS + 1];
  35.  
  36. char g_sCmdLogPath[256];
  37. char g_sSQLBuffer[3096];
  38.  
  39. bool g_bIsMySQl;
  40.  
  41. // DB handle
  42. Handle g_hDB = INVALID_HANDLE;
  43. Handle gF_OnInsertNewPlayer;
  44.  
  45. int g_iHours;
  46. int g_iMinutes;
  47. int g_iSeconds;
  48.  
  49. public Plugin myinfo = {
  50.     name = "SM Most Active",
  51.     author = "Franc1sco Steam: franug / shanapu",
  52.     description = "A rank based in time played",
  53.     version = VERSION,
  54.     url = "http://steamcommunity.com/id/franug"
  55. };
  56.  
  57. public APLRes AskPluginLoad2(Handle myself, bool late, char [] error, int err_max)
  58. {
  59.     CreateNative("MostActive_GetPlayTimeCT", Native_GetPlayTimeCT);
  60.     CreateNative("MostActive_GetPlayTimeT", Native_GetPlayTimeT);
  61.     CreateNative("MostActive_GetPlayTimeSpec", Native_GetPlayTimeSpec);
  62.     CreateNative("MostActive_GetPlayTimeTotal", Native_GetPlayTimeTotal);
  63.  
  64.     gF_OnInsertNewPlayer = CreateGlobalForward("MostActive_OnInsertNewPlayer", ET_Event, Param_Cell);
  65.  
  66.     RegPluginLibrary("mostactive");
  67.  
  68.     return APLRes_Success;
  69. }
  70.  
  71. public void OnPluginStart()
  72. {
  73.     CreateConVar("sm_mostactive_version", VERSION, "version", FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
  74.     RegConsoleCmd("sm_tempo", DOMenu);
  75.     RegConsoleCmd("sm_total", Command_Wasted);
  76.    
  77.     for(int i=0;;i++)
  78.     {
  79.         BuildPath(Path_SM, g_sCmdLogPath, sizeof(g_sCmdLogPath), "logs/mostactive_%d.log", i);
  80.         if( !FileExists(g_sCmdLogPath) )
  81.             break;
  82.     }
  83.    
  84.     SQL_TConnect(OnSQLConnect, "mostactive");
  85. }
  86.  
  87. public int OnSQLConnect(Handle owner, Handle hndl, char [] error, any data)
  88. {
  89.     if(hndl == INVALID_HANDLE)
  90.     {
  91.         LogError("Database failure: %s", error);
  92.        
  93.         SetFailState("Databases dont work");
  94.     }
  95.     else
  96.     {
  97.         g_hDB = hndl;
  98.        
  99.         SQL_GetDriverIdent(SQL_ReadDriver(g_hDB), g_sSQLBuffer, sizeof(g_sSQLBuffer));
  100.         g_bIsMySQl = StrEqual(g_sSQLBuffer,"mysql", false) ? true : false;
  101.        
  102.         if(g_bIsMySQl)
  103.         {
  104.             Format(g_sSQLBuffer, sizeof(g_sSQLBuffer), "CREATE TABLE IF NOT EXISTS `mostactive` (`playername` varchar(128) NOT NULL, `steamid` varchar(32) PRIMARY KEY NOT NULL,`last_accountuse` int(64) NOT NULL, `timeCT` INT( 16 ), `timeTT` INT( 16 ),`timeSPE` INT( 16 ), `total` INT( 16 ))");
  105.            
  106.             SQL_TQuery(g_hDB, OnSQLConnectCallback, g_sSQLBuffer);
  107.             LogToFileEx(g_sCmdLogPath, "Query %s", g_sSQLBuffer);
  108.         }
  109.         else
  110.         {
  111.             Format(g_sSQLBuffer, sizeof(g_sSQLBuffer), "CREATE TABLE IF NOT EXISTS mostactive (playername varchar(128) NOT NULL, steamid varchar(32) PRIMARY KEY NOT NULL,last_accountuse int(64) NOT NULL, timeCT INTEGER, timeTT INTEGER, timeSPE INTEGER, total INTEGER)");
  112.            
  113.             SQL_TQuery(g_hDB, OnSQLConnectCallback, g_sSQLBuffer);
  114.             LogToFileEx(g_sCmdLogPath, "Query %s", g_sSQLBuffer);
  115.         }
  116.         PruneDatabase();
  117.     }
  118. }
  119.  
  120. public int OnSQLConnectCallback(Handle owner, Handle hndl, char [] error, any data)
  121. {
  122.     if(hndl == INVALID_HANDLE)
  123.     {
  124.         LogError("Query failure: %s", error);
  125.         return;
  126.     }
  127.    
  128.     for(int client = 1; client <= MaxClients; client++)
  129.     {
  130.         if(IsClientInGame(client))
  131.         {
  132.             OnClientPostAdminCheck(client);
  133.         }
  134.     }
  135. }
  136.  
  137. public void InsertSQLNewPlayer(int client)
  138. {
  139.     char query[255], steamid[32];
  140.     GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid));
  141.     int userid = GetClientUserId(client);
  142.    
  143.     char Name[MAX_NAME_LENGTH+1];
  144.     char SafeName[(sizeof(Name)*2)+1];
  145.     if(!GetClientName(client, Name, sizeof(Name)))
  146.         Format(SafeName, sizeof(SafeName), "<noname>");
  147.     else
  148.     {
  149.         TrimString(Name);
  150.         SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
  151.     }
  152.    
  153.     Format(query, sizeof(query), "INSERT INTO mostactive(playername, steamid, last_accountuse, timeCT, timeTT, timeSPE, total) VALUES('%s', '%s', '%d', '0', '0', '0', '0');", SafeName, steamid, GetTime());
  154.     SQL_TQuery(g_hDB, SaveSQLPlayerCallback, query, userid);
  155.     LogToFileEx(g_sCmdLogPath, "Query %s", query);
  156.     g_iPlayTimeCT[client] = 0;
  157.     g_iPlayTimeT[client] = 0;
  158.     g_iPlayTimeSpec[client] = 0;
  159.    
  160.     Call_StartForward(gF_OnInsertNewPlayer);
  161.     Call_PushCell(client);
  162.     Call_Finish();
  163.    
  164.     g_bChecked[client] = true;
  165. }
  166.  
  167. public int Native_GetPlayTimeCT(Handle plugin, int argc)
  168. {
  169.     int client = GetNativeCell(1);
  170.    
  171.     return g_iPlayTimeCT[client];
  172. }
  173.  
  174. public int Native_GetPlayTimeT(Handle plugin, int argc)
  175. {
  176.     int client = GetNativeCell(1);
  177.    
  178.     return g_iPlayTimeT[client];
  179. }
  180.  
  181. public int Native_GetPlayTimeSpec(Handle plugin, int argc)
  182. {
  183.     int client = GetNativeCell(1);
  184.    
  185.     return g_iPlayTimeSpec[client];
  186. }
  187.  
  188. public int Native_GetPlayTimeTotal(Handle plugin, int argc)
  189. {
  190.     int client = GetNativeCell(1);
  191.    
  192.     return g_iPlayTimeSpec[client]+g_iPlayTimeCT[client]+g_iPlayTimeT[client];
  193. }
  194.  
  195. public int SaveSQLPlayerCallback(Handle owner, Handle hndl, char [] error, any data)
  196. {
  197.     if(hndl == INVALID_HANDLE)
  198.     {
  199.         LogError("Query failure: %s", error);
  200.     }
  201. }
  202.  
  203. public void CheckSQLSteamID(int client)
  204. {
  205.     char query[255], steamid[32];
  206.     GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid) );
  207.    
  208.     Format(query, sizeof(query), "SELECT timeCT, timeTT, timeSPE FROM mostactive WHERE steamid = '%s'", steamid);
  209.     SQL_TQuery(g_hDB, CheckSQLSteamIDCallback, query, GetClientUserId(client));
  210.     LogToFileEx(g_sCmdLogPath, "Query %s", query);
  211. }
  212.  
  213. public int CheckSQLSteamIDCallback(Handle owner, Handle hndl, char [] error, any data)
  214. {
  215.     int client;
  216.    
  217.     /* Make sure the client didn't disconnect while the thread was running */
  218.    
  219.     if((client = GetClientOfUserId(data)) == 0)
  220.     {
  221.         return;
  222.     }
  223.    
  224.     if(hndl == INVALID_HANDLE)
  225.     {
  226.         LogError("Query failure: %s", error);
  227.         return;
  228.     }
  229.     if(!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
  230.     {
  231.         InsertSQLNewPlayer(client);
  232.         return;
  233.     }
  234.    
  235.     g_iPlayTimeCT[client] = SQL_FetchInt(hndl, 0);
  236.     g_iPlayTimeT[client] = SQL_FetchInt(hndl, 1);
  237.     g_iPlayTimeSpec[client] = SQL_FetchInt(hndl, 2);
  238.     g_bChecked[client] = true;
  239. }
  240.  
  241. public void SaveSQLCookies(int client)
  242. {
  243.     char steamid[32];
  244.     GetClientAuthId(client, AuthId_Steam2,steamid, sizeof(steamid) );
  245.     char Name[MAX_NAME_LENGTH+1];
  246.     char SafeName[(sizeof(Name)*2)+1];
  247.     if(!GetClientName(client, Name, sizeof(Name)))
  248.         Format(SafeName, sizeof(SafeName), "<noname>");
  249.     else
  250.     {
  251.         TrimString(Name);
  252.         SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
  253.     }  
  254.  
  255.     char buffer[3096];
  256.     Format(buffer, sizeof(buffer), "UPDATE mostactive SET last_accountuse = %d, playername = '%s',timeCT = '%i',timeTT = '%i', timeSPE = '%i',total = '%i' WHERE steamid = '%s';",GetTime(), SafeName, g_iPlayTimeCT[client],g_iPlayTimeT[client],g_iPlayTimeSpec[client],g_iPlayTimeCT[client]+g_iPlayTimeT[client]+g_iPlayTimeSpec[client], steamid);
  257.     SQL_TQuery(g_hDB, SaveSQLPlayerCallback, buffer);
  258.     LogToFileEx(g_sCmdLogPath, "Query %s", buffer);
  259.     g_bChecked[client] = false;
  260. }
  261.  
  262. public void OnPluginEnd()
  263. {
  264.     for(int client = 1; client <= MaxClients; client++)
  265.     {
  266.         if(IsClientInGame(client))
  267.         {
  268.             OnClientDisconnect(client);
  269.         }
  270.     }
  271. }
  272.  
  273. public void OnClientDisconnect(int client)
  274. {
  275.     if(!IsFakeClient(client) && g_bChecked[client]) SaveSQLCookies(client);
  276. }
  277.  
  278. public void OnClientPostAdminCheck(int client)
  279. {
  280.     if(!IsFakeClient(client)) CheckSQLSteamID(client);
  281. }
  282.  
  283. public void PruneDatabase()
  284. {
  285.     if(g_hDB == INVALID_HANDLE)
  286.     {
  287.         LogToFileEx(g_sCmdLogPath, "Prune Database: No connection");
  288.         return;
  289.     }
  290.  
  291.     int maxlastaccuse;
  292.     maxlastaccuse = GetTime() - (IDAYS * 86400);
  293.  
  294.     char buffer[1024];
  295.  
  296.     if(g_bIsMySQl)
  297.         Format(buffer, sizeof(buffer), "DELETE FROM `mostactive` WHERE `last_accountuse`<'%d' AND `last_accountuse`>'0';", maxlastaccuse);
  298.     else
  299.         Format(buffer, sizeof(buffer), "DELETE FROM mostactive WHERE last_accountuse<'%d' AND last_accountuse>'0';", maxlastaccuse);
  300.  
  301.     LogToFileEx(g_sCmdLogPath, "Query %s", buffer);
  302.     SQL_TQuery(g_hDB, PruneDatabaseCallback, buffer);
  303. }
  304.  
  305. public int PruneDatabaseCallback(Handle owner, Handle hndl, char [] error, any data)
  306. {
  307.     if(hndl == INVALID_HANDLE)
  308.     {
  309.         LogToFileEx(g_sCmdLogPath, "Query failure: %s", error);
  310.     }
  311.     //LogMessage("Prune Database successful");
  312. }
  313.  
  314. public void OnMapStart()
  315. {
  316.     CreateTimer(1.0, PlayTimeTimer, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
  317. }
  318.  
  319. public Action PlayTimeTimer(Handle timer)
  320. {
  321.     for(int i = 1; i <= MaxClients; i++)
  322.     {
  323.         if(IsClientInGame(i))
  324.         {
  325.             int team = GetClientTeam(i);
  326.            
  327.             if(team == 2)
  328.             {
  329.                 ++g_iPlayTimeT[i];
  330.             }
  331.             else if(team == 3)
  332.             {
  333.                 ++g_iPlayTimeCT[i];
  334.             }
  335.             else
  336.             {
  337.                 ++g_iPlayTimeSpec[i];
  338.             }
  339.         }
  340.     }
  341. }
  342.  
  343. public void ShowTotal(int client)
  344. {
  345.     if(g_hDB != INVALID_HANDLE)
  346.     {
  347.         char buffer[200];
  348.         Format(buffer, sizeof(buffer), "SELECT playername, total, steamid FROM mostactive ORDER BY total DESC LIMIT 999");
  349.         SQL_TQuery(g_hDB, ShowTotalCallback, buffer, client);
  350.     }
  351.     else
  352.     {
  353.         PrintToChat(client, " \x03Rank System is now not avilable");
  354.     }
  355. }
  356.  
  357. public int ShowTotalCallback(Handle owner, Handle hndl, char [] error, any client)
  358. {
  359.     if(hndl == INVALID_HANDLE)
  360.     {
  361.         LogError(error);
  362.         PrintToServer("Last Connect SQL Error: %s", error);
  363.         return;
  364.     }
  365.    
  366.     Menu menu2 = CreateMenu(DIDMenuHandler2);
  367.     menu2.SetTitle("Top Total played");
  368.    
  369.     int order = 0;
  370.     char number[64];
  371.     char name[64];
  372.     char textbuffer[128];
  373.     char steamid[128];
  374.    
  375.     if(SQL_HasResultSet(hndl))
  376.     {
  377.         while (SQL_FetchRow(hndl))
  378.         {
  379.             order++;
  380.             Format(number,64, "option%i", order);
  381.             SQL_FetchString(hndl, 0, name, sizeof(name));
  382.             SQL_FetchString(hndl, 2, steamid, sizeof(steamid));
  383.             g_iHours = 0;
  384.             g_iMinutes = 0;
  385.             g_iSeconds = 0;
  386.             ShowTimer2(SQL_FetchInt(hndl, 1));
  387.             Format(textbuffer,128, "n%i %s - %d h. %d m. %d s.", order,name,g_iHours, g_iMinutes, g_iSeconds);
  388.             menu2.AddItem(steamid, textbuffer);
  389.         }
  390.     }
  391.     if(order < 1)
  392.     {
  393.         menu2.AddItem("empty", "TOP is empty!");
  394.     }
  395.    
  396.     menu2.ExitButton = true;
  397.     menu2.ExitBackButton = true;
  398.     menu2.Display(client,MENU_TIME_FOREVER);
  399. }
  400.  
  401. public void ShowSpec(int client)
  402. {
  403.     if(g_hDB != INVALID_HANDLE)
  404.     {
  405.         char buffer[200];
  406.         Format(buffer, sizeof(buffer), "SELECT playername, timeSPE, steamid FROM mostactive ORDER BY timeSPE DESC LIMIT 999");
  407.         SQL_TQuery(g_hDB, ShowSpecCallback, buffer, client);
  408.     }
  409.     else
  410.     {
  411.         PrintToChat(client, " \x03Rank System is now not avilable");
  412.     }
  413. }
  414.  
  415. public void ShowSpecCallback(Handle owner, Handle hndl, char [] error, any client)
  416. {
  417.     if(hndl == INVALID_HANDLE)
  418.     {
  419.         LogError(error);
  420.         PrintToServer("Last Connect SQL Error: %s", error);
  421.         return;
  422.     }
  423.    
  424.     Menu menu2 = CreateMenu(DIDMenuHandler2);
  425.     menu2.SetTitle("Top Spectator");
  426.    
  427.     int order = 0;
  428.     char number[64];
  429.     char name[64];
  430.     char textbuffer[128];
  431.     char steamid[128];
  432.    
  433.     if(SQL_HasResultSet(hndl))
  434.     {
  435.         while (SQL_FetchRow(hndl))
  436.         {
  437.             order++;
  438.             Format(number,64, "option%i", order);
  439.             SQL_FetchString(hndl, 0, name, sizeof(name));
  440.             SQL_FetchString(hndl, 2, steamid, sizeof(steamid));
  441.             g_iHours = 0;
  442.             g_iMinutes = 0;
  443.             g_iSeconds = 0;
  444.             ShowTimer2(SQL_FetchInt(hndl, 1));
  445.             Format(textbuffer,128, "n%i %s - %d h. %d m. %d s.", order,name,g_iHours, g_iMinutes, g_iSeconds);
  446.             menu2.AddItem(steamid, textbuffer);
  447.         }
  448.     }
  449.     if(order < 1)
  450.     {
  451.         menu2.AddItem("empty", "TOP is empty!");
  452.     }
  453.    
  454.     menu2.ExitButton = true;
  455.     menu2.ExitBackButton = true;
  456.     menu2.Display(client,MENU_TIME_FOREVER);
  457. }
  458.  
  459. public void ShowTerror(int client)
  460. {
  461.     if(g_hDB != INVALID_HANDLE)
  462.     {
  463.         char buffer[200];
  464.         Format(buffer, sizeof(buffer), "SELECT playername, timeTT, steamid FROM mostactive ORDER BY timeTT DESC LIMIT 999");
  465.         SQL_TQuery(g_hDB, ShowTerrorCallback, buffer, client);
  466.     }
  467.     else
  468.     {
  469.         PrintToChat(client, " \x03Rank System is now not avilable");
  470.     }
  471. }
  472.  
  473. public int ShowTerrorCallback(Handle owner, Handle hndl, char [] error, any client)
  474. {
  475.     if(hndl == INVALID_HANDLE)
  476.     {
  477.         LogError(error);
  478.         PrintToServer("Last Connect SQL Error: %s", error);
  479.         return;
  480.     }
  481.    
  482.     Menu menu2 = CreateMenu(DIDMenuHandler2);
  483.     menu2.SetTitle("Top T");
  484.    
  485.     int order = 0;
  486.     char number[64];
  487.     char name[64];
  488.     char textbuffer[128];
  489.     char steamid[128];
  490.    
  491.     if(SQL_HasResultSet(hndl))
  492.     {
  493.         while (SQL_FetchRow(hndl))
  494.         {
  495.             order++;
  496.             Format(number,64, "option%i", order);
  497.             SQL_FetchString(hndl, 0, name, sizeof(name));
  498.             SQL_FetchString(hndl, 2, steamid, sizeof(steamid));
  499.             g_iHours = 0;
  500.             g_iMinutes = 0;
  501.             g_iSeconds = 0;
  502.             ShowTimer2(SQL_FetchInt(hndl, 1));
  503.             Format(textbuffer,128, "n%i %s - %d h. %d m. %d s.", order,name,g_iHours, g_iMinutes, g_iSeconds);
  504.             menu2.AddItem(steamid, textbuffer);
  505.         }
  506.     }
  507.     if(order < 1)
  508.     {
  509.         menu2.AddItem("empty", "TOP is empty!");
  510.     }
  511.    
  512.     menu2.ExitButton = true;
  513.     menu2.ExitBackButton = true;
  514.     menu2.Display(client,MENU_TIME_FOREVER);
  515. }
  516.  
  517. public void ShowCT(int client)
  518. {
  519.     if(g_hDB != INVALID_HANDLE)
  520.     {
  521.         char buffer[200];
  522.         Format(buffer, sizeof(buffer), "SELECT playername, timeCT, steamid FROM mostactive ORDER BY timeCT DESC LIMIT 999");
  523.         SQL_TQuery(g_hDB, ShowCTCallback, buffer, client);
  524.     }
  525.     else
  526.     {
  527.         PrintToChat(client, " \x03Rank System is now not avilable");
  528.     }
  529. }
  530.  
  531. public int ShowCTCallback(Handle owner, Handle hndl, char [] error, any client)
  532. {
  533.     if(hndl == INVALID_HANDLE)
  534.     {
  535.         LogError(error);
  536.         PrintToServer("Last Connect SQL Error: %s", error);
  537.         return;
  538.     }
  539.    
  540.     Menu menu2 = CreateMenu(DIDMenuHandler2);
  541.     menu2.SetTitle("Top CT");
  542.    
  543.    
  544.     int order = 0;
  545.     char number[64];
  546.     char name[64];
  547.     char textbuffer[128];
  548.     char steamid[128];
  549.    
  550.     if(SQL_HasResultSet(hndl))
  551.     {
  552.         while (SQL_FetchRow(hndl))
  553.         {
  554.             order++;
  555.             Format(number,64, "option%i", order);
  556.             SQL_FetchString(hndl, 0, name, sizeof(name));
  557.             SQL_FetchString(hndl, 2, steamid, sizeof(steamid));
  558.             g_iHours = 0;
  559.             g_iMinutes = 0;
  560.             g_iSeconds = 0;
  561.             ShowTimer2(SQL_FetchInt(hndl, 1));
  562.             Format(textbuffer,128, "n%i %s - %d h. %d m. %d s.", order,name,g_iHours, g_iMinutes, g_iSeconds);
  563.             menu2.AddItem(steamid, textbuffer);
  564.         }
  565.     }
  566.     if(order < 1)
  567.     {
  568.         menu2.AddItem("empty", "TOP is empty!");
  569.     }
  570.    
  571.     menu2.ExitButton = true;
  572.     menu2.ExitBackButton = true;
  573.     menu2.Display(client,MENU_TIME_FOREVER);
  574. }
  575.  
  576. int ShowTimer(int Time, char[] buffer,int sizef)
  577. {
  578.     g_iHours = 0;
  579.     g_iMinutes = 0;
  580.     g_iSeconds = Time;
  581.    
  582.     while(g_iSeconds > 3600)
  583.     {
  584.         g_iHours++;
  585.         g_iSeconds -= 3600;
  586.     }
  587.     while(g_iSeconds > 60)
  588.     {
  589.         g_iMinutes++;
  590.         g_iSeconds -= 60;
  591.     }
  592.     if(g_iHours >= 1)
  593.     {
  594.         Format(buffer, sizef, "%d hours %d minutes %d seconds", g_iHours, g_iMinutes, g_iSeconds );
  595.     }
  596.     else if(g_iMinutes >= 1)
  597.     {
  598.         Format(buffer, sizef, "%d minutes %d seconds", g_iMinutes, g_iSeconds );
  599.     }
  600.     else
  601.     {
  602.         Format(buffer, sizef, "%d seconds", g_iSeconds );
  603.     }
  604. }
  605.  
  606. void ShowTimer2(int Time)
  607. {
  608.     g_iHours = 0;
  609.     g_iMinutes = 0;
  610.     g_iSeconds = Time;
  611.    
  612.     while(g_iSeconds > 3600)
  613.     {
  614.         g_iHours++;
  615.         g_iSeconds -= 3600;
  616.     }
  617.     while(g_iSeconds > 60)
  618.     {
  619.         g_iMinutes++;
  620.         g_iSeconds -= 60;
  621.     }
  622. }
  623.  
  624. bool GetCommunityID(char [] AuthID, char [] FriendID, int size)
  625. {
  626.     if(strlen(AuthID) < 11 || AuthID[0]!='S' || AuthID[6]=='I')
  627.     {
  628.         FriendID[0] = 0;
  629.         return false;
  630.     }
  631.     int iUpper = 765611979;
  632.     int iFriendID = StringToInt(AuthID[10])*2 + 60265728 + AuthID[8]-48;
  633.     int iDiv = iFriendID/100000000;
  634.     int iIdx = 9-(iDiv?iDiv/10+1:0);
  635.     iUpper += iDiv;
  636.     IntToString(iFriendID, FriendID[iIdx], size-iIdx);
  637.     iIdx = FriendID[9];
  638.     IntToString(iUpper, FriendID, size);
  639.     FriendID[9] = iIdx;
  640.     return true;
  641. }
  642.  
  643. public int DIDMenuHandler2(Menu menu2, MenuAction action, int client, int itemNum)
  644. {
  645.     if( action == MenuAction_Select )
  646.     {
  647.             char info[128], community[128];
  648.        
  649.             GetMenuItem(menu2, itemNum, info, sizeof(info));
  650.             GetCommunityID(info, community, sizeof(community));
  651.            
  652.             Format(community, sizeof(community), "http://steamcommunity.com/profiles/%s", community);
  653.             PrintToChat(client, community);
  654.             PrintToConsole(client, community);
  655.     }
  656.     else if(action == MenuAction_Cancel)
  657.     {
  658.         if(itemNum==MenuCancel_ExitBack)
  659.         {
  660.             DOMenu(client,0);
  661.         }
  662.         //PrintToServer("Client %d's menu was cancelled.Reason: %d", client, itemNum);
  663.     }
  664.     else if(action == MenuAction_End)
  665.     {
  666.         CloseHandle(menu2);
  667.     }
  668. }
  669.  
  670. public Action DOMenu(int client, int args)
  671. {
  672.     //PrintToChat(client, "number de arg %i", args);
  673.     if(args > 0)
  674.     {
  675.         char steamid[64];
  676.         GetCmdArgString(steamid, sizeof(steamid));
  677.         //PrintToChat(client, "tengo %s", steamid);
  678.        
  679.         char buffer[200];
  680.         Format(buffer, sizeof(buffer), "SELECT timeCT, timeTT, timeSPE, total, playername FROM mostactive WHERE steamid = '%s'", steamid);
  681.         SQL_TQuery(g_hDB, SQLShowPlayTime, buffer, GetClientUserId(client));
  682.         //LogToFileEx(g_sCmdLogPath, "Query %s", buffer);
  683.     }
  684.     else
  685.     {
  686.         Menu menu = CreateMenu(DIDMenuHandler);
  687.         menu.SetTitle("Most Active");
  688.         menu.AddItem("option1", "View your time");
  689.         menu.AddItem("option2", "View Top total played");
  690.         //menu.AddItem("option4", "View Top T");
  691.         //menu.AddItem("option5", "View Top CT");
  692.         //menu.AddItem("option3", "View Top Spectator");
  693.         menu.ExitButton = true;
  694.         menu.Display(client,MENU_TIME_FOREVER);
  695.     }
  696.     return Plugin_Handled;
  697. }
  698.  
  699. public int SQLShowPlayTime(Handle owner, Handle hndl, char [] error, any data)
  700. {
  701.     int client;
  702.    
  703.     /* Make sure the client didn't disconnect while the thread was running */
  704.     if((client = GetClientOfUserId(data)) == 0)
  705.     {
  706.         return;
  707.     }
  708.    
  709.     if(hndl == INVALID_HANDLE)
  710.     {
  711.         LogError("Query failure: %s", error);
  712.         return;
  713.     }
  714.     if(!SQL_GetRowCount(hndl) || !SQL_FetchRow(hndl))
  715.     {
  716.         PrintToChat(client, " \x03steamid not found in the database");
  717.         return;
  718.     }
  719.     char name[124];
  720.     SQL_FetchString(hndl, 4, name, 124);
  721.    
  722.     Menu menu = CreateMenu(DIDMenuHandlerHandler);
  723.     menu.SetTitle("Time for the player %s", name);
  724.    
  725.     char buffer[124];
  726.    
  727.     ShowTimer(SQL_FetchInt(hndl, 2), buffer, sizeof(buffer));
  728.     Format(buffer, 124, "Spectator: %s", buffer);
  729.     menu.AddItem("", buffer, ITEMDRAW_DISABLED);
  730.    
  731.     ShowTimer(SQL_FetchInt(hndl, 1), buffer, sizeof(buffer));
  732.     Format(buffer, 124, "Terrorist: %s", buffer);
  733.     menu.AddItem("", buffer, ITEMDRAW_DISABLED);
  734.    
  735.     ShowTimer(SQL_FetchInt(hndl, 0), buffer, sizeof(buffer));
  736.     Format(buffer, 124, "Counter-terrorist: %s", buffer);
  737.     menu.AddItem("", buffer, ITEMDRAW_DISABLED);
  738.    
  739.     ShowTimer(SQL_FetchInt(hndl, 3), buffer, sizeof(buffer));
  740.     Format(buffer, 124, "Total played: %s", buffer);
  741.     menu.AddItem("", buffer, ITEMDRAW_DISABLED);
  742.    
  743.     menu.ExitButton = true;
  744.     menu.ExitBackButton = true;
  745.     menu.Display(client,MENU_TIME_FOREVER);
  746. }
  747.  
  748. public int DIDMenuHandler(Menu menu, MenuAction action, int client, int itemNum)
  749. {
  750.     if( action == MenuAction_Select )
  751.     {
  752.         char info[32];
  753.        
  754.         GetMenuItem(menu, itemNum, info, sizeof(info));
  755.        
  756.         if( strcmp(info,"option1") == 0 )
  757.         {
  758.             Menu menu2 = CreateMenu(DIDMenuHandlerHandler);
  759.             menu2.SetTitle("Time for the player %N", client);
  760.            
  761.             char buffer[124];
  762.            
  763.             //ShowTimer(g_iPlayTimeSpec[client], buffer, sizeof(buffer));
  764.             //Format(buffer, 124, "Spectator: %s", buffer);
  765.             //menu2.AddItem("", buffer, ITEMDRAW_DISABLED);
  766.            
  767.             //ShowTimer(g_iPlayTimeT[client], buffer, sizeof(buffer));
  768.             //Format(buffer, 124, "Terrorist: %s", buffer);
  769.             //menu2.AddItem("", buffer, ITEMDRAW_DISABLED);
  770.            
  771.             //ShowTimer(g_iPlayTimeCT[client], buffer, sizeof(buffer));
  772.             //Format(buffer, 124, "Counter-terrorist: %s", buffer);
  773.             //menu2.AddItem("", buffer, ITEMDRAW_DISABLED);
  774.            
  775.             int totalt = (g_iPlayTimeT[client] + g_iPlayTimeCT[client] + g_iPlayTimeSpec[client]);
  776.             ShowTimer(totalt, buffer, sizeof(buffer));
  777.             Format(buffer, 124, "Total played: %s", buffer);
  778.             menu2.AddItem("", buffer, ITEMDRAW_DISABLED);
  779.             menu2.ExitButton = true;
  780.             menu2.ExitBackButton = true;
  781.             menu2.Display(client,MENU_TIME_FOREVER);
  782.             //DOMenu(client, 0);
  783.             //DID(client);
  784.         }
  785.         else if( strcmp(info,"option2") == 0 )
  786.         {
  787.             ShowTotal(client);
  788.             //DID(client);
  789.         }
  790.         else if( strcmp(info,"option3") == 0 )
  791.         {
  792.             ShowSpec(client);
  793.             //DID(client);
  794.         }
  795.         else if( strcmp(info,"option4") == 0 )
  796.         {
  797.             ShowTerror(client);
  798.             //DID(client);
  799.         }
  800.         else if( strcmp(info,"option5") == 0 )
  801.         {
  802.             ShowCT(client);
  803.             //DID(client);
  804.         }
  805.     }
  806.     if(action == MenuAction_Cancel)
  807.     {
  808.         if(itemNum==MenuCancel_ExitBack)
  809.         {
  810.             DOMenu(client,0);
  811.         }
  812.         //PrintToServer("Client %d's menu was cancelled.Reason: %d", client, itemNum);
  813.     }
  814.     else if(action == MenuAction_End)
  815.     {
  816.         CloseHandle(menu);
  817.     }
  818. }
  819.  
  820. public int DIDMenuHandlerHandler(Menu menu, MenuAction action, int client, int itemNum)
  821. {
  822.     if(action == MenuAction_Cancel)
  823.     {
  824.         if(itemNum==MenuCancel_ExitBack)
  825.         {
  826.             DOMenu(client,0);
  827.         }
  828.         //PrintToServer("Client %d's menu was cancelled.Reason: %d", client, itemNum);
  829.     }
  830.     else if(action == MenuAction_End)
  831.     {
  832.         CloseHandle(menu);
  833.     }
  834. }
  835.  
  836. public Action Command_Wasted(int client, int args)
  837. {
  838.     SQL_TQuery(g_hDB, SQLShowWasteTime, "SELECT sum(total) FROM mostactive");
  839.  
  840.     return Plugin_Handled;
  841. }
  842.  
  843. public int SQLShowWasteTime(Handle owner, Handle hndl, char [] error, any client)
  844. {
  845.     if(hndl == INVALID_HANDLE)
  846.     {
  847.         LogError("Query failure: %s", error);
  848.         return;
  849.     }
  850.  
  851.     while (SQL_FetchRow(hndl))
  852.     {
  853.         char buffer[124];
  854.         ShowTimer(SQL_FetchInt(hndl, 0), buffer, sizeof(buffer));
  855.         PrintToChatAll("[Most Active] Players wasted a total of %s on this server", buffer);
  856.     }
  857.  
  858.     delete hndl;
  859. }
Advertisement
Add Comment
Please, Sign In to add comment