Location: PHPKode > projects > Dominant Creature RPG > class/hunt_class.php
<?php

require_once ("data_class.php");



class Raid extends Data {





	function Init() {



		$this->GetUserData();



	}



	function FindTargets() {



		echo "<table width='300' align='right' border='0'>";

		echo "<tr><td colspan='2' style='margin-bottom: 2px; border-bottom: 1px solid #ccc;'>Search for opponents: </td></tr>";





		if (preg_match("/[^0-9]+/", $_GET["pg"]))

		{

			echo "<table align='center'><tr><td>Invalid action or no creatures exist</td></tr></table>";

			exit;

		}



		//some security check



		if (isset($_GET["pg"]))

		{



			$pg = $_GET["pg"];

			if ($pg == 1) {$start = 0;} else {$start = ($pg - 1)  * 10;}

			$end = $start + 10;

		}

		else {

			$pg = 1;

			$start = 0;

			$end = $start + 10;

		}





		$back = $pg - 1;

		$next = $pg + 1;



		if ($back < 1) {$back = 1;}



		$t = mysql_query("

		SELECT 

		name, 

		id, 

		user_id,

		level 

		FROM 

		player_creatures 

		WHERE 

		user_id != '".$this->creature[user_id]."' 

		AND

		hp > 0 

		ORDER by level ASC  

		LIMIT $start, $end");



		/*

		 * There is no need to create redundant GUI buttons which won't be used immediately :)

		 *

		 * 	<a href='?p=action&c=".$this->opp[$i][id]."'><b>

		 *  <img src='img/mglass.gif' border='0' title='Sneak closer and inspect creature...'></b></a>

		 *

		 */



		$i = 1;

		$max = mysql_num_rows($t);

		while ($i <= $max) {

			$this->opp[$i] = mysql_fetch_assoc($t);

			echo "<tr><td>

		".$this->opp[$i][name]." (".$this->opp[$i][level]. ") </td><td>

		 <a href='?p=attack&c=".$this->opp[$i][user_id]."'><b>

		 <img src='img/sword.gif' border='0' title='Attack creature...'></b></a>

		 <a href='?p=msg&write=".$this->opp[$i][user_id]."'>

		 <img src='img/msg.gif' border='0' title='Write message to ".$this->opp[$i][name]."'>

		 </a></td></tr>";

			$i++;

		}



		echo "<tr><td colspan='2' align='center' style='margin-bottom: 2px; border-top: 1px solid #ccc;'> ...<a href='?p=duel&pg=$back'>prev</a> | <a href='?p=duel&pg=$next'>next</a>... </td></tr>";

		echo "</table>";





	}



	function DuelDetails() {



		$huntID = $_GET["s"];



		if (empty($huntID) OR preg_match("/[^0-9]+/", $huntID)) {

			echo "<table align='center'><tr><td>Invalid action or no hunt exists</td></tr></table>";}



			else {

					

				// here goes big, clumsy data preparation :)

					

				//echo $huntID;



				$q = mysql_query("

		SELECT

			*

		FROM

			creature_hunts

		WHERE

			id = $huntID

		");



				$battle = mysql_fetch_assoc($q);



				$a = mysql_query("SELECT * FROM player_creatures WHERE user_id = '".$battle[uid]."'");

				$d = mysql_query("SELECT * FROM player_creatures WHERE user_id = '".$battle[tid]."'");

				$att = mysql_fetch_assoc($a);

				$def = mysql_fetch_assoc($d);



				if ($battle[active] != 1 ) {

					echo "<table align='center'><tr><td>Invalid action or no hunt exists</td></tr></table>"; exit;

				}





				echo "<table width='600' align='left' border='0'>";

				echo "<tr><td colspan='2' style='margin-bottom: 2px; border-bottom: 1px solid #ccc;'><center>Battle details</center></td></tr>";

					



				// Izvadi sve potrebne podatke iz attack_types, i skills itd.

				$i = 1;

				while ($i <= 6) {

					$a = "a$i";

					$d = "d$i";

					$aty = mysql_query("

		SELECT id, name, opposite FROM attack_types WHERE id = '".$battle[$a]."'

		");		

					$dty = mysql_query("

		SELECT id, name, opposite FROM attack_types WHERE id = '".$battle[$d]."'

		");		

					$att_a[$i] = mysql_fetch_assoc($aty);

					$def_a[$i] = mysql_fetch_assoc($dty);



					$i++;

				}





				// begin output



				$max = 6;

				$t = 1;

				$a_max_dmg = 0;

				$d_max_dmg = 0;

				while ($t <= $max) {





					// basically what we have here is sorted mess: $firstC (first creature to strike and stats) and

					// $firstA as attacks for first creature. Also, same is for second.



					if ($att_a[$t] == "") {

						$att_a[$t][name] = "no action";

					}

					if ($def_a[$t] == "") {

						$def_a[$t][name] = "no action";

					}



					echo "<tr><td>".$att[name]." attacked with ". $att_a[$t][name] . "<br />

			 ".$def[name]." responded with " . $def_a[$t][name] . "</td>";



					if ($att_a[$t][id] == $att_a[$t][opposite]) {$ddmg[$t] = " blocked attack";}



					else



					if ($att_a[$t][id] == $att_a[$t][opposite]) {$admg[$t] = 0 . ", was blocked";}



					else {

						// add opponent defence into equasion

						$adm[$t] = rand((($att[att] - $def[def]) / 3), (int)(($att[att] - ($def[def] - 1)) * 1.25));

						$ddm[$t] = rand((($def[att] - $att[def]) / 3), (int)(abs(($def[att] - 1) - $att[def]) * 1.25));

							

						$admg[$t] = " dealt ".$adm[$t]. " damage";

						$ddmg[$t] = " dealt ".$ddm[$t]. " damage";

					}

					if (empty($admg[$t])) {$admg[$t] = 0;}

					echo"<td>".$att[name]." dealt ".$admg[$t]."<br />".$def[name].$ddmg[$t] ."</td></tr>";





					$a_max_dmg = $a_max_dmg + $adm[$t];

					$d_max_dmg = $d_max_dmg + $ddm[$t];



					$t++;

				}





			}

				

				

			/*       Battle queries       */





			if ($a_max_dmg >= $def[hp]) {



				//echo $a_max_dmg >= $def[hp];



				//gold plunder calc

				$gold_won = (int)($def[gold] * (rand(450, 500)/1000));

				$gold_total = $att[gold] + $gold_won;

				$gold_left = $def[gold] - $gold_won;

				if ($def[gold] <= 0) {$def[gold] = 0;}



				//exp plunder calc

				$exp_gained = 12 * $def[level];

				$exp_total = $att[exp] + $exp_gained;



				$ahp_left = $def[hp] - $a_max_dmg; if ($ahp_left <= 0) {$ahp_left = 0;}

				$dhp_left = $att[hp] - $d_max_dmg; if ($dhp_left <= 0) {$dhp_left = 0;}





				//echo $att[name] . " killed " . $def[name];

				//target first

				$update1 = mysql_query("UPDATE player_creatures SET hp = '$dhp_left', gold = '$gold_left' WHERE id = '".$def[id]."'");

				//attacker then



				$update2 = mysql_query("UPDATE player_creatures SET hp = '". $ahp_left ."', exp = '$exp_total', gold = '$gold_total' WHERE id = '".$att[id]."'");



				$this->win = $att[id];





			}

			else



			if ($d_max_dmg >= $att[hp]) {



				//echo $d_max_dmg >= $att[hp];



				//gold plunder calc  - something goes wrong and attacker gets killed

				$gold_won = (int)($att[gold] * (rand(450, 500)/1000));

				$gold_total = $def[gold] + $gold_won;

				$gold_left = $att[gold] - $gold_won;

				if ($att[gold] <= 0) {$att[gold] = 0;}



				//exp plunder calc

				$exp_gained = 12 * $att[level];

				$exp_total = $def[exp] + $exp_gained;



				$ahp_left = $def[hp] - $a_max_dmg; if ($ahp_left <= 0) {$ahp_left = 0;}

				$dhp_left = $att[hp] - $d_max_dmg; if ($dhp_left <= 0) {$dhp_left = 0;}





				//echo $def[name] . " killed " . $att[name];

				//looser first

				$update1 = mysql_query("UPDATE player_creatures SET hp = '$ahp_left', gold = '$gold_left' WHERE id = '".$att[id]."'");



				//winner then



				$update2 = mysql_query("UPDATE player_creatures SET hp = '". $dhp_left ."', exp = '$exp_total', gold = '$gold_total' WHERE id = '".$def[id]."'");



				$this->win = $def[id];











			}



			else {



				//echo "nothing";



				//gold plunder calc

				$gold_won = (int)($def[gold] * (rand(50, 100)/1000)); // only a 5-10% of gold can be snitched instead of plundered

				$gold_total = $att[gold] + $gold_won;

				$gold_left = $def[gold] - $gold_won;

				if ($def[gold] <= 0) {$def[gold] = 0;}



				//exp plunder calc

				$exp_gained = 3 * $def[level]; // and a small amount of exp only.

				$exp_total = $att[exp] + $exp_gained;



				//echo $gold_won;

				//echo $exp_gained;



				$ahp_left = $def[hp] - $a_max_dmg;

				$dhp_left = $att[hp] - $d_max_dmg;





				//target first

				$update1 = mysql_query("UPDATE player_creatures SET hp = '". $dhp_left ."', gold = '$gold_left' WHERE id = '".$def[id]."'");

				//attacker then



				$update2 = mysql_query("UPDATE player_creatures SET hp = '". $ahp_left ."', exp = '$exp_total', gold = '$gold_total' WHERE id = '".$att[id]."'");



				$this->win = 0;







			}



			// set attack to inactive, so it can't be repeated



			$ia2 = mysql_query("UPDATE creature_hunts SET active = '0', gold = '$gold_won', exp = '$exp_gained', winner = '".$this->win."' WHERE id = '$huntID'");

				

				

			echo "<tr><td colspan='2' style='padding-top: 4px; border-top: 1px solid #ccc;'>

		In total, ".$att[name]." dealt $a_max_dmg damage and suffered $d_max_dmg HP of damage<br />

		$gold_won gold plundered and $exp_gained exp gained.</td></tr>";





		echo "</table>";

			

	}





	function CheckDuelStatus() {



		$this->Init();



		$q1 = mysql_query("

		SELECT *

			FROM creature_hunts

		WHERE active = 1

			AND (uid = '1' OR tid = '1')

		LIMIT 1");





		$this->duel = mysql_fetch_assoc($q1);



		if (mysql_num_rows($q1) > 0) {



			$a = mysql_query("SELECT * FROM player_creatures WHERE user_id = '".$this->duel[uid]."'");

			$d = mysql_query("SELECT * FROM player_creatures WHERE user_id = '".$this->duel[tid]."'");

			$att = mysql_fetch_assoc($a);

			$def = mysql_fetch_assoc($d);





			// Attack



			$difH = $this->duel[rto] - time();

			//echo $difH;

			if ($difH <= 0) {

				$status = "<b><a href='?p=duel&s=".$this->duel[id]."'>".$att[name]." attacked ".$def[name]." (click here to see combat details)...</a></b><br />";

			} else {

				$status = "<b>".$att[name]." is about to attack ".$def[name]." in $difH seconds...</b><br />";

			}









			echo "<table width='580' align='left' border='0'>";

			echo "<tr><td style='margin-bottom: 2px; border-bottom: 1px solid #ccc;'><center>$status</center><br /></td></tr>";

			echo "</table><br /><br />";



		}



	}





	function Intro() {



		$this->Init();



		if (isset($_GET["s"])) {$this->DuelDetails();} else {



			$this->CheckDuelStatus();







			echo "<br /><table width='280' align='left' border='0'>";

			echo "<tr><td style='margin-bottom: 2px; border-bottom: 1px solid #ccc;'>Opponents hunt overview: </td></tr>";

			echo "<tr><td>Here you can duel opponents...</td></tr>";

			echo "<tr><td style='margin-bottom: 2px; border-bottom: 1px solid #ccc;'>Latest duels:</td></tr>";



			$r = mysql_query("SELECT

		player_creatures.name, 

		creature_hunts.id, 

		creature_hunts.gold, 

		creature_hunts.exp, 

		creature_hunts.tid 

		FROM 

		player_creatures, 

		creature_hunts 

		WHERE 

		creature_hunts.uid = '" . $_SESSION["id"] . "' 

		AND 

		creature_hunts.tid = player_creatures.user_id 

		ORDER by creature_hunts.id DESC LIMIT 5");



			$max = mysql_num_rows($r);

			$i = 1;

			while ($i <= $max) {

				$this->raids[$i] = mysql_fetch_assoc($r);

				echo "<tr><td>Vs. ".$this->raids[$i][name].", Gold: ".$this->raids[$i][gold].", Exp: ".$this->raids[$i][exp]." <a href='?o=duel&did=".$this->raids[$i][id]."'>[?]</a></td></tr>";

				$i++;

			}

			echo "</table>";



			$this->FindTargets();



		}



	}



	function OppDetails() {



		$this->Init();



		$this->opp = mysql_real_escape_string($_GET["c"]);



		$d = mysql_query("SELECT * FROM player_creatures WHERE id = '".$this->opp."'");



		$this->opponent = mysql_fetch_assoc($d);



		echo "<table width='280' align='left' border='0'><tr>";

		echo "<td style='margin-bottom: 2px; border-bottom: 1px solid #ccc;'>Opponents details: ".$this->opponent[name]."</td></tr><tr>";

		echo "<td>Level: ".$this->opponent[level]."</td></tr><tr>";

		//echo "<td>Attack: ".$this->opponent[att]."</td></tr><tr>";

		//echo "<td>Defence: ".$this->opponent[def]."</td></tr><tr>";

		echo "<td style='padding-top: 8px; border-bottom: 1px solid #ccc; border-top: 1px solid #ccc;'>

	<form action='?o=sneak' method='post'>

	<input type='hidden' name='cr' value='".$this->opponent[id]."' />

	Sneak closer to creature and collect<br /> more info... <br />

			<select name=\"raidtime\">

					<option value=\"60\">1 Minute</option>

					<option value=\"120\">2 Minutes</option>

					<option value=\"180\">3 Minutes</option>

					<option value=\"300\">5 Minutes</option>

					<option value=\"600\">10 Minutes</option>

		</select>

	<input type='submit' name='observe' value='Go'/>

	</form>

	</td></tr><tr>";

		echo "</tr></table>";

	}

}

?>
Return current item: Dominant Creature RPG