Location: PHPKode > projects > Crown of Evanion > final/spellbook.php
<?php
/*
*   This file is part of 'Crown of Evanion'.
*
*    'Crown of Evanion' is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    'Crown of Evanion' is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with 'Crown of Evanion'; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
$title = "Spellbook";
include("include.php");
if(!$Username) {
	header("location: login.php");
}
$errors = array(
'1' => "<h5>This is not your spell.</h5>",
'2' => "<h5>Not enough mana.</h5>",
'3' => "<h5>No such dragon to cast your spell on.</h5>",
'4' => "<h5>You can't heal that dragon, it already has full health.</h5>",
'5' => "<h5>You can't heal that dragon to that health level.</h5>",
'6' => "<h5>$Dragon is already at full health.</h5>",
'7' => "<h5>You can't attack your own dragon!</h5>",
'8' => "<h5>That dragon does not enough health to be attacked.</h5>",
);


if(!$page) {
	echo "<div align=\"center\">
	<h3>Spellbook</h3>
	<p>This is a book contaning the instructions for casting the spells you know. Be careful with them.</p>
	$errors[$error]
	";
	$select = mysql_query("SELECT * FROM spellbooks WHERE owner = '$UserID'");
	while ($spell = mysql_fetch_array($select)) {
		$sel = mysql_query("SELECT * FROM spells WHERE id = '$spell[spell]'");
		$spe = mysql_fetch_array($sel);
		echo "<p class=\"drag\">
		<a href=\"$PHP_SELF?page=cast&id=$spell[id]\">$spe[name] - $spell[mana] mana</a></p>";
	}
}

if($page == "cast") {
	$select = mysql_query("SELECT * FROM spellbooks WHERE id = '$id'");
	$start = mysql_fetch_array($select);
	
	if($start[owner] != $UserID) {
		header("location: $PHP_SELF?error=1");
		die;
	}
	$select = mysql_query("SELECT * FROM spells WHERE id = '$start[spell]'");
	$spell = mysql_fetch_array($select);
	
	if($start[mana] > $MaxMana) {
		header("location: $PHP_SELF?error=2");
		die;
	}
	
	if($spell[type] == "heal" && !$who) {
		echo "<div align=\"center\">
		<form action=\"$PHP_SELF\" method=\"GET\">
		<input type=\"hidden\" name=\"page\" value=\"cast\">
		<input type=\"hidden\" name=\"id\" value=\"$id\">
		<p>Who would you like to cast $spell[name] upon?</p>
		<input type=\"text\" name=\"who\">
		<input type=\"submit\" value=\"Cast!\">
		</form>
		</div>
		";
	}
	if($spell[type] == "heal" && $who) {
		$select = mysql_query("SELECT * FROM dragons WHERE name LIKE '$who'");
		$num = mysql_num_rows($select);
		
		if(!$num) {
			header("location: $PHP_SELF?error=3");
			die;
		}
		
		$healed = mysql_fetch_array($select);
		
		if($healed[health] == $healed[maxhealth]) {
			header("location: $PHP_SELF?error=4");
			die;
		}
		
		$newhp = $healed[health]+$spell[heal];
		
		if($newhp > $healed[maxhealth]) {
			$newhp = $healed[maxhealth];
		}
		
		if($newhp <= 0) {
			header("location: $PHP_SELF?error=5");
			die;
		}
		
		$newmana = $Mana-$start[mana];
		
		if($newmana < 0) {
			header("location: $PHP_SELF?error=2");
			die;
		}
		
		mysql_query("UPDATE users SET mana = '$newmana' WHERE id = '$UserID'");
		mysql_query("UPDATE dragons SET health = '$newhp' WHERE name = '$healed[name]'");
		echo "<div align=\"center\">You cast a spell upon $who, and they now have $newhp health!</div>";	
	}
	
	if($spell[type] == "healself") {
		$newhp = $spell[heal]+$HP;
		
		if($newhp > $MaxHP) {
			$newhp = $MaxHP;
		}
		
		if($HP == $MaxHP) {
			header("location: $PHP_SELF?error=6");
			die;
		}
		
		$newmana = $Mana-$start[mana];
		
		if($newmana < 0) {
			header("location: $PHP_SELF?error=2");
			die;
		}
		
		mysql_query("UPDATE users SET mana = '$newmana' WHERE id = '$UserID'");
		mysql_query("UPDATE dragons SET health = '$newhp' WHERE name = '$Dragon'");
		echo "<div align=\"center\">You feel your magical abilities drain into $Dragon, making them healthier.</div>";
	}

	if($spell[type] == "offence" && !$who) {
		echo "<div align=\"center\">
		<form action=\"$PHP_SELF\" method=\"GET\">
		<input type=\"hidden\" name=\"page\" value=\"cast\">
		<input type=\"hidden\" name=\"id\" value=\"$id\">
		<p>Who would you like to cast $spell[name] upon?</p>
		<input type=\"text\" name=\"who\">
		<input type=\"submit\" value=\"Cast!\">
		</form>
		</div>
		";
	}
	
	if($spell[type] == "offence" && $who) {
		$who = $_GET['who'];
		$select = mysql_query("SELECT * FROM dragons WHERE name LIKE '$who'");
		$num = mysql_num_rows($select);
		
		if(!$num) {
			header("location: $PHP_SELF?error=3");
			die;
		}
		
		$attacked = mysql_fetch_array($select);
		
		if($who == $Dragon) {
		header("location: $PHP_SELF?error=7");
		die;
		}
		
		if($attacked[health] <= 0) {
			header("location: $PHP_SELF?error=8");
			die;
		}
		$select = mysql_query("SELECT defense FROM items WHERE id = '$attacked[armor]'");
		$enarm = mysql_fetch_array($select);
	
		$AS = $spell[damage];
		$DS = round($Stren / 3.5);
		$ET = round($attacked[tough] / 1.5);
		$EA = $enarm[defense];
		
		$dam1 = $AS+$DS;
		$dam2 = $ET+$EA;
		$dam = $dam1-$dam2;
		
		if($dam < 0) {
			$dam = 0;
		}	
		$newmana = $Mana-$start[mana];
		
		if($newmana < 0) {
			header("location: $PHP_SELF?error=2");
			die;
		}
		
		mysql_query("UPDATE users SET mana = '$newmana' WHERE id = '$UserID'");
		
		$select = mysql_query("SELECT id,dragon FROM users WHERE dragon = '$who'");
		$wtf = mysql_fetch_array($select);
		mysql_query("UPDATE dragons SET health = health-$dam WHERE name = '$attacked[name]'");
		mysql_query("INSERT INTO event (foruser,text) VALUES ('$wtf[id]','$wtf[dragon] has been attacked by $Username with the $spell[name] spell, doing $dam damage.')"); 
		
		$select = mysql_query("SELECT health FROM dragons WHERE name = '$who'");
		$final = mysql_fetch_array($select);
		if($final[health] < 0) {
			mysql_query("UPDATE dragons SET health = '0' WHERE name = '$who'");
		}
		echo "You have done $dam damage to $who.";
	}

	if($spell[type] == "item") {
		$newmana = $Mana-$start[mana];
		
		if($newmana < 0) {
			header("location: $PHP_SELF?error=2");
			die;
		}	
		mysql_query("UPDATE users SET mana = '$newmana' WHERE id = '$UserID'");
		mysql_query("INSERT INTO useritems (owner,itemid) VALUES ('$UserID','$spell[item]')");
		echo "You feel the magic exit your body and form into a hard shape. You now have a new item.";
	}		
}	
		
	
	

include("footer.php");
?>
Return current item: Crown of Evanion