
   /* AMX Mod script.
   *
   * HL Server Status Query
   *
   * (c) Copyright 2002, Phreak
   * This file is provided as is (no warranties).
   *
   * This plugin is made from OLOs UDP demo-plugin
   *  You  need  the  UDP  module and the mthread module running for this
   plugin.
   *
   * to query the servers use "serverstatus" in normal chat
   * to switch to one of the servers use "switchserver" in chat
   *
   * to add servers to query add this to your admin.cfg:
   *   amx_server <ip-address> <port> <pass> <servername>
   * example:
   *   amx_server "1.2.3.4" "27015" "pass" "servername"
   *
   * If the server has no password, leave it blank, but make
   * sure to set servername, it is shown in the switchserver menu
   *   amx_server "1.2.3.5" "27015" "" "servername"
   *
   * you can add up to 5 servers. if you need more you have to
   * edit the source code (#define MAX_SERVERS 5)
   *
   * Changelog:
   * v0.3
   *  - added multithreading (fixes crashs when a queried server is down)
   * v0.2
   *  - added menu to switch servers without leaving half-life
   * v0.1
   *  - initial version
   */
   #include <amxmod>
   #include <udp>
   #include <mthread>
   // be careful changing this value, the menu used for
   // switching servers can handle only 8 server at tis time.
   #define MAX_SERVER    5
   new address_one[MAX_SERVER]
   new server_num = 0
   new servers[MAX_SERVER][32]
   new ports[MAX_SERVER]
   new passwords[MAX_SERVER][32]
   new sv_names[MAX_SERVER][64]
   public add_server(){
       if (server_num >= MAX_SERVER){
           server_print("[AMX] server query limit reached!")
           return PLUGIN_HANDLED
       }
       new data[32]
       read_argv(1,servers[server_num],31)
       read_argv(2,data,31)
       ports[server_num] = strtonum(data);
       read_argv(3,passwords[server_num],31)
       read_argv(4,sv_names[server_num],63)
       server_print("[AMX]      Added     Query     Server     #%d     %s
   (%s:%d)",(server_num+1),sv_names[server_num],servers[server_num],ports
   [server_num])
       server_num++
       return PLUGIN_HANDLED
   }
   public show_server(s_id[1]) {
       new i,name[64],status[64],id = s_id[0]
       for (i=0;i<server_num;i++) {
           get_name(i,name)
           get_status(i,status)
           client_print(id, print_chat,"%s : %s",name,status)
       }
       return
   }
   public check_info(id){
       new s_id[1]
       s_id[0] = id
       thread_run("show_server",s_id,1)
       return PLUGIN_CONTINUE
   }
   get_status(i,status[]){
       new players[32]
       if  (address_one[i] == 0) // if haven't initialized it yet then do
   it now
           address_one[i] = set_iaddress(servers[i],ports[i])
       new response[128]
       new ok = query_udp_msg(address_one[i],response,127,"info")
       if (ok)    {
           new next = strlen(response[1]) + 2
           next += strlen(response[next]) + 1
           format (status,31,"Current Map: %s :: ",response[next])
           next += strlen(response[next]) + 1
           next += strlen(response[next]) + 1
           next += strlen(response[next]) + 1
           format(players,31,"Players:
   %d/%d",response[next],response[next+1])
           add(status,63,players)
       }
       else
           format (status,63,"No response!")
       return PLUGIN_CONTINUE
   }
   get_name(i,status[]){
       if  (address_one[i] == 0) // if haven't initialized it yet then do
   it now
           address_one[i] = set_iaddress(servers[i],ports[i])
       new response[128]
       new ok = query_udp_msg(address_one[i],response,127,"info")
       if (ok)    {
           new next = strlen(response[1]) + 2
           format (status,63,"%s",response[next])
       }
       else
           format (status,63,"No response!")
       return PLUGIN_CONTINUE
   }
   public show_switchmenu(id) {
       new curr_server[32], menu[256], i, keys = (1<<9)
       format(menu,255,"Switch Server^n^n")
       for (i=0;i<server_num;i++) {
           format(curr_server,31,"%d.  %s^n",(i+1),sv_names[i])
           add(menu,255,curr_server)
           keys |= (1<<i)
       }
       add(menu,255,"^n0.  Exit")
       show_menu(id,keys,menu,-1)
       return PLUGIN_CONTINUE
   }
   public sw_server(id,key) {
       if (key==9)
           return PLUGIN_HANDLED
       else {
           if     (!equal(passwords[key],""))     client_cmd(id,"password
   %s",passwords[key])
           client_cmd(id,"connect %s:%d",servers[key],ports[key])
       }
       return PLUGIN_HANDLED
   }
   public plugin_init(){
       register_plugin("HL Server Status Query","0.3","Phreak")
       register_menucmd(register_menuid("Switch
   Server"),1023,"sw_server")
       register_clcmd("say serverstatus","check_info")
       register_clcmd("say switchserver","show_switchmenu")
       register_srvcmd("amx_server","add_server")
       if (!get_udp_status())
           server_print("Failed to initialize UDP!")
       server_num = 0
       return PLUGIN_CONTINUE
   }
