<?php
///////////////////////////////////////////////////////////////////////////////////////
// PHPizabi 0.844b C1 [ALICIA]                               http://www.phpizabi.net //
///////////////////////////////////////////////////////////////////////////////////////
// Please read the license.txt file before using / modifying this software           //
// Original author:         Claude Desjardins, R - feedback@realitymedias.com        //
// Last modification date:  August 26th 2006                                         //
// Version:                 PHPizabi 0.844b C1                                       //
//                                                                                   //
// (C) 2005, 2006 Real!ty Medias / PHPizabi - All rights reserved                    //
///////////////////////////////////////////////////////////////////////////////////////

	/* Check Structure Availability */
	if (!defined("CORE_STRAP")) die("Out of structure call");
	
	// TEMPLATE HANDLING ////////////////////////////////////////////////////////////////// FINAL //
	/*
		Initialize the template engine and
		load the template file. Get objects,
		convert self user.
	*/
	$tpl = new template;
	$tpl -> Load("details");


	$row = myF(myQ("SELECT * FROM `[x]fight` WHERE `id`='{$_GET["id"]}'"));
	
	if ($row["opponent"] == "0") {
		
		if (isset($_POST["GetOpenFight"])) {
			
			myQ("
				UPDATE `[x]fight`
				SET 
					`opponent` = '".me("id")."',
					`start` = '".time()."',
					`expire` = '".(time() + $row["duration"])."',
					`votes` = '".pk(array(me("id"), $row["user"]))."'
				WHERE `id`='{$_GET["id"]}'
				LIMIT 1
			");
			reload(0, "?L=fight.details&id={$row["id"]}");				
		}
		
		$tpl -> Zone("mode", "open");
	}
	
	else if ($row["start"] > 0 and $row["expire"] < time()) {
		if ($row["user_points"] != $row["opponent_points"]) $tpl -> Zone("mode", "winner");
		else if ($row["user_points"] == $row["opponent_points"]) $tpl -> Zone("mode", "matchnull");
	}
		
	else if ($row["start"] == 0) {
		
		$tpl -> Zone("mode", "pending");
	}
	
	else {
	
		$votes_array = unpk($row["votes"]);
		
		if (isset($_POST["betUser"])) {
			
			if (!in_array(me("id"), $votes_array)) {
				$votes_array[] = me("id");
	
				myQ("
					UPDATE `[x]fight`
					SET 
						`user_points` = `user_points` + 1,
						`votes` = '".pk($votes_array)."'				
					WHERE `id`='{$_GET["id"]}'
				");
				
				$row["user_points"] ++;
			}
			$tpl -> Zone("mode", "votecast");
		}
		
		else if (isset($_POST["betOpponent"])) {
			
			if (!in_array(me("id"), $votes_array)) {
				$votes_array[] = me("id");

				myQ("
					UPDATE `[x]fight`
					SET 
						`opponent_points` = `opponent_points` + 1,
						`votes` = '".pk($votes_array)."'
					WHERE `id`='{$_GET["id"]}'
				");
				
				$row["opponent_points"] ++;
			}
			$tpl -> Zone("mode", "votecast");
		}
		
		else $tpl -> Zone("mode", "fight");
	}
	
	$nextFight = myF(myQ("
		SELECT `id` 
		FROM `[x]fight` 
		WHERE `id` > '{$_GET["id"]}'
		ORDER BY `id` ASC
		LIMIT 1
	"));
	if ($nextFight["id"] == "") $nextFight = myF(myQ("SELECT `id` FROM `[x]fight` ORDER BY `id` ASC	LIMIT 1"));
			
	$timelap = round(($row["expire"]-time())/60);
	$winner = ($row["user_points"] > $row["opponent_points"] ? $row["user"] : $row["opponent"]);
	
	$tpl -> AssignArray(array(
		"fight.user" => 			user($row["user"], "username"),
		"fight.opponent" =>			user($row["opponent"], "username"),
		"fight.user_picture" => 	user($row["user"], "mainpicture"),
		"fight.opponent_picture" =>	user($row["opponent"], "mainpicture"),
		"fight.id" =>				$row["id"],

		"fight.topic" =>			substr($row["topic"], 0, 80),
		"fight.time" => 			($timelap > 0 ? $timelap : 0),
		"fight.body" =>				convertBodyCodes($row["body"]),
		
		"fight.user_points" =>		$row["user_points"],
		"fight.opponent_points" =>	$row["opponent_points"],
		
		"fight.next" =>				$nextFight["id"],
		
		"fight.winner" =>			user($winner, "username"),
		"fight.winnerpicture" =>	user($winner, "mainpicture"),
	));
	
	

	$tpl -> Flush();
	
?>