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

[Solved] Spawn with weapon selection from db


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 08-23-2015 , 13:44   [Solved] Spawn with weapon selection from db
Reply With Quote #1

I have been working on a plugin for my community where cts can open a gun menu and the selection is saved in a mysql db. I have gotten that part down but I am facing a problem when trying to spawn them with those weapons at the beginning of each round.

PHP Code:
public SQLGiveWeapon(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (!
IsClientInGame(data) || !IsPlayerAlive(data) || !IsClientAuthorized(data) || GetClientTeam(data) != 3)
    {
        return;
    }

    if (
hndl == INVALID_HANDLE)
    {
        
LogError("Query failed! %s"error);
        
CPrintToChat(data"[{blue}Guns{default}] {red}Query failed!{default}");        
    }
    
    else if (!
SQL_GetRowCount(hndl)) 
    {
        return;
    }
    
    new 
String:Primary[1256];
    new 
String:Secondary[1256];
    
    
SQL_FetchString(hndl0Primarysizeof(Primary));
    
SQL_FetchString(hndl1Secondarysizeof(Secondary));
    
    new 
weapon GetPlayerWeaponSlot(data0);
    
RemovePlayerItem(dataweapon);    
    
weapon GetPlayerWeaponSlot(data1);
    
RemovePlayerItem(dataweapon);    

    
ServerCommand("sm_weapon \"%N\" \"%s\""dataPrimary);    
    
ServerCommand("sm_weapon \"%N\" \"%s\""dataSecondary);
}

public 
Action:OnPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    
    if (
GetClientTeam(client) != 3)
    {
        return;
    }
    
    
decl String:query[128];
    
decl String:steamID[64];
            
    
GetClientAuthId(clientAuthId_Steam2steamIDsizeof(steamID));
    
    
Format(querysizeof(query), "SELECT Primary_Weapon, Secondary_Weapon FROM guardweaponchoice WHERE steamid = '%s'"steamID);
    
SQL_TQuery(dbSQLGiveWeaponqueryclient);

This is what the database looks like:


There are no errors being logged.

Last edited by Addicted.; 08-24-2015 at 09:36. Reason: Solved
Addicted. is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 08-23-2015 , 15:13   Re: Spawn with weapon selection from db
Reply With Quote #2

you might need to add weapon_ either in the db or in the ServerCommand
xerox8521 is offline
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 08-23-2015 , 15:30   Re: Spawn with weapon selection from db
Reply With Quote #3

Quote:
Originally Posted by xerox8521 View Post
you might need to add weapon_ either in the db or in the ServerCommand
No the weapon plugin I am using working without the weapon_ prefix.

https://forums.alliedmods.net/showthread.php?t=195476
Addicted. is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 08-24-2015 , 05:55   Re: Spawn with weapon selection from db
Reply With Quote #4

Replace
PHP Code:
    else if (!SQL_GetRowCount(hndl)) 
    {
        return;
    } 
by
PHP Code:
    else if (!SQL_FetchRow(hndl)) 
    {
        return;
    } 
And you should have a "return" in
PHP Code:
if (hndl == INVALID_HANDLE

Last edited by h3bus; 08-24-2015 at 07:23.
h3bus is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 08-24-2015 , 09:02   Re: Spawn with weapon selection from db
Reply With Quote #5

I solved this.
1st, no SQL_FetchRow(hndl); before
Code:
SQL_FetchString(hndl, 0, Primary, sizeof(Primary)); 
SQL_FetchString(hndl, 1, Secondary, sizeof(Secondary));
2nd, no check if player don't have weapon.
Code:
new weapon = GetPlayerWeaponSlot(data, 0); 
RemovePlayerItem(data, weapon);     
weapon = GetPlayerWeaponSlot(data, 1); 
RemovePlayerItem(data, weapon);
Kailo is offline
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 08-24-2015 , 09:36   Re: Spawn with weapon selection from db
Reply With Quote #6

Thanks for the great help!

PHP Code:
public SQLGiveWeapon(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (!
IsClientInGame(data) || !IsPlayerAlive(data) || !IsClientAuthorized(data) || GetClientTeam(data) != 3)
    {
        return;
    }

    if (
hndl == INVALID_HANDLE)
    {
        
LogError("Query failed! %s"error);
        
CPrintToChat(data"[{blue}Guns{default}] {red}Query failed!{default}");        
    }
    
    else if (!
SQL_GetRowCount(hndl)) 
    {
        return;
    }
    
    new 
String:Primary[1256];
    new 
String:Secondary[1256];
    
    
SQL_FetchRow(hndl);
    
SQL_FetchString(hndl0Primarysizeof(Primary));
    
SQL_FetchString(hndl1Secondarysizeof(Secondary));
    
    new 
weapon = -1;
    if ((
weapon GetPlayerWeaponSlot(data0)) != -1)
        
RemovePlayerItem(dataweapon);    
    if ((
weapon GetPlayerWeaponSlot(data1)) != -1)
        
RemovePlayerItem(dataweapon);    

    if (
strlen(Primary))
        
ServerCommand("sm_weapon \"%N\" \"%s\""dataPrimary);
    if (
strlen(Secondary))
        
ServerCommand("sm_weapon \"%N\" \"%s\""dataSecondary);

Addicted. is offline
Darkness_
Veteran Member
Join Date: Nov 2014
Old 08-24-2015 , 12:00   Re: [Solved] Spawn with weapon selection from db
Reply With Quote #7

PHP Code:
ServerCommand("sm_weapon \"%N\" \"%s\""dataPrimary); 
Here you are targeting their them by name, which is dangerous. Instead target them by the userid that corresponds to their client index. Like so
PHP Code:
ServerCommand("sm_weapon \"#%i\" \"%s\""GetClientUserId(data), Primary); 
I explained why this practice is so important here.
Darkness_ is offline
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 08-24-2015 , 12:08   Re: [Solved] Spawn with weapon selection from db
Reply With Quote #8

Quote:
Originally Posted by Darkness_ View Post
PHP Code:
ServerCommand("sm_weapon \"%N\" \"%s\""dataPrimary); 
Here you are targeting their them by name, which is dangerous. Instead target them by the userid that corresponds to their client index. Like so
PHP Code:
ServerCommand("sm_weapon \"#%i\" \"%s\""GetClientUserId(data), Primary); 
I explained why this practice is so important here.
Just changed it, I think I had seen your post before but I had forgot about it.

Thanks for the heads up.
Addicted. 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 12:35.


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