Skip to content

Commit

Permalink
Added winning points game over logic.
Browse files Browse the repository at this point in the history
Added winnings points game over logic of 3 points.  Remaining issue of
how to deal same definition cards to each player instead of cycling
through new cards each time.
  • Loading branch information
djcheung committed Nov 5, 2013
1 parent c32eec9 commit e3bcd87
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 17 deletions.
44 changes: 35 additions & 9 deletions assets/www/crayplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ var nextPlayerName = "";

var currentStage = 0;
var maxRounds = 7;

var maxPoints = 3;
var currentPoints = 0;
var currentMax = 0;

var listCreated = true;
var numPlayers = 3;
var currentAllPlayers = new PlayerCollection();
Expand Down Expand Up @@ -69,6 +74,7 @@ function onStartClick(){

function onRankAnswersSubmit(){
var answers = $(".sub_answers");

ids = [];

for(var x=0; x<answers.length; x++)
Expand All @@ -78,7 +84,12 @@ function onRankAnswersSubmit(){

currentWinner = thePlayerAnswers.item(parseInt(ids[0])).player_id;
currentWinnerName = currentAllPlayers.item(currentWinner).p_name;

currentAllPlayers.item(currentWinner).p_points++;
currentPoints = currentAllPlayers.item(currentWinner).p_points;
if(currentPoints > currentMax)
{
currentMax = currentPoints;
}
for (var i = 0; i < ids.length; i++) {
thePlayerAnswers.setRank(parseInt(ids[i]), i+1);
// Trim the excess whitespace.
Expand Down Expand Up @@ -340,7 +351,7 @@ function moveToNextPage(){
case 10:
currentRound++;
// if we are on the last stage we will increment the round and set the stage back to 1
if (currentRound > maxRounds)
if (currentPoints >= maxPoints)
{
currentStage = 10;
$.mobile.changePage("game_over.html", {transition: "none"} );
Expand All @@ -349,8 +360,11 @@ function moveToNextPage(){
{
currentStage = 0;
//todo remove this - just for testing.
currentReader = currentAllPlayers.grabSecondPlayer().p_id;
currentReaderName = currentAllPlayers.grabSecondPlayer().p_name;
var tempRoundPlayer = RoundPlayerCollection();
tempRoundPlayer = currentAllPlayers.grabNextPlayer();
currentReader = tempRoundPlayer.p_id;
currentReaderName = tempRoundPlayer.p_name;
currentRoundPlayers.empty();
currentRoundPlayers.resetPlayers(currentReader);
currentPlayer = currentRoundPlayers.grabNextPlayer().p_id;
currentPlayerName = currentRoundPlayers.grabNextPlayer().p_name;
Expand All @@ -359,7 +373,7 @@ function moveToNextPage(){
}
break;
case 11:
$.mobile.changePage("index.html#mainmenu", {transition: "none"} );
$.mobile.changePage("index.html", {transition: "none"} );
break;
default:
}
Expand Down Expand Up @@ -388,14 +402,12 @@ function appendToRoundOne(){
}

function appendCurrentVariables(){
//$("#currentvariables").append("<li><label for='text-basic'>currentStage: " + currentStage + "</label></li>");
$("#currentvariables").append("<li><center><label for='text-basic'>Round: " + currentRound + "</label></center></li>");

// HEADER
switch(currentStage)
{
case 1:
//$("#currentvariables").append("<li><label for='text-basic'>currentPlayer: " + currentPlayer + " name: " + currentPlayerName + "</label></li>");
$("#currentvariables").append("<li><center><label for='text-basic'>" + currentReaderName + "</label></center></li>");
$("#currentvariables").append("<div data-role='fieldcontain'>");
$("#currentvariables").append("<fieldset data-role='controlgroup'>");
Expand All @@ -404,7 +416,6 @@ function appendCurrentVariables(){
$("#currentvariables").append("<li><center><span style='font-size:large;'>\"" + currentQText + "\"</span></center></li>");
$("#currentvariables").append("</fieldset>");
$("#currentvariables").append("</div>");
//$("#currentvariables").append("<li><label for='text-basic'>currentQuestionRealText: " + currentQRealText + "</label></li>");
break;
case 2:
$("#currentvariables").append("<li><center><label for='text-basic'>" + currentPlayerName + "</label></center></li>");
Expand Down Expand Up @@ -439,8 +450,11 @@ function appendCurrentVariables(){
$("#currentvariables").append("<li><label for='text-basic'>Re-define the following word or phase:</label></li>");
$("#currentvariables").append("<li><center><span style='font-size:large;'>\"" + currentQText + "\"</span></center></li>");
break;
case 10:
$("#currentvariables").append("<li><center><label for='text-basic'>" + currentWinnerName + "</label></center></li>");
$("#currentvariables").append("<li><center><label for='text-basic'>Congrats, You Win!</label></center></li>");
break;
default:
//$("#currentvariables").append("<li><label for='text-basic'>currentReader: " + currentReader + " name: " + currentReaderName + "</label></li>");
$("#currentvariables").append("<li><center><label for='text-basic'>" + currentPlayerName + "</label></center></li>");
break;
}
Expand Down Expand Up @@ -551,6 +565,14 @@ function appendCurrentVariables(){
case 8:
$("#currentvariables").append("<legend>Winner for the round is:</legend>");
$("#currentvariables").append("<li><center>"+currentWinnerName+"</center></li>");
if(currentPoints == 1)
{
$("#currentvariables").append("<li><center>You have "+currentPoints+" point</center></li>");
}
else
{
$("#currentvariables").append("<li><center>You have "+currentPoints+" points</center></li>");
}
$("#currentvariables").append("</ul>");
$("#currentvariables").listview("refresh");
$("#content").append("<input type='button' id='continueButton' onClick='moveToNextPage()' value='"+currentWinnerName+" - Click to Continue'/>");
Expand All @@ -574,6 +596,10 @@ function appendCurrentVariables(){
$("#content").append("<input type='button' id='continueButton' onClick='moveToNextPage()' value='Continue'/>");
$("#content").trigger("create");
break;
case 10:
$("#currentvariables").listview("refresh");
$("#content").append("<input type='button' id='continueButton' onClick='moveToNextPage()' value='Start Over'/>");
$("#content").trigger("create");
default:
break;
}
Expand Down
45 changes: 42 additions & 3 deletions assets/www/crayplaydb.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ function populateDB(tx){
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (18, 1, "Definition 18")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (19, 1, "Definition 19")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (20, 1, "Definition 20")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (21, 1, "Definition 21")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (22, 1, "Definition 22")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (23, 1, "Definition 23")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (24, 1, "Definition 24")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (25, 1, "Definition 25")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (26, 1, "Definition 26")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (27, 1, "Definition 27")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (28, 1, "Definition 28")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (29, 1, "Definition 29")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (30, 1, "Definition 30")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (31, 1, "Definition 31")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (32, 1, "Definition 32")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (33, 1, "Definition 33")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (34, 1, "Definition 34")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (35, 1, "Definition 35")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (36, 1, "Definition 36")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (37, 1, "Definition 37")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (38, 1, "Definition 38")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (39, 1, "Definition 39")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (40, 1, "Definition 40")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (41, 1, "Definition 41")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (42, 1, "Definition 42")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (43, 1, "Definition 43")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (44, 1, "Definition 44")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (45, 1, "Definition 45")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (46, 1, "Definition 46")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (47, 1, "Definition 47")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (48, 1, "Definition 48")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (49, 1, "Definition 49")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (50, 1, "Definition 50")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (51, 1, "Definition 51")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (52, 1, "Definition 52")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (53, 1, "Definition 53")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (54, 1, "Definition 54")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (55, 1, "Definition 55")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (56, 1, "Definition 56")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (57, 1, "Definition 57")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (58, 1, "Definition 58")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (59, 1, "Definition 59")');
tx.executeSql('INSERT INTO DEFINITIONS (def_id, def_type, def_text) VALUES (60, 1, "Definition 60")');

//CREATE SETUP PARAMETERS
tx.executeSql('INSERT INTO SETUPPARAMS (setup_id, setup_text, setup_value) VALUES (1, "handsize", "5")');
Expand All @@ -85,7 +125,7 @@ function deletePlayersDB(errorCallBack, successCallBack){
var db = window.openDatabase("CrayPlay", "1.0", "CrayPlay DB", 1000000);
db.transaction(deletePlayersDBCall, errorCallBack, successCallBack);
currentAllPlayers.forEach(
function(p_id, p_name)
function(p_id, p_name, p_points)
{
currentAllPlayers.remove(p_id);
}
Expand Down Expand Up @@ -123,15 +163,14 @@ function getPlayersDBCall(tx){
function getDBPlayersDBSuccess(tx, results) {
//alert("inside queryDBPlayersDBSuccess");
for(var i=0; i<results.rows.length;i++){
currentAllPlayers.add(results.rows.item(i).p_id, results.rows.item(i).p_name);
currentAllPlayers.add(results.rows.item(i).p_id, results.rows.item(i).p_name, 0);
}
numPlayers = currentAllPlayers.count;
}
function selectCurrentPlayer(){
currentReader = currentAllPlayers.grabFirstPlayer().p_id;
currentReaderName = currentAllPlayers.grabFirstPlayer().p_name;
currentRoundPlayers.resetPlayers(currentReader);

currentPlayer = currentRoundPlayers.grabNextPlayer().p_id;
currentPlayerName = currentRoundPlayers.grabNextPlayer().p_name;
createNewGameDB(errorCB, moveToNextPage);
Expand Down
34 changes: 30 additions & 4 deletions assets/www/crayplaystructures.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ var PlayerCollection=function()
{
this.count=0;
this.collection={};

this.add=function(key, name)
this.index=0;

this.add=function(key, name, points)
{
if(this.collection[key]!=undefined)
return undefined;
this.collection[key] = {
p_id:key,
p_name: name
p_name: name,
p_points: points
};
return ++this.count;
}
Expand Down Expand Up @@ -60,13 +62,37 @@ var PlayerCollection=function()
return this.collection[secondKey];
}

this.grabNextPlayer=function()
{
var nextKey = 0;
var counter = 0;
var tempIndex = ++this.index;
if (tempIndex > this.count)
{
tempIndex = 0;
this.index = 0;
}
for(key in this.collection)
{
nextKey = key;
if(counter == tempIndex)
break;
counter++;
}
if(this.collection[nextKey]==undefined)
return undefined;

return this.collection[nextKey];
}


this.forEach=function(block)
{
for(key in this.collection)
{
if(this.collection.hasOwnProperty(key))
{
block(key, this.collection[key].p_name);
block(key, this.collection[key].p_name, this.collection[key].p_points);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion assets/www/game_over.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>Game Over</h1>

<div data-role="content" id="content" data-theme="e">
<h2>Game Over</h2>
<input type="button" id="clicktoContinue" onClick="moveToNextPage()" value="Start Over">
<ul id='currentvariables' data-role='listview' data-inset='true'></ul>
</div><!-- /content -->

<div data-role="footer" class="ui-bar" data-theme="e">
Expand Down

0 comments on commit e3bcd87

Please sign in to comment.