<?php
$msg = preg_replace("/\[(.*)\] /", "", $msg);
$msg = str_replace($bots[0].", ", "", $msg);

if($msg == "!question" or $msg == "!вопрос")
{
	if($buff_action != 0)
	{
	$fd = fopen("bots/question.dat", "r");
	$question = fgets($fd);
	fclose($fd);
	$message = "$nickname, $question";
	mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$message."', '0', '".date("H:i:s")."', ".time().");");
	mysql_query("UPDATE `chat_users` SET `posts` = `posts` + 1 WHERE `id` = 2;");
	}
	else
	{
	$message = "$nickname, время вышло, жди следующего вопроса.";
	mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$message."', '0', '".date("H:i:s")."', ".time().");");
	}
}

$result = substr($msg, 0, 5);
if($result == "stats" && strlen($msg) > 6)
{
$nick = mysql_escape_string(substr($msg, 6));
$sql = mysql_query("SELECT `answers` FROM `chat_users` WHERE `nickname` = '".$nick."';");
	if(mysql_num_rows($sql) == 0)
	{
	$message = "Пользователя с ником $nick нет в базе данных!";
	mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$message."', '0', '".date("H:i:s")."', ".time().");");
	mysql_query("UPDATE `chat_users` SET `posts` = `posts` + 1 WHERE `id` = 2;");
	}
	else
	{
	$answers = mysql_result($sql, 0);
	$sql = mysql_query("SELECT `nickname` FROM `chat_users` ORDER BY `answers` DESC LIMIT 31;");

		$c = 0;

		while($users = mysql_fetch_array($sql))
		{
		$c++;
		if($users['nickname'] == $nick) break;
		}
		
		if($c != 31) $rating = "В рейтинге он занимает $c место!";
		if($c == 31) $rating = "В рейтинге он занимает место за пределами первых трех десятков умников.";

	$message = "$nick верно ответил(а) на <u>$answers</u> вопросов. $rating";
	mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$message."', '0', '".date("H:i:s")."', ".time().");");
	}
}

//ANSWER
$fd = fopen("bots/answer.dat", "r");
$answer = fgets($fd);
fclose($fd);

//TRAN ANSWER
$fd = fopen("bots/translit.dat", "r");
$tran_answer = fgets($fd);
fclose($fd);

$up = array("А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "C", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я");
$down = array("а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "c", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь", "э", "ю", "я");

$msg = str_replace($up, $down, $msg);

if(($msg == $answer or $msg == $tran_answer) && $buff_action != 0)
{
$sql = mysql_query("SELECT `answers` FROM `chat_users` WHERE `id` = '".$id."';");
$answers = mysql_result($sql, 0);
$message = "Отлично, $nickname! Правильный ответ был: <b>$answer</b>. $nickname отвечает на ".(++$answers)." вопрос. Следующий вопрос через ".$intervals[0]." секунд.";
mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$message."', '0', '".date("H:i:s")."', ".time().");");
mysql_query("UPDATE `chat_users` SET `answers` = `answers` + 1 WHERE `id` = '".$id."';");
mysql_query("UPDATE `chat_users` SET `posts` = `posts` + 1 WHERE `id` = 2;");

$fd = fopen("bots/time.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, (time() + $intervals[0])); //NEXT QUESTION
flock($fd, LOCK_UN);
fclose($fd);

$fd = fopen("bots/action.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, "0");
flock($fd, LOCK_UN);
fclose($fd);
}
?>