Raised This Month: $ Target: $400
 0% 

sql and saving some thinks


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
lis_16
Senior Member
Join Date: Feb 2008
Old 03-28-2011 , 14:49   sql and saving some thinks
Reply With Quote #1

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], FloatfPlayerActualSpeed[MAX_PLAYERS+1], szPlayerName[MAX_PLAYERS+1][32]
new 
szKeySpeed[32], szKeyName[34]
new 
bool:NewRecord falseszMapName[32];
new 
szChampionName[32], Float:fMapRecordFloat:rekord[33], mapa[33][51]
new 
HudObj
new pcvarEnabledpcvarUpadtepcvarTerro
new HudBot
public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
//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(szMapNamecharsmax(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(idwczytaj_gracza(id)
public 
client_disconnect(idzapisz_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(mapka49)
    
format(szTemp,charsmax(szTemp),"UPDATE `speed` SET `predkosc` = '%f', `nick` ='%s' WHERE `speed`.`mapka` = '%s';",fMapRecordszChampionNamemapka)
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    
SQL_FreeHandle(g_SqlTuple)
    for(new 
id=0id<=32id++){
        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(mapka50)
    
get_user_name(idname32)
    
Data[0]=id
    rekord
[id]=0.0
    format
(szTemp,charsmax(szTemp),"SELECT `gpredkosc` FROM `player` WHERE(`player`.`gracz` = '%s' AND `player`.`gmapa` = '%s')"namemapka)
    
SQL_ThreadQuery(g_SqlTuple,"register_client2",szTempData1)
}


public 
zapisz_gracza(id){
    new 
szTemp[512], name[33], mapka[50]
    
get_mapname(mapka49)
    
get_user_name(idname32)
    
format(szTemp,charsmax(szTemp),"UPDATE `player` SET `gpredkosc` = '%f' WHERE(`player`.`gracz`='%s' AND `player`.`gmapa` = '%s');",rekord[id], namemapka)
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    
SQL_FreeHandle(g_SqlTuple)
    
}
public 
wczytaj_predkosc(){
    new 
szTemp[512], mapka[50]
    
get_mapname(mapka49)
    
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"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }

    
    if(
SQL_NumResults(Query) < 1
    {
            
        new 
szTemp[512], mapka[50]
    
get_mapname(mapka49)
        
        
// now we will insturt the values into our table.
        
format(szTemp,charsmax(szTemp),"INSERT INTO `speed` ( `nick` , `predkosc`, `mapa`)VALUES ('%s','0', '%s');"szChampionNamefMapRecordmapka)
        
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"), szChampionName31)
    }
    
    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"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }
    new 
id
    id
=Data[0]
    
    if(
SQL_NumResults(Query) < 1
    {
            
        new 
szTemp[512]
        new 
name[33]
    
get_user_name(idname32)
        
// now we will insturt the values into our table.
        
format(szTemp,charsmax(szTemp),"INSERT INTO `player` ( `gracz` , `gpredkosc`, `gmapa`)VALUES ('%s','0', '%s');"namemapa[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(idszPlayerName[id], 31)
    
fPlayerMaxSpeed[id] = 0.0
}
public 
Fw_ClientUserInfoChanged(id)
{
    
get_user_name(idszPlayerName[id], 31)
}
public 
Fw_PlayerPreThink(id)
{
    if (!
is_user_alive(id)|| !get_pcvar_num(pcvarEnabled))
        return 
FMRES_IGNORED;
    
    if (
get_pcvar_num(pcvarTerro) == && get_user_team(id) == 1
        return 
FMRES_IGNORED;
        
    
fPlayerActualSpeed[id] = Player_Speed(id)
    new 
name[32]
    
get_user_name(idname31)
    if (
fPlayerActualSpeed[id] > fPlayerMaxSpeed[id]){
        
fPlayerMaxSpeed[id] = fPlayerActualSpeed[id]
        new 
m[51]
        
get_mapname(m50)
        
mapa[id]=m
        rekord
[id]=fPlayerMaxSpeed[id]
    }
    if(
fPlayerActualSpeed[id]>fMapRecordSetNewRecord(fPlayerActualSpeed[id], name)
    
    return 
FMRES_IGNORED;    
}
public 
CreateHudBot()
{
    
HudBot engfunc(EngFunc_CreateNamedEntityengfunc(EngFunc_AllocString,"info_target"));
    
    if(!
pev_valid(HudBot))
        return;
        
    
set_pev(HudBotpev_classname"HudBot");
    
set_pev(HudBotpev_nextthinkget_gametime()+ get_pcvar_float(pcvarUpadte))
    
    
RegisterHamFromEntity(Ham_ThinkHudBot"Think_HudBot")
}
public 
Think_HudBot(Bot)
{
    if (!
pev_valid(Bot) || Bot != HudBot)
        return 
HAM_IGNORED;
        
    if (
get_pcvar_num(pcvarEnabled))
    {
        new 
iPlayers[32], iNumid
        get_players
(iPlayersiNum)
        for(new 
i0i<iNumi++)
        {
            
id iPlayers[i];
            if (!
is_user_alive(id))
                continue
            
            
set_hudmessage(255170420.70.210.01get_pcvar_float(pcvarUpadte), 0.010.013)
            
            if (
get_pcvar_num(pcvarTerro) == && get_user_team(id) == 1
                
ShowSyncHudMsg(idHudObj"Rekord Mapy: %.2f^nRekordzista: %s^nTwoj rekord: %f"fMapRecord,szChampionNamerekord[id])
            else
                
ShowSyncHudMsg(idHudObj"Twoja predkosc: %.2f^nTwoj vMax: %.2f^nRekord Mapy: %.2f^nRekordzista: %s^nTwoj rekord: %.2f"fPlayerActualSpeed[id], fPlayerMaxSpeed[id], fMapRecord,szChampionNamerekord[id])
            
            if (
fPlayerMaxSpeed[id] > fMapRecord)
                
SetNewRecord(fPlayerMaxSpeed[id], szPlayerName[id])
        }
    }
    
    
set_pev(Botpev_nextthinkget_gametime()+get_pcvar_float(pcvarUpadte))
    
    return 
HAM_IGNORED;
}
stock SetNewRecord(Float:SpeedName[32])
{
    
fMapRecord Speed
    szChampionName 
Name
    NewRecord 
true
}
stock Float:Player_Speed(id)
{
    new 
Float:fVect[3]
    
pev(idpev_velocity,fVect)
    return 
floatsqroot(fVect[0]*fVect[0]+fVect[1]*fVect[1])
}
public 
CmdSpeedReset(idlevelcid

    if(!
cmd_access(id,levelcid1)) 
        return 
PLUGIN_HANDLED
    
    new 
iPlayers[32], iNum
    get_players
(iPlayersiNum)
    for(new 
i=0i<iNumi++)
        
fPlayerMaxSpeed[iPlayers[i]] = 0.0
        
    SetNewRecord
(0.0"Brak")
    
client_print(idprint_console"Rekord szybkosci na mapie zostal zresetowany")
    return 
PLUGIN_HANDLED 


I do it. EOT.

Last edited by lis_16; 03-29-2011 at 15:55.
lis_16 is offline
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:36.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode