/*

In order to use any part of this Merc Diku Mud, you must comply with
both the original Diku license in 'license.doc' as well the Merc
license in 'license.txt'.  In particular, you may not remove either of
these copyright notices.

Much time and thought has gone into this software and you are
benefitting.  We hope that you share your changes too.  What goes
around, comes around.

ROM 2.4 is copyright 1993-1998 Russ Taylor
ROM has been brought to you by the ROM consortium
Russ Taylor (rtaylor@hypercube.org)
Gabrielle Taylor (gtaylor@hypercube.org)
Brian Moore (zump@rom.org)
By using this code, you have agreed to follow the terms of the
ROM license, in the file Rom24/doc/rom.license

*/

/* Automated Trivia. Trivia code will spit out random trivia questions
 * over a trivia channel. Players can answer the questions for quest points.
 * This code was written by Eric Lowe, aka Dalsor of AWW-MUD.org
 * It has been released under the same restrictions as the Diku/Merc/ROM
 * restrictions. In other words, you can use it, you can modify it, but
 * you cannot claim ownership of it, or claim it as your intellectual
 * property.
 *
 * Kudos: Erwin (always) for obvious reasons, Kyndig for being the most excellent
 * boon to the entire MUD community he is, Markanth for releasing his battle
 * code which inspired Tourney which provided insight on how to make Trivia
 * work the way it does, Muerte for goading me into writing more code, even
 * if it wasn't always the code he wanted, Caxandra for leaning over my shoulder
 * and asking 'whatcha doin?', and the playtesters at AWW for nit-picking.
 */


#if defined(macintosh)
#include <types.h>
#include <time.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "aww.h"
#include "interp.h"
#include "recycle.h"
#include "tables.h"
#include "lookup.h"
#include "trivia.h"

int tQuestion;
int tHighRange;
int tLowRange;
int TRIV_QUESTIONS = 20;

int qAsked[20];
int currTQuestion;

void talk_trivia( char *argument )
{
    DESCRIPTOR_DATA *d;
    char buf[MSL];
    CHAR_DATA *original;

    sprintf( buf, "{w[{gT{Gr{Wiv{Gi{ga{w]{W:: {G%s{x\n\r", argument );

    for (d = descriptor_list; d != NULL; d = d->next)
    {
        original = d->original ? d->original : d->character; /* if switched */
        if ( d->connected == CON_PLAYING && IS_SET( d->character->auto_flags,AUTO_TRIVIA ) )
        stc( buf , original );

    }
}

void close_trivia( void )
{
	char buf[MSL];

	if ( !trivia->running )
	{
		return;
	}
	sprintf( buf,"{wThe {WAWW Official {gT{Gr{Wiv{Gi{ga{w Contest is now Closed!{x\n\r");
	talk_trivia( buf );
	trivia->running = FALSE;
	trivia->question = NULL;
	trivia->answer = NULL;
	trivia->reward = 0;
	trivia->qnumber = 0;
	trivia->timer = 0;
	return;
}

void trivia_question( void )
{
	char buf[MSL];
	int tQuestion = number_range( tLowRange, tHighRange );
	int aQ;

	/* this needs tweaking */
	tQuestion = number_range( tLowRange, tHighRange );
	for ( aQ = 0; aQ < 20; aQ++ )
	{
		if ( tQuestion == qAsked[aQ] )
		{
			sprintf( buf, "Trivia encountered a duplicate question. Please wait.\n\r" );
			talk_trivia( buf );
			trivia_question();
			return;
		}
	}
	/* end need tweaking */

	sprintf( buf, "Question number %d worth %d points.", trivia->qnumber +1, TriviaTable[tQuestion].reward );
	talk_trivia( buf );

	sprintf( buf, "%s", TriviaTable[tQuestion].question );
	talk_trivia( buf );
	trivia->answer = TriviaTable[tQuestion].answer;
	trivia->timer = 20;
	currTQuestion++;
	qAsked[currTQuestion] = tQuestion;
	return;
}

void trivia_update( void )
{
	char buf[MSL];

	if ( !trivia->running )
		return;

	if ( trivia->qnumber > TRIV_QUESTIONS )
	/* end the trivia */
	close_trivia();

	if ( trivia->timer <= 0 )
	{
		sprintf( buf, "Time ran out for Trivia Question %d with no winner!\n\r", trivia->qnumber +1 );
		talk_trivia( buf );

		trivia->qnumber++;

		if ( trivia->qnumber > TRIV_QUESTIONS )
		{
			close_trivia();
			return;
		}

		trivia_question();
	}

	if ( trivia->timer >= 1 )
		trivia->timer--;

	return;
}
void do_trivia( CHAR_DATA * ch, char *argument )
{
	char  arg1[MIL];
	char  arg2[MIL];
    char buf[MSL];

	argument = one_argument( argument, arg1 );

	if ( arg1[0] == '\0' )
	{
		stc( "Trivia Syntax:\n\r", ch );
		stc( "Trivia Talk <string>\n\r", ch );
		stc( "Trivia Open\n\r", ch );
		stc( "Trivia Close\n\r", ch );
		stc( "Trivia Answer <string>\n\r", ch );
		stc( "A question will be asked based on the books, the World of Robert Jordan, or the WoTRPG guide.\n\r", ch );
		stc( "The first to correctly answer the question receives the number of QPs specified.\n\r\n\r", ch );
		stc( "Spelling counts. Incorrect spelling means an incorrect answer. Properly formatting your\n\r", ch );
		stc( "answers. The correct format is to seperate each answer (for questions with more than one\n\r", ch );
		stc( "answer) with "", a comma and a space, like so: Trivia Answer 'Answer1, Answer2, Answer3' For\n\r", ch );
		stc( "questions with only one answer, the commas are not required. Do not place punctuation at\n\r", ch );
		stc( "the end of the answer. The "" marks are REQUIRED for answers with more than one word.\n\r\n\r", ch );
		stc( "When to use "" and when to use '' can be tricky. It is a good idea to always enclose your\n\r", ch );
		stc( "answer with double quotes just to be safe.\n\r\n\r", ch );
		stc( "There will be 20 randomly generated questions per Trivia and approximately 1 minute will\n\r", ch );
		stc( "be allowed to answer each question as it is displayed. If no one guesses the correct answer,\n\r", ch );
		stc( "that question will be removed and a new question posted. The faster the questions are answered,\n\r", ch );
		stc( "though, the faster the game will move. Quick answers mean a quicker game, though the timer will\n\r", ch );
		stc( "revert to about 1 minute if a question is not answered within the time limit.\n\r", ch );
		return;
	}
	if ( !str_prefix( arg1, "talk" ) )
	{
		if ( !trivia->running )
		{
			stc( "Trivia is not running.\n\r", ch );
			return;
		}
		sprintf( buf, "%s says, '%s'", ch->name, argument );
		talk_trivia( buf );
		return;
	}
	if ( !str_prefix( arg1, "open" ) )
	{
		int count = 0, total = 0, aQ = 0;
		if ( !IS_IMMORTAL( ch ) )
		{
			stc( "You are not authorized to do this.\n\r", ch );
			return;
		}
		if ( trivia->running )
		{
			stc( "Trivia is already running.\n\r", ch );
			return;
		}

		for ( count = 0; TriviaTable[count].question != NULL; count++ )
			total++;

		ptc( ch, "Found %d questions.\n\r", total );
		tLowRange = 0;
		tHighRange = total -1;
		sprintf( buf,"{wThe {WAWW Official {gT{Gr{Wiv{Gi{ga{w Contest is now Open!{x\n\r");
		talk_trivia( buf );
	    trivia->running = TRUE;
		trivia->question = NULL;
		trivia->answer = NULL;
		trivia->timer = 20;

		for ( aQ = 0; aQ < 20; aQ++ )
			qAsked[aQ] = 0;
		currTQuestion = -1;

		trivia_question();

		return;
	}
	if ( !str_prefix( arg1, "close" ) )
	{
		if ( !IS_IMMORTAL( ch ) )
		{
			stc( "You are not authorized to do this.\n\r", ch );
			return;
		}
		if ( !trivia->running )
		{
			stc( "Trivia is not running.\n\r", ch );
			return;
		}
		sprintf( buf,"{wThe {WAWW Official {gT{Gr{Wiv{Gi{ga{w Contest is now Closed!{x\n\r");
		talk_trivia( buf );
		trivia->running = FALSE;
		trivia->question = NULL;
		trivia->answer = NULL;
		trivia->reward = 0;
		trivia->qnumber = 0;
		trivia->timer = 0;
		return;
	}
	if ( !str_prefix( arg1, "answer" ) )
	{
		argument = one_argument( argument, arg2 );
		if ( !trivia->running )
		{
			stc( "Trivia is not running.\n\r", ch );
			return;
		}
		if ( arg2[0] == '\0' )
		{
			stc( "Trivia Answer what?\n\r", ch );
			return;
		}
		strcpy( argument, arg2 );

		sprintf( buf,"{w%s guesses, %s{x\n\r", ch->name, argument );
		talk_trivia( buf );

		if ( !str_cmp( argument, trivia->answer ) )
		{
			sprintf( buf,"{w%s correctly answered question #%d!{x\n\r", ch->name, trivia->qnumber +1 );
			talk_trivia( buf );
			ch->questpoints += TriviaTable[tQuestion].reward;
			ptc( ch, "You receive %d Quest Points!\n\r", TriviaTable[tQuestion].reward );
			trivia->qnumber++;

			if ( trivia->qnumber > TRIV_QUESTIONS )
			{
				close_trivia();
				return;
			}

			trivia_question();
		}
		else
		{
			ptc( ch, "%s was not the correct answer to Question %d.\n\r", argument, trivia->qnumber +1 );
			return;
		}
		return;
	}

}