Jump to content

game problem!


Goose87

Recommended Posts

Hi everyone,

 

I am creating a text-based game at the moment.  This one is more for testing/learning purposes than creating a game that will work and go live.  It's online, but I'm not advertising it.  At the moment I've created a good basic core for the game, but this script is giving me REAL problems at the moment!

 

What happens is every 24 hours the person gets a "gem" given to them, and they can use this to upgrade their unit's levels.  What I'm trying to do is find a solution for creating a page where the person can upgrade their unit.  The only thing is that they can't upgrade their next unit until the first unit is level 3.  i.e. first unit is level 3, so second unit can go to level 2, and then when that's level 3, you can level 3rd unit to level 2 (they all start at level1).

 

The script below is what I've ended up with after changing the method time after time and getting nowhere.

 

The switch with a button just for the upgrade worked up until i had a condition that went like this:

 

switch...

case($unit1_level == 3 || $unit2_level == 3 || $unit3_level == 1);

....

 

It worked for just unit1 and 2 being below level 3, but second it went to the third, it failed.

 

Anyways, this is what I've ended up with!!

$username=$_SESSION['username']; 


$result=mysql_query("SELECT * FROM accounts WHERE username='$username'"); 
while($r=mysql_fetch_array($result)){
$unit1_level=$r['unit1_level'];
$unit2_level=$r['unit2_level'];
$unit3_level=$r['unit3_level'];
$unit4_level=$r['unit4_level'];
$unit5_level=$r['unit5_level'];
$unit1=$r['unit1'];
$unit2=$r['unit2'];
$unit3=$r['unit3'];
$unit4=$r['unit4'];
$unit5=$r['unit5'];
$gems=$r['gems'];
$gems_spent == 1;

if($gems_spent > $gems){
echo "You cannot upgrade with 0 gems";
header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/overview.php");
}else{
switch(TRUE) {
case($_POST['submit1']);
$unit1_level++;
break;
case($_POST['submit2']);
$unit2_level++;
break;
case($_POST['submit3']);
$unit3_level++;
break;
case($_POST['submit4']);
$unit4_level++;
break;
case($_POST['submit5']);
$unit5_level++;
break;
}	

mysql_query("UPDATE accounts SET unit1_level='$unit1_level', unit2_level='$unit2_level', 
unit3_level='$unit3_level', unit4_level='$unit4_level', unit5_level='$unit5_level' WHERE username='$username'");
echo "Unit1 = $unit1_level, unit2=$unit2_level, unit3=$unit3_level, unit4=$unit4_level, unit5=$unit5_level";

}
}

?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<table cellpadding="10">
<tr>
	<th>Unit</th>
	<th>Level</th>
	<th>Upgrade</th>
</tr>
<tr>
	<td align="center"><?php echo "$unit1"; ?></td>
	<td align="center"><?php echo "$unit1_level"; ?></td>
	<td align="center"><input type="submit" name="submit1" value="Purchase" /></td>
</tr>
<tr>
	<td align="center"><?php echo "$unit2"; ?></td>
	<td align="center"><?php echo "$unit2_level"; ?></td>
	<td align="center"><input type="submit" name="submit2" value="Purchase" /></td>
</tr>
<tr>
	<td align="center"><?php echo "$unit3"; ?></td>
	<td align="center"><?php echo "$unit3_level"; ?></td>
	<td align="center"><input type="submit" name="submit3" value="Purchase" /></td>
</tr>
<tr>
	<td align="center"><?php echo "$unit4"; ?></td>
	<td align="center"><?php echo "$unit4_level"; ?></td>
	<td align="center"><input type="submit" name="submit4" value="Purchase" /></td>
</tr>
<tr>
	<td align="center"><?php echo "$unit5"; ?></td>
	<td align="center"><?php echo "$unit5_level"; ?></td>
	<td align="center"><input type="submit" name="submit5" value="Purchase" /></td>
</tr>
</table>

 

The code there is clearly going to have problems in it. I've just gone mad to be honest trying to work it out!!

 

Any help at all would be greatly appreciated! Thanks.

 

Goose.

Link to comment
Share on other sites

You have an error in your Switch Statement. You are a little backwards in your thinking on it.

 

PSEUDO CODE:

switch($statementToEvaluate)

{

    case $statementToEvaluate Equals '1'

 

    break;

}

 

You have SWITCH(TRUE)

 

What is TRUE??

 

The only time any of your case statements will work is if it sais:

 

CASE TRUE:

  //do whatever

break;

 

Unless you have your submit buttons pass the value of "true"? even still, they can only pass a string value "true", not a boolean.

 

SO, here is what I would do.

 

Make your submit buttons have the same NAME, but different VALUES

 

<input type="submit" name="Unit" value="Unit1" />

<input type="submit" name="Unit" value="Unit2" />

<input type="submit" name="Unit" value="Unit3" />

<input type="submit" name="Unit" value="Unit4" />

<input type="submit" name="Unit" value="Unit5" />

 

<?php
switch($_POST['Unit'])
{
    case "Unit1":

    break;

    case "Unit2":

    break;

    case "Unit3":

    break;

    case "Unit4":

    break;

    case "Unit5":

    break;
}
?>

 

 

 

 

Link to comment
Share on other sites

Also, if you wanted to keep it the way you had it, you would need to compare boolean values, so you need to convert the statement string "true" to a boolean value of true.

Slap a isset() around the POST var

<?
switch(TRUE) {
case(isset($_POST['submit1']));
$unit1_level++;
break;
case(isset($_POST['submit2']));
$unit2_level++;
break;
case(isset($_POST['submit3']));
$unit3_level++;
break;
case(isset($_POST['submit4']));
$unit4_level++;
break;
case(isset($_POST['submit5']));
$unit5_level++;
break;
}	
?>

 

I still like my way above better though.

 

Let me know how this works out for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.