// Simple Dialog based question system with email support
// Primary reason would be as questionaire but could be adapted to a quiz by using the PRIZE and WIN Options
// Use the first answer as always being the right one, the answer order is randomised each time it is read.
// To Give a prize every time use PRIZE or PRIZE MONEY
// To give only if they win use WIN and WIN MONEY
//
// Uses a single Notecard to define all of the questions.
//
// Format of Note Card
//
// QUIZ NAME=My Silly little Quiz
// EMAIL=myemail@myserver
// +-START OF QUIZ-+
// QUESTION=What would you like to do?
// ANSWER=My Answer 1
// ANSWER=My Answer 2
// ANSWER=My Answer 3
// PRIZE=MyPrize
// PRIZE MONEY=5
// WIN=MyPrize
// WIN MONEY=5
// +-END OF QUESTION-+
// QUESTION=Where would you like to do it?
// ANSWER=My Answer 1
// ANSWER=My Answer 2
// ANSWER=My Answer 3
// +-END OF QUESTION-+
// PRIZE=MyPrize
// PRIZE MONEY=5
// +-END OF QUIZ-+
//
// PRIZE will give this gift wether the user answers the question correctly or not
// PRIZE MONEY will give the user the amount wether they are right or not
// WIN will only give the prize if they win
// WIN MONEY will like wise only give that amount if they win.
//
string notecard = "Questions";
integer lineCounter;
key dataRequestID;
string Email;
string QuizName;
string Question;
list Answers;
string Prize;
integer PrizeMoney = 0;
string WinPrize;
integer WinMoney = 0;
list Randomised;
integer listenchannel = 0;
integer Listening = 0;
integer i;
key User;
string UserName;
string results;
integer first;
integer pagesize = 10;
integer len;
//-------------------------------------------------------
ErrorMessage(string error)
{
llInstantMessage(llGetOwner(),llGetObjectName() + " : " + error);
}
//-------------------------------------------------------
UpdateListen(key id)
{
CancelListen();
Listening = llListen(listenchannel,"",id,""

llSetTimerEvent(30);
}
//-------------------------------------------------------
CancelListen()
{
if(Listening > 0) llListenRemove(Listening);
Listening = 0;
llSetTimerEvent(0);
}
//-------------------------------------------------------
ProcessData(string data)
{
list ldata = llParseString2List(data, ["="], [""]);
string command = llList2String(ldata,0);
string value = llList2String(ldata,1);
if("QUESTION" == command)
{
Question = value;
Answers = [];
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
}
else if("ANSWER" == command)
{
Answers = (Answers = []) + Answers + [ value ];
}
else if("QUIZ NAME" == command)
{
QuizName = value;
}
else if("EMAIL" == command)
{
Email = value;
}
else if("PRIZE" == command)
{
Prize = value;
}
else if("PRIZE" == command)
{
Prize = value;
}
else if("PRIZE MONEY" == command)
{
PrizeMoney = (integer)value;
}
else if("WIN" == command)
{
WinPrize = value;
}
else if("WIN MONEY" == command)
{
WinMoney = (integer)value;
}
}
//-------------------------------------------------------
AskTheQuestion()
{
list choices = ["Quit"];
// Now add the Answers
len = llGetListLength( Randomised );
if(first > 0)
choices += "Prev";
if(len > (first + pagesize))
{
choices += "Next";
}
for(i = 0;i < pagesize;i++)
{
string strname = llList2String(Randomised,(i+first));
if( llStringLength(strname) > 0)
{
choices += (strname);
}
}
// finally show the dialog
llDialog(User,Question,choices,listenchannel);
UpdateListen(User);
}
//-------------------------------------------------------
HandlePrizes(string gift, integer amount)
{
if(llStringLength(gift) > 0)
{
if(llGetInventoryType(gift) != INVENTORY_NONE)
llGiveInventory(User,gift);
}
if(amount > 0)
{
llGiveMoney(User, amount);
}
}
//-------------------------------------------------------
GivePrizes(integer right)
{
HandlePrizes(Prize, PrizeMoney);
if(right == 0) HandlePrizes(WinPrize, WinMoney);
}
//-------------------------------------------------------
default
{
state_entry()
{
lineCounter = 0;
Question = "";
Answers = [];
User = NULL_KEY;
UserName = "";
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
// we need this permission to give change / winnings
integer perms = llGetPermissions();
if((perms & PERMISSION_DEBIT) ==0)
llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);
llSetText(llGetScriptName() + " Initialisation in Progress\nPlease Wait",<1.0,0.0,0.0>, 1);
state ReadPrelim;
}
on_rez(integer number)
{
llResetScript();
}
changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}
}
//-------------------------------------------------------
state StartQuiz
{
state_entry()
{
Question = "";
Answers = [];
User = NULL_KEY;
UserName = "";
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
llSetText(QuizName + " Ready.\nPlease Touch me to answer the questions",<0.0,1.0,0.0>, 1);
lineCounter++;
}
on_rez(integer number)
{
llResetScript();
}
touch_start(integer num_detected)
{
User = llDetectedKey(0);
UserName = llDetectedName(0);
llSetText("In Use by " + UserName + ". Please Wait",<0.7,0.3,0.7>, 1);
lineCounter = 0;
state ReadQuestion;
}
changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}
}
//-------------------------------------------------------
state AnswerQuestion
{
state_entry()
{
listenchannel = (integer)llFrand(4000);
AskTheQuestion();
}
on_rez(integer number)
{
llResetScript();
}
changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}
timer()
{
CancelListen();
results = (results = ""


llSay(0, UserName + ", Oh dear you took too long. Next Question."

lineCounter++;
state ReadQuestion;
}
listen(integer number, string name, key id, string message)
{
// Cancel the listen timer
CancelListen();
if("Quit" == message)
{
state GoodBye;
}
else if("Prev" == message)
{
first -= pagesize;
AskTheQuestion();
}
else if("Next" == message)
{
len = llGetListLength( Answers );
if((first + pagesize) < len)
first += pagesize;
AskTheQuestion();
}
else
{
// Check for a valid answer
integer selection = llListFindList(Answers, [ message ]);
if(selection >= 0)
{
results = (results = ""

if(selection == 0)
{
results += " (CORRECT)\n";
}
else
{
results += " (Should have been " + llList2String(Answers,0) + "

}
// Handle Prizes
GivePrizes(selection);
lineCounter++;
state ReadQuestion;
}
}
}
}
//--------------------------------------------------------------------------------
state EndOfQuiz
{
state_entry()
{
GivePrizes(1);
llSay(0, UserName + ", Thank you for taking part in our quiz."

llSetText("Processing Answers. Please Wait",<1.0,0.0,0.0>,1);
// Email The quiz details to the specified Email Address.
llEmail(Email, QuizName, UserName + "\n" + results );
lineCounter = 0;
state ReadPrelim;
}
on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state GoodBye
{
state_entry()
{
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
state EndOfQuiz;
}
on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state ErrorCondition
{
state_entry()
{
llSetText("Sorry I'm broken.\nPlease Try again later.",<1.0,0.0,0.0>,1);
}
changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}
on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state ReadPrelim
{
state_entry()
{
lineCounter = 0;
integer itemtype = llGetInventoryType(notecard);
if(INVENTORY_NOTECARD == itemtype)
{
llSetTimerEvent(10);
dataRequestID = llGetNotecardLine( notecard, lineCounter ); // start reading the notecard
}
else
{
ErrorMessage("Unable to read Quiz"

state ErrorCondition;
}
}
dataserver( key query_id, string data ) // read from the notecard
{
if(dataRequestID == query_id)
{
if (data != EOF)
{
if(llGetSubString(data, 0,0) != ";"

{
if("+-START OF QUIZ-+" == data)
{
llSetTimerEvent(0);
if(llStringLength(Email) == 0)
{
ErrorMessage("Email Address Not Specified."

state ErrorCondition;
}
else
state StartQuiz;
}
else
{
ProcessData(data);
}
}
lineCounter++;
dataRequestID = llGetNotecardLine( notecard, lineCounter );
}
else
{
llSetTimerEvent(0);
state AnswerQuestion;
}
}
}
timer()
{
llSetTimerEvent(0);
ErrorMessage("Timeout reading Quiz"

state ErrorCondition;
}
on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state ReadQuestion
{
state_entry()
{
llSetTimerEvent(10);
dataRequestID = llGetNotecardLine( notecard, lineCounter ); // start reading the notecard
}
dataserver( key query_id, string data ) // read from the notecard
{
if(dataRequestID == query_id)
{
if (data != EOF)
{
if(llGetSubString(data, 0,0) != ";"

{
if("+-END OF QUIZ-+" == data)
{
llSetTimerEvent(0);
state EndOfQuiz;
}
else if("+-END OF QUESTION-+" == data)
{
llSetTimerEvent(0);
Randomised = llListRandomize(Answers, 1);
state AnswerQuestion;
}
else
{
ProcessData(data);
}
}
lineCounter++;
dataRequestID = llGetNotecardLine( notecard, lineCounter );
}
else
{
llSetTimerEvent(0);
state AnswerQuestion;
}
}
}
timer()
{
llSetTimerEvent(0);
ErrorMessage("Timeout reading Quiz"

state ErrorCondition;
}
on_rez(integer number)
{
llResetScript();
}
}