

/* 
PlayTime MySQL, v0.5 
By
	:| bY |: Neosheep 
	_KaszpiR_
www.yaboclan.com/by 

CREATE TABLE amx_playtime ( 
  wonid varchar(33) NOT NULL default '', 
  name varchar(33) NOT NULL default '', 
  game_time int(20) NOT NULL default '0', 
  month int(2) NOT NULL default '0', 
  last_update timestamp(14) NOT NULL 
) TYPE=MyISAM; 

logs a player time to a mysql 
can later display a www using php (optional)
can search a playtime of certain player(s) during a game with console command and say command

Notes : 
. check $moddir/addons/amx/mysql.cfg for MySQL access configuration 


added by _KaszpiR_
 supported since MySQL 3.23 but there it is case sesnitive, in 4.0 it is case intensive

- playtime console command with search like
 playtime nick
- /playtime say command like from console
- changed amx_logtime to amx_playtime
- included a php addon to php-amx 
- fixed the say bug
- rewrite to suit more like oter Noplay's mysql plugins
- renamed do mysql_playtime
- search result in motd window
- added hlds standard logging

// update by wonid AND name (exact match)
*/ 

#include <amxmod> 
#include <amxmisc> 
#include <mysql> 



   new mysql 
   new merror[257] 
   new mhost[65] 
   new muser[33] 
   new mpass[33] 
   new mdb[33] 

   new month_[10] 
   new g_logFile[16] 

//save time into mysql 
public client_disconnect(id){ 
   if(mysql>0) 
   { 
   new wonid[32] 
   new clientname[64] 
   new mysqltime[32] 
   //get commands 
   get_user_name(id,clientname,32) 
   get_user_authid(id,wonid,32) 
   new newplaytime = get_user_time(id,1) 
   get_time("%m",month_,10) 
   new qd[513] 
   format(qd,513,"SELECT * FROM amx_playtime WHERE wonid='%s' AND name='%s' AND month='%s'",wonid,clientname,month_) 
   mysql_query(mysql,qd) 
   num_to_str(newplaytime,mysqltime,32) 
    
   if (mysql_nextrow(mysql) > 0){ 
        format(qd,513,"UPDATE amx_playtime SET game_time=game_time+%s WHERE wonid='%s' AND name='%s' AND month='%s'",mysqltime,wonid,clientname,month_) 
        mysql_query(mysql,qd) 
        server_print("[AMX] PlayTime - User Updated: ^"%s<%s>^" time+=%s month=%s",clientname,wonid,mysqltime,month_) 
        log_to_file(g_logFile,"[AMX] PlayTime - User Updated: ^"%s<%s>^" time+=%s month=%s",clientname,wonid,mysqltime,month_) 
      }
      else
      { 
        format(qd,513,"INSERT INTO amx_playtime (name,wonid,game_time,month) VALUES ('%s','%s','%s','%s')",clientname,wonid,mysqltime,month_) 
        mysql_query(mysql,qd) 
        server_print("[AMX] PlayTime - User Added: ^"%s<%s>^" time=%s month=%s",clientname,wonid,mysqltime,month_) 
        log_to_file(g_logFile,"[AMX] PlayTime - User Added: ^"%s<%s>^" time=%s month=%s",clientname,wonid,mysqltime,month_) 
      } 

   }
   else
   {
   mysql_error(mysql,merror,255) 
   server_print("[AMX] PlayTime - MySQL Error (upd): %s",merror) 
   log_to_file(g_logFile,"[AMX] PlayTime - MySQL Error (upd): %s",merror) 
   }
   return PLUGIN_CONTINUE //disconnect event can be parsed by also othr plugins
} 

//------------------------------------------------------------------------------------------- 
public playtime_say(id){
 /*
   
   new player[33], won_id[33]
   read_argv(2,player,32)
   if(!strlen(player))
   {
   get_user_name(id,player,32)
   get_user_authid(id,won_id,32)
   client_print(id,print_console,"[AMX] PlayTime - %s <%s> - that is You!",player,won_id) 
   }
   else
   {
   remove_quotes(player)
   }
   playtime_report(id,player)

   return PLUGIN_CONTINUE
*/

   new speech[192] 
   new query[33] 
   read_args(speech,192) 
   remove_quotes(speech)
//   client_print(id,print_console,"[AMX] PlayTime - DEbug s '%s'", speech) 

   new i = 0
   if(equal(speech,"/playtime", 9))
	while(speech[10+i]){//copying the resto of the say text as a query text (witout /playtime)
           query[i] = speech[10+i]
           i++
	 }
//      client_print(id,print_console,"[AMX] PlayTime - Debug q '%s'", query) 

   if(!strlen(query))
	get_user_name(id,query,32)
//      client_print(id,print_console,"[AMX] PlayTime - Debug q2 '%s'", query) 

   playtime_report(id,query)
   //playtime_report(id,speech[10])


   return PLUGIN_CONTINUE //the say text can be parsed by other lugins so use plugin_continue

}
public playtime_cmd(id){
   new player[33], won_id[33]
   read_argv(1,player,32)
   if(!strlen(player))
   {
     get_user_name(id,player,32)
     get_user_authid(id,won_id,32)
     client_print(id,print_console,"[AMX] PlayTime - %s <%s> - that is You!",player,won_id) 
   }
   else
     remove_quotes(player)
   playtime_report(id,player)

   return PLUGIN_HANDLED
}
//------------------------------------------------------------------------------------------- 
playtime_report(id, string[33]){
   if(mysql>0) 
   { 
   new pos = 0
   new window[2048]
   new player[33]
   new won_id[33]
  
   new pt
   new playtime[33] 
   new playseconds 
   new playminutes 
   new playhours
   new playdays
   new results = 0
   //get commands 
   get_time("%m",month_,10) 

   player = string
//   client_print(id,print_console,"[AMX] PlayTime - Querying for ^"%s^"",player) 
   new qd[513] 
   //the sql should return a list of matching nicks like we call elvis then he should return all nicks containing string 'elvis'
   //unfortunately amx 0.9.7 does not support %% as a one %, so all LIKE '%text%' sql fuctions are useless
   //fortunately we can use regexp in sql but some specialchars may be lost
#if !defined NO_STEAM
   pos = format(window[pos],2047,"<html><head><style type=^"text/css^">pre{color:#FFB000;}body{background:#000000;margin-left:8px;margin-top:0px;}</style></head><pre><body>")
#endif
   
   pos += format(window[pos],2047-pos, "^nSearching for ^"%s^"^n",player)

   format(qd,512,"SELECT wonid,name,game_time FROM amx_playtime WHERE name REGEXP '%s' AND month='%s' ORDER BY game_time LIMIT 15",player,month_) 
   if(mysql_query(mysql,qd)>0){
    
       while (mysql_nextrow(mysql)>0){ 
           mysql_getfield(mysql,1,won_id,32) 
           mysql_getfield(mysql,2,player,32) 
           mysql_getfield(mysql,3,playtime,32) 
           
	   pt = strtonum(playtime)
           //calculate the nice output
           if (pt>=24*60*60)
           {
                   playdays = pt/(24*3600)
                   pt -=playdays*24*3600
           }
           if (pt>=60*60)
           {
                   playhours = pt/(3600)
                   pt -= playhours*3600
           }
           if (pt>=60)
           {
                   playminutes = pt/60
                   pt -=playminutes*60
           }
           playseconds = pt
	   
	   
    
//           client_print(id,print_console,"[AMX] PlayTime - %s played %dd %dh %dm %ds in this month. (authid <%s>)",player,playdays, playhours, playminutes,won_id) 
           //window version
	   pos += format(window[pos],2047-pos, "%-28.27s %dd %dh %dm %ds (%-28.27s)^n",
	                                    player, playdays, playhours,playminutes,playseconds,won_id)

	   results++
       }
       if(results==0)
         client_print(id,print_chat,"[AMX] PlayTime - No results - try different name.") 
       else{
          if(results>14)
//         client_print(id,print_console,"[AMX] PlayTime - Top 15 of %d Displayed.",results) 
           pos += format(window[pos],2047-pos, "^nTop 15 of %d Results Displayed.",results)
          else
//         client_print(id,print_console,"[AMX] PlayTime - %d Results.",results) 
           pos += format(window[pos],2047-pos, "^n%d Results.",results)

#if !defined NO_STEAM
          format(window[pos],2047-pos,"</pre></body></html>")
#endif
          show_motd(id,window,"Playtime")
            }
   }
   else
   {
       mysql_error(mysql,merror,64) 
       client_print(id,print_console,"[AMX] PlayTime - Query Failed: '%s'",merror) 
       log_to_file(g_logFile,"[AMX] PlayTime - Query Failed: '%s'",merror) 
   }

   }
   else
   { 
       mysql_error(mysql,merror,64) 
       client_print(id,print_console,"[AMX] PlayTime - Sorry, No Mysql Connection: '%s'",merror)
       log_to_file(g_logFile,"[AMX] PlayTime - Sorry, No Mysql Connection: '%s'",merror)
   } 


   return PLUGIN_HANDLED
}

public sql_initiate()
{
	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,255)

	if(mysql < 1)
	{
		server_print("[AMX] PlayTime - MySQL error on init : '%s'",merror)
	        log_to_file(g_logFile,"[AMX] PlayTime - MySQL error on init : '%s'",merror)

	}

  	
	return PLUGIN_CONTINUE
} 

public plugin_init() 
{ 
   register_plugin("PlayTime MySQL","0.5","Neosheep") 
   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") 
   
   get_logfile(g_logFile,15) 

   register_clcmd("playtime","playtime_cmd",0," [part of nick] - get the playtime of player in current month.")
   register_clcmd("amx_playtime","playtime_cmd",0," [part of nick] - get the playtime of player in current month.")
   register_clcmd("say","playtime_say")
   register_clcmd("say_team","playtime_say")

   set_task(5.0,"sql_initiate")

} 
