
   /*-----------------------------------------------------------------*
   |
   | DuckStat Rank plugin for AMX Mod
   |
   | written by MrSpock (hint@home.se)
   | inspired by Johnny's HLStats Rank plugin
   |
   *-----------------------------------------------------------------*
   |
   | Commands:
   |   /say rank            -- display player's rank
   |   /say stats           -- display duckstat main page
   |
   | Requirements:
   |    MySQL module
   |    DuckStat 2.2
   |
   | Installation:
   |    1. Define preferred language (english or swedish) below.
   |    2. Set url for your DuckStat main page. It will be
   |       shown ingame when player says "stats".
   |    3. Recompile!
   |    4. Put cvars for this plugin in your server.cfg.
   |
   | Cvars:
   |   amx_duckstat_host    -- DuckStat database hostname
   |   amx_duckstat_user    -- DuckStat database username
   |   amx_duckstat_pass    -- DuckStat database password
   |   amx_duckstat_db      -- DuckStat database name
   |   amx_duckstat_private -- Do not send rank to everybody (1/0)
   |
   | Revision history:
   |   v0.1.0               -- Initial version
   |   v0.2.0               -- Fixed a bug regarding dead players
   |                           seeing rank of alive players and
   |                           vice versa.
   |   v0.2.1               -- Fixed small bug regarding
   |                           calculation of "rank".
   |   v0.2.2               -- Removed Multi-threading support
   |                           due to some server crashes
   |                           related to it.
   |   v0.3.0               -- Users with privacy mode enabled are
   |                           not able to get their rank anymore.
   |                           Swedish languge support added.
   |   v0.3.1               -- Added support for SteamID and
   |                           replaced "top15" with "stats".
   |
   *-----------------------------------------------------------------*
   | DuckStat: http://www.duckstat.com/
   | DuckNet:  http://ducknet.se/
   *-----------------------------------------------------------------*/
   #include <amxmod>
   #include <mysql>
   // Set this constant to 1 if you want client messages in swedish
   // instead of english. Don't forget to re-compile.
   #define SWEDISH 0
   #define DEBUG 0
   new mhost[64]
   new muser[32]
   new mpass[32]
   new mdb[32]
   new mysql = 0
   new url[128]
   public url_init()
   {
       // SET URL FOR DUCKSTAT MAIN PAGE
       format(url, 127, "http://ducknet.se/duckstat/index.php")
       // SET URL FOR DUCKSTAT MAIN PAGE
   }
   public sql_init()
   {
       new merror[32]
       // Get database connectivity cvars.
       get_cvar_string("amx_duckstat_host",mhost,63)
       get_cvar_string("amx_duckstat_user",muser,31)
       get_cvar_string("amx_duckstat_pass",mpass,31)
       get_cvar_string("amx_duckstat_db",mdb,31)
       // Try to connect.
       mysql = mysql_connect(mhost,muser,mpass,mdb,merror,31)
       // Log result.
       if(mysql < 1)
       {
           log_message("[AMX]   [DuckStat  Rank]  Could  not  connect  to
   database: '%s'",merror)
       }
       else
       {
           #if DEBUG == 1
               log_message("[AMX]     [DuckStat     Rank]    Successfully
   connected!")
           #endif
       }
       return PLUGIN_CONTINUE
   }
   public say_rank(id)
   {
       // Player data
       new idx[2]
       new recpx[2]
       new authid[32]
       new name[64]
       new rating[16]
       new fRating[16]
       new privacy[8]
       new rank[16]
       new maxplayers[16]
       // SQL Data
       new query[256]
       new merror[32]
       new result[256]
       // Flags
       new playerexist = 1
       // Get user's authid.
       get_user_authid(id,authid,31)
       // Get user's name.
       get_user_name(id,name,63)
       idx[0] = id
       //
       // GET RATING
       //
       // Build query string.
       format(query,255,"SELECT ROUND(rating), rating, privacy \
           FROM player \
           INNER JOIN playerpersonal \
           ON player.PlayerID = playerpersonal.PlayerID \
           WHERE WonID = '%s' AND Name = '%s'",authid,name)
       // Execute query.
       mysql_query(mysql,query)
       mysql_error(mysql,merror,31)
       if (merror[0])
       {
           #if DEBUG == 1
               log_message("[AMX]    [DuckStat    Rank]    MySQL   Error:
   %s",merror)
           #endif
           return PLUGIN_HANDLED
       }
       if(mysql_nextrow(mysql)>0)
       {
           mysql_getfield(mysql,1,rating,15)
           mysql_getfield(mysql,2,fRating,15)
           mysql_getfield(mysql,3,privacy,7)
       }
       else
       {
           playerexist = 0
       }
       if (playerexist)
       {
           if (equal(privacy, "1"))
           {
               // Message will only be sent to requesting player.
               recpx[0] = id
               #if SWEDISH == 1
                   format(result,255,"^"%s^"  ^"%s^" ^"(%s) DuckStat: Din
   rang  kan  inte  visas,  eftersom  du  har  sekretesslaget  aktiverat.
   Redigera din profil.^"", idx, recpx, name)
               #else
                   format(result,255,"^"%s^" ^"%s^" ^"(%s) DuckStat: Your
   rank cannot be displayed as you are in privacy mode. Please, edit your
   profile.^"", idx, recpx, name)
               #endif
           }
           else
           {
               //
               // GET RANK
               //

               // Build query string.
               format(query,255,"SELECT COUNT(*)+1 \
                   FROM player INNER JOIN playerpersonal \
                   ON player.PlayerID = playerpersonal.PlayerID \
                   WHERE player.Privacy = 0 AND rating > %s", fRating)
               // Execute query.
               mysql_query(mysql,query)
               mysql_error(mysql,merror,31)
               if (merror[0])
               {
                   #if DEBUG == 1
                       log_message("[AMX]  [DuckStat  Rank]  MySQL Error:
   %s",merror)
                   #endif

                   return PLUGIN_HANDLED
               }

               if(mysql_nextrow(mysql)>0)
               {
                   mysql_getfield(mysql,1,rank,15)

               }
               //
               // GET TOTAL PLAYERS
               //
               // Build query string.
               format(query,255,"SELECT COUNT(*) \
                   FROM player INNER JOIN playerpersonal \
                   ON player.PlayerID = playerpersonal.PlayerID \
                   WHERE player.Privacy = 0")
               // Execute query.
               mysql_query(mysql,query)
               mysql_error(mysql,merror,31)
               if (merror[0])
               {
                   #if DEBUG == 1
                       log_message("[AMX]  [DuckStat  Rank]  MySQL Error:
   %s",merror)
                   #endif
                   return PLUGIN_HANDLED
               }
               if(mysql_nextrow(mysql)>0)
               {
                   mysql_getfield(mysql,1,maxplayers,15)

               }
               if (get_cvar_num("amx_duckstat_private") != 0)
               {
                   // Message will only be sent to requesting player.
                   recpx[0] = id
                   #if SWEDISH == 1
                       format(result,255,"^"%s^"  ^"%s^" ^"(%s) DuckStat:
   Du ar rankad som nr %s av %s med %s poang.^"", idx, recpx, name, rank,
   maxplayers, rating)
                   #else
                       format(result,255,"^"%s^"  ^"%s^" ^"(%s) DuckStat:
   You  are  ranked  %s of %s with %s points.^"", idx, recpx, name, rank,
   maxplayers, rating)
                   #endif
               }
               else
               {
                   // Message will be sent to all players.
                   recpx[0] = 0

                   #if SWEDISH == 1
                       format(result,255,"^"%s^"  ^"%s^" ^"* DuckStat: %s
   ar  rankad  som  nr %s av %s med %s poang.^"", idx, recpx, name, rank,
   maxplayers, rating)
                   #else
                       format(result,255,"^"%s^"  ^"%s^" ^"* DuckStat: %s
   is  ranked  %s  of  %s  with  %s  points.^"",  idx, recpx, name, rank,
   maxplayers, rating)
                   #endif
               }
           }
       }
       else
       {
           // Message will only be sent to requesting player.
           recpx[0] = id
           #if SWEDISH == 1
               format(result,255,"^"%s^"  ^"%s^"  ^"(%s) DuckStat: %s med
   SteamID %s finns inte i databasen.^"", idx, recpx, name, name, authid)
           #else
               format(result,255,"^"%s^"  ^"%s^" ^"(%s) DuckStat: %s with
   SteamID  %s  does  not  exist in database.^"", idx, recpx, name, name,
   authid)
           #endif
       }
       set_task(0.1, "ClientPrint", 0, result, 255)

       return PLUGIN_CONTINUE
   }
   public say_stats(id)
   {
       // View page
       new motd[256]
          format(motd,     255,    "<head><meta    HTTP-EQUIV=^"Refresh^"
   CONTENT=^"0;    URL=%s^"></head><body    bgcolor=^"#000000^"></body>",
   url)
          show_motd(id, motd, "DuckStat")
       return PLUGIN_CONTINUE
   }
   public ClientPrint(text[])
   //  text[]  contains  three  parameters  enclosed  in quotation marks:
   "sender", "recepient" and "message"
   {
       new idx[2]
       new recpx[2]

       // Split text[] into "sender", "recepient" and "message".
       parse(text, idx, 2, recpx, 2, text, 255)
       new id = idx[0]
       new recp = recpx[0]
       new is_alive = is_user_alive(id);
       new player_count = get_playersnum();
       new players[32];
       get_players(players, player_count, "c");
       if (recp == 0) {
           // If alive, send message to all alive players.
           // If dead, send message to all dead players.
           for (new i = 0; i < player_count; i++) {
               if (is_alive == is_user_alive(players[i]))
                   client_print(players[i],       print_chat,       text)

           }
       }
       else
       {
           client_print(recp, print_chat, text)
       }
       return PLUGIN_CONTINUE
   }
   public plugin_end()
   {
       if (mysql > 0)
       {
           // Close database connection.
           mysql_close(mysql)
           #if DEBUG == 1
               log_message("[AMX] [DuckStat Rank] Connection closed!")
           #endif
       }
       return PLUGIN_CONTINUE
   }
   public plugin_init()
   {
       // Register plugin.
       register_plugin("DuckStat Rank","0.3.1","MrSpock")
       // Register say commands.
       register_clcmd("say  rank","say_rank",0," - displays your DuckStat
   rank")
       register_clcmd("say  stats","say_stats",0,"  -  displays  DuckStat
   main page")
       // Register cvars.
       register_cvar("amx_duckstat_host","127.0.0.1")
       register_cvar("amx_duckstat_user","root")
       register_cvar("amx_duckstat_pass","")
       register_cvar("amx_duckstat_db","duckstat")
       register_cvar("amx_duckstat_private","0")
       // Set url for DuckStat
       url_init()
       // Delay MySQL access initialization.
       set_task(3.0,"sql_init")
       return PLUGIN_CONTINUE
   }
