Jump to content
MakeWebGames

[mccode] Crystal Market - Buy Amount of Crystals, Not All


hobbes

Recommended Posts

Here's a script for the Crystal Market that allows you to buy a certain amount of crystals rather than all of them at once (as in the stock mccode). I am sure many experienced scripters can update this to make it more simple, but it works and I haven't had any problems with it.

First things first, open your cmarket.php and add the following case statement at the top:

 

case "get":

get_crystals();

break;

Here are the edited crystal_buy and crystal_get functions that I am using. Place these in your cmarket.php script.

This one is an edited version of the crystal_buy function. You can replace the whole function with this one.

 

function crystal_buy()

{

global $db,$ir,$c,$userid,$h;

$q=$db->query("SELECT * FROM crystalmarket cm WHERE cmID={$_GET['ID']}");

//non-existant crystals

if(!$db->num_rows($q))

{

print "Error, either these crystals do not exist, or they have already been bought.

";

$h->endpage();

exit;

}

$r=$db->fetch_row($q);

//crystal price

$price=$r['cmPRICE'] / $r['cmQTY'];

$id=($_GET['ID']);

print

"How many crystals would you like to buy?

You can buy a maximum of {$r['cmQTY']} crystals for $ \$".number_format($price)." each.

 

<form name='buy' action='cmarket.php?action=get&ID={$id}' method='post'>

<input type='int' name='qty' class='texta' size='8'>

<input type='submit' value='Buy Crystals'>

</form>";

}

And the new function, get_crystals:

 

function get_crystals()

{

global $db,$ir,$c,$userid,$h;

$q=$db->query("SELECT * FROM crystalmarket cm WHERE cmID={$_GET['ID']}");

//non-existant tokens

if(!$db->num_rows($q))

{

print "Error, either these crystals do not exist, or they have already been bought.

";

$h->endpage();

exit;

}

$r=$db->fetch_row($q);

$id=($_GET['ID']);

$qty=($_POST['qty']);

$price=($r ['cmPRICE'] / $r['cmQTY']);

$total=($qty * $price);

$cmprice=($r['cmPRICE'] - $total);

$cmqty=($r['cmQTY'] - $qty);

//not enough money

if($total > $ir['money'])

{

die("You do not have enough money to purchase {$qty} crystals.");

}

if(!is_numeric($qty))

{

die('Your quantity must be numeric! (and no white spaces)');

}

if(!isset($qty))

{

die('You must enter how many crystals you would like to purchase.');

}

if($qty > $r['cmQTY'])

{

die('There are not that may crystals available to purchase');

}

$db->query("UPDATE users SET crystals=crystals+{$qty} where userid=$userid");

$db->query("UPDATE crystalmarket SET cmQTY={$cmqty} WHERE cmID={$id}");

$db->query("UPDATE crystalmarket SET cmPRICE={$cmprice} WHERE cmID={$id}");

if($cmqty < 1)

{

$db->query("DELETE FROM crystalmarket WHERE cmID={$id}");

}

$db->query("UPDATE users SET money=money-{$total} where userid=$userid");

$db->query("UPDATE users SET money=money+{$total} where userid={$r['cmADDER']}");

$db->query("INSERT INTO clogs VALUES('',$userid,{$r['cmADDER']},{$qty},{$total},unix_timestamp());",$c);

event_add($r['cmADDER'],"{$ir['username']} bought {$qty} crystals from the market for \$".number_format($total).".",$c);

print "You bought the {$qty} crystals from the market for \$".number_format($total).".";

}

My version is slightly different as I use something other than crystals, but I have edited it down to a pure mccode stock substitute.

This is my first php script post on CE, so be nice. :roll:

Link to comment
Share on other sites

Re: Crystal Market - Buy Amount of Crystals, Not All

 

I don't have time to edit it but * is a bad idea mate, sorry about not having time to edit it.

Why is it a bad idea to buy a set amount rather than a large lump sum? Many games allow players to buy a certain amount of crystals, points, etc. Could you be a bit more specific?

Not quite sure why it's not working for you Neji, as long as it directs to the buy function in cmarket, the following should display how many 'crystals' you want to buy.

 

print

"How many crystals would you like to buy?

You can buy a maximum of {$r['cmQTY']} crystals for $ \$".number_format($price)." each.

 

<form name='buy' action='cmarket.php?action=get&ID={$id}' method='post'>

<input type='int' name='qty' class='texta' size='8'>

<input type='submit' value='Buy Crystals'>

</form>";

Link to comment
Share on other sites

Re: Crystal Market - Buy Amount of Crystals, Not All

i got it to add mate but when i went on the cmarket i had to click on buy to type in a amount of how many i wanted and then once i typed in the box i got a query error but it still let me buy the crystals how do i stop that error can anyone plz help me with it

Link to comment
Share on other sites

  • 3 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...