Trade System

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
Tim
Posts: 37
Joined: Fri Jun 10, 2011 12:49 am

Trade System

Post by Tim »

Hello community,

This is my first post here. I have been developing my own web based game for a few months now after completing Halls' browser based game video series. I never did anything in PHP before this, so I'm a total noob, but I think I'm finally starting to get the hang of it.

What I'm recently trying to do it a trade system similar to the "Grand Exchange" on Runescape (horrible game, I know ... but a very nice trade design concept). With this UserA can go to the Trade screen and say I would like to buy or sell 'x' of 'y' item for 'z' price. This then gets logged into a db table.

Code: Select all

$update_trade = "INSERT into trade (username, stance, quantity, item_name, price) VALUES ('$username','$trade_stance','$trade_quantity','$trade_item_name','$trade_price')";
mysql_query($update_trade) or die("System: Could not update the trade table.");
So say I want to buy a 3 Vegetables for 15 gold coins. In the database it would show up as:

Code: Select all

Tim | Buying | 3 | Vegetables | 15
Now that's working perfectly. Now I made a new user called "test" and I have him selling what I (Tim) am wanting to buy. So now the table looks like this.

Code: Select all

Tim | Buying | 3 | Vegetables | 15
test | Selling | 3 | Vegetables | 15
Again, that works perfectly. Now I want to make a cron job to cross check the stances and item names in this table. This is where I run into my problem. Here is what I have so far, and it's not working. Can anyone think of another way I can do this, or possibly alter this script to make it work correctly?

Code: Select all

<?php
include_once 'connect.php';
?>

<?php

$buying = "Buying";
$selling = "Selling";

$buying_info_1 = "SELECT * from trade where stance = '$buying'";
$buying_info_2 = mysql_query($buying_info_1) or die ("System: Could not get trade information.");

$selling_info_1 = "SELECT * from trade where stance = '$selling'";
$selling_info_2 = mysql_query($selling_info_1) or die("System: Could not get trade information.");


while($selling_info_3 = mysql_fetch_array($selling_info_2) && $buying_info_3 = mysql_fetch_array($buying_info_2))
{
	$buying_username = $buying_info_3['username'];
	$selling_username = $selling_info_3['username'];
	
	$buying_item = $buying_info_3['item_name'];
	$selling_item = $selling_info_3['item_name'];

	if ($buying_item == $selling_item)
	{
		echo $selling_username . " is selling what " . $buying_username . " is buying.";
	}
}
?>
I hope you guys understand what I'm trying to do. If not, just ask and I will do my best to explain it better.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Trade System

Post by Jackolantern »

What do you mean "cross-checking"? I can't really tell what you are trying to do without knowing more about the system, really. It seems you are running a cron to make sure the trade system is working correctly? If so, I applaud the pro-active coding practices, as most newer coders don't do those types of things nearly enough.

What error are you getting, or what is going on?

Also, I really, really, really, really, really cannot stress enough that you need to be commenting your code. Not just for getting help on the forums, but because you will come back to your code in a few months and will be clueless on how it works, no matter how well you understand it now. I promise you ;)
The indelible lord of tl;dr
Tim
Posts: 37
Joined: Fri Jun 10, 2011 12:49 am

Re: Trade System

Post by Tim »

Thank you for the prompt response. By cross-checking I mean I want the system to go through the entire database table and select all the rows that have a stance of buying and remember them. Then I want the system to go through the entire database table and select all the rows that have a stance of selling and remember them. Once both these arrays are declared, I want the system to go down the list and cross-check and in the example I gave above say "Tim is buying 3 Vegetables for 15 gold and Test is selling 3 Vegetables for 3 gold, we have a match, let's do the trade". After that it should swap the items and increase/reduce the amount of gold that the players have and take the buying/selling listing out of the table.

In the example code it's just giving me a blank screen. I'm almost positive it has something to do with my while loop. To be honest I'm not 100% sure on how that while loop/array works anyways. I took the example from the Inventory piece of code that Halls' wrote. "Select all from this table that matches this criteria and display it all until there is nothing left" is what I got out of it, so I'm not sure why it wouldn't work with two arrays?

For instance, on the trade.php page I also have what the user is currently trying to buy or sell using this code:

Code: Select all

$trade_info_1 = "SELECT * from trade where username = '$username'";
$trade_info_2 = mysql_query($trade_info_1) or die ("System: Could not get trade information.");
while($trade_info_3 = mysql_fetch_array($trade_info_2))
	{
		echo "<tr><td><span id = 'text'><center>$trade_info_3[option]</center></span></td>";
		echo "<td><span id = 'text'><center>$trade_info_3[quantity]</center></span></td>";
		echo "<td><span id = 'text'><center>$trade_info_3[item_name]</center></span></td>";
		echo "<td><span id = 'text'><center>$trade_info_3[price]</center></span></td>";
	}
Also, thank you for your suggestion about commenting code. I really need to go through it all and do this. I have already ran into sections where I'm saying to myself "Hmmm, what was I thinking here? Why am I doing this instead of this?"

I hope you can help. Thanks again for your time.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Trade System

Post by Jackolantern »

Ohhh, I think I may have spotted something. In the condition of your WHILE loop, change "mysql_fetch_array()" to "mysql_fetch_row()". It looks like you want to loop through all the results and have the WHILE loop exit when the results have each been processed. That is not the way mysql_fetch_array() works, so you would want to use mysql_fetch_row(). The only thing that gets a little hard to keep up with is the fact that fetch_row returns a numerically indexed array rather than an associative array. That means, for example, the first field returned (aka the first column returned, which is actually your first column if you are querying for all of the columns) would be something like $results[0] instead of $results["playerid"]. Yes, not as user-friendly, but very workable.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Trade System

Post by Xaleph »

Just a small comment, but if you are selling items and the quantity is more then 1, be sure to price the individual items and not only the sum of it. This way you can sell 100 potions, but is noone required to buy a total of 100 potions. Becuase you know the quantity price, people can buy 10 potions. See my point?
Tim
Posts: 37
Joined: Fri Jun 10, 2011 12:49 am

Re: Trade System

Post by Tim »

I think I may have figured out another way of doing it. This seems to be working for me so far.

Code: Select all

<?php
include_once 'connect.php';
?>

<?php
$buying = "Buying";
$selling = "Selling";

$buying_info_1 = "SELECT * from trade where stance = '$buying'";
$buying_info_2 = mysql_query($buying_info_1) or die("System: Could not get user inventory.");

while ($buying_info_3 = mysql_fetch_array($buying_info_2))
{
	$item_name = $buying_info_3['item_name'];
	
	$selling_info_1 = "SELECT * from trade where stance = '$selling' && item_name = '$item_name'";
	$selling_info_2 = mysql_query($selling_info_1) or die("System: Could not get user inventory.");
	$selling_info_3 = mysql_fetch_array($selling_info_2);
	
	echo $buying_info_3['username'] . " is buying " . $buying_info_3['item_name'];
	echo "<br>";
	echo $selling_info_3['username'] . " is selling " . $selling_info_3['item_name'];
	echo "<br>";
	
	if ($buying_info_3['item_name'] == $selling_info_3['item_name'])
	{
		echo "We have a match!";
		echo "<br><br>";
	}
}
?>
My cron.php output is:

Code: Select all

Tim is buying Blue Lupine
test is selling Blue Lupine
We have a match!

test is buying Air Essences
Tim is selling Air Essences
We have a match!
Just gotta keep playing with it, but I'm happy I finally figured out a possible foundation for it so I can expand on it this weekend while I'm off from work. I will keep you guys updated. :)

@Xaleph: I was totally just thinking the same thing before reading your post. Usually I will code something once as a rough draft and then totally recode the entire document to clean up and add/delete/change things. I will definitely change the total price variable to a price per item to make everything easier on the user, and on me during coding and also comment everything as Jackolantern suggested. :)

@Jackolantern: Thanks for your suggestion. I did not know about fetching rows from a table. I already know where I am going to implement this.
Post Reply

Return to “Advanced Help and Support”