A. Monsters to not delete from the database upon death
B. Monsters not to have their HP changed in the database
C. To not pass on the monster type through the address line
D. And to record where the player is in a fight, so even if they close the browser, when they come back they'll come back to the fight just where they left off!
here is some coding you can plug in for that!
Part of how it works is that two new variables are added to the players table in the database. They are
enemyid
enemyhpoints
I used medium integers, length 3, for both. But you can adjust that. And assuming you've done tutorials 1 to 5, you know how to add these variable to the player database.
What these do is make a copy of the enemy id number and hitpoints, and these things will be changed or deleted when the monsters hurt or killed, rather than the monsters entry in the database. And for people more familiar with coding you might already get this, but I thought I'd describe it here for newer people like myself.
Okay, now for the code. The changes are in attack.php and battle.php. I've commented them showing in the comments where a replacement or addition has been made (and also a line of ////////// where the replacement/addition ends).
So you could either copy them whole or just find the changes and put them into your code.
attack.php
Code: Select all
<?php
include_once 'connect.php';
session_start();
if (isset($_SESSION['player']))
{
$player=$_SESSION['player'];
}
else
{
echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
exit;
}
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
// Addition
// this grabs the enemies id from the player database
$playerenemyid = $playerinfo3['enemyid'];
/////////////////////////////////////////////////////
// Replacement
// this looks at $playerenemyid instead of the command line
//if (isset($_GET['creature']))
//{
//$creature=$_GET['creature'];
//$creatureinfo="SELECT * from creatures where name = '$creature'";
if ($playerenemyid > 0)
{
$creatureinfo="SELECT * from creatures where id = '$playerenemyid'";
///////////////////////////////////////////////////////////////////
$creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);
}
else
{
echo "<a href='battle.php'>No Creature selected. Go Back!";
exit;
}
$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];
// Addition
$playerenemyhpoints = $playerinfo3['enemyhpoints'];
///////////
$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];
///////////////////////players turn////////////////////
echo "<u> " . $playerinfo3['name'] . "'s Attack</u><br>";
$playerattack = rand(1,20) + $playerattack;
$creaturedefense = rand(1,20) + $creaturedefense;
echo $playerinfo3['name'] . "'s Attack roll is " . $playerattack . "<br>";
echo $creature . "'s defense roll is " . $creaturedefense. "<br>";
if ($playerattack > $creaturedefense)
{
echo $playerinfo3['name'] . " hits! <br>";
$playerdamage = rand(1,6);
// Replacement
// $newcreaturehp = $creaturehp - $playerdamage;
// Because we want to use the copy of the monsters hitpoints that is attached
// to the players stats
$newcreaturehp = $playerenemyhpoints - $playerdamage;
/////////////////////////////////////////////////////////////////////////////
echo "For " . $playerdamage . " points of damage. <br>";
if ($newcreaturehp < 1)
{
echo "The " . $creature . " has been killed";
// Removal
// This is exactly one of the things this mod is about - ceasing to delete
// monsters from the database. Though perma killing monsters is pretty cool!
// $updatecreature="DELETE from creatures where name='$creature' limit 1";
//mysql_query($updatecreature) or die("Could not update creature");
$updatecreature="update players set enemyid='0' where name='$player' limit 1";
mysql_query($updatecreature) or die("Could not update creature");
//////////////////////////////////////////////////////////////////////////////
echo "<a href='battle.php'>Go Back";
exit;
}
// Replacement
// This replaces changing the monsters hitpoints in the database and instead
// just changes the copy attached to player data
//$updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
//mysql_query($updatecreature) or die("Could not update creature");
$updateplayer="update players set enemyhpoints='$newcreaturehp' where name='$player' limit 1";
mysql_query($updateplayer) or die("Could not update playerenemyhp");
/////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
echo $playerinfo3['name'] . " misses!<br>";
}
//////////////////////creatures turn //////////////////
echo "<u> " . $creature . "'s Attack</u><br>";
$creatureattack = rand(1,20) + $creatureattack;
$playerdefense = rand(1,20) + $playerdefense;
echo $creature . "'s Attack roll is " . $creatureattack . "<br>";
echo $playerinfo3['name'] . "'s defense roll is " . $playerdefense . "<br>";
if ($creatureattack > $playerdefense)
{
echo $creature . " hits! <br>";
$creaturedamage = rand(1,6);
$newplayerhp = $playerhp - $creaturedamage;
echo "For " . $creaturedamage . " points of damage. <br>";
if ($newplayerhp < 1)
{
echo $playerinfo3['name'] . " has been killed<br>";
echo "<a href='gameover.php'>Continue";
exit;
}
$updateplayer="update players set hpoints='$newplayerhp' where name='$player'";
mysql_query($updateplayer) or die("Could not update player");
}
else
{
echo $creature . " misses!";
}
// Replacement
// Because were not using the command line to pass on creature id anymore
// echo "<br><br><a href='battle.php?creature=$creature'>Battle Again!";
echo "<br><br><a href='battle.php>Battle Again!";
/////////////////////////////////////////////////////////////////////////
?>
Code: Select all
<?php
include_once 'connect.php';
session_start();
if (isset($_SESSION['player']))
{
$player=$_SESSION['player'];
}
else
{
echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
exit;
}
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];
// Addition - this is getting the enemy hitpoints into memory from the DB
$playerenemyhpoints = $playerinfo3['enemyhpoints'];
$playerenemyid = $playerinfo3['enemyid'];
/////////////////////////////////////////////////////////////////////////
// Replacement
// this uses 0 to represent when no monster id is present
// This wont work out if you have a monster with id 0
// Replacing the following...
//if (isset($_GET['creature']))
//{
// $creature=$_GET['creature'];
// $creatureinfo="SELECT * from creatures where name = '$creature'";
if ($playerenemyid > 0)
{
$creatureinfo="SELECT * from creatures where id = '$playerenemyid'";
//////////////////////////////////////////////////////////////////////
$creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);
}
else
{
$creatureinfo="SELECT * from creatures order by rand() limit 1";
$creatureinfo2=mysql_query($creatureinfo) or die("could get a creature!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);
// Addition
// this takes the newly chosen random creatures id & hitpoints and puts it into
// the players 'enemyid' and 'enemyhpoints' integer records in the DB
// this acts as a copy of the monster that the player fights against
// so the original copy wont be changed or deleted
$creatureid = $creatureinfo3['id'];
$updateplayer="update players set enemyid='$creatureid' where name='$player' limit 1";
mysql_query($updateplayer) or die("Could not update player enemyhpoints");
// now for the hitpoint
$creaturehp = $creatureinfo3['hpoints'];
$updateplayer="update players set enemyhpoints='$creaturehp' where name='$player' limit 1";
mysql_query($updateplayer) or die("Could not update player enemyhpoints");
// The line below updates the integer so it shows properly when printed below
$playerenemyhpoints = $creaturehp;
//////////////////////////////////////////////////////////////////////////////////////
}
$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];
/////player info
echo "<u> " . $playerinfo3['name'] . "</u><br>";
echo "Hit points = " . $playerhp . "<br>";
echo "Attack = " . $playerattack . "<br>";
echo "Defense = " . $playerdefense . "<br><br><br>";
///////creature info
echo "<u> " . $creatureinfo3['name'] . "</u><br>";
// Replacement
// replacing echo "Hit points = " . $creaturehp . "<br>";
// Since were using $playerenemeyhpoints to make it distinct from $creaturehp
echo "Hit points = " . $playerenemyhpoints . "<br>";
//////////////
echo "Attack = " . $creatureattack . "<br>";
echo "Defense = " . $creaturedefense . "<br><br><br>";
// Replacement
// Because we don't need to pass on the creature name anymore
// echo "<a href='attack.php?creature=$creature'>Attack!";
echo "<a href='attack.php'>Attack!";
/////////////////////////////////////////////////////////////
?>

Oh, and here's the entry for it on my blog, in case you want to visit it

.