<?

$a = $db->sql_query ("Select * from vopros where ids ='3'");
$b = $db->sql_fetchrow ($a);
$nom = $b['number'];
$vr = $b['time'];
$answ = $b['answer'];
$amsg = rus_to_k($msg);
$tran = $b['tran'];

$nik_zag = '<font color="blue"><b>Загадай-ка</b></font>';

if ($msg=='!загадка'||$msg=='!zagadka'){
$r = $db->sql_query ("select * from vopros where ids ='3'");
$a = $db->sql_fetchrow($r);
$vp = $a ['question'];
$i = $a ['number'];
if ($i!=5){
$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,emo,bb,color) VALUES ('<b>$nik_zag</b>','0','".($time+1)."','".$vp."','$rm','0','0','0')");
}else{
$vp = 'Загадка еще не задана...';
$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,emo,bb,color) VALUES ('<b>$nik_zag</b>','0','".($time+1)."','".$vp."','$rm','0','0','0')");
}
}

if ($msg=='!ответ'||$msg=='!otvet'){
$b = $db->sql_fetchrow ($db->sql_query ("Select answer from vopros"));
$user['byeotv']++;
if ($rooms['baryga']!=0){
if ($user['posts']>=$set['cena_answ']){
$db->sql_query ("Update users set posts='".($user['posts']-$set['cena_answ'])."', byeotv='".$user['byeotv']."' where id='".$user["id"]."'");

$mes = "Правильный ответ на текущую загадку: [b]".$b["answer"]."[/b] =)";
$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,komu,privat) VALUES (''<b>$nik_zag</b>'','0','".($time+1)."','".$mes."','$rm','$user[id]','$user[id]')");

}else{

$mes = 'У вас недостаточно постов! Ответ стоит [b]'.$set['cena_answ'].'[/b] постов!';
$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,komu,privat) VALUES (''<b>$nik_zag</b>'','0','".($time+1)."','".$mes."','$rm','$user[id]','$user[id]')");

}

}else{

$mes = "Извините, но в этой комнате я не работаю...";
$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,komu,privat) VALUES (''<b>$nik_zag</b>'','0','".($time+1)."','".$mes."','$rm','$user[id]','$user[id]')");

}

}
$s = substr ($msg,0,5);
if (($s == "stats")&&(strlen($msg) > 6)){
$stsus = substr ($msg,6,strlen($msg)-6);
$a = @$db->sql_query ("Select credits2 from users where user='".$stsus."'");
if (mysql_affected_rows() != 0){

$b = $db->sql_fetchrow($a);
$i = $b["credits2"];
$a = @$db->sql_query ("Select * from users order by credits2 desc LIMIT 101");
$j = 1;
$b = $db->sql_fetchrow($a);

while (($b["user"] != $stsus)&&($j <= 100)) {$b = $db->sql_fetchrow($a); $j++;}
if ($j<=100) $s= "И он занимает в рейтинге $j место";
else $s = "И его место в рейтинге за пределами первой сотни.";
$mes = "У пользователя $stsus $i правильных ответов. $s";


$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,emo,bb,color) VALUES ('<b>$nik_zag</b>','0','".($time+1)."','".$mes."','$rm','0','0','0')");

} else {

$mes = "Игрока с ником $stsus нет в базе данных!";


$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,emo,bb,color) VALUES ('<b>$nik_zag</b>','0','".($time+1)."','".$mes."','$rm','0','0','0')");

}
}

////$answ = iconv('windows-1251', 'utf-8', $answ);
function utf8_to_win1251($text)
 {
    $text=str_replace("\xD0\xB0","а",$text);  $text=str_replace("\xD0\x90","А",$text);
    $text=str_replace("\xD0\xB1","б",$text);  $text=str_replace("\xD0\x91","Б",$text);
    $text=str_replace("\xD0\xB2","в",$text);  $text=str_replace("\xD0\x92","В",$text);
    $text=str_replace("\xD0\xB3","г",$text);  $text=str_replace("\xD0\x93","Г",$text);
    $text=str_replace("\xD0\xB4","д",$text);  $text=str_replace("\xD0\x94","Д",$text);
    $text=str_replace("\xD0\xB5","е",$text);  $text=str_replace("\xD0\x95","Е",$text);
    $text=str_replace("\xD1\x91","ё",$text);  $text=str_replace("\xD0\x81","Ё",$text);
    $text=str_replace("\xD0\xB6","ж",$text);  $text=str_replace("\xD0\x96","Ж",$text);
    $text=str_replace("\xD0\xB7","з",$text);  $text=str_replace("\xD0\x97","З",$text);
    $text=str_replace("\xD0\xB8","и",$text);  $text=str_replace("\xD0\x98","И",$text);
    $text=str_replace("\xD0\xB9","й",$text);  $text=str_replace("\xD0\x99","Й",$text);
    $text=str_replace("\xD0\xBA","к",$text);  $text=str_replace("\xD0\x9A","К",$text);
    $text=str_replace("\xD0\xBB","л",$text);  $text=str_replace("\xD0\x9B","Л",$text);
    $text=str_replace("\xD0\xBC","м",$text);  $text=str_replace("\xD0\x9C","М",$text);
    $text=str_replace("\xD0\xBD","н",$text);  $text=str_replace("\xD0\x9D","Н",$text);
    $text=str_replace("\xD0\xBE","о",$text);  $text=str_replace("\xD0\x9E","О",$text);
    $text=str_replace("\xD0\xBF","п",$text);  $text=str_replace("\xD0\x9F","П",$text);
    $text=str_replace("\xD1\x80","р",$text);  $text=str_replace("\xD0\xA0","Р",$text);
    $text=str_replace("\xD1\x81","с",$text);  $text=str_replace("\xD0\xA1","С",$text);
    $text=str_replace("\xD1\x82","т",$text);  $text=str_replace("\xD0\xA2","Т",$text);
    $text=str_replace("\xD1\x83","у",$text);  $text=str_replace("\xD0\xA3","У",$text);
    $text=str_replace("\xD1\x84","ф",$text);  $text=str_replace("\xD0\xA4","Ф",$text);
    $text=str_replace("\xD1\x85","х",$text);  $text=str_replace("\xD0\xA5","Х",$text);
    $text=str_replace("\xD1\x86","ц",$text);  $text=str_replace("\xD0\xA6","Ц",$text);
    $text=str_replace("\xD1\x87","ч",$text);  $text=str_replace("\xD0\xA7","Ч",$text);
    $text=str_replace("\xD1\x88","ш",$text);  $text=str_replace("\xD0\xA8","Ш",$text);
    $text=str_replace("\xD1\x89","щ",$text);  $text=str_replace("\xD0\xA9","Щ",$text);
    $text=str_replace("\xD1\x8A","ъ",$text);  $text=str_replace("\xD0\xAA","Ъ",$text);
    $text=str_replace("\xD1\x8B","ы",$text);  $text=str_replace("\xD0\xAB","Ы",$text);
    $text=str_replace("\xD1\x8C","ь",$text);  $text=str_replace("\xD0\xAC","Ь",$text);
    $text=str_replace("\xD1\x8D","э",$text);  $text=str_replace("\xD0\xAD","Э",$text);
    $text=str_replace("\xD1\x8E","ю",$text);  $text=str_replace("\xD0\xAE","Ю",$text);
    $text=str_replace("\xD1\x8F","я",$text);  $text=str_replace("\xD0\xAF","Я",$text);
    $text=str_replace("\xe2\x84\x96","№",$text);
    return $text;
 }
////$answ = utf8_to_win1251($answ);
/*$answ = iconv('utf-8', 'windows-1251', $answ);
$answ = substr($answ,0,512);
$answ = iconv('windows-1251', 'utf-8', $answ);*/

if ($msg == $answ){

$agent = htmlentities(addslashes($soft));
if (((strpos ($agent,"M3Gate") !== false)||(strpos ($agent,"Opera") !== false)||(strpos ($agent,"emulator") !== false)||(strpos ($agent,"WinWAP") !== false)||(strpos ($agent,"Wapsilon") !== false)||(strpos ($agent,"Mozilla") !== false)||(strpos ($agent,"M3GATE") !== false))&&($set["vict"] == 0)){

$mes = "Вы правильно ответили на эту загадку. Но с компа ответы не засчитываются.LoL(:";

$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,komu,privat) VALUES (''<b>$nik_zag</b>'','0','".($time+1)."','".$mes."','$rm','$user[id]','$user[id]')");

}else{

$victint = $set['victint'];
$st = $time + $victint;
if ($victint=="10") $interval="10 секунд";
else if ($victint=="30") $interval="30 секунд";
else if ($victint=="60") $interval="минуту";
else $interval="2 минуты";
$db->sql_query ("Update vopros set number = '5', time = '".$st."', answer = ' ', tran = ' ' WHERE ids ='3'");
$st = getmicrotime();

#cred_add();

$today=date ("H:i");
$creditss1=$user["credits2"];
if($user["sex"]==1)$sex1="его";
else if($user["sex"]==0)$sex1="её";
else $sex1="их";
$mes = "[b]".$user['user']."[/b],Правильно отвечает на загадку! Следующая <b>Загадка</b> через ".$interval.".";
mysql_query("UPDATE `users` SET `credits2` = `credits2` + '1' WHERE `id` = '$user[id]'");
$rnd = rand(0,99999999);


$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,emo,bb,color) VALUES ('<b>$nik_zag</b>','0','".($time+1)."','".$mes."','$rm','0','0','1')");

}
}/*
else
{
$mes = $msg.' отличаеться от '. $answ;
$db->sql_query ("INSERT INTO message (user,user_id,time,msg,rm,emo,bb,color) VALUES ('<b>$nik_zag</b>','0','".($time+1)."','".$mes."','$rm','0','0','1')");

}*/

?>