Thread: Need Help Sql !
View Single Post
SpliN
Junior Member
Join Date: Jun 2020
Location: Egypt
Old 04-21-2021 , 17:27   Re: Need Help Sql !
Reply With Quote #5

Here

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#include <hamsandwich>
#include <ColorChat>

//i used pragma to use the modules of sqlx
//i really dont know which one is using but i requested and loaded both

#pragma reqlib sqlite
#pragma reqlib mysql

#pragma loadlib sqlite
#pragma loadlib mysql

new steamid[35][33],points[33],error[512],Handle:connection
new Handle:g_sqldb,g_error[512]
new 
data[40],permhud

public plugin_init()
{
    
register_plugin("Points System" "1.0" ,  "SpliN")
    
//get host and user and pass and database to Make a Database Tuple
    
new host[64],user[64],pass[64],db[64]
    
get_cvar_string("amx_sql_host",host,63)
    
get_cvar_string("amx_sql_user",user,63)
    
get_cvar_string("amx_sql_pass",pass,63)
    
get_cvar_string("amx_sql_db",db,63)
    
//Hooking Dead event
    
register_event("DeathMsg","playerdeath_event","a")
    
register_concmd("amx_points","givepoints",ADMIN_IMMUNITY,"Usage: amx_points <player> <points>")
    
//Make A DataBase Tuble To use it to thread a query
    
g_sqldb SQL_MakeDbTuple(host,user,pass,db)
    
permhud CreateHudSyncObj();
    
//We Here Thread The Query Or Basically Executing Our Sql Code
    
SQL_ThreadQuery(g_sqldb,"handle","CREATE TABLE IF NOT EXISTS info (steamid VARCHAR(255),points INT(11);")
}

public 
client_putinserver(id)
{
    
//after 3 seconds show the synchud of points
    
set_task(3.0,"ShowSyncHud",id)
}

public 
ShowSyncHud(id)
{
    
//Show Points in synchud
    
set_hudmessage(0255850.760.9126.012.0)
    
ShowSyncHudMsg(id,permhud,"Points: %s",points)
    
}

public 
plugin_end()
{
    
//It is Important To Free The Handle At The end Of The Plugin
    
SQL_FreeHandle(g_sqldb)

}

public 
client_connect(id)
{
    
//connect To The DataBase
    
new errorcode
    
new connection SQL_Connect(g_sqldb,errorcode,error,511)
    
//If Connection Field Stop a Plugin With Showing a Message
    
if(connection == Empty_Handle
    {
    
set_fail_state(error
    }
    
//We Here Used Formatex To Format The Text We Want And It is Sql Code To Prepare and execute it to get the data we want
    
new cache[100]
    
formatex(cache,99,"SELECT points FROM info WHERE steamid = '%s'",steamid[id])
    new 
Handle:query SQL_PrepareQuery(connection,cache)
    
//If There is Problem In The Sql Code Stop The Plugin With Showing a Message
    
if(!SQL_Execute(query))
    {
        
SQL_QueryError(query,g_error,511)
        
set_fail_state(g_error)
    }
    
//If There Is A result Save it Into The Variable data
    
new data
    
while(SQL_MoreResults(query))
    {
        
data SQL_ReadResult(query,0)
        
SQL_NextRow(query)
    }
    
points[id] = data
    
//Free Our Query And Our Connection
    
SQL_FreeHandle(query)
    
SQL_FreeHandle(connection)
}

public 
client_authorized(id)
{
    
// Get Steamid We Will Use it to get the data and saving it also you can use name or anything make this client Private to get the data
    //But The Best Thing And Secure To Use Steamid
    
get_user_authid(id,steamid[id],charsmax(steamid[]))
}

public 
givepoints(id)
{
    
//Simple Admin Command We , i used register_concmd up
    
new arg1[40],arg2[40],name[50],targetname[50]
    
read_argv(1,arg1,39)
    
read_argv(2,arg2,39)
    
get_user_name(id,name,49)
    new 
target cmd_target(id,arg1)
    
get_user_name(target,targetname,49)
    new 
pts str_to_num(arg2)
    
points[target] += pts
    ColorChat
(0,GREEN,"^x01[ADMIN] %s gave %s %i Points",name,targetname,pts)
}

public 
client_disconnect(id)
{
    new 
cache[100]
    
formatex(cache,charsmax(cache),"INSERT INTO info(steamid,points) VALUES(%s,%i);",steamid[id],points[id])
    
SQL_ThreadQuery(g_sqldb,"handle",cache)
}

public 
playerdeath_event(id)
{
    
//get the killer and victim and if it was headshot or not
    
new killer read_data(1)
    new 
victim read_data(2)
    new 
headshot read_data(3)
    new 
weapon get_user_weapon(id)
    
//if headshot give him 30
    
if(headshot)
    {
        
points[killer] += 30
        
    
}
    
//if headshot and Killed With Awp give him 35
    
else if(headshot && weapon == CSW_AWP)
    {
        
points[killer] += 35
    
}
    
//if Killed with awp give 30
    
else if(weapon == CSW_AWP)
    {
        
points[killer] += 30
    
}
    
//if killed with knife give him 35
    
else if(weapon == CSW_KNIFE)
    {
        
points[killer] += 35
    
}
    
//if headshot and killed with the knife give him 40
    
else if(weapon == CSW_KNIFE && headshot)
    {
        
points[killer] += 40
    
}
    
//if Normal Kill Give Him 15
    
else
    {
        
points[killer] += 15
    
}
}
// Handle Of SQL_TheradQuery
public handle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
//if query failed 
    
if(FailState == TQUERY_QUERY_FAILED)
    {
        
console_print(0,"Query Failed !")

    }
    
//if Connection Failed
    
if(FailState == TQUERY_CONNECT_FAILED)
    {
        
console_print(0,"Connection Failed !")
    }

SpliN is offline