<?
include_once '../../../sys/inc/start.php';
include_once '../../../sys/inc/compress.php';
include_once '../../../sys/inc/sess.php';
include_once '../../../sys/inc/home.php';
include_once '../../../sys/inc/settings.php';
include_once '../../../sys/inc/db_connect.php';
include_once '../../../sys/inc/ipua.php';
include_once '../../../sys/inc/fnc.php';
include_once '../../../sys/inc/user.php';

$set['title'] = 'Крестики - Нолики'; // заголовок страницы

include_once '../../../sys/inc/thead.php';

echo '<link rel="stylesheet" href="style/css.css" type="text/css"/>'; // Стиль в игре

title();
aut();


$game = mysql_fetch_assoc(mysql_query("SELECT * FROM `kr_nol_3x3` WHERE `id_user` = '$user[id]' OR `id_prot` = '$user[id]'"));

$avtor = get_user($game['id_user']);
$prot = get_user($game['id_prot']);

$game_my = mysql_result(mysql_query("SELECT COUNT(*) FROM `kr_nol_3x3` WHERE `id_user` = '$user[id]' OR `id_prot` = '$user[id]'"),0);

if ($avtor['id']==$user['id'])
$ank2 = get_user($prot['id']);

elseif ($prot['id']==$user['id'])
$ank2 = get_user($avtor['id']);

if (isset($_GET['us']))
{
	$ank=get_user(intval($_GET['us']));
	$game_ank = mysql_result(mysql_query("SELECT COUNT(*) FROM `kr_nol_3x3` WHERE `id_user` = '$ank[id]' OR `id_prot` = '$ank[id]'"),0);
}


if (isset($_GET['plus']) && $game['read']!=2 && $game['read']!=1 && $avtor['id']==$user['id'])
{
	if ($avtor['balls']>=$game['stav'])
	{
		if ($prot['balls']>=$game['stav'])
		{

			mysql_query("UPDATE `kr_nol_3x3` SET `read` = '5', `game_over` = '0', `1` = 'z', `2` = 'z', `3` = 'z', `4` = 'z', `5` = 'z', `6` = 'z', `7` = 'z', `8` = 'z', `9` = 'z', `hod` = '$ank2[id]', `u1` = '0', `u2` = '0', `u3` = '0', `u4` = '0', `u5` = '0', `u6` = '0', `u7` = '0', `u8` = '0', `u9` = '0' WHERE `id` = '$game[id]' LIMIT 1");
			header("Location: ?");
			exit;

		}else{

			msg('У противника не достаточно средств');

		}
	}else{

		msg('У вас не достаточно средств');

	}
}

if (isset($_GET['plus']) && $_GET['plus']=='ok' && $game['read']==5 && $prot['id']==$user['id'])
{
	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '2', `time` = '$time' WHERE `id` = '$game[id]' LIMIT 1");
	header("Location: ?");
	exit;
}
elseif (isset($_GET['plus']) && $_GET['plus']=='no' && $game['read']==5 && $prot['id']==$user['id'])
{
	mysql_query("DELETE FROM `kr_nol_3x3_komm` WHERE `id_game` = '".$game['id']."'");
	mysql_query("DELETE FROM `kr_nol_3x3` WHERE `id` = '".$game['id']."'");
	header("Location: ?");
	exit;
}


if (isset($_GET['exit']))
{
	if ($_GET['exit']=='es' && $game['read']!=2)
	{
		mysql_query("DELETE FROM `kr_nol_3x3_komm` WHERE `id_game` = '".$game['id']."'");
		mysql_query("DELETE FROM `kr_nol_3x3` WHERE `id` = '".$game['id']."'");
		header("Location: ?");
	}else{
		msg('Сначала завершите игру');
	}
}


if (isset($_GET['null']) && $_GET['null']=='es' && $game['read']==3 && $avtor['id']==$user['id'])
{
	mysql_query("UPDATE `kr_nol_3x3` SET `stav` = '0' WHERE `id` = '$game[id]' LIMIT 1");
	msg('Ставка обнулена');
}


if (isset($_GET['game']) && $game['read']==1 && $prot['id']==$user['id'])
{
	if (isset($_GET['game']) && $_GET['game']=='es')
	{
		mysql_query("UPDATE `kr_nol_3x3` SET `read` = '2', `time` = '$time' WHERE `id` = '$game[id]' LIMIT 1");
		header("Location: ?");
		exit;
	}
	else if (isset($_GET['game']) && $_GET['game']=='no')
	{
		mysql_query("DELETE FROM `kr_nol_3x3_komm` WHERE `id_game` = '".$game['id']."'");
		mysql_query("DELETE FROM `kr_nol_3x3` WHERE `id` = '".$game['id']."'");

		// отправка уведомления
		$msg = 'К сожалению, пользователь [b]'.$user['nick'].'[/b] отказался играть с вами в [b]"[url=/plugins/games/xo/index.php]Крестики - Нолики[/url]"[/b]';
		mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ank2[id]', '".my_esc($msg)."', '$time')");

		header("Location: index.php?");
		exit;
	}
}

if (isset($_GET['us']) && $game_my==0)
{
	if ($user['id']!=$_GET['us'])
	{
		if ((isset($_POST['stav'])) && ($_POST['stav']==0 ||  $_POST['stav']==100 || $_POST['stav']==200 || $_POST['stav']==300))
		{
			if ($user['balls']>=$_POST['stav'])
			{
				if ($ank['balls']>=$_POST['stav'])
				{
					if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' LIMIT 1"),0)==1)
					{
						if ($game_ank==0)
						{
							if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' AND `date_last` > '".(time()-600)."' LIMIT 1"),0)==1)
							{
								mysql_query("INSERT INTO `kr_nol_3x3` (`stav`, `read`, `hod`, `id_user`, `id_prot`, `time`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`)
								values('".intval($_POST['stav'])."', '1', '".intval($_GET['us'])."', '$user[id]', '".intval($_GET['us'])."', '$time', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z')");
									// отправка приглашения

								$msg = 'Пользователь [b]'.$user['nick'].'[/b] приглашает вас сыграть в [b]"[url=/plugins/games/xo/index.php]Крестики - Нолики[/url]"[/b]';
								mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ank[id]', '".my_esc($msg)."', '$time')");

								header("Location: ?uved=ok");
								exit;
							}else{
								msg('Пользователь не в сети');
							}
						}else{
							msg('Пользователь уже учавствует в игре');
						}
					}else{
						msg('Такого пользователя не сущечтвует');
					}
				}else{
					msg('У противника не достаточно средств');
				}
			}else{
				msg('У вас не достаточно средств');
			}
		}
	}


	echo "<div class='mess'>";
	echo "Игра с ";
	echo user::avatar($ank['id'], 2) . user::nick($ank['id'], 1, 1, 1)."</div>";


	echo "<form method=\"post\" name=\"message\" action=\"?us=$ank[id]\">\n";



	echo "Ставка баллов: <select name='stav'>\n";
	echo "<option value='300' selected='selected'>300 </option>\n";
	echo "<option value='200' selected='selected'>200 </option>\n";
	echo "<option value='100' selected='selected'>100 </option>\n";
	echo "<option value='0' selected='selected'>Нет</option>\n";
	echo "</select><br />\n";


	echo "<input value=\"Отправить приглашение\" type=\"submit\" />\n";
	echo "</form>\n";

	include_once '../../../sys/inc/tfoot.php';
	exit;

}


$q = mysql_query("SELECT * FROM `kr_nol_3x3` WHERE `id` = '$game[id]' limit 1");
$o = '<img src="style/0.jpg" alt="*"/>';
$x = '<img src="style/x.jpg" alt="*"/>';
$z = '<img src="style/z.jpg" alt="*"/>';

if ($avtor['id']==$user['id'])
$n = 'x';
else
$n = 'o';

$p = $game['stav'];

$sec2 = -90-$game['time']+$time;

if ($sec2>0 && $game['read']==2)
{
    if ($avtor['id']==$game['hod'])
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$prot[id]' WHERE `id` = '$game[id]' LIMIT 1");
    	$game['game_over']=$prot['id'];
    }
    elseif ($prot['id']==$game['hod'])
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$avtor[id]' WHERE `id` = '$game[id]' LIMIT 1");
    	$game['game_over']=$avtor['id'];
    }

    if ($game['stav']>0)
    {
    	if ($game['game_over']==$prot['id'])
    		mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    	elseif ($game['game_over']==$avtor['id'])
    		mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    	if ($game['game_over']==$prot['id'])
    		mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    	elseif ($game['game_over']==$avtor['id'])
    		mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");

    }

    header("Location: ?");
    exit;
}


if (isset($_GET['hod']) && $game['hod']==$user['id'] && $game['u'.intval($_GET['hod']).'']==0 && $game['game_over']==0 && $game['read']==2 && $sec2<0)
{
    mysql_query("UPDATE `kr_nol_3x3` SET `time` = '$time', `".intval($_GET['hod'])."` = '$n', `u".intval($_GET['hod'])."` = '$user[id]', `hod` = '$ank2[id]' WHERE `id` = '$game[id]' LIMIT 1");
    header("Location: ?");
    exit;
}

if ($game['read']==2)
{
    /*-------------------горизонталь---------------------------*/
    if (($game['1']=='x' && $game['2']=='x' && $game['3']=='x') || ($game['1']=='o' && $game['2']=='o' && $game['3']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u1]' WHERE `id` = '$game[id]' LIMIT 1");

    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u1'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}

    	header("Location: ?");
    	exit;
    }
    elseif (($game['4']=='x' && $game['5']=='x' && $game['6']=='x') || ($game['4']=='o' && $game['5']=='o' && $game['6']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u4]' WHERE `id` = '$game[id]' LIMIT 1");

    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u4'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}
    	header("Location: ?");
    	exit;
    }
    elseif (($game['7']=='x' && $game['8']=='x' && $game['9']=='x') || ($game['7']=='o' && $game['8']=='o' && $game['9']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u7]' WHERE `id` = '$game[id]' LIMIT 1");
    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u7'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}
    	header("Location: ?");
    	exit;
    }
    /*--------------------вертикаль----------------------------*/
    if (($game['1']=='x' && $game['4']=='x' && $game['7']=='x') || ($game['1']=='o' && $game['4']=='o' && $game['7']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u1]' WHERE `id` = '$game[id]' LIMIT 1");
    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u1'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}
    	header("Location: ?");
    	exit;
    }
    elseif (($game['2']=='x' && $game['5']=='x' && $game['8']=='x') || ($game['2']=='o' && $game['5']=='o' && $game['8']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u2]' WHERE `id` = '$game[id]' LIMIT 1");
    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u2'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}
    	header("Location: ?");
    	exit;
    }
    elseif (($game['3']=='x' && $game['6']=='x' && $game['9']=='x') || ($game['3']=='o' && $game['6']=='o' && $game['9']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u3]' WHERE `id` = '$game[id]' LIMIT 1");
    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u3'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}
    	header("Location: ?");
    }
    /*-------------------------угол----------------------------*/
    if (($game['1']=='x' && $game['5']=='x' && $game['9']=='x') || ($game['1']=='o' && $game['5']=='o' && $game['9']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u1]' WHERE `id` = '$game[id]' LIMIT 1");
    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u1'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    			echo "ok $p - $game[game_over]";
    	}
    	header("Location: ?");
    	exit;
    }
    elseif (($game['7']=='x' && $game['5']=='x' && $game['3']=='x') || ($game['7']=='o' && $game['5']=='o' && $game['3']=='o'))
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '3', `game_over` = '$game[u7]' WHERE `id` = '$game[id]' LIMIT 1");
    	if ($game['stav']>0)
    	{
    		$game['game_over']=$game['u7'];
    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']+$p)."' WHERE `id` = '$prot[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']+$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");

    		if ($game['game_over']==$prot['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($avtor['balls']-$p)."' WHERE `id` = '$avtor[id]' LIMIT 1");
    		elseif ($game['game_over']==$avtor['id'])
    			mysql_query("UPDATE `user` SET `balls` = '".($prot['balls']-$p)."' WHERE `id` = '$prot[id]' LIMIT 1");

    	}
    	header("Location: ?");
    	exit;
    }

    if ($game['1']!='z' && $game['2']!='z' && $game['3']!='z' && $game['4']!='z' && $game['5']!='z' && $game['6']!='z' && $game['7']!='z' &&
    $game['8']!='z' && $game['9']!='z' && $game['game_over']==0)
    {
    	mysql_query("UPDATE `kr_nol_3x3` SET `read` = '4' WHERE `id` = '$game[id]' LIMIT 1");
    	header("Location: ?");
        exit;
    }
}

if (isset($game['id']) && $game['read']==1)
{
    if ($avtor['id']==$user['id'])
    {
    	if (isset($_GET['uved']) && $_GET['uved']=='ok')
    	msg('Ваше приглашение отправлено');

    	echo "<div class='nav1'>";
    	echo "Игра с ";
    	echo user::avatar($ank2['id'], 2) . user::nick($ank2['id'], 1, 1, 1)."<br />";
    	echo "Ставка: ";
    	if ($game['stav']>0)echo "$game[stav] баллов!";
    	else echo "Не используется!";
    	echo "</div>";

      ?><div class="nav2"><?
    	echo "<a href='?$passgen' class='knopka' >Обновить</a>";
      ?></div><?

    	echo "<div class='mess'>";
    	echo "Ожидание второго игрока...";
    	echo "</div>";
      
    	echo "<div class='foot'>";
    	echo "<img src='/style/icons/delete.gif' alt='*' /> <a href='?exit=es' >Закончить игру</a><br />";
    	echo "</div>";
    	include_once '../../../sys/inc/tfoot.php';
    	exit;
    } else {
    	echo "<div class='nav1'>";
    	echo "Игра с ";
    	echo user::avatar($ank2['id'], 2) . user::nick($ank2['id'], 1, 1, 1)."<br />";
    	echo "Ставка: ";
    	if ($game['stav']>0)echo "$game[stav] баллов!";
    	else echo "Не используется!";
    	echo "</div>";

    	if ($game_my>0)
    	{
    		if ($game['read']==1)
    		{
    			if ($game['id_prot']==$user['id'])
    				echo "<br /><center><a href='?game=es' class='knopka'><img src='/style/icons/ok.gif' alt='*'> Принять игру</a>
    				<a href='?game=no' class='knopka'><img src='/style/icons/delete.gif' alt='*'> Отказаться</a></center><br />";
    		}
    	}

    	include_once '../../../sys/inc/tfoot.php';
    	exit;
    }
}


if (isset($_POST['msg']) && isset($user))
{
    $msg=$_POST['msg'];
    if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);

    $mat=antimat($msg);
    if ($mat)$err[]='В тексте сообщения обнаружен мат: '.$mat;

    if (strlen2($msg)>512){$err='Сообщение слишком длинное';}
    elseif (strlen2($msg)<1){$err='Короткое сообщение';}
    elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `kr_nol_3x3_komm` WHERE `id_game` = '$game[id]' AND `id_user` = '$user[id]' AND `msg` = '".my_esc($msg)."' LIMIT 1"),0)!=0){$err='Ваше сообщение повторяет предыдущее';}
    elseif(!isset($err)){
    	mysql_query("INSERT INTO `kr_nol_3x3_komm` (`id_user`, `time`, `msg`, `id_game`) values('$user[id]', '$time', '".my_esc($msg)."', '$game[id]')");
    	mysql_query("UPDATE `user` SET `balls` = '".($user['balls']+1)."' WHERE `id` = '$user[id]' LIMIT 1");
    	msg('Ваш комментарий успешно принят');
    }
}

if (isset($game['id']) && $game['read']>0)
{
    echo "<div class='nav1'>";
    echo "Игра с ";
    echo user::nick($ank2['id'], 1, 1, 1)."<br />";
    echo "Ставка: ";
    if ($game['stav']>0)echo "$game[stav] баллов!<br />";
    else echo "Не используется!<br />";

    if ($game['hod']==$user['id'] && $game['read']==2)
    echo "<b>Ваш ход</b> $sec2<br />";
    else if ($game['read']==2)
    echo "<b>Ход противника</b> $sec2<br />";
    echo "Вы играете ";
    if ($n=='o')
    echo "$o<br />";
    else
    echo "$x<br />";


    echo "</div>";

    if ($game['game_over']!=0)
    {
    	echo "<div class='msg'>";
    	if ($game['game_over']==$user['id'])
    	{
    		echo "Вы победили! Поздравляем!<br />";
    		if ($game['stav']>0)echo "Вам начислено $game[stav] баллов";
    	}
    	else
    	{
    		echo "Вы проиграли!<br />";
    		if ($game['stav']>0)echo "C вас списано $game[stav] баллов";
    	}
    	echo "</div>";
    }
    if ($game['1']!='z' && $game['2']!='z' && $game['3']!='z' && $game['4']!='z' && $game['5']!='z' && $game['6']!='z' && $game['7']!='z' &&
    $game['8']!='z' && $game['9']!='z' && $game['game_over']==0 && $game['read']==4)
    {
    	echo "<div class='msg'>";
    	echo "Ничья! Победила дружба!";
    	echo "</div>";
    }

    echo '<div class="bod">';

    echo "<a href='?$passgen' class='knopka' >Обновить поле</a>";
    echo "<div class='blok'>";

    if ($game['hod']==$user['id'] && $game['game_over']==0 && $game['read']==2)
    {
    	while ($kr = mysql_fetch_assoc($q))
    	{
    		if ($kr['1']=='z')
    		echo "<a href='?hod=1'>$z</a> ";
    		else if ($kr['1']=='o')
    		echo "<a href='?hod=1'>$o</a> ";
    		else if ($kr['1']=='x')
    		echo "<a href='?hod=1'>$x</a> ";


    		if ($kr['2']=='z')
    		echo "<a href='?hod=2'>$z</a> ";
    		else if ($kr['2']=='o')
    		echo "<a href='?hod=2'>$o</a> ";
    		else if ($kr['2']=='x')
    		echo "<a href='?hod=2'>$x</a> ";


    		if ($kr['3']=='z')
    		echo "<a href='?hod=3'>$z</a> ";
    		else if ($kr['3']=='o')
    		echo "<a href='?hod=3'>$o</a> ";
    		else if ($kr['3']=='x')
    		echo "<a href='?hod=3'>$x</a> ";


    		if ($kr['4']=='z')
    		echo "<a href='?hod=4'>$z</a> ";
    		else if ($kr['4']=='o')
    		echo "<a href='?hod=4'>$o</a> ";
    		else if ($kr['4']=='x')
    		echo "<a href='?hod=4'>$x</a> ";


    		if ($kr['5']=='z')
    		echo "<a href='?hod=5'>$z</a> ";
    		else if ($kr['5']=='o')
    		echo "<a href='?hod=5'>$o</a> ";
    		else if ($kr['5']=='x')
    		echo "<a href='?hod=5'>$x</a> ";


    		if ($kr['6']=='z')
    		echo "<a href='?hod=6'>$z</a> ";
    		else if ($kr['6']=='o')
    		echo "<a href='?hod=6'>$o</a> ";
    		else if ($kr['6']=='x')
    		echo "<a href='?hod=6'>$x</a> ";


    		if ($kr['7']=='z')
    		echo "<a href='?hod=7'>$z</a> ";
    		else if ($kr['7']=='o')
    		echo "<a href='?hod=7'>$o</a> ";
    		else if ($kr['7']=='x')
    		echo "<a href='?hod=7'>$x</a> ";


    		if ($kr['8']=='z')
    		echo "<a href='?hod=8'>$z</a> ";
    		else if ($kr['8']=='o')
    		echo "<a href='?hod=8'>$o</a> ";
    		else if ($kr['8']=='x')
    		echo "<a href='?hod=8'>$x</a> ";


    		if ($kr['9']=='z')
    		echo "<a href='?hod=9'>$z</a> ";
    		else if ($kr['9']=='o')
    		echo "<a href='?hod=9'>$o</a> ";
    		else if ($kr['9']=='x')
    		echo "<a href='?hod=9'>$x</a> ";

    	}
    }else{

    	while ($kr = mysql_fetch_assoc($q))
    	{
    		if ($kr['1']=='z')
    		echo "$z ";
    		else if ($kr['1']=='o')
    		echo "$o ";
    		else if ($kr['1']=='x')
    		echo "$x ";


    		if ($kr['2']=='z')
    		echo "$z ";
    		else if ($kr['2']=='o')
    		echo "$o ";
    		else if ($kr['2']=='x')
    		echo "$x ";


    		if ($kr['3']=='z')
    		echo "$z ";
    		else if ($kr['3']=='o')
    		echo "$o ";
    		else if ($kr['3']=='x')
    		echo "$x ";


    		if ($kr['4']=='z')
    		echo "$z ";
    		else if ($kr['4']=='o')
    		echo "$o ";
    		else if ($kr['4']=='x')
    		echo "$x ";


    		if ($kr['5']=='z')
    		echo "$z ";
    		else if ($kr['5']=='o')
    		echo "$o ";
    		else if ($kr['5']=='x')
    		echo "$x ";


    		if ($kr['6']=='z')
    		echo "$z ";
    		else if ($kr['6']=='o')
    		echo "$o ";
    		else if ($kr['6']=='x')
    		echo "$x ";


    		if ($kr['7']=='z')
    		echo "$z ";
    		else if ($kr['7']=='o')
    		echo "$o ";
    		else if ($kr['7']=='x')
    		echo "$x ";


    		if ($kr['8']=='z')
    		echo "$z ";
    		else if ($kr['8']=='o')
    		echo "$o ";
    		else if ($kr['8']=='x')
    		echo "$x ";


    		if ($kr['9']=='z')
    		echo "$z ";
    		else if ($kr['9']=='o')
    		echo "$o ";
    		else if ($kr['9']=='x')
    		echo "$x ";

    	}
    }
    echo "</div>";


    echo "<a href='?' class='knopka'>Обновить поле</a><br />";

    echo '</div>';


    if ($game['read']==5 && $prot['id']==$user['id'])
    {
    	echo "<div class='mess'>";
    	echo "Ставка: ";
    	if ($game['stav']>0)echo "$game[stav] баллов!<br />";
    	else echo "Не используется!<br />";
    	echo "Вам предложение продолжить игру...<br /><a href='?plus=ok'>[<img src='/style/icons/ok.gif'> Да играть]</a> <a href='?plus=no' >[<img src='/style/icons/delete.gif'> Закончить]</a><br />";
      echo "</div>";
    }
    else if ($game['read']==5 && $avtor['id']==$user['id'])
    {
    	echo "<div class='mess'>";
    	echo "Ожидайте подтверждения второго игрока...";
    	echo "</div>";

    }

    	if (isset($_GET['plus']) && $game['read']==3)
    	{
    	echo "<div class='mess'>";
    	echo "<a href='?null=es' ><font color='red'>Обнулить ставку?</font></a>";
    	echo "</div>";
    	}

    if (($game['read']==3 || $game['read']==4) && ($avtor['id']==$user['id']))
    {
    	echo "<div class='mess'>";
    	echo "Вы можете продолжить игру... ";
    	echo "</div>";
    	echo "<div class='foot'>";
    	echo "<img src='/style/icons/ok.gif' alt='*' /> <a href='?plus' >Играть еще</a><br />";
    	echo "</div>";
    }

    $k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `kr_nol_3x3_komm` WHERE `id_game` = '$game[id]'"),0);

    $q=mysql_query("SELECT * FROM `kr_nol_3x3_komm` WHERE `id_game` = '$game[id]' ORDER BY `id` DESC LIMIT 5");

    echo "  <div class='foot'>\n";
    echo "Комментарии:";
    echo "  </div>\n";

    echo "<form method=\"post\" name='message' action=\"?\">\n";
    echo "<textarea name=\"msg\"></textarea><br />\n";
    echo "<input value=\"Отправить\" type=\"submit\"/>\n";
    echo "</form>\n";



    if ($k_post==0)
    {
    	echo "  <div class='mess' style='text-align:left;'>\n";
    	echo "Нет комментариев\n";
    	echo "  </div>\n";
    }

    $nnn=0;
    while ($post = mysql_fetch_assoc($q))
    {
    	if ($nnn==0)
    	{
    		echo "<div class='nav1'>";
    		$nnn = 1;
    	}
    	elseif ($nnn==1)
    	{
    		echo "<div class='nav2'>";
    		$nnn = 0;
    	}
    	//$ank_c=mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
    	$ank_c=get_user($post['id_user']);

    	echo "<b>$ank_c[nick]</b> (".vremja($post['time']).") &raquo; \n";

    	echo output_text($post['msg'])."";echo " <br />";
    	echo "</div>";
    }

    echo "<div class='foot'>";
    echo "<img src='/style/icons/delete.gif' alt='*' /> <a href='?exit=es' >Закончить игру</a><br />";
    echo "</div>";

}else{

if ($game_my==0)
{
	echo "  <div class='foot'>\n";
	echo "<img src='/style/icons/str2.gif' alt='*' /> <a href='/plugins/games/' >Онлайн игры</a> / Крестики - Нолики";
	echo "   </div>\n";

	echo "  <div class='nav1'>\n";
	echo "<center><img src='style/logo.png'></center>";
	echo "   </div>\n";

	echo "  <div class='foot' style='padding:9px;'>\n";
	echo "Онлайн пользователи";
	echo "</div>";

	$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` != '$user[id]' AND `date_last` > '".(time()-600)."'"), 0);
	$k_page=k_page($k_post,$set['p_str']);
	$page=page($k_page);
	$start=$set['p_str']*$page-$set['p_str'];
	$q = mysql_query("SELECT `id` FROM `user` WHERE `id` != '$user[id]' AND `date_last` > '".(time()-600)."' ORDER BY `date_last` DESC LIMIT $start, $set[p_str]");
	$nnn = 0;
	if ($k_post==0)
	{

		echo "  <div class='mess'>\n";
		echo "Сейчас на сайте никого нет\n";
		echo "   </div>\n";
	}
	while ($ank = mysql_fetch_assoc($q))
	{
		if ($nnn==0)
		{
			echo "<div class='nav1'>";
			$nnn = 1;
		}
		elseif ($nnn==1)
		{
			echo "<div class='nav2'>";
			$nnn = 0;
		}
    
		echo user::avatar($ank['id'], 2) . user::nick($ank['id'], 1, 1, 1) . "<br /> <a href='?us=$ank[id]' >Предложить играть</a>";
		echo "</div>";
	}



	if ($k_page>1)str("?",$k_page,$page); // Вывод страниц

}
	echo "  <div class='foot'>\n";
	echo "<img src='/style/icons/str2.gif' alt='*' /> <a href='/plugins/games/' >Онлайн игры</a> / Крестики - Нолики";
	echo "   </div>\n";
}

include_once '../../../sys/inc/tfoot.php';
?>