AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Save Points On mysql (https://forums.alliedmods.net/showthread.php?t=214841)

oxygen935 04-30-2013 09:59

Save Points On mysql
 
Hello everybody,
As the title says i have a points system on my server and i want to save the player points on a mysql database. I really need help... I found some xp mods that saves the xp on a mysql database but i want some help on how to save points and not xp...
btw i found this tutorial: http://forums.alliedmods.net/showthread.php?t=132686

PS:I really know that vault is faster and easier, so if you want to suggest me to use vault, better not reply. I want to learn how to save them on MySql DataBase...

Sylwester 04-30-2013 21:47

Re: Save Points On mysql
 
Asking us to help you learn how to save points and not xp makes no sense, because in both cases you save some integer value in database and there is no difference. If you need more examples, then take a look at my sql rank mod.

oxygen935 05-01-2013 01:54

Re: Save Points On mysql
 
Oh ok, i didn't know if it's the same. Thanks. I'll try it!

oxygen935 05-01-2013 15:15

Re: Save Points On mysql
 
I did something with mysql but i have some debug errors...
Code:

L 05/01/2013 - 21:01:40: Start of error session.
L 05/01/2013 - 21:01:40: Info (map "de_dust2") (file "addons/amxmodx/logs/error_20130501.log")
L 05/01/2013 - 21:01:40: [MySQL] Thread worker was unable to start.
L 05/01/2013 - 21:01:40: [AMXX] Displaying debug trace (plugin "points.amxx")
L 05/01/2013 - 21:01:40: [AMXX] Run time error 10: native error (native "SQL_ThreadQuery")
L 05/01/2013 - 21:01:40: [AMXX]    [0] points.sma::register_client (line 526)

Here is the part of mysql...
PHP Code:

#include <amxmodx>
#include <sqlx>

new points[33];

new 
Host[]     = "blahblah"
new User[]    = "blahblah"
new Pass[]     = "blahblah"
new Db[]     = "blahblah"

new Handle:g_SqlTuple
new g_Error[512]

public 
plugin_init() 
{
    
set_task(1.0"MySql_Init"
}

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 user_points (name varchar(32),points INT(11))")
    
    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 
plugin_end()
{
    
// free the tuple - note that this does not close the connection,
    // since it wasn't connected in the first place
    
SQL_FreeHandle(g_SqlTuple)
}

public 
Load_MySql(id)
{
    new 
szName[32], szTemp[512]
    
get_user_name(idszNamecharsmax(szName))
    
    new 
Data[1]
    
Data[0] = id
    
    format
(szTemp,charsmax(szTemp),"SELECT * FROM `user_points` WHERE (`user_points`.`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"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
    {
        
//.if there are no results found
        
        
new szName[32]
        
get_user_name(idszNamecharsmax(szName))
        new 
szTemp[512]
        
        
// now we will insturt the values into our table.
        
format(szTemp,charsmax(szTemp),"INSERT INTO `user_points` ( `name` , `points`)VALUES ('%s','0');"szName)
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    } 
    else 
    {
        
// if there are results found
        
points[id]         = SQL_ReadResult(Query1)
    }
    
    return 
PLUGIN_HANDLED
}

public 
Save_MySql(id)
{
    new 
szName[32], szTemp[512]
    
get_user_name(idszNamecharsmax(szName))
    
    
format(szTemp,charsmax(szTemp),"UPDATE `user_points` SET `points` = '%i' WHERE `user_points`.`name` = '%s';",points[id], szName)
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}

public 
IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
SQL_FreeHandle(Query)
    
    return 
PLUGIN_HANDLED
}

public 
client_putinserver(id)
{
    
Load_MySql(id)
}

public 
client_disconnect(id)
{
    
Save_MySql(id)



YamiKaitou 05-01-2013 18:13

Re: Save Points On mysql
 
Quote:

Originally Posted by oxygen935 (Post 1943920)
I did something with mysql but i have some debug errors...
Code:

L 05/01/2013 - 21:01:40: Start of error session.
L 05/01/2013 - 21:01:40: Info (map "de_dust2") (file "addons/amxmodx/logs/error_20130501.log")
L 05/01/2013 - 21:01:40: [MySQL] Thread worker was unable to start.
L 05/01/2013 - 21:01:40: [AMXX] Displaying debug trace (plugin "points.amxx")
L 05/01/2013 - 21:01:40: [AMXX] Run time error 10: native error (native "SQL_ThreadQuery")
L 05/01/2013 - 21:01:40: [AMXX]    [0] points.sma::register_client (line 526)


I'm told that is because you are calling a Threaded Query when the map is changing. There is a thread in the Issues section that has a workaround, or you can ignore the error or bug the AMXX Devs to fix it

Kia 05-04-2013 06:20

Re: Save Points On mysql
 
Try this :

PHP Code:

#include <amxmodx>
#include <sqlx>

new PLUGIN[]  = "Points"
new AUTHOR[]  = "Kia Armani"
new VERSION[] = "1.0"

new Host[]        = ""
new User[]        = ""
new Pass[]        = ""
new Db[]       = ""

new Points[33];
new 
bool:iStart[33];

new 
Handle:g_SqlConnection
new Handle:g_SqlTuple
new g_Error[512]
new 
g_authed[33]
new 
g_sql_ready false
new bool:g_loaded[33]

new 
maxplayers;

public 
plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
maxplayers get_maxplayers()
    
set_task(0.1"MySql_Init")
}

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
    g_SqlConnection 
SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    if(
g_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(g_SqlConnection,"CREATE TABLE IF NOT EXISTS pts (steamid varchar(32), name varchar(64), money INT(11))")
    
    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)
    
    
g_sql_ready true
    
for(new i=1i<=maxplayersi++)
    {
        if(
g_authed[i])
        {
            
Load_MySql(i)
        }
    }
}

public 
Load_MySql(id)
{
    if(
g_sql_ready)
    {
        if(
g_SqlTuple == Empty_Handle)
        {
            
set_fail_state(g_Error)
        }
        
        new 
szSteamId[32], szTemp[512]
        
get_user_authid(idszSteamIdcharsmax(szSteamId))
        
        new 
Data[1]
        
Data[0] = id
        
        
//we will now select from the table `furienmoney` where the steamid match
        
format(szTemp,charsmax(szTemp),"SELECT * FROM `pts` WHERE (`pts`.`steamid` = '%s')"szSteamId)
        
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"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)
    {
        
//.if there are no results found
        
        
new szSteamId[32], szName[32], szQuotedName[64]
        
get_user_authid(idszSteamIdcharsmax(szSteamId)) // get user's steamid
        
get_user_name(idszName31// get user's name
        
SQL_QuoteString(g_SqlConnectionszQuotedName63szName)
        
        
//  if its still pending we can't do anything with it
        
if (equal(szSteamId,"ID_PENDING"))
        {
            return 
PLUGIN_HANDLED
        
}
        
        new 
szTemp[512]
        
        
// now we will insert the values into our table.
        
format(szTemp,charsmax(szTemp),"INSERT INTO `coins` ( `steamid` , `name` , `pts`) VALUES ('%s','%s','0');",szSteamIdszQuotedName)
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    }
    else
    {
        
// if there are results found
        
Points[id]        = SQL_ReadResult(Query2)
    }
    
g_loaded[id] = true
    
return PLUGIN_HANDLED
}

public 
Save_MySql(id)
{
    if(
g_loaded[id])
    {
        new 
szSteamId[32], szTemp[512], szName[32], szQuotedName[64]
        
get_user_authid(idszSteamIdcharsmax(szSteamId))
        
get_user_name(idszName31)
        
SQL_QuoteString(g_SqlConnectionszQuotedName63szName)
        
        
// Here we will update the user hes information in the database where the steamid matches.
        
format(szTemp,charsmax(szTemp),"UPDATE `coins` SET `name` = '%s', `pts` = '%i' WHERE `coins`.`steamid` = '%s';",szQuotedName,Points[id],szSteamId)
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    }
}

public 
IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
SQL_FreeHandle(Query)
    
    return 
PLUGIN_HANDLED
}

public 
plugin_end()
{
    if(
g_SqlConnection != Empty_Handle)
    {
        
SQL_FreeHandle(g_SqlConnection//free connection handle here
    
}
}

public 
client_disconnect(id)
{
    
    
Save_MySql(id)
    
Points[id] = 0
    iStart
[id] = false
}

public 
client_authorized(id)
{
    
g_authed[id] = true
    Load_MySql
(id)



BaD CopY 07-31-2016 17:39

Re: Save Points On mysql
 
How to make top15 functions using mysql ?

Bugsy 08-01-2016 17:44

Re: Save Points On mysql
 
Code:

SELECT TOP 15 FieldValue
FROM YourTable
ORDER BY FieldValue ASC;



All times are GMT -4. The time now is 10:46.

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