AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQL_ThreadQuery error (https://forums.alliedmods.net/showthread.php?t=330128)

LithuanianJack 01-21-2021 13:58

SQL_ThreadQuery error
 
Hi. That error occurred after I updated my server's amxmodx to 1.9.0. What is need to be changed in the code? Thank you.

L 01/21/2021 - 19:50:28: [AMXX] Displaying debug trace (plugin "csgo-contracts.amxx", version "1.0")
L 01/21/2021 - 19:50:28: [AMXX] Run time error 10: native error (native "SQL_ThreadQuery")
L 01/21/2021 - 19:50:28: [AMXX] [0] csgo-contracts.sma::loadContracts (line 412)
L 01/21/2021 - 19:50:28: [AMXX] [1] csgo-contracts.sma::client_connect (line 82)
L 01/21/2021 - 19:50:28: [MySQL] Invalid info tuple handle: 0

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <engine>
#include <fakemeta>
#include <fakemeta_util>
#include <cstrike>
#include <fun>
#include <sqlx>
#include <csgo_skins>

#define MAX_CONTRACT_SKINS 1000

new const Float:chances_classification[][] = {
    {
0.01.0},
    {
1.02.0},
    {
2.03.0},
    {
3.04.0},
    {
4.05.0},
    {
5.010.0},
    {
10.015.0},
    {
15.020.0},
    {
20.025.0},
    {
25.030.0},
    {
30.035.0},
    {
35.040.0},
    {
40.045.0},
    {
45.050.0},
    {
50.055.0},
    {
55.060.0},
    {
60.065.0},
    {
65.070.0},
    {
70.075.0},
    {
75.080.0},
    {
80.085.0},
    {
85.090.0},
    {
90.095.0},
    {
95.0100.0}
}


new 
skins[33][MAX_CONTRACT_SKINS], pContracts[33];

new 
item;
new 
cvar_contracts_buycvar_contracts_costcvar_contract_min_skinscvar_contract_max_skinscvar_contract_lower_per_skin;
new 
Handle:tuple;
new 
bool:contractsLoaded[33];
public 
plugin_init()
{
    
register_plugin("CSGO SKINS: Contracts Menu""1.0""InvIs");

    
item cs_menu_additem("Contracts");
    
    
cvar_contracts_buy register_cvar("csgo_contract_buy_amount""5"); // How many contracts player will buy at once (0 - unlimited uses)
    
cvar_contracts_cost register_cvar("csgo_contract_cost""5000"); // How much contracts cost
    
cvar_contract_min_skins register_cvar("csgo_contract_minimum_skins""5"); // Minimum amount of skins to make a contract
    
cvar_contract_max_skins register_cvar("csgo_contract_maximum_skins""1000"); // Maximum amount of skins to make a contract
    
cvar_contract_lower_per_skin register_cvar("csgo_contract_percent_decrease""0.025"); // How many percents will decrease making a contract per skin
    
    
check_extra_plugin();
}
public 
csgo_register_menu_enter(menucommand[])
{
    if(
menu == item)
    {
        
register_clcmd(command"showContractsMenu");
    }
}
public 
csgo_client_info_loaded(idloadContracts(id);
public 
csgo_sql_connected()
{
    
tuple cs_get_sql_handle();
    new 
szQuery[256];
    
    
formatex(szQuerycharsmax(szQuery), "CREATE TABLE IF NOT EXISTS `player_contracts` (`player_id` varchar(64) COLLATE utf8_lithuanian_ci, `contracts` int(16), PRIMARY KEY(`player_id`))");
    
cs_send_sql_query(szQuery);
}
public 
client_connect(id)
{
    for(new 
iMAX_CONTRACT_SKINSi++) skins[id][i] = -1;
    
pContracts[id] = 0;
    
contractsLoaded[id] = false;
}
public 
csgo_on_client_reset(id)
{
    for(new 
iMAX_CONTRACT_SKINSi++) skins[id][i] = -1;
    
pContracts[id] = 0;
    
contractsLoaded[id] = false;
    
//loadContracts(id);
}
public 
OnCallBackMainMenu(idmenuitem)
{
    new 
counter getContractSkins(id);
    if(
item == 1)
    {
        if(
counter get_pcvar_num(cvar_contract_max_skins) && counter MAX_CONTRACT_SKINS) return ITEM_ENABLED;
        else return 
ITEM_DISABLED;
    }
    else if(
item == 2)
    {
        if(
counter get_pcvar_num(cvar_contract_min_skins) || pContracts[id] < 1) return ITEM_DISABLED;
        else return 
ITEM_ENABLED;
    }
    return 
ITEM_ENABLED;
}
public 
csgo_menuItemSelected(iditem_id) if(item_id == itemshowContractsMenu(id0)
public 
showContractsMenu(idpage)
{
    new 
szText[128];
    
formatex(szTextcharsmax(szText), "\d[\rCSGO\d] \wContracts\d: \r%d Contracts"pContracts[id]);
    new 
menu menu_create(szText"contractsMenuHandler");
    
    new 
callback menu_makecallback("OnCallBackMainMenu");
    
    
formatex(szTextcharsmax(szText), "Buy contract \d- \r$%d"get_pcvar_num(cvar_contracts_cost));
    
menu_additem(menuszText);
    
menu_additem(menu"\wAdd skin^n"__callback);
    
    new 
counter getContractSkins(id)
    
formatex(szTextcharsmax(szText), "\rMake contract^n^n\w-- \yContract skins \d(\y%d\d) \w--^n^n"counter);
    
menu_additem(menuszText__callback);
    
    new 
sztext[16];
    new 
skinName[64], menuLine[128], w_name[64], w_name_final[64];
    new 
Float:chance;
    new 
skin_id;
    for(new 
iMAX_CONTRACT_SKINSi++)
    {
        if(
skins[id][i] != -1)
        {
            
skin_id skins[id][i];
            
formatex(sztextcharsmax(sztext), "%d"i);
            
            
cs_get_skin_name(skin_idskinName);
            
cs_get_weapon_name(cs_get_skin_weapon_id(skin_id), w_name);
            
chance 0.0;
            
chance getSkinChance(skin_idid)
            
            if(
chance 75.0formatex(w_name_finalcharsmax(w_name_final), "\d%s"w_name);
            else if(
chance 50.0formatex(w_name_finalcharsmax(w_name_final), "\w%s"w_name);
            else if(
chance 15.0formatex(w_name_finalcharsmax(w_name_final), "\y%s"w_name);
            else 
formatex(w_name_finalcharsmax(w_name_final), "\r%s"w_name);
            
            
            
formatex(menuLinecharsmax(menuLine), "\w%s \d| %s"skinNamew_name_final);
            
            
            
menu_additem(menumenuLinesztext);
        }
    }

    
menu_display(idmenupage);
}
stock getContractSkins(id)
{
    new 
counter 0;
    for(new 
iMAX_CONTRACT_SKINSi++)
    {
        if(
skins[id][i] != -1counter++;
    }
    return 
counter;
}
stock Float:getSkinChance(iid)
{
    new 
Float:chance 0.0;
    new 
Float:tempChance;
    new 
counter;
    for(new 
jcs_get_chests_count(); j++)
    {
        
tempChance cs_get_skin_chance(ijid);
        
chance += tempChance;
        if(
tempChance 0.0counter++;
    }
    
    
chance /= counter;
    return 
chance;
}
stock getSkinsAddedToContract(idskin)
{
    new 
counter 0;
    for(new 
iMAX_CONTRACT_SKINSi++)
    {
        if(
skins[id][i] == skincounter++;
    }
    return 
counter;
}
public 
contractsMenuHandler(idmenuitem)
{
    if(
item == MENU_EXIT)
    {
        
menu_destroy(menu);
        
cs_open_main_menu(id);
        return 
PLUGIN_HANDLED;
    }
    
    switch(
item)
    {
        case 
0// Buy contract
        
{
            if(
cs_get_money(id) >= get_pcvar_num(cvar_contracts_cost))
            {
                
cs_set_money(idcs_get_money(id) - get_pcvar_num(cvar_contracts_cost))
                if(
get_pcvar_num(cvar_contracts_buy) == 0pContracts[id] += 1;
                else 
pContracts[id] += get_pcvar_num(cvar_contracts_buy);
                
saveContracts(id);
                
client_printcolor(id"/y[/ctrCSGO/y] You bought some contracts!");
            }
            else 
client_printcolor(id"/y[/ctrCSGO/y] You don't have enough money!");
            
showContractsMenu(iditem/7);
            return 
PLUGIN_HANDLED;
        }
        case 
1// Add skin
        
{
            
showSkinsMenu(id0);
            return 
PLUGIN_HANDLED;
        }
        case 
2// Make a contract
        
{
            new 
contractWillHappen true;
            new 
which 0Float:chance 0.0;
            new 
counter getContractSkins(id);
            new 
bool:skin_checked[MAX_CONTRACT_SKINS];
            for(new 
iMAX_CONTRACT_SKINSi++)
            {
                if(
skins[id][i] != -&& !skin_checked[i])
                {
                    
skin_checked[i] = true;
                    new 
skin_count cs_get_skin_count(idskins[id][i]);
                    if(
skin_count getSkinsAddedToContract(idskins[id][i]))
                    {
                        
contractWillHappen false;
                        break;
                    }
                    
                    
chance getSkinChance(skins[id][i], id);
                    
                    
which += addChanceClassification(chance);
                }
            }
            if(
contractWillHappen)
            {
                if(!
contractsLoaded[id])
                {
                    
showContractsMenu(iditem/7);
                    
client_printcolor(id"/y[/ctrCSGO/y] You need to wait for MySQL data to load first!");
                    return 
PLUGIN_HANDLED;
                }
                
which /= counter;
                new 
skin_id = -1;
                
                
client_print(idprint_chat"DEBUG: %d %d %d"whichcountersizeof(chances_classification));
                
                if(
which sizeof(chances_classification)) {
                    new 
temp which;
                    
/*while(skin_id == -1 && temp < sizeof(chances_classification)) {
                        skin_id = cs_pick_random_skin_percentage(chances_classification[which][0] - ( get_pcvar_float(cvar_contract_lower_per_skin) * counter ), chances_classification[temp][1]);
                        
                        temp++;
                    }*/
                    
                    
skin_id cs_pick_random_skin_percentage(chances_classification[which][0] - ( get_pcvar_float(cvar_contract_lower_per_skin) * counter ), chances_classification[temp][1]);
                    
                }
                                
                if(
skin_id != -1)
                {
                    for(new 
iMAX_CONTRACT_SKINSi++)
                    {
                        if(
skins[id][i] != -1)
                        {
                            
cs_remove_skin(idskins[id][i], 0);
                            
skins[id][i] = -1;
                        }
                    }
                    
                    if(
get_pcvar_num(cvar_contracts_buy) > 0pContracts[id]--;
                    
saveContracts(id);
                    
                    new 
s_name[64], w_name[64], name[64];
                    
get_user_name(idnamecharsmax(name));
                    
cs_get_skin_name(skin_ids_name);
                    
cs_get_weapon_name(cs_get_skin_weapon_id(skin_id), w_name);
                    
client_printcolor(0"/y[/ctrCSGO/y] /g%s /ymade a contract from /g%d /yskins and got a /g%s /y(/g%s/y) skin!"namecounters_namew_name);
                    
                    
cs_give_skin(idskin_id0);
                }
                else
                {
                    for(new 
iMAX_CONTRACT_SKINSi++) skins[id][i] = -1;
                    
client_printcolor(id"/y[/ctrCSGO/y] Something went wrong! Contract failed to make a skin!");
                }
            }
            else 
client_printcolor(id"/y[/ctrCSGO/y] Something went wrong! We didn't found some of your skins added to contract!");
            
showContractsMenu(iditem/7);
            return 
PLUGIN_HANDLED;
        }
    }
    
    new 
szData[6], szName[64];
    new 
_accessitem_callback;
    
menu_item_getinfomenuitem_accessszData,charsmaxszData ), szName,charsmaxszName ), item_callback );
    
    new 
index str_to_numszData );
    
skins[id][index] = -1;
    
    
showContractsMenu(iditem/7);
    return 
PLUGIN_HANDLED;
}
public 
addChanceClassification(Float:chance) {
    for(new 
isizeof(chances_classification); i++) {
        if(
chances_classification[i][0] <= chance <= chances_classification[i][1]) {
            return 
i;
        }
    }
    return 
0;
}
public 
showSkinsMenu(idpage)
{
    new 
menu menu_create("\d[\rCSGO\d] \wChoose skin\d:""skinsMenuHandler");
    
    new 
skinName[64], menuLine[128], szid[6], w_name[64], w_name_final[64], count;
    new 
Float:chancecount_added;
    for(new 
ics_get_skins_count(); i++)
    {
        
count cs_get_skin_count(idi);
        
count_added getSkinsAddedToContract(idi);
        if(
count-count_added 0)
        {    
            
formatex(szidcharsmax(szid), "%d"i);
            
cs_get_skin_name(iskinName);
            
cs_get_weapon_name(cs_get_skin_weapon_id(i), w_name);
            
chance getSkinChance(iid);
            
            if(
chance 75.0formatex(w_name_finalcharsmax(w_name_final), "\d%s"w_name);
            else if(
chance 50.0formatex(w_name_finalcharsmax(w_name_final), "\w%s"w_name);
            else if(
chance 15.0formatex(w_name_finalcharsmax(w_name_final), "\y%s"w_name);
            else 
formatex(w_name_finalcharsmax(w_name_final), "\r%s"w_name);
            
            
            
formatex(menuLinecharsmax(menuLine), "\w%s \d| %s \d- \r%d"skinNamew_name_finalcount-count_added);
            
menu_additem(menumenuLineszid);
        }
    }
    
    if(
menu_items(menu)) menu_display(idmenupage);
    else
    {
        
client_printcolor(id"/y[/ctrCSGO/y] You don't have any skins!");
        
showContractsMenu(id0);
    }
}
public 
skinsMenuHandler(idmenuitem)
{
    if(
item == MENU_EXIT)
    {
        
menu_destroy(menu);
        
showContractsMenu(id0);
        return 
PLUGIN_HANDLED;
    }
    
    new 
szData[6], szName[64];
    new 
_accessitem_callback;
    
menu_item_getinfomenuitem_accessszData,charsmaxszData ), szName,charsmaxszName ), item_callback );
    
    new 
skin_id str_to_numszData );
    if(
getContractSkins(id) < MAX_CONTRACT_SKINS)
    {
        new 
skins_added getSkinsAddedToContract(idskin_id);
        if(
skins_added cs_get_skin_count(idskin_id))
        {
            for(new 
iMAX_CONTRACT_SKINSi++)
            {
                if(
skins[id][i] == -1)
                {
                    
skins[id][i] = skin_id;
                    break;
                }
            }
        }
        else 
client_printcolor(id"/y[/ctrCSGO/y] You already added all of these skins!");
    }
    else
    {
        
client_printcolor(id"/y[/ctrCSGO/y] You have added maximum amount of skins to this contract!");
        
showContractsMenu(id0);
        return 
PLUGIN_HANDLED;
    }
    
showSkinsMenu(iditem/7);
    
    return 
PLUGIN_HANDLED;
}
public 
saveContracts(id)
{
    if(!
contractsLoaded[id]) return;
    new 
szQuery[256];
    new 
player_id[64];
    
cs_get_player_id(idplayer_id);
    
formatex(szQuerycharsmax(szQuery), "REPLACE INTO `player_contracts` (`player_id`, `contracts`) VALUES ('%s', '%d');"player_idpContracts[id]);
    
cs_send_sql_query(szQuery);
}
public 
loadContracts(id)
{
    static 
szQuery256 ];
    new 
player_id[64];
    
cs_get_player_id(idplayer_id);
    
formatexszQuery255"SELECT * FROM `player_contracts` WHERE `player_id` = '%s';"player_id);
    
    new 
iData[1];
    
iData] = id;
    
SQL_ThreadQuerytuple"QuerySelecPlayerContracts"szQueryiData);
}
public 
QuerySelecPlayerContractsiFailStateHandle:hQueryszError[ ], iErroriData[ ], iDataSizeFloat:fQueueTime 

    if( 
iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED 
    { 
        
log_amx"%s"szError );
        return;
    } 
    else 
    { 
        new 
id iData[0];
        
        new 
col_contracts_count SQL_FieldNameToNum(hQuery"contracts"
        while (
SQL_MoreResults(hQuery)) 
        {
            
pContracts[id] = SQL_ReadResult(hQuerycol_contracts_count);                    
            
SQL_NextRow(hQuery)
        }
        
contractsLoaded[id] = true;
    } 



JocAnis 01-21-2021 16:07

Re: SQL_ThreadQuery error
 
Could be a problem with making 'SQL_MakeDbTuple' but we dont see that part of code here
Could be a problem that you are loading players mysql info (in client_connect) before mysql db is being created/loaded on your server

Also shouldnt this be a integer?
Code:

formatex( szQuery, 255, "SELECT * FROM `player_contracts` WHERE `player_id` = '%s';", player_id);
->
formatex( szQuery, 255, "SELECT * FROM `player_contracts` WHERE `player_id` = '%d';", player_id);


r0ma 01-22-2021 01:22

Re: SQL_ThreadQuery error
 
Quote:

Originally Posted by JocAnis (Post 2733637)
Could be a problem with making 'SQL_MakeDbTuple' but we dont see that part of code here
Could be a problem that you are loading players mysql info (in client_connect) before mysql db is being created/loaded on your server

Also shouldnt this be a integer?
Code:

formatex( szQuery, 255, "SELECT * FROM `player_contracts` WHERE `player_id` = '%s';", player_id);
->
formatex( szQuery, 255, "SELECT * FROM `player_contracts` WHERE `player_id` = '%d';", player_id);


L 01/21/2021 - 19:50:28: [MySQL] Invalid info tuple handle: 0
the problem is he is trying to send the query before the connect to the database (Invalid tuple handle 0)

and player_id it's an array, it's fine to use %s

check if the main plugin who call "csgo_sql_connected" it's working


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

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