Weapon saving
#1

Hi, I followed this: https://sampforum.blast.hk/showthread.php?tid=505081

It works perfectly but I was trying to figure out how to update the values when it's the same weapon slot, by example If I have an ak which is slot 5, and I am gettin an m4 which is also slot 5, it will create a new row and the ammo will just duplicate, any way I can juste update when it's the same slot ? for the new weaponid and ammo ?
Reply
#2

Hey, nice to see someone actually normalizing their tables, however I suggest adding a 4th column in your table called slot, make it unique (and un-unique the weaponid if it is) then you can use the 'INSERT INTO ... ON DUPLICATE ..) for same slots
here is a function to know which slot each weapon belongs (credits to Hiddos)
PHP код:
GetWeaponSlot(weaponid)
{
new 
slot;
switch(
weaponid)
{
case 
0,1slot 0;
case 
.. 9slot 1;
case 
10 .. 15slot 10;
case 
16 .. 1839slot 8;
case 
22 .. 24slot =2;
case 
25 .. 27slot 3;
case 
282932slot 4;
case 
3031slot 5;
case 
3334slot 6;
case 
35 .. 38slot 7;
case 
40slot 12;
case 
41 .. 43slot 9;
case 
44 .. 46slot 11;
}
return 
slot;

Reply
#3

This is exactly what I did yesterday but that way:

PHP код:
for(new i13i++)
        {
            
GetPlayerWeaponData(playeridiweaponidammo);
            if(!
weaponid) continue;
            
mysql_format(mysqlquerysizeof(query), "INSERT INTO weapons VALUES (%d, %d, %d, %d) ON DUPLICATE KEY UPDATE Slot=%d"PlayerInfo[playerid][pID], weaponidammoii);
            
mysql_pquery(mysqlquery);
        } 
But I didnt un-unique the weaponid, however, I want it to also update the weaponid, ammo and slot so it is right the way I am doing it ?
Reply
#4

Yes you are doing it right as far as I see, but no need to update slot since you are measuring your uniqueness with it, also weaponid would be unique either way since you can't have a weapon in some other type of slot, and make sure you actually select playerid+slot (and not the player+slot+weaponid or any of them alone) and make them unique together, making slot(or any other column for that matter) unique by itself will make your inserts fail in some cases you don't want them for example when you have slot 10 saved for my playerid you saving your own weaps will fail on slot 10.
Reply
#5

So it would be like:

PHP код:
mysql_format(mysqlquerysizeof(query), "INSERT INTO weapons VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE Slot=%d"PlayerInfo[playerid][pID], weaponidammo,  i); 
Then to the select part like that:

PHP код:
mysql_format(mysqlquerysizeof(query), "SELECT Weaponid, Ammo, Slot FROM weapons WHERE Userid=%d"PlayerInfo[playerid][pID]);
    
mysql_tquery(mysqlquery"OnPlayerLoadWeapons""i"playerid); 
OR

that :

PHP код:
mysql_format(mysqlquerysizeof(query), "SELECT Ammo, Slot FROM weapons WHERE Userid=%d"PlayerInfo[playerid][pID]);
    
mysql_tquery(mysqlquery"OnPlayerLoadWeapons""i"playerid); 
Reply
#6

So it would be like:

PHP код:
mysql_format(mysqlquerysizeof(query), "SELECT Weaponid, Ammo, Slot FROM weapons WHERE Userid=%d"PlayerInfo[playerid][pID]);
    
mysql_tquery(mysqlquery"OnPlayerLoadWeapons""i"playerid); 
Then:

PHP код:
mysql_format(mysqlquerysizeof(query), "INSERT INTO weapons VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE Slot=%d"PlayerInfo[playerid][pID], weaponidammo,  i);
            
mysql_tquery(mysqlquery""""); 
I made userid+slot unique and un-unique weaponid, also userid has the primary key and right now, it doesn't update the current weapon but add a new one when its a different slot.


Sorry for double reply, the first time I couldn't see it then I replied back but now both showed up
Reply
#7

Select query is right, insert one however should be
Quote:

"INSERT INTO weapons VALUES (%d, %d, %d, %d) ON DUPLICATE KEY UPDATE Weaponid=%d , Ammo=%d"

Edit: select one could be this too, since we really don't need the slot to giveplayerweapon
Quote:

SELECT Weaponid, Ammo FROM weapons WHERE Userid=%d

Reply
#8

Got it thank you ! +rep
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)