<?
include 'config.php';
include 'func.php';

head();
aut();
ban();
////////////////////////////////////////
mysql_query("UPDATE `profile` SET `online` = 'garem' WHERE `id` = '".$id_session."'");

echo head . "Гарем" . div;   

$act=htmlspecialchars(trim($_GET['act']));
$oke = htmlspecialchars(trim($_GET['oke']));
$ids = intval($_GET['id']);

switch ($act) {

case 'reyt':
$all=mysql_result(mysql_query("SELECT COUNT(*) FROM `profile` WHERE `aa2` > '0'"), 0);

if ($all==0) echo link1 . "Нет гаремов." . div;

if($all){
$onpage = 5;
$allpage = ceil($all/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;
$result = mysql_query("SELECT * FROM `profile` WHERE `aa2` > '0' ORDER BY `aa2` DESC limit ".num($start).",".num($onpage)."");
while ($us = mysql_fetch_array($result)) {

    echo link2 . user($us['id']) . '<br/>';

    echo "<a href='garem.php?act=garem&id=".$us['id']."'>В гареме</a> <strong>".$us['aa2']."</strong> чел." . div;

}
echo link1 . '<b>Страницы:</b> ';
navigation($allpage,"garem.php?act=reyt&p={p}",1,1);
echo div;
}
break;

case 'spisok':
$all=mysql_result(mysql_query("SELECT COUNT(*) FROM `profile` WHERE `aa1` = '".$ids."'"), 0);

if ($all==0) echo link1 . "Гарем пуст." .div;

if($all){
$onpage = 5;
$allpage = ceil($all/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;
$result = mysql_query("SELECT * FROM `profile` WHERE `aa1` = '".$ids."' limit ".num($start).",".num($onpage)."");
while ($us = mysql_fetch_array($result)) {
    $cena = (($us['aa3']*10)+25);
    if($us['upload_ava'])
        echo link2 . '<img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width='.$ava_w.'&height" alt=""/><br/>';
    else 
        echo link2 . '<img src="resize.php?img=noava.png&width='.$ava_w.'&height" alt=""/><br/>';
    if ($us['aa1'] == $id_session)
    echo user($us['id']) . ' ['.$cena.' монет] | <a href="garem.php?act=otpusk&id='.$us['id'].'">Отпустить</a>' . div;
    else
    echo user($us['id']) . ' ['.$cena.' монет]' . div;
}
echo div . link1 . '<b>Страницы:</b> ';
navigation($allpage,"garem.php?act=spisok&id=".$ids."&p={p}",1,1);
echo div;
}
break;

case 'reytg':
echo link2 . 'Сюда попадают пользователи, которых украли наибольшее количество раз' . div;
$all=mysql_result(mysql_query("SELECT COUNT(*) FROM `profile` WHERE `aa3` > '0'"), 0);
if ($all==0) echo link1 . "Нет украденных" . div;

if($all){
$onpage = 5;
$allpage = ceil($all/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;
$result = mysql_query("SELECT * FROM `profile` WHERE `aa3` > '0' ORDER BY `aa3` DESC limit ".num($start).",".num($onpage)."");
while ($us = mysql_fetch_array($result)) {
        if($us['upload_ava'])
        echo link2 . '<img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width='.$ava_w.'&height" alt=""/><br/>';
    else 
        echo link2 . '<img src="resize.php?img=noava.png&width='.$ava_w.'&height" alt=""/><br/>';
echo user($us['id']) . '<br/>';

if($us['aa3'] == 0) $cena = 25;

if($us['aa3'] == 1) $cena = 35;
else
$cena = (($us['aa3']*10)+25);
echo "Украден".$a.": <strong>".$us['aa3']."</strong> раз. | Цена: ".$cena." монет" . div;
}
echo div . link1 . '<b>Страницы:</b> ';
navigation($allpage,"garem.php?act=reytg&p={p}",1,1);
echo div;
}
break;


case 'garem':
$m=mysql_result(mysql_query("SELECT COUNT(*) FROM `profile` WHERE `id` = $ids"),0);
if (empty($m)) 
{
    echo "Нет такого пользователя<br/>";
} else {
$all=mysql_result(mysql_query("SELECT COUNT(*) FROM `profile` WHERE `aa1` = '$ids'"), 0);



$ankers = mysql_fetch_array(mysql_query("SELECT * FROM `profile` WHERE `id` = $ids LIMIT 1"));
echo link1 . "<center>Гарем <strong>".$ankers['login']."</strong></center>\n" . div;

if ($all==0) echo link1 . "Нет украденных" . div;

if($all){
$onpage = 5;
$allpage = ceil($all/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;

$result = mysql_query("SELECT * FROM `profile` WHERE `aa1` = '$ids' ORDER BY `aa3` DESC limit ".num($start).",".num($onpage)."");
while ($ank = mysql_fetch_array($result))
{
    if($ank['upload_ava'])
        echo link2 . '<img src="resize.php?img=upload_ava/'.$ank['upload_ava'].'&width='.$ava_w.'&height" alt=""/><br/>';
    else 
        echo link2 . '<img src="resize.php?img=noava.png&width='.$ava_w.'&height" alt=""/><br/>';
echo  user($ank['id']) ." | Украден".$a.": <strong>".$ank['aa3']."</strong> раз." . div;

}
echo div . link1 . '<b>Страницы:</b> ';
navigation($allpage,"garem.php?act=garem&id=".$ids."&p={p}",1,1);
echo div;
}

}
break;

case 'otpusk':
$ank = mysql_fetch_array(mysql_query("SELECT * FROM `profile` WHERE `id` = $ids LIMIT 1"));
$user = mysql_fetch_array(mysql_query("SELECT * FROM `profile` WHERE `id` = $id_session LIMIT 1"));

if ($ank['aa1'] != $id_session) {
    echo link1 . 'Пользователь не состоит в вашем гареме' . div;
} else {
        $c = $user['aa2'] - 1;
        if (mysql_query("UPDATE profile SET aa2 = '".$c."' WHERE id='".$id_session."';")) {
            echo link1 . 'Вы отпустили пользователя '.$ank[login].' из вашего гарема' . div;
                mysql_query("UPDATE profile SET aa1 = '0' WHERE id='".$ank['id']."';");
                $msg = 'Пользователь '.$login_session.' отпустил вас из своего гарема';
                mysql_query("INSERT INTO `mail` SET `to_user` = '".$ank['id']."', `ot_user` = '".$id_session."', `msg` = '".$msg."', `status` = 0, `date` = '".(time() + 15)."'");
            } else {
                echo link1 . 'Возникла ошибка' . div;
            }
}
break;

case 'kraz':

$ank = mysql_fetch_array(mysql_query("SELECT * FROM `profile` WHERE `id` = $ids LIMIT 1"));
if($ank['sex'] == $user['sex'])
{
echo link1 . 'Разрешено красть только людей противоположного пола!' . div;
navig();
foot();
exit;
}

$ank = mysql_fetch_array(mysql_query("SELECT * FROM `profile` WHERE `id` = $ids LIMIT 1"));
if($ank['aa1'] == $user['id'])
{
echo link1 . 'Пользователь уже состоит в вашем гареме!' . div;
navig();
foot();
exit;
}

if($ank['sett_garem'] == 1)
{
echo link1 . 'Вы не можете купить пользователя '.$ank['login'].', так как он/она отключилась от модуля Гарем' . div;
navig();
foot();
exit;
}

if($ank['aa3'] == 0)
{
$cena = 25;
} else if($ank['aa3'] == 1) {
$cena = 35;
} else {
$cena = (($ank['aa3']*10)+25);
}
if($oke == 'ok')
{
if($user['many'] < $cena)
{
echo link1 . 'У вас недостаточно монет!' . div;
navig();
foot();
exit;
}


if ($user['sex']==1)
{
$a = '';
} else {
$a = 'a';
}
if ($ank['sex']==1)
{
$apt = 'его';
} else {
$apt = 'её';
}

$m = $ank['aa3'] + 1;
$b = $user['aa2'] + 1;
$many = $many_session - $cena;
$msg = 'Вас украли в гарем к '.$user['login'].' весёлого новоселья.';
mysql_query("INSERT INTO `mail` SET `to_user` = '".$ank['id']."', `ot_user` = '".$id_session."', `msg` = '".$msg."', `status` = 0, `date` = '".(time() + 15)."'");
mysql_query("UPDATE `profile` SET `many` = '".$many."', `aa2` = '".$b."' WHERE `id` = '".$user['id']."';");
mysql_query("UPDATE `profile` SET `aa3` = '".$m."', `aa1` = '".$user['id']."' WHERE `id` = '".$ank['id']."';");
$msg1 = '<b>'.user($id_session).'</b> купил себе в гарем <b>'.user($ank['id']).'</b>';
mysql_query("INSERT INTO `jurnal` SET  `msg` = '".$msg1."', `user` = '".$id_session."', `date` = '".time()."'");
if(intval($ank['aa1']) != 0)
{
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$ank['aa1']."'"));
    $c = $us['aa2'] - 1;
mysql_query("UPDATE profile SET aa2 = '".$c."' WHERE id='".$us['id']."';");
$msg = 'Пользователь '.$user['login'].' украл'.$a.' у вас из гарема жителя: '.$ank['login'].', удачи отвоевать '.$apt.' обратно ;)';
mysql_query("INSERT INTO `mail` SET `to_user` = '".$us['id']."', `ot_user` = '".$id_session."', `msg` = '".$msg."', `status` = 0, `date` = '".(time() + 15)."'");

}
echo link1 . 'Успешно! Желаем тебе большого гарема ;)';
} else {
echo link2 . 'Вы хотите украсть <a href="info.php?id='.$ids.'"><strong>'.$ank['nick'].'</strong></a><br />
Стоимость: <strong>'.$cena.'</strong> монет.<br />
<a href="garem.php?act=kraz&oke=ok&id='.$ids.'"><strong>Украсть</strong></a>' . div;
}

break;

default:
if($user['sex'] == 0) $pol = 1; else $pol = 0;
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `sex` = '".$pol."' AND `time` > '".time()."'ORDER BY RAND() DESC LIMIT 1"));

echo link1 . '<center><big><b>' . $us['login'] . '</big></b></center>' . div;
if($us['upload_ava']){
    $ava = '<a href="profile.php?id='.$us['id'].'"><img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width=160&height" alt=""/></a>';
} else {
    $ava = '<a href="profile.php?id='.$us['id'].'"><img src="resize.php?img=noava.png&width=160&height" alt=""/></a>';
}

echo link2 . '<center>';
echo $ava . '<br/>';

if($us['aa3'] == 0) $cena = 25;
elseif($us['aa3'] == 1) $cena = 35;
else
$cena = (($us['aa3']*10)+25);

echo '<b>Цена:</b> ' . $cena . ' монет<br/>';

if ($us['aa1'] != $id_session)
    echo '<b><a href="garem.php?act=kraz&id='.$us['id'].'">Купить</a></b><br/>';
echo '<a href="?">Пропустить</a><br/>';
echo '</center>' . div;
echo link2 . '<a href="garem.php?act=spisok&id='.$id_session.'">Мой гарем</a><br/>';
echo '<a href="garem.php?act=reyt">Рейтинг гаремов</a><br/>';
echo '<a href="garem.php?act=reytg">Самые дорогие</a>' . div;
break;
}


/////////////////////////////////////////

navig();
foot();
?>