Weapon saving -
vernz - 08.05.2016
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 ?
Re: Weapon saving -
PrO.GameR - 08.05.2016
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,1: slot = 0;
case 2 .. 9: slot = 1;
case 10 .. 15: slot = 10;
case 16 .. 18, 39: slot = 8;
case 22 .. 24: slot =2;
case 25 .. 27: slot = 3;
case 28, 29, 32: slot = 4;
case 30, 31: slot = 5;
case 33, 34: slot = 6;
case 35 .. 38: slot = 7;
case 40: slot = 12;
case 41 .. 43: slot = 9;
case 44 .. 46: slot = 11;
}
return slot;
}
Re: Weapon saving -
vernz - 08.05.2016
This is exactly what I did yesterday but that way:
PHP код:
for(new i; i < 13; i++)
{
GetPlayerWeaponData(playerid, i, weaponid, ammo);
if(!weaponid) continue;
mysql_format(mysql, query, sizeof(query), "INSERT INTO weapons VALUES (%d, %d, %d, %d) ON DUPLICATE KEY UPDATE Slot=%d", PlayerInfo[playerid][pID], weaponid, ammo, i, i);
mysql_pquery(mysql, query);
}
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 ?
Re: Weapon saving -
PrO.GameR - 08.05.2016
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.
Re: Weapon saving -
vernz - 08.05.2016
So it would be like:
PHP код:
mysql_format(mysql, query, sizeof(query), "INSERT INTO weapons VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE Slot=%d", PlayerInfo[playerid][pID], weaponid, ammo, i);
Then to the select part like that:
PHP код:
mysql_format(mysql, query, sizeof(query), "SELECT Weaponid, Ammo, Slot FROM weapons WHERE Userid=%d", PlayerInfo[playerid][pID]);
mysql_tquery(mysql, query, "OnPlayerLoadWeapons", "i", playerid);
OR
that :
PHP код:
mysql_format(mysql, query, sizeof(query), "SELECT Ammo, Slot FROM weapons WHERE Userid=%d", PlayerInfo[playerid][pID]);
mysql_tquery(mysql, query, "OnPlayerLoadWeapons", "i", playerid);
Re: Weapon saving -
vernz - 08.05.2016
So it would be like:
PHP код:
mysql_format(mysql, query, sizeof(query), "SELECT Weaponid, Ammo, Slot FROM weapons WHERE Userid=%d", PlayerInfo[playerid][pID]);
mysql_tquery(mysql, query, "OnPlayerLoadWeapons", "i", playerid);
Then:
PHP код:
mysql_format(mysql, query, sizeof(query), "INSERT INTO weapons VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE Slot=%d", PlayerInfo[playerid][pID], weaponid, ammo, i);
mysql_tquery(mysql, query, "", "");
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
Re: Weapon saving -
PrO.GameR - 08.05.2016
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
|
Re: Weapon saving -
vernz - 08.05.2016
Got it thank you ! +rep