Jump to content

Adding Items Through a Website Script [ PHP + MySQL ]


Recommended Posts

Posted
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b][color=red]Introduction
[/color][/b]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Hello. I was wondering how to add items (vote coins - for example) to a player's inventory, using PHP and MySQL. I know this is possible, but I have had no luck finding a simpler solution than the one I have.

My solution (so far) is as follows. It is sloppy, but works flawlessly.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b][color=red]Method
[/color][/b]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

1. [b]Execute a MySQL "SELECT" query[/b] on the characters.character_inventory table that reads the values for characters.character_inventory.item and sorts them descending. This gives the script the last added item.
(1 MySQL "SELECT" statement)


2. [b]Execute another PHP for() loop[/b] that does multiple MySQL "SELECT" queries on the characters.character_inventory table, this time only selecting the values for characters.character_inventory.bag that are not equal to a result that we found before to get all of the character's bags . Bags are returned as '0', and up to four others, by characters.character_inventory.bag
(4 MySQL "SELECT" statements)


3. [b]Execute up to five PHP for() loops[/b] that each do MySQL "SELECT" queries to find the first available bag slot for a character, by the bag number found in (2). The loop starts with the first bag, and the first slot (up to the maximum slotted bag, 36) and loops until it reaches the last slot of the final bag.
(Up to 180 MySQL "SELECT" statements).


4. [b]Executes two MySQL "INSERT INTO" queries[/b], one that adds an entry to characters.character_inventory and another that adds an entry to characters.item_instance
(2 MySQL "INSERT INTO" statements)

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b][color=red]Examples
[/color][/b]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b]These elements.....[/b]
• Character GUID = 31
• The last entry added to characters.item_instance was given a GUID of 100
• The character has four bags, as well as their inventory. ID's are: 0, 1001, 1002, 1003, and 1004.
• The character has no slots open in their default bag; the second bag has room.
• The item to be added to the character has an ID of 10000 in the mangos.item_template table.

[b]result in this output.[/b]
1. [10000]
2. [0] [1001] [1002] [1003] [1004]
3. [12]
4. [(adds the item to the database's tables)]

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b][color=red]Problem
[/color][/b]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The only problem I foresee would be rare, but might still pose a problem once I attain a higher population than 10 players. The problem would lie in the fact that the items that are deleted from players' inventories are deleted from MySQL. If there were more than (example) 250 items deleted from characters' inventories (server-wide ; the items would have to all be the last (example) 300 items added to the table) and no new items added and then someone used this PHP script, the GUID of the newly item added.

[b]Example :[/b]
1. Items with the GUID range of 100 to 500 are added to players' inventory.
2. More than 250 of those items are deleted, all in order, 249 being the first item in the table now.
3. The PHP script runs the MySQL "SELECT" statement, getting the entries in descending order. This returns the value 249.
4. It then adds the item as GUID 250.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b][color=red]Conclusion
[/color][/b]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

[b]Does this matter? All help is appreciated.[/b]

I have no problem sharing this script with the MaNGOS community. I will make a reply with the complete source code and instructions, once I have this working to the best of its ability.

Thank you.



Chris

Posted

Wow dude, that's SO much easier than the heartache I was getting over MySQL. :D

Thank you.

Edit: Wait... What? You can't use .additem in SOAP.. Well, I can't at least.

Chris

Posted

Hmm, you can't ? I thought you can but apparently not then.

You could send the items in a mail though, through SOAP that is. Or with my external_mail patch.

And another option is to implement an additem for the SOAP yourself.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use