I was trying couple of hours without results. I want save best score from a map and his owner, best score of a map every player. I don't have any ideas how to fix it.
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <amxmisc>
#include <sqlx>
new Host[] = "xxxxxxx"
new User[] = "xxxxxxx"
new Pass[] = "xxxxxxx"
new Db[] = "xxxxxx"
new Handle:g_SqlTuple
new g_Error[512]
#define PLUGIN "SpeedMeter"
#define VERSION "2.1"
#define AUTHOR "Vertricus"
#define ACCESS_RESET ADMIN_IMMUNITY
#define MAX_PLAYERS 32
//Zmienne
new Float:fPlayerMaxSpeed[MAX_PLAYERS+1], Float: fPlayerActualSpeed[MAX_PLAYERS+1], szPlayerName[MAX_PLAYERS+1][32]
new szKeySpeed[32], szKeyName[34]
new bool:NewRecord = false, szMapName[32];
new szChampionName[32], Float:fMapRecord, Float:rekord[33], mapa[33][51]
new HudObj
new pcvarEnabled, pcvarUpadte, pcvarTerro
new HudBot
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
//Cvars
pcvarEnabled = register_cvar("sm_enabled", "1")
pcvarUpadte = register_cvar("sm_upadte", "0.1")
pcvarTerro = register_cvar("sm_terro", "1")
//FM Part
register_forward(FM_PlayerPreThink, "Fw_PlayerPreThink")
register_forward(FM_ClientUserInfoChanged, "Fw_ClientUserInfoChanged", 1)
//Others
get_mapname(szMapName, charsmax(szMapName))
HudObj = CreateHudSyncObj()
register_clcmd("sm_reset", "CmdSpeedReset", ACCESS_RESET)
//nVaultPart
formatex(szKeySpeed,63,"%s-Speed",szMapName)
formatex(szKeyName,63,"%s-Name",szMapName)
set_task(1.0, "MySql_Init") // set a task to activate the mysql_init
set_task(2.0, "MySql_Init2") // set a task to activate the mysql_init
}
public plugin_cfg()
{
CreateHudBot()
}
public client_connect(id) wczytaj_gracza(id)
public client_disconnect(id) zapisz_gracza(id)
public plugin_end()
{
//if(!NewRecord) return;
// Here we will update the user hes information in the database where the steamid matches.
new szTemp[512], mapka[50]
get_mapname(mapka, 49)
format(szTemp,charsmax(szTemp),"UPDATE `speed` SET `predkosc` = '%f', `nick` ='%s' WHERE `speed`.`mapka` = '%s';",fMapRecord, szChampionName, mapka)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
SQL_FreeHandle(g_SqlTuple)
for(new id=0; id<=32; id++){
if(is_user_connected(id)) zapisz_gracza(id)
}
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}
public MySql_Init()
{
// we tell the API that this is the information we want to connect to,
// just not yet. basically it's like storing it in global variables
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
// ok, we're ready to connect
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
// stop the plugin with an error message
set_fail_state(g_Error)
new Handle:Queries
// we must now prepare some random queries
Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS speed (nick varchar(32), predkosc FLOAT(5), mapa varchar(50))")
if(!SQL_Execute(Queries))
{
// if there were any problems
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
// close the handle
SQL_FreeHandle(Queries)
// you free everything with SQL_FreeHandle
SQL_FreeHandle(SqlConnection)
set_task(1.0, "wczytaj_predkosc")
}
public MySql_Init2()
{
// we tell the API that this is the information we want to connect to,
// just not yet. basically it's like storing it in global variables
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
// ok, we're ready to connect
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
// stop the plugin with an error message
set_fail_state(g_Error)
new Handle:Queries
// we must now prepare some random queries
Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS player (gracz varchar(32), gmapa varchar(51), gpredkosc FLOAT(5))")
if(!SQL_Execute(Queries))
{
// if there were any problems
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
// close the handle
SQL_FreeHandle(Queries)
// you free everything with SQL_FreeHandle
SQL_FreeHandle(SqlConnection)
}
public wczytaj_gracza(id){
new szTemp[512], name[33], Data[1], mapka[50]
get_mapname(mapka, 50)
get_user_name(id, name, 32)
Data[0]=id
rekord[id]=0.0
format(szTemp,charsmax(szTemp),"SELECT `gpredkosc` FROM `player` WHERE(`player`.`gracz` = '%s' AND `player`.`gmapa` = '%s')", name, mapka)
SQL_ThreadQuery(g_SqlTuple,"register_client2",szTemp, Data, 1)
}
public zapisz_gracza(id){
new szTemp[512], name[33], mapka[50]
get_mapname(mapka, 49)
get_user_name(id, name, 32)
format(szTemp,charsmax(szTemp),"UPDATE `player` SET `gpredkosc` = '%f' WHERE(`player`.`gracz`='%s' AND `player`.`gmapa` = '%s');",rekord[id], name, mapka)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
SQL_FreeHandle(g_SqlTuple)
}
public wczytaj_predkosc(){
new szTemp[512], mapka[50]
get_mapname(mapka, 49)
format(szTemp,charsmax(szTemp),"SELECT `nick`, `predkosc` FROM `speed` WHERE `speed`.`mapa` = '%s'", mapka)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp)
}
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)
}
if(SQL_NumResults(Query) < 1)
{
new szTemp[512], mapka[50]
get_mapname(mapka, 49)
// now we will insturt the values into our table.
format(szTemp,charsmax(szTemp),"INSERT INTO `speed` ( `nick` , `predkosc`, `mapa`)VALUES ('%s','0', '%s');", szChampionName, fMapRecord, mapka)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
else
{
// if there are results found
fMapRecord = float((SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"predkosc"))))
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"nick"), szChampionName, 31)
}
return PLUGIN_HANDLED
}
public register_client2(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 szTemp[512]
new name[33]
get_user_name(id, name, 32)
// now we will insturt the values into our table.
format(szTemp,charsmax(szTemp),"INSERT INTO `player` ( `gracz` , `gpredkosc`, `gmapa`)VALUES ('%s','0', '%s');", name, mapa[id])
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
else
{
// if there are results found
rekord[id] = float(SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"gpredkosc")))
}
return PLUGIN_HANDLED
}
public client_authorized(id)
{
if (!get_pcvar_num(pcvarEnabled))
return
get_user_name(id, szPlayerName[id], 31)
fPlayerMaxSpeed[id] = 0.0
}
public Fw_ClientUserInfoChanged(id)
{
get_user_name(id, szPlayerName[id], 31)
}
public Fw_PlayerPreThink(id)
{
if (!is_user_alive(id)|| !get_pcvar_num(pcvarEnabled))
return FMRES_IGNORED;
if (get_pcvar_num(pcvarTerro) == 0 && get_user_team(id) == 1)
return FMRES_IGNORED;
fPlayerActualSpeed[id] = Player_Speed(id)
new name[32]
get_user_name(id, name, 31)
if (fPlayerActualSpeed[id] > fPlayerMaxSpeed[id]){
fPlayerMaxSpeed[id] = fPlayerActualSpeed[id]
new m[51]
get_mapname(m, 50)
mapa[id]=m
rekord[id]=fPlayerMaxSpeed[id]
}
if(fPlayerActualSpeed[id]>fMapRecord) SetNewRecord(fPlayerActualSpeed[id], name)
return FMRES_IGNORED;
}
public CreateHudBot()
{
HudBot = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString,"info_target"));
if(!pev_valid(HudBot))
return;
set_pev(HudBot, pev_classname, "HudBot");
set_pev(HudBot, pev_nextthink, get_gametime()+ get_pcvar_float(pcvarUpadte))
RegisterHamFromEntity(Ham_Think, HudBot, "Think_HudBot")
}
public Think_HudBot(Bot)
{
if (!pev_valid(Bot) || Bot != HudBot)
return HAM_IGNORED;
if (get_pcvar_num(pcvarEnabled))
{
new iPlayers[32], iNum, id
get_players(iPlayers, iNum)
for(new i= 0; i<iNum; i++)
{
id = iPlayers[i];
if (!is_user_alive(id))
continue
set_hudmessage(255, 170, 42, 0.7, 0.2, 1, 0.01, get_pcvar_float(pcvarUpadte), 0.01, 0.01, 3)
if (get_pcvar_num(pcvarTerro) == 0 && get_user_team(id) == 1)
ShowSyncHudMsg(id, HudObj, "Rekord Mapy: %.2f^nRekordzista: %s^nTwoj rekord: %f", fMapRecord,szChampionName, rekord[id])
else
ShowSyncHudMsg(id, HudObj, "Twoja predkosc: %.2f^nTwoj vMax: %.2f^nRekord Mapy: %.2f^nRekordzista: %s^nTwoj rekord: %.2f", fPlayerActualSpeed[id], fPlayerMaxSpeed[id], fMapRecord,szChampionName, rekord[id])
if (fPlayerMaxSpeed[id] > fMapRecord)
SetNewRecord(fPlayerMaxSpeed[id], szPlayerName[id])
}
}
set_pev(Bot, pev_nextthink, get_gametime()+get_pcvar_float(pcvarUpadte))
return HAM_IGNORED;
}
stock SetNewRecord(Float:Speed, Name[32])
{
fMapRecord = Speed
szChampionName = Name
NewRecord = true
}
stock Float:Player_Speed(id)
{
new Float:fVect[3]
pev(id, pev_velocity,fVect)
return floatsqroot(fVect[0]*fVect[0]+fVect[1]*fVect[1])
}
public CmdSpeedReset(id, level, cid)
{
if(!cmd_access(id,level, cid, 1))
return PLUGIN_HANDLED;
new iPlayers[32], iNum
get_players(iPlayers, iNum)
for(new i=0; i<iNum; i++)
fPlayerMaxSpeed[iPlayers[i]] = 0.0
SetNewRecord(0.0, "Brak")
client_print(id, print_console, "Rekord szybkosci na mapie zostal zresetowany")
return PLUGIN_HANDLED
}
I do it. EOT.