<?

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';
only_reg();

$set['title']='Дуэли'; // заголовок страницы
include_once '../sys/inc/thead.php';
title();

include_once 'inc.php';

if (isset($_POST['golos']) && isset($_POST['duel_id']) && isset($user))
{
$id = intval($_POST['duel_id']);
if (isset($_POST['vote_user'])) {
$opp = intval($_POST['vote_user']);
}elseif (isset($_POST['vote_opponent'])) {
$opp = intval($_POST['vote_opponent']);
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `duel` WHERE `id` = $id"),0)==0){$err = 'Нет такой дуэли';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = $id AND `id_user` = $user[id]"),0)!=0){$err = 'Вы уже голосовали за эту дуэль';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `duel` WHERE (`user`='$user[id]' OR `opponent`='$user[id]') AND `id` = $id"),0)!=0){$err = 'Нельзя голосовать за свою дуэль';}

if(!isset($err)){
mysql_query("INSERT INTO `duel_vote` (`id_user`, `id_opponent`, `id_duel`, `man`) VALUES ('".$user['id']."', '".$opp."', '".$id."', '".$user['pol']."')");
$msg = 'За вас проголосовали в [url=/duel/myduels.php]дуэле[/url]';
mysql_query("INSERT INTO `jurnal` (`id_user`, `id_kont`, `msg`, `time`) values('0', '".$opp."', '".$msg."', '".$time."')");
}
}

if (isset($_GET['no']) && isset($user))
{
$id = intval($_GET['no']);

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `duel` WHERE `id` = $id"),0)==0){$err = 'Нет такой дуэли';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = $id AND `id_user` = $user[id]"),0)!=0){$err = 'Вы уже голосовали за эту дуэль';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `duel` WHERE (`user`='$user[id]' OR `opponent`='$user[id]') AND `id` = $id"),0)!=0){$err = 'Нельзя голосовать за свою дуэль';}

if(!isset($err)){
mysql_query("INSERT INTO `duel_vote` (`id_user`, `id_opponent`, `id_duel`, `man`) VALUES ('".$user['id']."', '0', '".$id."', '".$user['pol']."')");
}
}

err();


$num=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel` WHERE (SELECT COUNT(*) FROM `duel_vote` WHERE `duel_vote`.`id_duel` = `duel`.`id` AND `id_user` = '$user[id]') = 0 AND `duel`.`act` = '1' AND `opponent` != '$user[id]' AND `user` != '$user[id]'"),0);

if($num!=0){
$post=mysql_fetch_array(mysql_query("SELECT * FROM `duel` WHERE (SELECT COUNT(*) FROM `duel_vote` WHERE `duel`.`id` = `duel_vote`.`id_duel` AND `id_user` = '$user[id]') = 0  AND `act` = '1' AND `opponent` != '$user[id]' AND `user` != '$user[id]' ORDER by rand()"));
$rand = rand(1000,9999);
echo "<div class='rowup'>";
echo "<a href=\"index.php?$rand\">Следующая &gt;&gt;</a><br/>";

if($post['rubric'] == 1) $cat = 'Кто красивее?';
if($post['rubric'] == 2) $cat = 'Кто сильнее?';
if($post['rubric'] == 3) $cat = 'Кто моднее?';
if($post['rubric'] == 4) $cat = 'Кто выглядит моложе?';
if($post['rubric'] == 5) $cat = 'Кто смешнее?';
if($post['rubric'] == 6) $cat = 'У кого лучше фигура?';
if($post['rubric'] == 7) $cat = 'Кто сексуальнее?';
if($post['rubric'] == 8) $cat = 'Кто гламурнее?';
if($post['rubric'] == 9) $cat = 'Кто аппетитнее?';

echo "<b>Категория:</b> <u><b>$cat</b></u><br/>";
echo "</div>";

$us = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".$post['user']."' LIMIT 1"));
$count=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]'"),0);

$vote=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]' AND `id_opponent` = '$us[id]'"),0);
$man=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]' AND `id_opponent` = '$us[id]' AND `man` = '1'"),0);
$woman=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]' AND `id_opponent` = '$us[id]' AND `man` = '0'"),0);
echo "<div class='rowdown'>\n";
echo "Оппонент: <a href=\"/info.php?id=$us[id]\">$us[nick]</a><br/>\n";
avatar($post['user']);
echo "<br/>---<br/>\n";
echo "<b>Голосов:</b> $vote (".@round($vote/$count*100,1)."%) <br />\n";
echo "M: (".@round($man/$vote*100,1)."%) / Ж: (".@round($woman/$vote*100,1)."%)<br />\n";
echo "<form action=\"index.php?$rand\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"vote_user\" value=\"$us[id]\"/>\n";
echo "<input type=\"hidden\" name=\"duel_id\" value=\"$post[id]\"/>\n";
echo "<input type=\"submit\" name=\"golos\" class=\"form\" value=\"Голосовать\"/>\n";
echo "</form>\n";
echo "</div>\n";

echo "<div class='rowup'>\n";
echo "<a href=\"index.php?no=$post[id]\">Затрудняюсь ответить &gt;&gt;</a><br/>\n";
echo "</div>\n";

$us2 = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".$post['opponent']."' LIMIT 1"));

$vote2=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]' AND `id_opponent` = '$us2[id]'"),0);
$man2=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]' AND `id_opponent` = '$us2[id]' AND `man` = '1'"),0);
$woman2=mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_vote` WHERE `id_duel` = '$post[id]' AND `id_opponent` = '$us2[id]' AND `man` = '0'"),0);
echo "<div class='rowdown'>\n";
echo "Оппонент: <a href=\"/info.php?id=$us2[id]\">$us2[nick]</a><br/>\n";
avatar($post['opponent']);
echo "<br/>---<br/>\n";
echo "<b>Голосов:</b> $vote2 (".@round($vote2/$count*100,1)."%) <br />\n";
echo "M: (".@round($man2/$vote2*100,1)."%) / Ж: (".@round($woman2/$vote2*100,1)."%)<br />\n";
echo "<form action=\"index.php?$rand\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"vote_opponent\" value=\"$us2[id]\"/>\n";
echo "<input type=\"hidden\" name=\"duel_id\" value=\"$post[id]\"/>\n";
echo "<input type=\"submit\" name=\"golos\" class=\"form\" value=\"Голосовать\"/>\n";
echo "</form>\n";
echo "</div>\n";

echo '<div class="str">Время окончания: <strong>'.vremja($post['time']).'</strong></div>';
}
else
{
echo '<div class="str">Нет активных дуэлей для голосований</div>';
}

echo '<div class="page_foot">
- <a href="myduels.php">Моя дуэль</a><br/>
- <a href="voted.php">Активные (Вы голосовали)</a><br/>
- <a href="finished.php">Завершенные</a><br/></div>';

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