Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Trivia script

Wyldchard Vanguard
Registered User
Join date: 23 Oct 2006
Posts: 32
11-27-2006 08:28
I want to learn to create a trivia script that draws from a random question base. Can anyone help me?
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
11-27-2006 09:15
From: Wyldchard Vanguard
I want to learn to create a trivia script that draws from a random question base. Can anyone help me?


Try this Quiz Script

its designed to be sequentially answered rather than random, but with minor mods would probably suit your purpose.
Wyldchard Vanguard
Registered User
Join date: 23 Oct 2006
Posts: 32
Thx for the link, Newgate!
12-08-2006 04:07
From: someone
// Quiz Master v1.0
//
// 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 = "";) + results + Question + " = TIMED OUT. (Should have been " + llList2String(Answers,0) + ";)\n";
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 = "";) + results + Question + " = " + message;
if(selection == 0)
{
results += " (CORRECT)\n";
}
else
{
results += " (Should have been " + llList2String(Answers,0) + ";)\n";
}

// 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();
}
}




I've tried to mod this script to draw randomly from question notecard. For some reason, I can't get it to do that. Can someone help?
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
12-08-2006 04:27
From: Wyldchard Vanguard
I've tried to mod this script to draw randomly from question notecard. For some reason, I can't get it to do that. Can someone help?


Sure :) Quiet a few different possibilities really

The script currently just reads sequentially through a single notecard of questions.
I'd suggest one method would be to prereading the list of questions and storing the index for each new question in a list. Then randomise that list on each new start and use the index into the main notecard to obtain a question. This would probably be the least amount of code rework.

The prereading could take a while so an alternative would be to split each question into a seperate notecard. This would also speed up editing of the quiz.

The quiz parameters will need to be split out into their own notecard or stored in the description and name fields of the prim. If stored in their own notecard then that notecard could be extended to list the questions for this quiz (i.e. names of the note cards)

Any of that help?
Script Su
Professional SOA Designer
Join date: 23 Aug 2006
Posts: 79
12-08-2006 06:23
I think I am going to try to make an online service for trivia. You just put the name of the trivia database in the notecard and it fetches one random question.

Sounds pretty simple actually.

And you can even set the price and prize from the server.
_____________________
The LSL Repository @ sf.net. The LSL Repository is dedicated to bring open source lsl programmers together and develop the best service oriented scripts. Gridworks gives you the whole package. We also have in-house builders, scripters, web programmers, and salesmen. Premium Account||Age Verified||Gridworks Executive
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
12-08-2006 09:56
Well sticking to an inworld solution, heres my quiz rscript rewritten to use individual notecards for the questiosn.


its untested as I'm not in world.
CODE

// Quiz Master v2.0
//
// 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 multiple Notecard to define the questions.
// One question in each Notecard.
//
// 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 ConfigName = "Config";
string notecard = "Config";
integer lineCounter;
key dataRequestID;

string Email;
string QuizName;
string Question;
list Answers;
string Prize;
integer PrizeMoney = 0;
string WinPrize;
integer WinMoney = 0;

integer RandomQuiz = 0;

list Randomised;
list Questions;

integer NumQuestions = 0;

integer QuestionCounter = 0;

integer listenchannel = 0;
integer Listening = 0;
integer i;

key User;
string UserName;
string results;

integer first;
integer pagesize = 10;

integer len;

//-------------------------------------------------------
ReadListOfQuestions()
{
// Build a list of all Notecards
Questions = [];
integer num = llGetInventoryNumber(INVENTORY_NOTECARD);
while(i < num)
{
string name = llGetInventoryName(INVENTORY_NOTECARD,i);
++i;
if(name != ConfigName)Questions = (Questions = []) + Questions + [name];
}
NumQuestions = llGetListLength(Questions);
}

SetOrder()
{
// Sort questiosn - useful for building ordered rather than random quiz order.
if(RandomQuiz > 0)
{
Questions = llListRandomize(Questions, 1);
}
else
{
Questions = llListSort(Questions, 1, 1);
}
}
//-------------------------------------------------------
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);
integer ivalue = (integer)value;
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 =ivalue;
}
else if("WIN" == command)
{
WinPrize = value;
}
else if("WIN MONEY" == command)
{
WinMoney = ivalue;
}
else if("RANDOM QUIZ" == command)
{
RandomQuiz = ivalue;
}
}
//-------------------------------------------------------
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);
ReadListOfQuestions();

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);
SetOrder();
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 = "") + results + Question + " = TIMED OUT. (Should have been " + llList2String(Answers,0) + ")\n";
llSay(0, UserName + ", Oh dear you took too long. Next Question.");
QuestionCounter++;
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 = "") + results + Question + " = " + message;
if(selection == 0)
{
results += " (CORRECT)\n";
}
else
{
results += " (Should have been " + llList2String(Answers,0) + ")\n";
}

// Handle Prizes
GivePrizes(selection);

QuestionCounter++;
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()
{
if(QuestionCounter < NumQuestions)
{
lineCounter = 0;
notecard = llList2String(Questions,QuestionCounter);
dataRequestID = llGetNotecardLine( notecard, lineCounter ); // start reading the notecard
llSetTimerEvent(10);
}
else
{
state EndOfQuiz;
}
}

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();
}
}




Uses the same format notecards as before but each question is in a seperate notecard.
The configuration data should be moved to a notecard called Config.