View file wip-site-portal/src/chat_quiz.php

File size: 2.92Kb
<?

$select = mysql_query("SELECT `t`.`stage`, `t`.`time`, `t`.`qid`, `q`.`text`, `q`.`answer` FROM `wip_quiz_timer` AS `t`, `wip_quiz_questions` AS `q` WHERE `t`.`qid`=`q`.`id` ;");
if(mysql_affected_rows()<1) exit();

$now = mysql_fetch_assoc($select);

$diff = time() - $now['time'];

# Запрос ответов
if($now['stage']!=0){

	$answers = mysql_query("SELECT `u`.`nick` FROM `wip_chat_msgs` AS `m`, `wip_users` AS `u` WHERE `m`.`time` > '".$now['time']."' AND `m`.`time` < '".($now['time']+330)."' AND `m`.`msg`='".$now['answer']."' AND `u`.`id`=`m`.`uid` ORDER BY `m`.`time` DESC LIMIT 1;");
	
	# Найден верный ответ
	if(mysql_affected_rows()>0){
		$nick = mysql_fetch_row($answers);
		
		mysql_query("INSERT INTO `wip_chat_msgs` SET `rid`=1, `uid`=1, `time`='".time()."', `msg`='<b>".$nick[0]."</b> выиграл! Следующий вопрос через минуту.';");
		mysql_query("UPDATE `wip_quiz_timer` SET `stage`=0, `time`='".time()."' WHERE `id`=1");
		mysql_query("UPDATE `wip_users` SET `chat_quiz`=`chat_quiz`+1 WHERE `id`='".$user['id']."';");
	}
}
###

# Первая подсказка
if($diff>180 and $now['stage']==1){
	$help_one = mb_substr($now['answer'], 0, 1, 'UTF-8') . str_repeat('.', mb_strlen($now['answer'], 'UTF-8')-1 );
	mysql_query("UPDATE `wip_quiz_timer` SET `stage`=2 WHERE `id`=1");
	
	mysql_query("INSERT INTO `wip_chat_msgs` SET `rid`=1, `uid`=1, `time`='".time()."', `msg`='Первая подсказка: <b>".$help_one."</b>';");
}

# Вторая подсказка
if($diff>270 and $now['stage']==2){
	$help_two = mb_substr($now['answer'], 0, 2, 'UTF-8') . str_repeat('.', mb_strlen($now['answer'], 'UTF-8')-2 );
	mysql_query("UPDATE `wip_quiz_timer` SET `stage`=3 WHERE `id`=1");
	
	mysql_query("INSERT INTO `wip_chat_msgs` SET `rid`=1, `uid`=1, `time`='".time()."', `msg`='Вторая подсказка: <b>".$help_two."</b>';");
}

# Никто не ответил на вопрос
if($diff>360 and $now['stage']<=3 and $now['stage']!=0){
	mysql_query("INSERT INTO `wip_chat_msgs` SET `rid`=1, `uid`=1, `time`='".time()."', `msg`='Никто не ответил на вопрос. А правильный ответ был: <b>".$now['answer']."</b>. Следующий вопрос через минуту.';");

	mysql_query("UPDATE `wip_quiz_timer` SET `stage`=0, `time`='".time()."' WHERE `id`=1");
}

# Новый вопрос
if( $diff>60 and $now['stage']==0){
	$rand = mysql_fetch_assoc( mysql_query("SELECT `id`, `text`, `answer` FROM `wip_quiz_questions` ORDER BY RAND() LIMIT 1") );
	
	mysql_query("UPDATE `wip_quiz_timer` SET `stage`=1, `time`='".time()."', `qid`='".$rand['id']."' WHERE `id`=1");

	$len = mb_strlen($rand['answer'], 'UTF-8');
	
	mysql_query("INSERT INTO `wip_chat_msgs` SET `rid`=1, `uid`=1, `time`='".time()."', `msg`='".$rand['text']." (".$len." ".word_case('букв:буквы:буква', $len).") ';");
}


?>