Hey guys I'm having a problem, Whenever a new map loads on the server I get SQL lag while the server checks all the clients if they have rated or not. Is there anyway i could add a delay so that it does not check all the clients simotaniously.. Thanks for any help. I posted the code below that i am using.
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#include <colorchat>
#define PLUGIN "ratemap"
#define VERSION "0.9.6"
#define AUTHOR "sTmN"
new mapname[30],tag[30]
new Handle:g_SqlTuple
new g_Error[512]
new ErrorCode,Handle:SqlConnection
new Host[64] = "DB Host"
new User[64] = "DB User"
new Pass[64] = "DB Pass"
new Db[64] = "DB Name"
new Table[32] = "DB Table"
new g_maxplayers
new bool:g_selected = false
new bool:do_vote[33] = false
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("ratemap.txt");
register_cvar("rm_tag","[RateMap]")
get_cvar_string("rm_tag",tag,30)
get_mapname(mapname,30)
set_task(15.0, "RateMap", 272456, "", 0, "b")
g_maxplayers = get_maxplayers()
table_init();
}
public sql_connect()
{
SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
if(SqlConnection == Empty_Handle)
set_fail_state(g_Error)
}
public table_init()
{
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
sql_connect();
new Handle:Query = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS %s (steamid TEXT(32),mapname TEXT(32),rate INT(11))",Table)
if(!SQL_Execute(Query))
{
SQL_QueryError(Query,g_Error,511)
set_fail_state(g_Error)
}
server_print("# Connected to database succesfully.")
SQL_FreeHandle(SqlConnection)
}
public ratemenu(id)
{
new Temp[64]
format(Temp, 64, "%L", LANG_PLAYER, "MENU_TITLE");
new menu = menu_create(Temp,"func_ratemenu")
format(Temp, 64, "%L", LANG_PLAYER, "OPTION_1");
menu_additem(menu, Temp,"1",0)
format(Temp, 64, "%L", LANG_PLAYER, "OPTION_2");
menu_additem(menu, Temp,"2",0)
format(Temp, 64, "%L", LANG_PLAYER, "OPTION_3");
menu_additem(menu, Temp,"3",0)
format(Temp, 64, "%L", LANG_PLAYER, "OPTION_4");
menu_additem(menu, Temp,"4",0)
format(Temp, 64, "%L", LANG_PLAYER, "OPTION_5");
menu_additem(menu, Temp,"5",0)
menu_setprop(menu,MPROP_EXIT,MEXIT_ALL)
menu_display(id,menu,0)
}
public func_ratemenu(id,menu,item)
{
if(item==MENU_EXIT)
{
menu_destroy(menu);
return PLUGIN_HANDLED
}
new data[6], iName[64]
new access, callback
menu_item_getinfo(menu, item, access, data,5, iName, 63, callback);
new key = str_to_num(data);
switch(key)
{
case 1 : ratesql(id,key);
case 2 : ratesql(id,key);
case 3 : ratesql(id,key);
case 4 : ratesql(id,key);
case 5 : ratesql(id,key);
case 6 : ratesql(id,key);
}
return PLUGIN_HANDLED
}
public ratesql(id,key)
{
new steamid[32]
get_user_authid(id,steamid,32)
new rate[15]
num_to_str(key,rate,12)
sql_connect();
new Handle:Queries = SQL_PrepareQuery(SqlConnection,"INSERT INTO %s VALUES('%s','%s','%s')",Table,steamid,mapname,rate)
if(key==1)
format(rate, 15, "%L", LANG_PLAYER, "OPTION_1");
else if(key==2)
format(rate, 15, "%L", LANG_PLAYER, "OPTION_2");
else if(key==3)
format(rate, 15, "%L", LANG_PLAYER, "OPTION_3");
else if(key==4)
format(rate, 15, "%L", LANG_PLAYER, "OPTION_4");
else if(key==5)
format(rate, 15, "%L", LANG_PLAYER, "OPTION_5");
do_vote[id] = false
ColorChat(0, RED,"%L",LANG_PLAYER,"RATEINFO",tag,steamid,rate)
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,511)
set_fail_state(g_Error)
}
SQL_FreeHandle(SqlConnection)
}
public client_authorized(id)
{
new steamid[32]
get_user_authid(id,steamid,31)
sql_connect();
new Handle:Query = SQL_PrepareQuery(SqlConnection,"SELECT steamid FROM %s WHERE steamid='%s' AND mapname='%s'",Table,steamid,mapname)
if(!SQL_Execute(Query))
{
SQL_QueryError(Query,g_Error,511)
set_fail_state(g_Error)
}
if(!SQL_MoreResults(Query))
{
do_vote[id] = true
}
else
{
do_vote[id] = false
}
SQL_FreeHandle(SqlConnection)
}
public client_disconnect(id)
{
do_vote[id] = false
}
public RateMap()
{
new timeleft = get_timeleft()
if (timeleft < 60 || timeleft > 308)
{
g_selected = false
return
}
if (g_selected)
return
g_selected = true
votecheck()
}
public votecheck()
{
for(new i = 1; i <= g_maxplayers; ++i)
{
if (do_vote[i] && !is_user_connected(i) && !is_user_connecting(i))
{
ratemenu(i)
}
}
}