<?php

/*
* ////////////////////////////////////////////////////////////////////////////////
* // JohnCMS                             Content Management System              //
* // Официальный сайт сайт проекта:      http://johncms.com                     //
* // Дополнительный сайт поддержки:      http://gazenwagen.com                  //
* ////////////////////////////////////////////////////////////////////////////////
* // JohnCMS core team:                                                         //
* // Евгений Рябинин aka john77          john77@gazenwagen.com                  //
* // Олег Касьянов aka AlkatraZ          alkatraz@gazenwagen.com                //
* //                                                                            //
* // Информацию о версиях смотрите в прилагаемом файле version.txt              //
* ////////////////////////////////////////////////////////////////////////////////
* // Головоломка                                                                //
* // Автор Regan                                                                //
* ////////////////////////////////////////////////////////////////////////////////
*/
define('_IN_JOHNCMS', 1);
$headmod = 'game_Puzzle';
$textl = 'Головоломка';
$rootpath = '../../';
require_once ("../../incfiles/core.php");
require_once ("../../incfiles/head.php");
if($user_id)
{
	switch($act)
	{

		case "hod" :
			$ms = mysql_fetch_array(mysql_query("SELECT *
			FROM `game`
			WHERE type='Puzzle' and users='".$user_id."' LIMIT 1;"));
			if(!$ms['id'])
			{
				header("Location: index.php");
			}
			$motion = trim($_GET['motion']);
			$dump = unserialize($ms['dump']);
			$cursor = $dump[$ms['cursor']];
			switch($motion)
			{

				case "up" :
					if($ms['cursor'] != 0 and $ms['cursor'] != 1 and $ms['cursor'] != 2)
					{
						$dump[$ms['cursor']] = $dump[$ms['cursor'] - 3];
						$dump[$ms['cursor'] - 3] = $cursor;
						mysql_query("UPDATE `game` SET
            `cursor`='".($ms['cursor'] - 3)."',
            `dump`='".serialize($dump)."',
            `motion`=motion+1
            WHERE type='Puzzle' and id='".$ms['id']."' LIMIT 1;") or die('Error: UPDATE game');
					}
					break;

				case "down" :
					if($ms['cursor'] != 6 and $ms['cursor'] != 7 and $ms['cursor'] != 8)
					{
						$dump[$ms['cursor']] = $dump[$ms['cursor'] + 3];
						$dump[$ms['cursor'] + 3] = $cursor;
						mysql_query("UPDATE `game` SET
            `cursor`='".($ms['cursor'] + 3)."',
            `dump`='".serialize($dump)."',
            `motion`=motion+1
            WHERE type='Puzzle' and id='".$ms['id']."' LIMIT 1;") or die('Error: UPDATE game');
					}
					break;

				case "left" :
					if($ms['cursor'] != 0 and $ms['cursor'] != 3 and $ms['cursor'] != 6)
					{
						$dump[$ms['cursor']] = $dump[$ms['cursor'] - 1];
						$dump[$ms['cursor'] - 1] = $cursor;
						mysql_query("UPDATE `game` SET
            `cursor`='".($ms['cursor'] - 1)."',
            `dump`='".serialize($dump)."',
            `motion`=motion+1
            WHERE type='Puzzle' and id='".$ms['id']."' LIMIT 1;") or die('Error: UPDATE game');
					}
					break;

				case "right" :
					if($ms['cursor'] != 2 and $ms['cursor'] != 5 and $ms['cursor'] != 8)
					{
						$dump[$ms['cursor']] = $dump[$ms['cursor'] + 1];
						$dump[$ms['cursor'] + 1] = $cursor;
						mysql_query("UPDATE `game` SET
            `cursor`='".($ms['cursor'] + 1)."',
            `dump`='".serialize($dump)."',
            `motion`=motion+1
            WHERE type='Puzzle' and id='".$ms['id']."' LIMIT 1;") or die('Error: UPDATE game');
					}
					break;
			}
			header("Location: ?act=game");
			break;
			////////////////////////////////////////////////////
			// Играем                                         //
			////////////////////////////////////////////////////
		case "game" :
			$ms = mysql_fetch_array(mysql_query("SELECT *
			FROM `game`
			WHERE type='Puzzle' and users='".$user_id."' LIMIT 1;"));
			if(!$ms['id'])
			{
				header("Location: index.php");
			}
			if($ms['motion'] == 0)
			{
				echo '<img src="temp/'.$ms['sum'].'/img.jpg" alt="+" border="1"/>&nbsp;';
			}
			$dump = unserialize($ms['dump']);
			echo '<img src="img.php?name='.$ms['sum'].'&amp;1='.$dump[0].'&amp;2='.$dump[1].'&amp;3='.$dump[2].'&amp;4='.$dump[3].'&amp;5='.$dump[4].'&amp;6='.$dump[5].'&amp;7='.$dump[6].'&amp;8='.$dump[7].'&amp;9='.$dump[8].'" alt="+" border="1"/><br />';
			echo '<div class="menu"><a href="?act=hod&amp;motion=up">Вверх</a>|';
			echo '<a href="?act=hod&amp;motion=down">Вниз</a>';
			echo '<br /><a href="?act=hod&amp;motion=left">Влево</a>|';
			echo '<a href="?act=hod&amp;motion=right">Вправо</a></div>';
			echo '<div class="gmenu">';
			$time_game = time() - $ms['time'];
			if($dump['0'] == 1 and $dump['1'] == 2 and $dump['2'] == 3 and $dump['3'] == 4 and $dump['4'] == 5 and $dump['5'] == 6 and $dump['6'] == 7 and $dump['7'] == 8)
			{
				echo '<h3><span class="green">Вы выиграли!</span></h3>';
			}
			echo "Время игры: ".gmdate('H:i:s', $time_game)."<br/>
				Ходов: ".$ms['motion']."<br /></div>";
			echo "<div class='menu'><a href='?'>Заново</a>";
			echo '<br/><a href="../?">В раздел</a></div>';
			break;
			////////////////////////////////////////////////////
			// Главная                                        //
			////////////////////////////////////////////////////
		default :
			$old = ceil(time() - (3 * 24 * 3600));
			mysql_query("DELETE FROM `game` WHERE type='Puzzle' and
				(`users`='".$user_id."' or `time`<='".$old."');");
			mysql_query("OPTIMIZE TABLE `game`");
			$dropen = opendir("temp");
			while(($file1 = readdir($dropen)))
			{
				if($file1 != "." && $file1 != ".." && $file1 != "index.php")
				{
					if(is_dir("temp/$file1"))
					{
						$drt[] = $file1;
					}
				}
			}
			shuffle($drt);
			$arr = array('1', '2', '3', '4', '5', '6', '7', '8');
			shuffle($arr);
			$arr['8'] = '0';
			mysql_query("INSERT INTO `game` SET
				`time`='".time()."',
				`sum`='".$drt[0]."',
				`users`='".$user_id."',
				`cursor`='8',
				`type`='Puzzle',
				`dump`='".serialize($arr)."';") or die('Error: INSERT INTO game');
			echo "<div class='phdr'>Головоломка</div>
			<div class='gmenu'>Цель игры:<br />Собрать картинку из кусочков.</div>";
			echo "<div class='menu'><a href='?act=game'>Начать игру</a><br />";
			echo '<a href="../?">В раздел</a></div>';
			break;
	}
}
else
{
	echo functions :: display_error("Игра доступна только для зарегистрированых пользователей сайта");
	echo '<a href="../?">В раздел</a>';
}
require_once ("../../incfiles/end.php");

?>