Counting and showing in MOTD
Hey, how to count like how many times player wrote a command (would like to save it in SQL) and then show it in motd like (Should be descending order):
Name1 Count
Name2 Count
Name3 Count
like:
Reinert 9
someone 4
mememe 2
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx> #include <amxmisc> #include <sqlx>
#define PLUGIN "Plugin name" #define VERSION "1.0" #define AUTHOR "reinert"
new Host[] = "hostname" new User[] = "username" new Pass[] = "password" new Db[] = "database"
new Handle:g_SqlTuple new g_Error[512]
new iMenuUsed[33];
public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) register_clcmd("say /amenu", "CmdAdminMenu") set_task(1.0, "MySql_Init") }
public MySql_Init() { g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db) new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error)) if(SqlConnection == Empty_Handle) set_fail_state(g_Error) new Handle:Queries Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS tutorial (steamid varchar(32),exp INT(11))")
if(!SQL_Execute(Queries)) { SQL_QueryError(Queries,g_Error,charsmax(g_Error)) set_fail_state(g_Error) } SQL_FreeHandle(Queries) SQL_FreeHandle(SqlConnection) }
public Load_MySql(id) { new szName[32], szTemp[512] get_user_name(id, szName, charsmax(szName)) new Data[1] Data[0] = id format(szTemp,charsmax(szTemp),"SELECT * FROM `count` WHERE (`count`.`name` = '%s')", szName) SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1) }
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) { if(FailState == TQUERY_CONNECT_FAILED) { log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error) } else if(FailState == TQUERY_QUERY_FAILED) { log_amx("Load Query failed. [%d] %s", Errcode, Error) }
new id id = Data[0] if(SQL_NumResults(Query) < 1) { new szName[32] get_user_name(id, szName, charsmax(szName)) new szTemp[512] format(szTemp,charsmax(szTemp),"INSERT INTO `count` ( `name` , `count`)VALUES ('%s','0');",szName) SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp) } else { iMenuUsed[id] = SQL_ReadResult(Query, 1) } return PLUGIN_HANDLED }
public Save_MySql(id) { new szName[32], szTemp[512] get_user_name(id, szName, charsmax(szName)) format(szTemp,charsmax(szTemp),"UPDATE `count` SET `count` = '%i' WHERE `count`.`name` = '%s';",iMenuUsed[id], szName) SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp) }
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) { SQL_FreeHandle(Query) return PLUGIN_HANDLED }
public CmdAdminMenu(id) { if(get_user_flags(id) & ADMIN_MENU) { iMenuUsed[id]+=1; } }
public client_disconnect(id) { if(get_user_flags(id) & ADMIN_MENU) { Save_MySql(id) iMenuUsed[id] = 0; } }
public client_putinserver(id) { if(get_user_flags(id) & ADMIN_MENU) { Load_MySql(id) } }
public plugin_end() { SQL_FreeHandle(g_SqlTuple) }
How to get all names from SQLx and show them on MOTD.
PHP Code:
public PrintCountMotd(id) { new Motd[ MAX_BUFFER_LENGTH ], Name[ MAX_NAME_LENGTH ], Player, Len;
while( ++Player <= gMaxPlayers ) if( is_user_connected( Player ) && get_user_flags( Player ) & ADMIN_KICK ) { get_user_name( Player, Name, 32 ); Len += formatex( Motd[ Len ], MAX_BUFFER_LENGTH - Len - 1, "%d.%s/* - %d*/", Player, Name/*, iCount*/ ); } show_motd( Id, Motd, "Admin Countings" ); }
|