
   /* AMX Mod Script
   *
   * AMX MyStatus by SDE  <sde@ocsclan.com>
   * (c) Copyright 2002
   *
   * View README for installation and configuration.
   *
   changelog for v.3
   - PHP 4.0.6 install script compatibility fixed.
   - PHP 4.3.1 compatibility with the template class
   changelog for v.2
   - added _amx prefix to db tables
   - implimented cvar for server id 'amx_server_id' default is 1
   -  implimented cvar for query frequency 'amx_mystatus_freq' default is
   15
   */
   #include <amxmod>
   #include <mysql>
   new mhost[64]
   new muser[32]
   new mpass[32]
   new mdb[32]
   new merror[32]
   new mysql = 0
   public update_server_info(){
     new server_id = get_cvar_num("amx_server_id")

     // cvars
     new hostname[64]
     get_cvar_string("hostname",hostname,63)
     new hostport = get_cvar_num("hostport")
     new map[16]
     get_mapname(map,16)
     new maxplayers
     maxplayers = get_maxplayers()
     new modname[10]
     get_modname(modname,10)
     new mp_autokick = get_cvar_num("mp_autokick")
     new mp_autoteambalance = get_cvar_num("mp_autoteambalance")
     new mp_c4timer = get_cvar_num("mp_c4timer")
     new mp_flashlight = get_cvar_num("mp_flashlight")
     new mp_footsteps = get_cvar_num("mp_footsteps")
     new mp_forcechasecam = get_cvar_num("mp_forcechasecam")
     new mp_freezetime = get_cvar_num("mp_freezetime")
     new mp_friendlyfire = get_cvar_num("mp_friendlyfire")
     new mp_hostagepenalty = get_cvar_num("mp_hostagepenalty")
     new mp_limitteams = get_cvar_num("mp_limitteams")
     new mp_logmessages = get_cvar_num("mp_logmessages")
     new Float:mp_mapvoteratio = get_cvar_float("mp_mapvoteratio")
     new mp_roundtime = get_cvar_num("mp_roundtime")
     new mp_startmoney = get_cvar_num("mp_startmoney")
     new mp_timelimit = get_cvar_num("mp_timelimit")
     new mp_tkpunish = get_cvar_num("mp_tkpunish")
     new net_address[32]
     get_cvar_string("net_address",net_address,31)
     new nextmap[16]
     get_cvar_string("amx_nextmap",nextmap,16)
     new query[1535]
     format(query,1535,"UPDATE        amx_mystatus_server_info        set
   mp_autokick='%d',mp_autoteambalance='%d',mp_c4timer='%d',mp_flashlight
   ='%d',mp_footsteps='%d',mp_forcechasecam='%d',mp_freezetime='%d',mp_fr
   iendlyfire='%d',mp_hostagepenalty='%d',mp_limitteams='%d'        where
   server_id='%d'",mp_autokick,mp_autoteambalance,mp_c4timer,mp_flashligh
   t,mp_footsteps,mp_forcechasecam,mp_freezetime,mp_friendlyfire,mp_hosta
   gepenalty,mp_limitteams,server_id)
     mysql_query(mysql,query)

     new query_b[1535]
     format(query_b,1535,"UPDATE       amx_mystatus_server_info       set
   hostname='%s',hostport='%d',map='%s',maxplayers='%d',modname='%s',mp_l
   ogmessages='%d',mp_mapvoteratio='%.2f',mp_roundtime='%d',mp_startmoney
   ='%d',mp_timelimit='%d',mp_tkpunish='%d',net_address='%s',nextmap='%s'
   where
   server_id='%d'",hostname,hostport,map,maxplayers,modname,mp_logmessage
   s,mp_mapvoteratio,mp_roundtime,mp_startmoney,mp_timelimit,mp_tkpunish,
   net_address,nextmap,server_id)
     mysql_query(mysql,query_b)
     return PLUGIN_CONTINUE
   }
   public update_player_info(){
     new server_id = get_cvar_num("amx_server_id")
     new players[32]
     new team[16]
     new name[32]
     new wonid
     new status
     new frags
     new deaths
     new ping
     new loss
     new i
     new inum

     new query_a[256]
     format(query_a,256,"DELETE FROM amx_mystatus_player_info")
     mysql_query(mysql,query_a)

     // set player info
     get_players(players,inum)
     for(i = 0 ;i < inum; ++i){
       get_user_name(players[i],name,32)
       status = is_user_alive(players[i])
       wonid = get_user_wonid(players[i])
       frags = get_user_frags(players[i])
       deaths = get_user_deaths(players[i])
       get_user_ping(players[i], ping, loss)
       get_user_team(players[i], team, 16 )

       new query_b[1024]
       format(query_b,1024,"INSERT   INTO   amx_mystatus_player_info  set
   server_id='%d',wonid='%d',team='%s',name='%s',status='%d',frags='%d',d
   eaths='%d',ping='%d',loss='%d'",server_id,wonid,team,name,status,frags
   ,deaths,ping,loss)
       mysql_query(mysql,query_b)
     }

     return PLUGIN_CONTINUE
   }
   // reset scores
   //
   public reset_scores(){
     new server_id = get_cvar_num("amx_server_id")
     new query[256]
     format(query,256,"Update         amx_mystatus_server_info        set
   ct_score='0',terrorist_score='0' where server_id='%d'",server_id)
     mysql_query(mysql,query)

     return PLUGIN_CONTINUE
   }
   public init_functions(){
     server_cmd("exec addons/amx/mysql.cfg")
     get_cvar_string("amx_mysql_host",mhost,64)
     get_cvar_string("amx_mysql_user",muser,32)
     get_cvar_string("amx_mysql_pass",mpass,32)
     get_cvar_string("amx_mysql_db",mdb,32)
     mysql=mysql_connect(mhost,muser,mpass,mdb,merror,32)

     if(mysql < 1){
       server_print("[AMX]   MyStatus   error  :  could  not  connect  ::
   init_function failed")
     }
     else{
       reset_scores()
       update_server_info()
     }
   }
   // Short Functions
   public short_functions(){
     new server_id = get_cvar_num("amx_server_id")
     get_cvar_string("amx_mysql_host",mhost,64)
     get_cvar_string("amx_mysql_user",muser,32)
     get_cvar_string("amx_mysql_pass",mpass,32)
     get_cvar_string("amx_mysql_db",mdb,32)
     new timeleft = get_timeleft()

     mysql=mysql_connect(mhost,muser,mpass,mdb,merror,32)

     if(mysql < 1){
       server_print("[AMX]   MyStatus  error  ::  could  not  connect  ::
   short_function() failed")
     }
     else{
       new query[256]
       format(query,256,"Update        amx_mystatus_server_info       set
   timeleft='%d' where server_id='%d'",timeleft,server_id)
       mysql_query(mysql,query)

       update_player_info()

       mysql_close(mysql)
       mysql=0
     }

     return PLUGIN_CONTINUE
   }
   // Get Team Score
   //
   public team_score(){
     new server_id = get_cvar_num("amx_server_id")

     get_cvar_string("amx_mysql_host",mhost,64)
     get_cvar_string("amx_mysql_user",muser,32)
     get_cvar_string("amx_mysql_pass",mpass,32)
     get_cvar_string("amx_mysql_db",mdb,32)

     new ct_score = 0
     new terrorist_score = 0
     new team[32]
     read_data(1,team,32)
     mysql=mysql_connect(mhost,muser,mpass,mdb,merror,32)

     if(mysql < 1){
       server_print("[AMX]   MyStatus  error  ::  could  not  connect  ::
   team_score() failed")
     }
     else{
       if (equal(team,"CT")){
         ct_score = read_data(2)
         new query[256]
         format(query,256,"Update       amx_mystatus_server_info      set
   ct_score='%d' where server_id='%d'",ct_score,server_id)
         mysql_query(mysql,query)
       }
       else if (equal(team,"TERRORIST")){
         terrorist_score = read_data(2)
         new query[256]
         format(query,256,"Update       amx_mystatus_server_info      set
   terrorist_score='%d' where server_id='%d'",terrorist_score,server_id)
         mysql_query(mysql,query)
       }
       mysql_close(mysql)
       mysql=0
     }
     return PLUGIN_CONTINUE
   }
   public plugin_init(){
     register_plugin("AMX MyStatus","0.3","SDE")
     register_cvar("amx_server_id","0")
     register_cvar("amx_mystatus_freq","15.0")
     register_cvar("amx_mysql_host","127.0.0.1")
     register_cvar("amx_mysql_user","root")
     register_cvar("amx_mysql_pass","")
     register_cvar("amx_mysql_db","amx")
     init_functions()
     new Float:freq = get_cvar_float("amx_mystatus_freq")
     set_task(freq,"short_functions",0,"",0,"b")
     register_event("TeamScore", "team_score", "a")
     return PLUGIN_CONTINUE
   }
