//***********************
// Define some constants.
//***********************
var dlg = {
'Window' : '#dialog',
'Header' : '#dlgtitle',
'Body' : '#dlgbody',
'Buttons' : '#dlgokcancel'
};
// Global variables.
var teamCount=0; // Number of teams playing.
//******************************************************
// This function is called first to get number of teams.
//******************************************************
function initialize()
{
// Hide elements that need to be hidden.
closeWindow();
closeDialog();
document.title = Title;
var userInput = prompt('How many teams are playing?', '2');
if (userInput != '' && userInput != null)
{
teamCount = parseInt(userInput);
}
// Once we know how many teams there are, we can create the score table
$("#score").html( makeScoreTable());
// And then the controls for adjusting scores after answers.
$("#controls").html(makeControls());
}
function getRandom(min,max)
{
return (Math.round(Math.random()*(max-min)))+min;
}
/*******************************************************************
* Title: Title in the dialog title portion.
* Body: The content that goes in the dialog's body portion.
* OnFinish: The function that is called after the dialog closes.
* -- default ""
* ok, cancel: The text in the buttons for ok and cancel.
* -- defaults: "Ok", "Cancel"
******************************************************************/
function showDialog(title, body, onfinish, oktext, canceltext)
{
// If variabls not passed in use a default.
onfinish=argv(onfinish,"");
oktext=argv(oktext, "Ok");
canceltext=argv(canceltext, "Cancel");
$(dlg.Header).html(title);
$(dlg.Body).html(body);
var ok = "<input type='button' value='" + oktext + "' > + onfinish + "' />";
var cancel = "<input type='button' value='" + canceltext + "' />";
$(dlg.Buttons).html(ok + cancel);
$(dlg.Window).show();
}
function closeDialog(onfinish)
{
$(dlg.Window).hide();
}
/*******************************************************************
* Process:
* Get wager amounts from each team
* Show question, teams must write their answer down
* Read answers / Reveal answer
* Show options for correct or incorrect to change totals.
******************************************************************/
function getFinalWagers()
{
var title = "Final Category: " + FinalCategory;
var body = "<table id='finalwager'>";
body += "<tr><th>Team</th><th>Wager</th><th>Current $</th></tr>";
for (var i=1; i<=teamCount; i++)
{
var money = parseInt($("#team" + i).html());
body += "<tr>";
body += "<td align=center> Team " + i + "</th>";
body += "<td> <input type='text' name='finalwageramt" + i + "' value='0' /> </td>";
body += "<td>" + money + "</td>";
body += "</tr>";
}
body += "</table>";
// Show the get wagers dialog, when finished call revealFinalAnswer().
showDialog(title, body, "revealFinalAnswer()");
}
function getScore(team)
{
var id = "#team" + team;
var val = parseInt($(id).html());
return val;
}
// team is a number identifying the team.
function incScore(team)
{
// Get the team's current winnings.
var id = "#team" + team;
var val = parseInt($(id).html());
// Get the team's current wager.
var amount = wagerAmount.getTeamWager(team-1);
// Adjust accordingly.
$(id).html(val + amount);
if (autoCloseOnScore()) closeWindow();
}
function decScore(team)
{
var id = "#team" + team;
var val = parseInt($(id).html());
var amount = wagerAmount.getTeamWager(team-1);
$(id).html(val - amount);
if (autoCloseOnScore()) closeWindow();
}
//**********************
// Countdown timer code.
//**********************
var timerVariable=null;
function startTimer(numSeconds)
{
// Use default if nothing passed in.
numSeconds=argv(numSeconds, TimePerQuestion);
if (timerVariable != null) stopTimer();
// Reset countdown timer panel.
$(qa.Timer).css("color", "white");
$(qa.Timer).html(numSeconds);
// Reset the timer to go off every second.
timerVariable = setInterval("decTimer()", 1000);
}
function stopTimer()
{
if (timerVariable != null)
{
clearInterval(timerVariable);
timerVariable = null;
}
}
// This decrements the timer 1 second at a time.
function decTimer()
{
var val = parseInt($(qa.Timer).html());
val--;
$(qa.Timer).html(val);
// Change the color to red for the last 10 seconds.
if (val < 10) $(qa.Timer).css("color", "red");
// Stop the countdown at 0.
if (val == 0) stopTimer();
}
//*******************************************************************
// Set an argument to its value, if it is undefined, use the default.
// Allows you to have defaults arguments with functions such as:
// function x(a,b,c) { a=argv(a,1); b=argv(b,2); c=argv(c,3); }
//*******************************************************************
function argv(arg, val) { return typeof arg !== 'undefined' ? arg : val; }
function WebSocketTest()
{
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
}
//*************************************************************************
// While this appears to be a function, it is actually a javascript object.
// value, answer and question are it's member variables.
//*************************************************************************
function Question(v, q, a)
{
// Set instance variables for this object.
this.value = v;
this.answer = a;
this.question = q;
// Add methods to this object.
this.getValue = getValue;
this.getQuestion = getQuestion;
this.getAnswer = getAnswer;
}
function getValue() { return this.value; }
function getQuestion() { return this.question; }
function getAnswer() { return this.answer; }