
/* AMX Mod script.
*
* (c) Copyright 2002, Noplay
* This file is provided as is (no warranties).
*
* A part of code is from amx_lastname by JustinHoMi
*/

/*

This AMX plugin requires MySQL MetaSmall module.

For this to work, you might create your MySQL table with this SQL query :


CREATE TABLE `status` (
  `srv` tinyint(4) NOT NULL default '0',
  `hostname` varchar(64) NOT NULL default '',
  `map` varchar(32) NOT NULL default '',
  `nextmap` varchar(32) NOT NULL default '',
  `maxplayers` tinyint(3) NOT NULL default '0',
  UNIQUE KEY `srv` (`srv`)
) TYPE=MyISAM;

CREATE TABLE `players` (
`srv` TINYINT( 4 ) NOT NULL ,
`id` TINYINT( 4 ) NOT NULL ,
`name` VARCHAR( 32 ) NOT NULL
);

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

*/


/*
All server need have an unique ID give by the cvar amx_server_id

you can this in addons/amx/phpamx.cfg file

for example:
amx_server_id "1"

If you have many server on the same computer and on different port, 
i recommend to use the PORT.cfg amx plugin 
*/


#include <amxmod>
#include <mysql>

new srv_id[5]

new mysql

new merror[257]
new mhost[65]
new muser[33]
new mpass[33]
new mdb[33]
new mquery[256]

public client_disconnect(id)
{

	new authid[17],name[33]

	get_user_authid(id,authid,16)
	
	get_user_name(id,name,32)



	format(mquery,255,"DELETE FROM `players` WHERE `srv`=('%s') and `id`=('%s') and name=('%s')",srv_id,authid,name)

	mysql_query(mysql,mquery)

	mysql_error(mysql,merror,31)
	
	if (merror[0])
	{
		server_print("[AMX] MYSQL error: %s",merror)
	}

	return PLUGIN_CONTINUE
}

public client_connect(id)
{

	new authid[17],name[33]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)

	format(mquery,255,"INSERT INTO `players` (srv,id,name) values ('%s','%s','%s')",srv_id,authid,name)

	mysql_query(mysql,mquery)

	mysql_error(mysql,merror,31)
	
	if (merror[0])
	{
		server_print("[AMX] MYSQL error: %s",merror)
	}

	return PLUGIN_CONTINUE
}


public client_infochanged(id)
{
	if (is_user_connected(id))
	{
		new newname[33], oldname[33]
		get_user_info(id, "name", newname,32)
		get_user_name(id,oldname,32)

		if (!equal(oldname,newname))
		{
			new authid[17]
			get_user_authid(id,authid,16)
	

			format(mquery,255,"REPLACE into players (srv,id,name) values ('%s','%s','%s')",srv_id,authid,newname)
			mysql_query(mysql,mquery)
			mysql_error(mysql,merror,31)
			
			if (merror[0])
			{
				server_print("[AMX] MYSQL error: %s",merror)
			}
	         }
    	}
	return PLUGIN_CONTINUE
}


public connect_srv()
{

	new hostname[65]
	new map[33]
	new nextmap[33]

	new maxplayers


	sql_init()

	
	maxplayers=get_maxplayers()
		
	get_cvar_string("amx_server_id",srv_id,4) 

	get_cvar_string("hostname",hostname,64)
	get_cvar_string("amx_nextmap",nextmap,32)

	get_mapname(map,32)
	
	
	if(mysql>0)
	{
		format(mquery,255,"REPLACE into status (srv,hostname,map,nextmap,maxplayers) values ('%s','%s','%s','%s','%d')",srv_id,hostname,map,nextmap,maxplayers)
		mysql_query(mysql,mquery)
		mysql_error(mysql,merror,31)
		
		if (merror[0])
		{
			server_print("[AMX] MYSQL error: %s",merror)
		}


	}
	else
	{
		server_print("MySQL error : could not connect : '%s'",merror)
	}

	return PLUGIN_HANDLED
}


public plugin_end()
{
	if (mysql > 0)
	{

		format(mquery,255,"DELETE FROM `players` WHERE `srv`=('%s')",srv_id)
		mysql_query(mysql,mquery)


		mysql_close(mysql)
	}
	
	return PLUGIN_CONTINUE
}

public sql_init()
{
	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,256)

	if(mysql < 1)
	{
		server_print("[AMX] MySQL error : could not connect : '%s'",merror)
	}
   
	if (merror[0])
	{
		server_print("[AMX] MYSQL error: %s",merror)
   	}

   	
	return PLUGIN_CONTINUE
} 

public plugin_init()
{
	register_plugin("Server Status in MySQL","0.2","Noplay")
	register_cvar("amx_mysql_mhost","127.0.0.1")
	register_cvar("amx_mysql_muser","root")
	register_cvar("amx_mysql_mpass","")
	register_cvar("amx_mysql_db","amx")
	register_cvar("amx_server_id","0")

   	server_cmd("exec addons/amx/phpamx.cfg")
  
   	set_task(0.25,"connect_srv")
   	  	
	return PLUGIN_CONTINUE
}
