<?php
define('ROOT','../');
define('GCMS',true);
require_once (ROOT.'system/power.php');
if ($id) {
$type = $db -> query("SELECT * FROM `chat` WHERE `id` = '".$id."'");
if ($type -> num_rows > 0) $the = $type -> fetch_assoc(); else $the = false;
} else $the = false;
if($the != false){
////Боты
$bot = $db -> query("SELECT * FROM `chat_bots` WHERE `room` = '".$id."'")-> fetch_assoc();
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////умник////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
if($bot['id']=='1'){
$v = $db -> query("SELECT * FROM `chat_vik` WHERE `id` = '".$bot['sid']."'")-> fetch_assoc();
///задаем вопрос
if($bot['status']=='1'){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Вопрос: ".$v['vopros']." (".mb_strlen($v['otvet'])." буквы)',`time` = '".time()."'");
$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='2',`doptime`='".time()."' WHERE `id` = '".$bot['id']."'");
}
$botset = $db -> query("SELECT * FROM `chat_bots` WHERE `room` = '".$id."'")-> fetch_assoc();
$u = $db -> dbcount('COUNT(*)','chat_post',"`time`>$botset[doptime] and `text`=' $v[otvet]'");
//даём 1 подсказку
if($u==0 and $bot['status']=='2' and time()>=$bot['time']){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Подсказка: ".mb_substr($v['otvet'],0,1)."',`time` = '".time()."'");
$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='3' WHERE `id` = '".$bot['id']."'");
}
///даём 2 подсказку
if($u==0 and $bot['status']=='3' and time()>=$bot['time']){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Подсказка: ".mb_substr($v['otvet'],0,2)."',`time` = '".time()."'");

$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='4' WHERE `id` = '".$bot['id']."'");
}
///ни кто неугадал
if($u==0 and $bot['status']=='4' and time()>=$bot['time']){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Ответ ни кто не дал,следующий вопрос через ".$bot['int']." сек.',`time` = '".time()."'");
$db -> query("UPDATE `chat_bots` SET `status`='5' WHERE `id` = '".$bot['id']."'");
}
///Отгадываем слово
if($u>0 and $bot['status']!='5' and $bot['status']!='6'){
$up=$db -> query("SELECT * FROM `chat_post` WHERE `time`>$botset[doptime] and `text`=' $v[otvet]'")->fetch_assoc();
$db -> query("UPDATE `users` SET `ym`=ym+1,`balls`=balls+$nastr[ym] WHERE `login` = '".$up['yaid']."'");
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = '".$up['yaid']." угадывает слово!Следующий вопрос через ".$bot['int']." сек.',`time` = '".time()."'");
$db -> query("UPDATE `chat_bots` SET `status`='6' WHERE `id` = '".$bot['id']."'");
}
if((time()>=$bot['time']+$bot['int']*4)and($bot['status']=='5' or $bot['status']=='6')){
$v_up = $db -> dbcount('COUNT(*)','chat_vik',"`id`='".($bot['sid']+1)."'");
if($v_up>0){
$db -> query("UPDATE `chat_bots` SET `status`='1',`sid`=sid+1 WHERE `id` = '".$bot['id']."'");}
else{$db -> query("UPDATE `chat_bots` SET `status`='1',`sid`=1 WHERE `id` = '".$bot['id']."'");}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////ФУРА////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
if($bot['id']=='2'){
$v = $db -> query("SELECT * FROM `chat_god` WHERE `id` = '".$bot['sid']."'")-> fetch_assoc();
///задаем вопрос
if($bot['status']=='1'){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Отгадайте слово из четырёх букв!',`time` = '".time()."'");
$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='2',`doptime`='".time()."' WHERE `id` = '".$bot['id']."'");
}
$botset = $db -> query("SELECT * FROM `chat_bots` WHERE `room` = '".$id."'")-> fetch_assoc();
$u = $db -> dbcount('COUNT(*)','chat_post',"`time`>$botset[doptime] and `text`=' $v[otvet]'");
//даём 1 подсказку
if($u==0 and $bot['status']=='2' and time()>=$bot['time']){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Подсказка: ".mb_substr($v['otvet'],0,1)."',`time` = '".time()."'");
$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='3' WHERE `id` = '".$bot['id']."'");
}
///даём 2 подсказку
if($u==0 and $bot['status']=='3' and time()>=$bot['time']){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Подсказка: ".mb_substr($v['otvet'],0,2)."',`time` = '".time()."'");

$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='4' WHERE `id` = '".$bot['id']."'");
}
///ни кто неугадал
if($u==0 and $bot['status']=='4' and time()>=$bot['time']){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = 'Ответ ни кто не дал,следующий вопрос через ".$bot['int']." сек.',`time` = '".time()."'");
$db -> query("UPDATE `chat_bots` SET `status`='5' WHERE `id` = '".$bot['id']."'");
}
///Отгадываем слово
if($u>0 and $bot['status']!='5' and $bot['status']!='6'){
$up=$db -> query("SELECT * FROM `chat_post` WHERE `time`>$botset[doptime] and `text`=' $v[otvet]'")->fetch_assoc();
$db -> query("UPDATE `users` SET `ym`=ym+1,`balls`=balls+$nastr[ym] WHERE `login` = '".$up['yaid']."'");
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = '".$up['yaid']." угадывает слово!Следующий вопрос через ".$bot['int']." сек.',`time` = '".time()."'");
$db -> query("UPDATE `chat_bots` SET `status`='6' WHERE `id` = '".$bot['id']."'");
}
if((time()>=$bot['time']+$bot['int']*4)and($bot['status']=='5' or $bot['status']=='6')){
$v_up = $db -> dbcount('COUNT(*)','chat_god',"`id`='".($bot['sid']+1)."'");
if($v_up>0){
$db -> query("UPDATE `chat_bots` SET `status`='1',`sid`=sid+1 WHERE `id` = '".$bot['id']."'");}
else{$db -> query("UPDATE `chat_bots` SET `status`='1',`sid`=1 WHERE `id` = '".$bot['id']."'");}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////ШУТ////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
if($bot['id']=='3'){
$v = $db -> query("SELECT * FROM `chat_shut` WHERE `id` = '".$bot['sid']."'")-> fetch_assoc();
///задаем вопрос
if($bot['status']=='1'){
$db -> query("INSERT INTO `chat_post` SET `room` = '".(int)$id."', `yaid` = '".$bot['name']."', `text` = '$v[text]',`time` = '".time()."'");
$time=time()+$bot['int'];
$db -> query("UPDATE `chat_bots` SET `time`='".$time."',`status`='2',`doptime`='".time()."' WHERE `id` = '".$bot['id']."'");
}
if((time()>=$bot['time']+$bot['int']*4)and($bot['status']=='2')){
$v_up = $db -> dbcount('COUNT(*)','chat_shut',"`id`='".($bot['sid']+1)."'");
if($v_up>0){
$db -> query("UPDATE `chat_bots` SET `status`='1',`sid`=sid+1 WHERE `id` = '".$bot['id']."'");}
else{$db -> query("UPDATE `chat_bots` SET `status`='1',`sid`=1 WHERE `id` = '".$bot['id']."'");}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
$diz -> head('Комната '.$the['name'].'');
$diz -> title("$the[topik]");
$count = $db -> dbcount('COUNT(`id`)','chat_post',"`room` = '".$id."'");
$num = isset($ya['num']) ? (int)$ya['num'] : 10;
$total = intval(($count - 1) / $num) + 1; 
if (!isset($_GET['page']) || !is_numeric($_GET['page']) || $_GET['page'] < 1 )
{
$page = 1;
} elseif($_GET['page'] > $total)
{ $page = $total;
} else $page = (int)$_GET['page'];
$start = $page * $num - $num;
$who = $db -> dbcount('COUNT(`login`)','online',"`where` = 'chat' and `idwh` = '$id'");
if ($aut) {$msg .= '<div class="tochechki"><a href="newpost.php?id='.(int)$id.'&amp;page='.(int)$page.'">Сказать</a> | <a href="'.ROOT.'help/smile.php">Смайлы</a>
 | <a href="./who.php?mode=room&amp;id='.$id.'">Кто здесь?</a> ('.$who.')</div>';
 if($ya['chatobon']=='1')header('refresh: '.$ya['chatob'].'; url=room.php?id='.$id.'');
 }
 if($s!=null){
 $msg.='<div class="page">'.$s.'';
 if($p!=null){$msg.='<br/>'.$p;}
 $msg.='</div>';
 }
$for = $db -> query("SELECT * FROM `chat_post` WHERE `room` = '".(int)$id."' ORDER BY `time` DESC LIMIT ".$start.",".$num);
if($count==0){$msg.='<div class="fon">Сообщений пока нет</div>';}
while ($assoc = $for -> fetch_assoc()) {
$arr = $db -> query("SELECT `id` FROM `users` WHERE `login` = '$assoc[yaid]' LIMIT 1")-> fetch_assoc();
$otv = null;
if ($aut) {
if ($ya['level'] > 3) {
$del = '| <a href="delpost.php?id='.$assoc['id'].'">[Удал]</a>';}
if ($ya['id']!=$arr['id'] and $assoc['yaid']!=$bot['name']) {
$otv = '<a href="newpost.php?id='.$id.'&amp;otv='.$arr['id'].'&amp;page='.$page.'">'.$assoc['yaid'].'</a>';
}else{$otv = ''.$assoc['yaid'].'';
}
}
$msg .= '<div class="top"> '.$otv.','.$func -> times(date('H:i:s',$assoc['time'])).''.$del.'</div>';
$msg.='<div class="fon">'.$func -> smiles($assoc['text']).'</div>';
}
$db -> query("UPDATE `online` SET `where` = 'chat',`idwh` = '$id' WHERE `login` = '".$ya['login']."' limit 1");
$msg .= '<div class="fon"><form action="newpost.php?id='.$id.'&amp;page='.$page.'" method="post" enctype="multipart/form-data"><div>';
$msg .='<hr><textarea name="text" rows="1" cols="15"></textarea><br/>
<input type="submit" value="Сказать"/>
</div></form>';
$v = $db -> query("SELECT * FROM `chat_vik` WHERE `id` = '".$bot['sid']."'")-> fetch_assoc();
$act = 'room.php?id='.$id;
$msg .= ''.$func -> pagenav($act,$page,$total).'';}
 else header('location: index.php');
$msg .= '</div><div class="levo">'.$diz -> img('back.png').'<a href="index.php">Комнаты</a>';
$msg .= '</br>'.$diz -> img('home.png').'<a href="/index.php">На главную</a></div>';
#####Копирайт#######
require_once (ROOT.'system/kopy.php');
$diz -> out($msg);
?>