<?
//BOT FOR QUIZ

//GET TIME
$fd = fopen("bots/time.dat", "r");
if(!$fd) return;
$buff_time = intval(fgets($fd));
fclose($fd);

//GET ACTION
$fd = fopen("bots/action.dat", "r");
if(!$fd) $buff_action = "unknown";
else $buff_action = fgets($fd);
fclose($fd);

if($buff_time < time() && $buff_action != 0)
{
if($buff_action != 0) $action = 3;
}
else
{
if($buff_action == 0 && ($buff_time - time() < 0)) $action = 0;
if($buff_action == 1 && ($buff_time - time() < 135)) $action = 1;
if($buff_action == 2 && ($buff_time - time() < 75)) $action = 2;
if($buff_action == 3 && ($buff_time - time() < 15)) $action = 3;
}

//FIRST START
if($buff_time == 0) $action = 0;

switch($action)
{
//NEW QUESTION
case '0':
$sql = mysql_query("SELECT * FROM `chat_questions` ORDER BY RAND() LIMIT 1;");
$ques_arr = mysql_fetch_array($sql);
$question = $ques_arr['question'];
$answer = $ques_arr['answer'];
$tran_answer = $ques_arr['translit'];
$answer = iconv('utf-8', 'windows-1251', $answer);
$length = strlen($answer);
$answer = iconv('windows-1251', 'utf-8', $answer);

$question = "$question ($length букв)";

mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$question."', '0', '".date("H:i")."',".time().",0);");
mysql_query("UPDATE `chat_users` SET `posts` = `posts` + 1 WHERE `id` = 2;");

$fd = fopen("bots/question.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, $question);
flock($fd, LOCK_UN);
fclose($fd);

$fd = fopen("bots/answer.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, $answer);
flock($fd, LOCK_UN);
fclose($fd);

$fd = fopen("bots/translit.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, $tran_answer);
flock($fd, LOCK_UN);
fclose($fd);

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

$fd = fopen("bots/time.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, (time() + 180 + $intervals[0])); //TIME FOR ANSWER - 15
flock($fd, LOCK_UN);
fclose($fd);
break;

//FIRST HELP
case '1':
$fd = fopen("bots/answer.dat", "r");
$answer = fgets($fd);
fclose($fd);

$answer = iconv('utf-8', 'windows-1251', $answer);
$help = substr($answer, 0, 1);
$answer = iconv('windows-1251', 'utf-8', $answer);
$help = iconv('windows-1251', 'utf-8', $help);
$help = "Слово начинается на: $help...";

mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$help."', '0', '".date("H:i")."',".time().",0);");
mysql_query("UPDATE `chat_users` SET `posts` = `posts` + 1 WHERE `id` = 2;");

$fd = fopen("bots/action.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, "2");
flock($fd, LOCK_UN);
fclose($fd);
break;

//SECOND HELP
case '2':
$fd = fopen("bots/answer.dat", "r");
$answer = fgets($fd);
fclose($fd);

$answer = iconv('utf-8', 'windows-1251', $answer);
$help = substr($answer, 0, 3);
$answer = iconv('windows-1251', 'utf-8', $answer);
$help = iconv('windows-1251', 'utf-8', $help);
$help = "Первые 3 буквы: $help...";

mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$help."', '0', '".date("H:i")."', ".time().",0);");
mysql_query("UPDATE `chat_users` SET `posts` = `posts` + 1 WHERE `id` = 2;");

$fd = fopen("bots/action.dat", "w");
flock($fd, LOCK_EX);
$puts = fputs($fd, "3");
flock($fd, LOCK_UN);
fclose($fd);
break;

//NO ANSWER
case '3':
$fd = fopen("bots/answer.dat", "r");
$answer = fgets($fd);
fclose($fd);
$message = "Время истекло! Правильный ответ был: $answer. Следующий вопрос через ".$intervals[0]." секунд.";
mysql_query("INSERT INTO `chat".$rid."` VALUES(0, '2', '".$bots[0]."', '".$message."', '0', '".date("H:i")."', ".time().",0);");
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);
break;
}
?>