From LolZ, 1 Year ago, written in C++.
This paste will give up the ghost in 1 Second.
Embed
  1. #pragma semicolon 1
  2.  
  3. #define PLUGIN_NAME "Tango Win Tracker"
  4. #define PLUGIN_AUTHOR "Jordan Allard (LolZ)"
  5. #define PLUGIN_DESCRIPTION "Tracks players' Arena 1 wins on Multi1v1 Arena servers."
  6. #define PLUGIN_VERSION "1.00"
  7. #define PLUGIN_CONTACT "www.lolztv12.com"
  8.  
  9. #define PLUGIN_PREFIX "[\x06Wins\x01]"
  10. #define winDb "storage-local"
  11.  
  12. #include <sourcemod>
  13. #include <sdktools>
  14. #include <multi1v1>
  15.  
  16. #pragma newdecls required
  17.  
  18. bool resetPending = false;
  19.  
  20. char SQL_CreateWins[] = "CREATE TABLE IF NOT EXISTS win_tracker (player_name VARCHAR(64), player_id VARCHAR(64), player_wins UNSIGNED INTEGER)";
  21. char SQL_AddPlayer[] = "INSERT INTO win_tracker VALUES ('%s', '%s', %i)";
  22. char SQL_ResetPlayer[] = "DELETE FROM win_tracker WHERE player_id = '%s'";
  23. char SQL_ResetAllStats[] = "DELETE FROM win_tracker";
  24. char SQL_AddWin[] = "UPDATE win_tracker SET player_wins = (player_wins + 1) WHERE player_id = '%s'";
  25. char SQL_GetAllWins[] = "SELECT * FROM win_tracker ORDER BY player_wins DESC LIMIT %i";
  26. char SQL_GetPlayerWins[] = "SELECT player_wins FROM win_tracker WHERE player_id = '%s'";
  27. char SQL_UpdatePlayer[] = "UPDATE win_tracker SET player_name = '%s' WHERE player_id = '%s'";
  28. char SQL_CheckPlayer[] = "SELECT player_id FROM win_tracker WHERE player_id = '%s'";
  29. char SQL_CheckName[] = "SELECT player_name FROM win_tracker WHERE player_id = '%s'";
  30.  
  31. Database db = null;
  32.  
  33. Handle hTimer = null;
  34.  
  35. int resetAdmin = -1;
  36.  
  37. public Plugin myinfo =
  38. {
  39.         name = PLUGIN_NAME,
  40.         author = PLUGIN_AUTHOR,
  41.         description = PLUGIN_DESCRIPTION,
  42.         version = PLUGIN_VERSION,
  43.         url = PLUGIN_CONTACT
  44. };
  45.  
  46. public void OnPluginStart()
  47. {
  48.         RegAdminCmd("sm_winstatsresetall", Command_ResetAllStats, ADMFLAG_RCON, "Resets all win statistics.");
  49.         //RegAdminCmd("sm_winstatsreset", Command_ResetStats, ADMFLAG_RCON, "Resets a player's win statistics.");
  50.         RegConsoleCmd("sm_wins", Command_Wins, "Shows a player's total arena 1 wins.");
  51.         //RegConsoleCmd("sm_wintop", Command_WinTop, "Shows the top 10 players' arena 1 wins.");
  52.        
  53.         AddCommandListener(Listener_Yes, "RESETYES");
  54.        
  55.         if(SQL_CheckConfig(winDb))
  56.         {
  57.                 SQL_TConnect(OnDatabaseConnect, winDb);
  58.         }
  59.         else
  60.         {
  61.                 SetFailState("Cannot find \"storage-local\" entry in \"sourcemod/configs/databases.cfg\"");
  62.         }
  63. }
  64.  
  65. public void OnDatabaseConnect(Handle owner, Handle handle, const char[] error, any data)
  66. {
  67.         if(handle == null || strlen(error) > 0)
  68.         {
  69.                 LogError("[TangoWins] Unable to connect to database. (%s)", error);
  70.                 return;
  71.         }
  72.        
  73.         db = view_as<Database>(CloneHandle(handle));
  74.        
  75.         char query[512];
  76.         FormatEx(query, sizeof(query), SQL_CreateWins);
  77.         db.Query(SQL_ErrorCheckCallback, query, _, DBPrio_High);
  78.         LogMessage("[TangoWins] Successfully connected to database.");
  79. }
  80.  
  81. public void Multi1v1_OnRoundWon(int winner, int loser, bool forceLoss)
  82. {
  83.         int totalClients = GetClientCount(true);
  84.         if(Multi1v1_GetArenaNumber(winner) == 1 && !forceLoss && totalClients >= 8)
  85.         {
  86.                 char id[32];
  87.                 GetClientAuthId(winner, AuthId_Steam2, id, sizeof(id));
  88.                 char query[256];
  89.                 Format(query, sizeof(query), SQL_AddWin, id);
  90.                 db.Query(SQL_AddWin_Add, query, GetClientUserId(winner), DBPrio_High);
  91.         }
  92. }
  93.  
  94. public Action Command_Wins(int client, int args)
  95. {
  96.         char id[32];
  97.         GetClientAuthId(client, AuthId_Steam2, id, sizeof(id));
  98.         char query[256];
  99.         Format(query, sizeof(query), SQL_GetPlayerWins, id);
  100.         db.Query(SQL_GetPlayerWins_Check, query, GetClientUserId(client), DBPrio_High);
  101.        
  102.         return Plugin_Handled;
  103. }
  104.  
  105. public Action Command_ResetAllStats(int client, int args)
  106. {
  107.         if(!resetPending)
  108.         {
  109.                 hTimer = CreateTimer(10.0, Timer_Reset);
  110.                 resetAdmin = GetClientUserId(client);
  111.                 resetPending = true;
  112.                 PrintToChat(client, "%s \x02Type \"\x07RESETYES\x02\" in console to reset all wins\x01. \x02THIS ACTION CANNOT BE UNDONE\x01.", PLUGIN_PREFIX);
  113.                 LogMessage("%N has attempted to reset ALL player wins.", client);
  114.         }
  115.         else
  116.         {
  117.                 PrintToChat(client, "%s There is already a reset pending.", PLUGIN_PREFIX);
  118.         }
  119.        
  120.         return Plugin_Handled;
  121. }
  122.  
  123. public Action Listener_Yes(int client, const char[] command, int argc)
  124. {
  125.         int rsAdmin = GetClientOfUserId(resetAdmin);
  126.        
  127.         if(client == rsAdmin && resetPending)
  128.         {
  129.                 resetPending = false;
  130.                 char query[256];
  131.                 Format(query, sizeof(query), SQL_ResetAllStats);
  132.                 //Instead of using SQL_Error..., add all players in server back to DB.
  133.                 db.Query(SQL_ErrorCheckCallback, query, _, DBPrio_High);
  134.                 PrintToChat(client, "%s \x02All wins have been reset\x01!", PLUGIN_PREFIX);
  135.                 LogMessage("%N has reset ALL player wins. Clearing table...", rsAdmin);
  136.         }
  137.        
  138.         return Plugin_Handled;
  139. }
  140.  
  141. public Action Timer_Reset(Handle timer)
  142. {
  143.         int client = GetClientOfUserId(resetAdmin);
  144.        
  145.         if(resetPending)
  146.         {
  147.                 PrintToChat(client, "%s \x02Reset has been cancelled\x01.", PLUGIN_PREFIX);
  148.                 LogMessage("%N's attempted reset has been cancelled.", client);
  149.                 resetPending = false;
  150.         }
  151.        
  152.         resetAdmin = -1;
  153.         delete hTimer;
  154.        
  155.         return Plugin_Handled;
  156. }
  157.  
  158. public void OnMapEnd()
  159. {
  160.         if(hTimer != null)
  161.         {
  162.                 KillTimer(hTimer);
  163.                 delete hTimer;
  164.         }
  165.         if(resetPending)
  166.         {
  167.                 resetPending = false;
  168.         }
  169.         if(resetAdmin != -1)
  170.         {
  171.                 resetAdmin = -1;
  172.         }
  173. }
  174.  
  175. /**
  176. *
  177. * SQL Queries & Database Functions
  178. *
  179. */
  180.  
  181. public void OnClientPostAdminCheck(int client)
  182. {
  183.         if(IsFakeClient(client))
  184.         {
  185.                 return;
  186.         }
  187.        
  188.         char id[32];
  189.         GetClientAuthId(client, AuthId_Steam2, id, sizeof(id));
  190.         char query[256];
  191.         Format(query, sizeof(query), SQL_CheckPlayer, id);
  192.         db.Query(SQL_CheckSteamID, query, GetClientUserId(client), DBPrio_High);
  193. }
  194.  
  195. public void SQL_CheckSteamID(Handle owner, DBResultSet results, const char[] error, any userid)
  196. {
  197.         int client = GetClientOfUserId(userid);
  198.        
  199.         if(client == 0 || !IsClientConnected(client))
  200.         {
  201.                 return;
  202.         }
  203.         if(results == null || strlen(error) > 0)
  204.         {
  205.                 LogError("[TangoWins] Query failes. (%s)", error);
  206.         }
  207.         else if(results.RowCount == 0)
  208.         {
  209.                 char fsteam[64];
  210.                 GetClientAuthId(client, AuthId_Steam2, fsteam, sizeof(fsteam));
  211.                 DataPack pack = new DataPack();
  212.                 pack.WriteCell(userid);
  213.                 pack.WriteString(fsteam);
  214.                 char query[256];
  215.                 Format(query, sizeof(query), SQL_CheckPlayer, fsteam);
  216.                 db.Query(SQL_AddPlayer_Check, query, pack, DBPrio_High);
  217.         }
  218.         else
  219.         {
  220.                 char fsteam[64];
  221.                 GetClientAuthId(client, AuthId_Steam2, fsteam, sizeof(fsteam));
  222.                 DataPack pack = new DataPack();
  223.                 pack.WriteCell(userid);
  224.                 pack.WriteString(fsteam);
  225.                 char query[256];
  226.                 Format(query, sizeof(query), SQL_CheckName, fsteam);
  227.                 db.Query(SQL_PlayerName_Check, query, pack, DBPrio_High);
  228.         }
  229. }
  230.  
  231. public void SQL_AddPlayer_Check(Handle owner, DBResultSet results, const char[] error, DataPack data)
  232. {
  233.         data.Reset();
  234.         int client = GetClientOfUserId(data.ReadCell());
  235.         char fsteam[64];
  236.         data.ReadString(fsteam, 64);
  237.        
  238.         if(IsValidClient(client))
  239.         {
  240.                 if(results == null || strlen(error) > 0)
  241.                 {
  242.                         LogError("[TangoWins] Query failed. (%s)", error);
  243.                 }
  244.                 else if(results.RowCount == 0)
  245.                 {
  246.                         char name[64];
  247.                         GetClientName(client, name, sizeof(name));
  248.                         DataPack iPack = new DataPack();
  249.                         iPack.WriteCell(GetClientUserId(client));
  250.                         iPack.WriteString(fsteam);
  251.                         char query[256];
  252.                         Format(query, sizeof(query), SQL_AddPlayer, name, fsteam, 0);
  253.                         db.Query(SQL_AddPlayer_Add, query, iPack, DBPrio_High);
  254.                 }
  255.         }
  256.        
  257.         delete view_as<DataPack>(data);
  258. }
  259.  
  260. public void SQL_AddPlayer_Add(Handle owner, DBResultSet results, const char[] error, DataPack data)
  261. {
  262.         data.Reset();
  263.         int client = GetClientOfUserId(data.ReadCell());
  264.         char steamid[64];
  265.         data.ReadString(steamid, 64);
  266.        
  267.         if(IsValidClient(client))
  268.         {
  269.                 if(results == null || strlen(error) > 0)
  270.                 {
  271.                         LogError("[TangoWins] Query failed. (%s)", error);
  272.                 }
  273.         }
  274.        
  275.         delete view_as<DataPack>(data);
  276. }
  277.  
  278. public void SQL_PlayerName_Check(Handle owner, DBResultSet results, const char[] error, DataPack data)
  279. {
  280.         data.Reset();
  281.         int client = GetClientOfUserId(data.ReadCell());
  282.         char fsteam[64];
  283.         data.ReadString(fsteam, 64);
  284.        
  285.         if(IsValidClient(client))
  286.         {
  287.                 if(results == null || strlen(error) > 0)
  288.                 {
  289.                         LogError("[TangoWins] Query failed. (%s)", error);
  290.                 }
  291.                 else if(results.RowCount)
  292.                 {
  293.                         char name[64];
  294.                         GetClientName(client, name, sizeof(name));
  295.                         DataPack iPack = new DataPack();
  296.                         iPack.WriteCell(GetClientUserId(client));
  297.                         iPack.WriteString(fsteam);
  298.                         char query[256];
  299.                         Format(query, sizeof(query), SQL_UpdatePlayer, name, fsteam);
  300.                         db.Query(SQL_PlayerName_Update, query, iPack, DBPrio_High);
  301.                 }
  302.         }
  303.        
  304.         delete view_as<DataPack>(data);
  305. }
  306.  
  307. public void SQL_PlayerName_Update(Handle owner, DBResultSet results, const char[] error, DataPack data)
  308. {
  309.         data.Reset();
  310.         int client = GetClientOfUserId(data.ReadCell());
  311.         char steamid[64];
  312.         data.ReadString(steamid, 64);
  313.        
  314.         if(IsValidClient(client))
  315.         {
  316.                 if(results == null || strlen(error) > 0)
  317.                 {
  318.                         LogError("[TangoWins] Query failed. (%s)", error);
  319.                 }
  320.         }
  321.        
  322.         delete view_as<DataPack>(data);
  323. }
  324.  
  325. public void SQL_AddWin_Add(Handle owner, DBResultSet results, const char[] error, any userid)
  326. {
  327.         int client = GetClientOfUserId(userid);
  328.        
  329.         if(IsValidClient(client))
  330.         {
  331.                 if(results == null || strlen(error) > 0)
  332.                 {
  333.                         LogError("[TangoWins] Query failed. (%s)", error);
  334.                 }
  335.         }
  336. }
  337.  
  338. public void SQL_GetPlayerWins_Check(Handle owner, DBResultSet results, const char[] error, any userid)
  339. {
  340.         int client = GetClientOfUserId(userid);
  341.        
  342.         if(results == null || strlen(error) > 0)
  343.         {
  344.                 LogError("[TangoWins] Query failed. (%s)", error);
  345.         }
  346.         else
  347.         {
  348.                 SQL_FetchRow(results);
  349.                 int wins = SQL_FetchInt(results, 0);
  350.                 PrintToChat(client, "%s You have \x04%i\x01 Arena 1 wins this month!", PLUGIN_PREFIX, wins / 2);
  351.         }
  352. }
  353.  
  354. public void SQL_ErrorCheckCallback(Handle owner, DBResultSet results, const char[] error, any data)
  355. {
  356.         if(results == null || strlen(error) > 0)
  357.         {
  358.                 LogError("[TangoWins] Query failed. (%s)", error);
  359.         }
  360. }
  361.  
  362. stock bool IsValidClient(int client)
  363. {
  364.         return (1 <= client <= MaxClients && IsClientInGame(client));
  365. }