Raised This Month: $ Target: $400
 0% 

SQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Floody
Member
Join Date: Jan 2012
Old 07-17-2013 , 13:07   SQL
Reply With Quote #1

Hello,
I am new to SQL in SourcePawn so I want to know something... :-D

I want to print a message to everyone if a player join the game which is in the database and I also want that players which are in the database will get +10 hp when they spawn.

Maybe you can give me a short snippet or ex?

Thanks in advance,
Floody is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 07-17-2013 , 13:24   Re: SQL
Reply With Quote #2

PHP Code:
#pragma semicolon 1

#include <sourcemod>
#include <sdktools>

#define DB_TABLE_PREFIX "tbl_"
#define DB_CONFIG_NAME "yourDBname"

new Handle:hDataBase INVALID_HANDLE;
new 
Handle:hUpdateTimer INVALID_HANDLE;

new 
String:strStorage[PLATFORM_MAX_PATH];
new 
Handle:hStorage INVALID_HANDLE;

public 
Plugin:myinfo = {
    
name "Hi",
    
author "Leonardo",
    
description "",
    
version "0.0",
    
url ""
};

public 
OnPluginStart()
{
    
HookEvent"player_activate"OnPlayerActivateEventHookMode_Post );
    
HookEvent"player_spawn"OnPlayerSpawnEventHookMode_Post );
    
    if( !
SQL_CheckConfigDB_CONFIG_NAME ) )
        
SetFailState"Can't connect to DB: '%s' config not found in databases.cfg"DB_CONFIG_NAME );
    
decl String:strError[256];
    
hDataBase SQL_ConnectDB_CONFIG_NAMEtruestrErrorsizeof(strError) );
    if( 
hDataBase == INVALID_HANDLE )
        
SetFailState("Can't connect to DB: %s"strError);
    
    
SQL_LockDatabasehDataBase );
    
SQL_FastQueryhDataBase"SET NAMES \"UTF8\";" );
    
SQL_UnlockDatabasehDataBase );
}

public 
OnMapStart()
{
    
hUpdateTimer CreateTimer60.0Timer_UpdateData_TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE );
    
TriggerTimerhUpdateTimer );
}

public 
OnMapEnd()
    
KillTimerhUpdateTimer );

public 
OnPlayerActivateHandle:hEvent, const String:strEventName[], bool:bDontBroadcast )
    
TriggerTimerhUpdateTimer );

public 
Action:Timer_UpdateDataHandle:hTimer )
{
    new 
String:strQuery[2048], String:strCond[1024], String:strSteamID[32];
    
    for( new 
iClient 1iClient <= MaxClientsiClient++ )
        if( 
IsValidClient(iClient) )
        {
            
GetClientAuthStringiClientstrSteamIDsizeof(strSteamID) );
            
FormatstrCondsizeof(strCond), "%s%s`steamid` = '%s'"strCondstrlen(strCond) > ?" OR " ""strSteamID );
        }
    
    if( 
strlen(strCond) > )
    {
        
FormatstrQuerysizeof(strQuery), "SELECT `steamid` WHERE %s AND `enabled` <> 0;"DB_TABLE_PREFIXstrCond );
        
SQL_TQueryhDataBaseSQL_GetDatastrQuery );
    }
    
    return 
Plugin_Continue;
}

public 
OnPlayerSpawnHandle:hEvent, const String:strEventName[], bool:bDontBroadcast )
    
CreateTimer0.0Timer_OnPlayerSpawnGetEventInthEvent"userid" ) );

public 
Action:Timer_OnPlayerSpawnHandle:hTimnerany:iUserID )
{
    new 
iClient GetClientOfUserIdiUserID );
    if( !
IsValidClient(iClient) || !IsPlayerAlive(iClient) )
        return;
    
    if( 
hStorage == INVALID_HANDLE )
        return;
    
    
decl String:strBuffer[32];
    
GetClientAuthStringiClientstrBuffersizeof(strBuffer) );
    
    
KvRewindhStorage );
    if( !
KvJumpToKeyhStoragestrBufferfalse ) || !KvGetNumhStorage"enabled") )
        return;
    
    
SetEntityHealthiClientGetClientHealthiClient ) + 10 );
}

public 
SQL_GetDataHandle:hOwnerHandle:hQuery, const String:strError[], any:data )
{
    if( 
hStorage != INVALID_HANDLE )
        
CloseHandlehStorage );
    
hStorage INVALID_HANDLE;
    
    if( 
hQuery == INVALID_HANDLE )
    {
        
LogError"SQL Error: %s"strlen(strError)>0?strError:"Unknown" );
        return;
    }
    
    if( 
SQL_GetRowCount(hQuery) <= )
        return;
    
    
hStorage CreateKeyValues("storage");
    
    while( 
SQL_FetchRowhQuery ) )
    {
        
iFlags SQL_FetchInthQuery);
        if( 
iFlags <= )
            continue;
        
        
KvRewindhStorage );
        
        
// steam id
        
SQL_FetchStringhQuery0strBuffersizeof(strBuffer) );
        
KvJumpToKeyhStoragestrBuffertrue );
        
        
KvSetNumhStorage"enabled");
    }
    
    
KvRewindhStorage );
}

public 
SQL_ErrorCheckHandle:hOwnerHandle:hQuery, const String:strError[], any:data )
    if( 
hQuery == INVALID_HANDLE )
        
LogError"SQL Error: %s"strlen(strError)>0?strError:"Unknown" );

stock bool:IsValidClientiClient )
{
    if( 
iClient <= ) return false;
    if( 
iClient MaxClients ) return false;
    if( !
IsClientConnected(iClient) ) return false;
    if( !
IsClientInGame(iClient) ) return false;
    return !
IsFakeClient(iClient);

__________________

Last edited by Leonardo; 07-17-2013 at 13:25.
Leonardo is offline
Floody
Member
Join Date: Jan 2012
Old 07-17-2013 , 20:33   Re: SQL
Reply With Quote #3

Thanks so much! But is there an easier way to do an action only if the client is in the database?
Floody 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 06:17.


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