Raised This Month: $51 Target: $400
 12% 

[L4D2]Adding database to point system


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dtor
Member
Join Date: Nov 2015
Old 10-31-2017 , 03:06   [L4D2]Adding database to point system
Reply With Quote #1

Since McFluffy is offline for awhile and I don't know how to contact to him , so I decided to move on and add some Database stuff into his code and try to make his famous PointSystem work with database.
Link : https://forums.alliedmods.net/showthread.php?p=1257852
The compiler is fine , just some warning and not fatal error or error
The database setup is quite fine too , but when put the smx to plugin folder , the plugins did not create any tables in database.
Any suggestion will be welcome.
Thanks

PHP Code:
//DATABASE
//Database handle
new Handle:db INVALID_HANDLE;
//Database configuration name convar
new Handle:g_hDbConfName;
//DATABASE_CONVAR
    
g_hDbConfName CreateConVar(
        
"l4d_pointsys_db_conf_name" ,
        
"default" ,
        
"The DB connection configuration read from databases.cfg." ,
        
FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_NOTIFY );
//=============================
//    DATABASE
//=============================

//create database connection
public ConnectDB()
{
    new 
String:confname[32];
    
GetConVarString(g_hDbConfNameconfnamesizeof(confname));
    
        if (
SQL_CheckConfig(confname))
  {
    new 
String:error[256];
    
db SQL_Connect(confnametrueerrorsizeof(error));

        if (
db == INVALID_HANDLE)
        
LogError("Failed to connect to database: %s"error);
        else
            
InitDB(); //Initialize database
  
}
        else
    
LogError("Database.cfg missing '%s' entry!"confname);
}

//create table if necessary
public InitDB()
{
  new 
String:error[256], String:query[1024];

    
Format(querysizeof(query), "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
        
"CREATE TABLE IF NOT EXISTS `Syspoints` (",
        
"  `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,",
        
"  `steamid` VARCHAR(32) NOT NULL,",
        
"  `CPoint` TINYINT NOT NULL,",
        
"  INDEX (`id`),",
        
"  INDEX (`steamid`))",
        
"  DEFAULT CHARSET=utf8",
        
";"
    
);

  if (!
SQL_FastQuery(dbquery))
  {
    if (
SQL_GetError(dberrorsizeof(error)))
      
LogError("Failed to create table: %s"error);
    else
      
LogError("Failed to create table: unknown");
  }
}

//get default values for connected player
public Start(client)
{
    if (
db == INVALID_HANDLE)
    {
        
points[client] = GetConVarInt(StartPoints);
        return;
    }
    
    new 
String:authid[32];
    
    
GetClientAuthString(clientauthidsizeof(authid));
    
  
decl String:query[256];
  
Format(querysizeof(query), "SELECT CPoint FROM Syspoints WHERE steamid = '%s'"authid);
  
SQL_TQuery(dbSQLPS_GetPointsqueryclient);
}

//callback from SaveDefault
public SQLPS_GetPoints(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (
hndl == INVALID_HANDLE)
    {
        
LogError("SQLPS_GetPoints / error = \"%s\""error);
        return;
    }
        
    new 
String:authid[32], String:query[256];

    if (
SQL_GetRowCount(hndl) > 0)
    {
        
GetClientAuthString(dataauthidsizeof(authid));
      
Format(querysizeof(query), "UPDATE Syspoints SET CPoints = '%d' WHERE steamid = '%s'"points[data], authid);
    }
    else
    {
        
GetClientAuthString(dataauthidsizeof(authid));
      
Format(querysizeof(query), "INSERT INTO Syspoints (CPoints, steamid) VALUES ('%d')"points[data], authid);
    }
    
    
CloseHandle(hndl);
    
  
SQL_TQuery(dbSQLPS_GetPointsFinalizequery);
}

//callback from SQLSaveDefault
public SQLPS_GetPointsFinalize(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (
hndl == INVALID_HANDLE)
    {
        
LogError("SQLPS_GetPointsFinalize / error = \"%s\""error);
        return;
    }

    
CloseHandle(hndl);


Last edited by Dtor; 11-02-2017 at 07:12. Reason: Changed idea of title
Dtor is offline
Dtor
Member
Join Date: Nov 2015
Old 11-02-2017 , 06:56   Re: Plugins not work with database
Reply With Quote #2

Trying to implement some code from perkmod2 with db , tweaking and fixing
These code is unfinished and for now the only thing this code do is get default point to survivor if the data of that player on sql is not exist.
PHP Code:
//=============================
//    DATABASE
//=============================

//create database connection
InitDB(&Handle:dbcn)


    
// Errormessage Buffer 
    
new String:Error[255]; 
     
    
// COnnect to the DB 
    
dbcn SQL_Connect("dbtest"trueErrorsizeof(Error)); 
     
     
    
// If something fails we quit 
    
if(dbcn == INVALID_HANDLE
    { 
        
SetFailState(Error); 
        
LogError("DATABASE FAILED TO CONNECT!"); 
    } 

    
//Create tables QUERY
    
new String:query[255];
    
Format(querysizeof(query), "CREATE TABLE IF NOT EXISTS `db_test`(`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, `steamid` VARCHAR(32) NOT NULL, `var` TINYINT NOT NULL, INDEX (`id`), INDEX (`steamid`)) DEFAULT CHARSET=utf8;");
    
LogError("Created tabels!");

    
//QUERY Init
    
SQL_FastQuery(dbcnquery);

}

//get default values for connected player
public Start(client)
{
    if (
db == INVALID_HANDLE)
    {
        
points[client] = GetConVarInt(StartPoints);
        return;
    }
    
    new 
String:authid[32];
    
    
GetClientAuthString(clientauthidsizeof(authid));
    
  
decl String:query[256];
  
Format(querysizeof(query), "SELECT var FROM "db_test" WHERE steamid = '%s'"authid);
  
SQL_TQuery(dbSQLpointsqueryclient);
}

//callback from Get default point for connected player
public SQLpoints(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (
hndl == INVALID_HANDLE)
        
LogError("SQLpoints  error = \"%s\""error);

    if (
hndl != INVALID_HANDLE && SQL_FetchRow(hndl))
    {
        
//if any of the perks are set to 0, set default values
        
points[data] = SQL_FetchInt(hndl0);
        if (
points[data]==0)
            
points[data] = GetConVarInt(StartPoints);

        
points[data]=0;

        
CloseHandle(hndl);
    }
    else
    
points(client);


Last edited by Dtor; 11-02-2017 at 07:11.
Dtor is offline
Reply



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 11:50.


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