<?php
/*
 * Created on 12.07.2008
 *
 * @copyright (c) 2010 animegame.eu
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
 *
 */


function displayCharTable($charbild, $charname, $starke, $ver, $speed, $ausdauer, $glueck = -1, $hp_akt, $hp_max, $mp_akt, $mp_max ){
?>
<table class="stylish">
	<tr>
		<td>
			<img src="<?php echo $charbild; ?>" width="75" height="75">
		</td>
		<td class="stats">
			<span class="stats">Name:</span>
			<span class="statvals"><?php echo $charname; ?></span>
			<br>
			<span class="stats">HP:</span>
			<span class="statvals"><?php echo $hp_akt.' / '.$hp_max; ?></span>
			<br>
			<span class="stats">MP:</span>
			<span class="statvals"><?php echo $mp_akt.' / '.$mp_max; ?></span>
			<br>
			<span class="stats">St&auml;rke:</span>
			<span class="statvals"><?php echo round($starke); ?></span>
			<br>
			<span class="stats">Verteidigung:</span>
			<span class="statvals"><?php echo round($ver); ?></span>
			<br>
			<span class="stats">Geschwindigkeit:</span>
			<span class="statvals"><?php echo round($speed); ?></span>
			<br>
			<span class="stats">Gl&uuml;ck</span>
			<span class="statvals"><?php echo round($glueck); ?></span>
			<br>
			<span class="stats">Ausdauer:</span>
			<span class="statvals"><?php echo round($ausdauer); ?></span>
		</td>
	</tr>
</table>	
<?php
}

function displayAttacks($attacke1, $schaden1, $attacke2, $schaden2){
?>
<table border="0" width="100%">
	<tr>
		<td width="50%">
			<span class="stats">Technik:</span>
			<span class="statvals"><?php echo $attacke1; ?></span>
			<br><br>
			<span class="stats">Schaden:</span>
			<span class="statvals"><?php echo $schaden1; ?></span>
		</td>
		<td width="50%">
			<span class="stats">Technik:</span>
			<span class="statvals"><?php echo $attacke2; ?></span>
			<br><br>
			<span class="stats">Schaden:</span>
			<span class="statvals"><?php echo $schaden2; ?></span>
		</td>
	</tr>
</table>
<?php
}

function displaySummary($char1_name, $char2_name, $sieger, $exp_char1, $exp_char2, $money_char1, $money_char2, $arena_exp1 = NULL, $arena_exp2 = NULL){

echo '<tr><td colspan="3"><table class="stylish" width="50%""><tr><td><table width="100%">';

if($sieger == $char1_name){
	$char1_status = 'Sieger';
	$char2_status = 'Verlierer';
} else{
	$char1_status = 'Verlierer';
	$char2_status = 'Sieger';
}
echo '<tr>' .
	'<td class="greenstats">'.$char1_status.'</td>'.
	'<td class="bold">'.$char1_name.'</td>'.
	'<td class="greenstats">'.$char2_status.'</td>'.
	'<td class="bold">'.$char2_name.'</td>' .
	'</tr>';

echo '<tr>' .
	'<td class="greenstats">Exp +</td>' .
	'<td class="bold">'.$exp_char1.'</td>' .
	'<td class="greenstats">Exp +</td>' .
	'<td class="bold">'.$exp_char2.'</td>'.
	'</tr>';

echo '<tr>'.
	'<td class="greenstats">Geld +</td>'.
	'<td class="bold">'.$money_char1.'</td>'.
	'<td class="greenstats">Geld +</td>'.
	'<td class="bold">'.$money_char2.'</td>' .
	'</tr>';
	
echo '<tr>'.
	'<td class="greenstats">Arena Exp +</td>'.
	'<td class="bold">'.$arena_exp1.'</td>'.
	'<td class="greenstats">Arena Exp +</td>'.
	'<td class="bold">'.$arena_exp2.'</td>' .
	'</tr>';


echo '</table></td></tr></table></td></tr>';
}

function displayFinalResult($items, $exp, $money, $final_result){
echo '<tr><td colspan="3"><table class="stylish" width="50%"><tr><td>' .
		'<table width="100%">';
echo '<tr><th class="greenstats" colspan="2">Zusammenfassung</th></tr>';
if($final_result == 'lost') {
	echo '<tr><td class="greenstats" colspan="2">Ihr m&uuml;sst euch selbst eingestehen, dass diese Monster zu stark waren. Der Zweifel der euch plagt verringert eure Erfahrung und Diebe den Betrag in eurem Geldbeutel.</td></tr>';
}

echo '<tr>' .
		'<td class="greenstats" align="center">Exp +</td>' .
		'<td class="bold">'.$exp.'</td>'.
	'</tr>';
echo '<tr>' .
		'<td class="greenstats" align="center">Geld +</td>' .
		'<td class="bold">'.$money.'</td>'.
	'</tr>';
echo '<tr>' .
		'<td class="greenstats" align="center">Items</td>' .
		'<td class="bold">';
	// Letze Eintrag von Items ist false!!
	if(!$items[0]){
		echo '&nbsp;';		
	} else{
		for($i=0;$items[$i];$i++){
			echo $items[$i]['name'].'<br>';
		}	
	}
echo'</td></tr>';
echo '</table>' .
		'</td></tr></table></td></tr>';
}

function displaySchatzSuche($char){
	include_once($_SERVER['DOCUMENT_ROOT'].'ag/include/schatz.inc.php');
	// Erstmal die Schatzsuche-stammdaten laden!
	$sql = 'Select * from quests where charid ='.$char['id'].' and dauer < now()';
	$quest = mysql_fetch_assoc(mysql_query($sql));
//	echo $sql.'<br>';
	if(!$quest){
		return 'Schatzsuche noch nicht beendet!!';
	}

	// Uberschreibe die Char-Daten mit den Daten aus der Datenbank
	$char['starke'] = $quest['st'];
	$char['verteidigung'] = $quest['ver'];
	$char['speed'] = $quest['sp'];
	$char['glueck'] = $quest['gl'];
	$char['ausdauer'] = $quest['aus'];
	$char['hp_max'] = $quest['hp'];
	$char['mp_max'] = $quest['mp'];

	// Erstmal die Seite starten
	echo '<html><head><link rel="stylesheet" type="text/css" href="design/FightStyle.css"></head><body><table width="100%">';
	// Einfach mal ne dicke Ueberschrift
	$ort = mysql_fetch_assoc(mysql_query('select * from quest_ort where id = '.$quest['ortid']));
	echo '<tr><th class="top" colspan="3">Schatzsuche in '.$ort['stadt'].'</th></tr>';

	// Hole die Ergebnisse die auch in die Datenbank eingetragen werden muessen!
	$row = mysql_fetch_assoc(mysql_query('Select sum(exp) as exp, sum(geld) as geld, count(*) as anzahl from quest_fights where charid = '.$char['id']));	

	// Ermittle den Endzustand des Chars
	$end_qf = mysql_fetch_assoc(mysql_query('select max(fightnr) as mf from quest_fights where charid = '.$char['id']));
	$end_qr = mysql_fetch_assoc(mysql_query('select max(roundnr) as mr from quest_rounds where fightnr = '.$end_qf['mf'].' and charid = '.$char['id']));
	$last_round = mysql_fetch_assoc(mysql_query('Select * from quest_rounds where roundnr = '.$end_qr['mr'].' and fightnr = '.$end_qf['mf'].' and charid = '.$char['id']));
	$last_fight = mysql_fetch_assoc(mysql_query('Select * from quest_fights where fightnr = '.$end_qf['mf'].' and charid = '.$char['id']));
	
	// Ermittle die Reqs des Chars!
	$sql = 'SELECT c_attack as a_name, count(*) as anzahl FROM quest_rounds q where roundnr > 0 and charid = '.$char['id'].' group by c_attack';
//	echo $sql.'<br>';
	$reqs = mysql_query($sql);
	
	while($row_r = mysql_fetch_assoc($reqs)){
		$c_attacken[] = $row_r;
//		print_r($c_attacken);
//		echo '<br>';
	}
	
	if($last_fight['sieger'] == $char['id']){
		$final_result = 'won';
		// Nur wer gewinnt bekommt was!
		$items = getSchatzItems($row['anzahl']);
	} else{
		$final_result = 'lost';
		$lose_rate = mt_rand(25,60)/100;
		$row['exp'] = round($row['exp'] * $lose_rate);
		$row['geld'] = round($row['geld'] * $lose_rate);
		for($i = 0;$i< count($row_r); $i++){
			$c_attacken[$i]['anzahl'] = floor($c_attacken[$i]['anzahl'] * $lose_rate);
		}
	}
	

	// Zeige als erstes die Zusammenfassung an!
	displayFinalResult($items, $row['exp'], $row['geld'], $final_result);

	
	// Lade nun die Fights runter
	$sql = 'Select * from quest_fights where charid = '.$char['id'];
	$qry = mysql_query($sql);
	while($fight = mysql_fetch_assoc($qry)){
		displaySchatzSucheFight($quest, $char, $fight);
	}
	
	submitCharChanges($char['id'], $items, $row['exp'], $row['geld'], $quest['ortid'], array($last_round['c_hp']) , array($last_round['c_mp']), $c_attacken);
	echo '</table></body></html>';
	return null;
}

function displaySchatzSucheFight($quest, $char, $fight){
	// Lade das Monster aus der Datenbank
	$sql = 'select * from quest_monster where id = '.$fight['monsterid'];
	$monster = mysql_fetch_assoc(mysql_query($sql));
	
	if($monster['bild'] == null || $monster['bild'] == ''){
		$monster['bild'] = 'design/bilder/avatare/noavart.gif';
	}
	
	// Platziere den Table-Header
	echo '<tr><th class="top" colspan="3">'.$char['name'].' Vs '.$monster['name'].'</th></tr>';
	$sql = 'Select * from quest_rounds where charid = '.$char['id']. ' and fightnr = '.$fight['fightnr'];
//	echo $sql.'<br>';
	$qry = mysql_query($sql);
	while($round = mysql_fetch_assoc($qry)){
		displaySchatzSucheFightRound($char, $monster, $fight, $round);
	}
	displaySummary($char['name'], $monster['name'], $fight['sieger']==$char['id']?$char['name']:$monster['name'], $fight['exp'], 0, $fight['geld'], 0);
	// Und dann halt immer noch etwas platz nach unten lassen
	
	echo '<tr height="5px"><td>&nbsp;</td></tr>';
}

function displaySchatzSucheFightRound($char, $monster, $fight, $round){
	// Rundennummer:
	if($round['roundnr'] == 0){
		$runde = 'Start:';
	} else{
		$runde = 'Runde: '.$round['roundnr'];
	}
	echo '<tr><th class="round" colspan="3">'.$runde.'</th></tr>';

	// Bild + Avantarinfo | Attacken + Schaden | Bild + Avantarinfo 
	// In einer Reihe allerdings
	echo '<tr><td>';
	displayCharTable($char['bild'], $char['name'], $round['c_st'], $round['c_ver'], $round['c_sp'], $round['c_aus'], $round['c_gl'], $round['c_hp'], $char['hp_max'], $round['c_mp'], $char['mp_max'] );
	echo '</td><td>';
	if($round['roundnr'] != 0){
		displayAttacks($round['c_attack'], $round['c_dmg'], $round['m_attack'], $round['m_dmg']);
	} else{
		echo '&nbsp;';
	}
	echo '</td><td>';
	displayCharTable($monster['bild'], $monster['name'], $round['m_st'], $round['m_ver'], $round['m_sp'], $round['m_aus'], $round['m_gl'], $round['m_hp'], $monster['hp'], $round['m_mp'], $monster['mp'] );
	echo '</td></tr>';
}

?>