
/* AMX Mod script. 
* 
* Personnalised MySQL Enter Message, v 2.6 
* 
* (c) Copyright 2003, #StrikeR 
* This file is provided as is (no warranties). 
* 
* [FR] : Ce plugin charge un message personnalisé via MySQL pour chaque joueur lorsqu'il rejoint le serveur, celui ci s'affiche pour tout le monde.
* [EN] : This plugin load a personnalised (hud)message from a MySQL server, the message is shown when you're connecting to server.
* 
* First, create `huds` tbl. (i.e. with PHPMyAdmin)

CREATE TABLE `huds` (
  `auth` varchar(60) NOT NULL default '',
  `fraz` varchar(80) NOT NULL default '',
  PRIMARY KEY  (`auth`)
) TYPE=MyISAM;

* Then you can insert your text and authid from a phpscript or PHPMyAdmin or with that :

Insert Into `huds` Values ('STEAM_0:x:yyyyy', 'your_text');

* 
* 
* Report any bug to striker_AT_carabidule.net
* 
*/ 

#include <amxmod> 
#include <mysql>


public plugin_init(){ 
 register_plugin("PersosMSG MySQL","2.6","#StrikeR") 
 register_clcmd("amx_settext","SetText",0,"Change ton texte d'entre [Quand tu te connecte au serveur]") 
} 

public client_putinserver(id){ 

  new host[64],user[32],pass[32],db[32],error[128],steamid[60]
  get_user_authid(id,steamid,60)
  get_cvar_string("amx_mysql_host",host,63)
  get_cvar_string("amx_mysql_user",user,31)
  get_cvar_string("amx_mysql_pass",pass,31)
  get_cvar_string("amx_mysql_db",db,31)

  new mysql = mysql_connect(host,user,pass,db,error,127)
  if(mysql < 1){
    server_print("MySQL error: can't connect: '%s'",error)
    return PLUGIN_HANDLED 
  }
	
  new fraz[80], authid[32]
  new name[32]
  get_user_name(id,name,32)


  new query[1024]
  format(query,1023,"SELECT auth,fraz FROM huds WHERE auth = '%s'", steamid)
  mysql_query(mysql,query)
  mysql_error(mysql,error,32)

  if (error[0]) {
   server_print("[AMX] MYSQL Error in huds.fraz: %s", error)
   return PLUGIN_HANDLED
  } 



  server_print("QUERY : %s",query)

  if (mysql_nextrow(mysql) > 0)
  {
    mysql_getfield(mysql, 1, authid ,60)
    mysql_getfield(mysql, 2, fraz ,80)
  }

  
  server_print("Loaded hud for %s <%s>",name,steamid)
  server_print("sID FOR QUERY : ^"%s^"",steamid)
  server_print("AUTHID : %s  HUDMSG : %s",authid,fraz)

  mysql_close(mysql)

  if(fraz[0]) {
   set_hudmessage(200,100,0,0.05,0.55,1,6.0,15.0,0.3,0.15,4) 
   show_hudmessage(0,"%s Vient de se connecter : %s",name,fraz) 
  }
  else {
   set_hudmessage(200,100,0,0.05,0.55,1,6.0,15.0,0.3,0.15,4) 
   show_hudmessage(0,"%s Vient de se connecter.",name) 
  }
  setEnterText(id)

  return PLUGIN_HANDLED 
}

setEnterText(id)
  set_task(25.0,"dispTxt",id)

public dispTxt(id) {
  new name[30],hostname[128]
  get_cvar_string("hostname",hostname,127)
  get_user_name(id,name,29)

  client_print(id,print_chat,"Salut %s, bienvenue sur %s",name,hostname)
  client_print(id,print_chat,"Ce serveur propose de personnaliser ton message d'entree tappe amx_settext < ton texte > dans la console pour le mettre")
}

public client_disconnect(id) { 

  new name[32]
  get_user_name(id,name,32)
  set_hudmessage(200,100,0,0.05,0.55,1,6.0,15.0,0.3,0.15,4) 
  show_hudmessage(0,"%s a quitte le serveur.",name)  
  return PLUGIN_HANDLED 
} 

public SetText(id){

  new host[64],user[32],pass[32],db[32],error[128],steamid[60]
  get_user_authid(id,steamid,60)
  get_cvar_string("amx_mysql_host",host,63)
  get_cvar_string("amx_mysql_user",user,31)
  get_cvar_string("amx_mysql_pass",pass,31)
  get_cvar_string("amx_mysql_db",db,31)

  new mysql = mysql_connect(host,user,pass,db,error,127)

  if(mysql < 1) {
  	server_print("MySQL error: can't connect: '%s'",error)
  	return PLUGIN_HANDLED 
  }
	
  new auth[80],fraz[80],query[1024],arg[80]
  read_args(arg,80)
  format(query,1023,"SELECT auth,fraz FROM huds WHERE auth = '%s'", steamid)
  mysql_query(mysql,query)
  mysql_error(mysql,error,32)
  mysql_close(mysql)

  if (error[0]) {
  	server_print("[AMX] MYSQL Error in huds.fraz: %s", error)
  	return PLUGIN_HANDLED
  }
  else 
  {
  if (mysql_nextrow(mysql) > 0)
   {
  	mysql_getfield(mysql, 1, auth ,80)
  	mysql_getfield(mysql, 2, fraz ,80)
   }
  }
  if(!fraz[0] && !auth[0]) {
   format(query,1023,"Insert Into `huds` (auth,fraz) values (^"%s^",^"%s^")", steamid, arg)
   mysql_query(mysql,query)
   mysql_error(mysql,error,32)
   mysql_close(mysql)
   if (error[0]) {
     server_print("[AMX] MYSQL Error in huds.fraz: %s", error)
     return PLUGIN_HANDLED
   }
   client_print(id,print_console,"Votre message d'entree a bien ete mis a jour. [INSERT]")
  }
  else {
   format(query,1023,"Update huds Set fraz = ^"%s^" Where auth = '%s'", arg, steamid)
   mysql_query(mysql,query)
   mysql_error(mysql,error,32)
   mysql_close(mysql) 
   if (error[0]) {
     server_print("[AMX] MYSQL Error in huds.fraz: %s", error)
     client_cmd(0,"echo [AMX] MYSQL Error in huds.fraz: %s", error)
     return PLUGIN_HANDLED
   }
   client_print(id,print_console,"Votre message d'entree a bien ete mis a jour. [UPDATE]")
  }
  return PLUGIN_HANDLED
}
