Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game ending #80

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let tradeContracts = {
// Picks a random initial amount for delivery plus a recurring weekly amount
let initialAmount = 0;
if (gameManagement.gameDate <= 24) {
initialAmount = Math.floor(Math.random() * (resourceManagement.resourcePieces[resourceNumber].maxProduction * 2)) + 1 + 2;
initialAmount = Math.floor(Math.random() * (resourceManagement.resourcePieces[resourceNumber].maxProduction * 2)) + 1 + 1;
} else if (gameManagement.gameDate > 24 && gameManagement.gameDate <= 40) {
initialAmount = Math.floor(Math.random() * (resourceManagement.resourcePieces[resourceNumber].maxProduction * 4)) + 1 + 2;
} else {
Expand Down Expand Up @@ -528,7 +528,7 @@ let tradeContracts = {
}
}
}
console.log(this.contractsArray);
//console.log(this.contractsArray);
},


Expand Down
70 changes: 50 additions & 20 deletions gamemanagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ let gameManagement = {

// Players eliminated at the end of moon 4 / start of moon 5 and end of moon 6 / start of moon 7
if ((gameManagement.gameDate == (4 * gameManagement.phaseCount + 1) || gameManagement.gameDate == (6 * gameManagement.phaseCount + 1)) && gameManagement.turn == gameManagement.teamArray[1]) {
gameManagement.eliminatePlayer(gameManagement.lastPlayer());
gameManagement.eliminatePlayer(gameManagement.firstLastPlayer());
}

// Winner for intro scroll
if (gameManagement.gameDate == (8 * gameManagement.phaseCount + 1)) {
gameManagement.presentWinner(gameManagement.firstLastPlayer());
}

// Intro scroll pop up
Expand All @@ -105,20 +110,23 @@ let gameManagement = {
[scrollPanel.children[1].textContent, scrollPanel.children[2].textContent, scrollPanel.children[3].textContent, scrollPanel.children[4].textContent] = gameManagement.scrollText();
scrollPopup.style.display = "block";

// Sets up event listener to close when screen (or cross) is clicked
scrollPopup.addEventListener('click', gameManagement.scrollClose);
if (gameManagement.gameDate < (8 * gameManagement.phaseCount + 1)) {
// Sets up event listener to close when screen (or cross) is clicked
scrollPopup.addEventListener('click', gameManagement.scrollClose);
} else {
console.log('game over');
// Game Over
}
} else {
// Second half of next turn functionality called if scroll is not activated (here) or once scroll is clicked to close (scrollClose)
gameManagement.afterNextTurn();
}

},


// Next turn - after moon change
// ----------------------------
afterNextTurn: function() {

// Wind direction is set for next turn
windDirection = compass.newWindDirection(windDirection);
needleDirection = compass.directionArray[windDirection].needle;
Expand Down Expand Up @@ -191,9 +199,11 @@ let gameManagement = {

// Method to determine player to eliminate
// ---------------------------------------
lastPlayer: function() {
firstLastPlayer: function() {
let lastPlayerArray = [];
let firstPlayerArray = [];
let lowestScore = 10000;
let highestScore = 0;
// Loops through all players, building an array of competing players with the lowest score
for (var i = 0; i < this.playerListing.length; i++) {
if (this.playerListing[i].status == 'competing') {
Expand All @@ -205,23 +215,31 @@ let gameManagement = {
lastPlayerArray.push([this.playerListing[i].teamColour, gameScore.scoreSummary.Total[index].score]);
lowestScore = gameScore.scoreSummary.Total[index].score;
}
if (gameScore.scoreSummary.Total[index].score == highestScore) {
firstPlayerArray.push([this.playerListing[i].teamColour, gameScore.scoreSummary.Total[index].score]);
} else if (gameScore.scoreSummary.Total[index].score > highestScore) {
firstPlayerArray = [];
firstPlayerArray.push([this.playerListing[i].teamColour, gameScore.scoreSummary.Total[index].score]);
highestScore = gameScore.scoreSummary.Total[index].score;
}
}
}

// Picks a player at random if more than one player have the lowest score
let eliminatedPlayer = lastPlayerArray.splice(Math.floor(Math.random() * lastPlayerArray.length), 1)[0][0];
return eliminatedPlayer;
let lastPlayer = lastPlayerArray.splice(Math.floor(Math.random() * lastPlayerArray.length), 1)[0][0];
let firstPlayer = firstPlayerArray.splice(Math.floor(Math.random() * firstPlayerArray.length), 1)[0][0];
return [firstPlayer, lastPlayer];

},

// Method to remove a player's pieces from the game on elimination
// ---------------------------------------------------------------
eliminatePlayer: function(localTeam) {
eliminatePlayer: function(localFirstLast) {
if(workFlow == 1) {console.log('Eliminating player: ' + localTeam + ' : ' + (Date.now() - launchTime)); }
for (var i = 0; i < gameBoard.boardArray.length; i++) {
// Loop through all board tiles
for (var j = 0; j < gameBoard.boardArray[i].length; j++) {
if (gameBoard.boardArray[i][j].pieces.team == localTeam) {
if (gameBoard.boardArray[i][j].pieces.team == localFirstLast[1]) {

// Remove all transport ships
if (gameBoard.boardArray[i][j].pieces.category == 'Transport') {
Expand All @@ -241,20 +259,31 @@ let gameManagement = {
}

// Update marker for competing player / eliminated player in player listing
index = this.playerListing.findIndex(fI => fI.teamColour == localTeam);
index = this.playerListing.findIndex(fI => fI.teamColour == localFirstLast[1]);
this.playerListing[index].status = 'eliminated';

// Remove player from teamArray
let index2 = this.teamArray.indexOf(localTeam);
let index2 = this.teamArray.indexOf(localFirstLast[1]);
if (index2 != -1) {
this.teamArray.splice(index2, 1);
}

// Update scrollTextArray
let eliminationDate = this.moonDate(this.gameDate).moonMonth;
this.scrollTextArray[eliminationDate-1][0] = this.playerListing[index].teamColour + ' has been eliminated.'
console.log(eliminationDate);
},


presentWinner: function(localFirstLast) {
if(workFlow == 1) {console.log('Determining winner: ' + localTeam + ' : ' + (Date.now() - launchTime)); }

// Show scoreboard
scoreHeader.style.top = '0%';

// Update scrollTextArray
index = this.playerListing.findIndex(fI => fI.teamColour == localFirstLast[0]);
let winnerDate = this.moonDate(this.gameDate).moonMonth;
this.scrollTextArray[winnerDate-1][0] = this.playerListing[index].teamColour + ' is the winner!'
},

// ------------------------------------------------------------------------------------
Expand All @@ -279,7 +308,7 @@ let gameManagement = {
scrollOutline.setAttribute('width', 50 * localScale);
scrollOutline.setAttribute('height', 70 * localScale);
scrollOutline.setAttribute('x', 25 * localScale);
scrollOutline.setAttribute('y', 10 * localScale);
scrollOutline.setAttribute('y', 13 * localScale);
scrollOutline.setAttribute('rx', '2');
scrollOutline.setAttribute('ry', '2');
scrollOutline.setAttribute('fill', 'rgb(246, 232, 206)');
Expand All @@ -293,7 +322,7 @@ let gameManagement = {
scrollTitle.setAttribute('font-size', 16 * screenReduction);
scrollTitle.setAttribute('fill', 'rgb(179, 156, 128)');
scrollTitle.setAttribute('x', 50 * localScale);
scrollTitle.setAttribute('y', 38 * localScale);
scrollTitle.setAttribute('y', 41 * localScale);
scrollTitle.setAttribute('text-anchor', 'middle');
scrollTitle.setAttribute('font-weight', 'bold');

Expand All @@ -303,7 +332,7 @@ let gameManagement = {
scrollText.setAttribute('font-size', 16 * screenReduction);
scrollText.setAttribute('fill', 'rgb(179, 156, 128)');
scrollText.setAttribute('x', 50 * localScale);
scrollText.setAttribute('y', 45 * localScale);
scrollText.setAttribute('y', 48 * localScale);
scrollText.setAttribute('text-anchor', 'middle');
scrollText.setAttribute('font-style', 'italic');

Expand All @@ -313,7 +342,7 @@ let gameManagement = {
scrollText2.setAttribute('font-size', 16 * screenReduction);
scrollText2.setAttribute('fill', 'rgb(179, 156, 128)');
scrollText2.setAttribute('x', 50 * localScale);
scrollText2.setAttribute('y', 50 * localScale);
scrollText2.setAttribute('y', 53 * localScale);
scrollText2.setAttribute('text-anchor', 'middle');
scrollText2.setAttribute('font-style', 'italic');

Expand All @@ -323,7 +352,7 @@ let gameManagement = {
scrollText3.setAttribute('font-size', 16 * screenReduction);
scrollText3.setAttribute('fill', 'rgb(179, 156, 128)');
scrollText3.setAttribute('x', 50 * localScale);
scrollText3.setAttribute('y', 55 * localScale);
scrollText3.setAttribute('y', 58 * localScale);
scrollText3.setAttribute('text-anchor', 'middle');
scrollText3.setAttribute('font-style', 'italic');

Expand All @@ -349,6 +378,7 @@ let gameManagement = {
['At the next new moon ...', ' ... the last player will be eliminated!', ''],
['', 'Head to head!', 'Down to the final two.'],
['The last moon!', 'To the winner the spoils!', ''],
['', 'Thanks for playing!', ''],
],

// Method to update title and text of scroll
Expand All @@ -365,12 +395,12 @@ let gameManagement = {
// Included to potentially allow future actions to be taken using buttons in scroll
scrollPopup.style.display = "none";
if(workFlow == 1) {console.log('Scroll Close event listener removed ' + gameManagement.turn + ' : ' + (Date.now() - launchTime)); }
scrollPopup.removeEventListener('click', this.scrollClose);
scrollPopup.removeEventListener('click', gameManagement.scrollClose);
gameManagement.afterNextTurn();
} else {
scrollPopup.style.display = "none";
if(workFlow == 1) {console.log('Scroll Close event listener removed ' + gameManagement.turn + ' : ' + (Date.now() - launchTime)); }
scrollPopup.removeEventListener('click', this.scrollClose);
scrollPopup.removeEventListener('click', gameManagement.scrollClose);
gameManagement.afterNextTurn();
}
},
Expand Down
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ gameManagement.createTurnCircle(false, 0.7*surroundSize/100, -20*screenReduction
let scrollPopup = document.querySelector('.scroll_popup');
let scrollPanel = gameManagement.createScroll(screenWidth*(12/2000), -50*screenReduction, (screenWidth*4/20), scrollPopup);
scrollPopup.appendChild(scrollPanel);
scrollPanel.appendChild(gameBoard.drawMoon((screenWidth*4.2/20), 150*screenReduction, 50*screenReduction, 1));
scrollPanel.appendChild(gameBoard.drawMoon((screenWidth*7.8/20), 540*screenReduction, 50*screenReduction, 7));
scrollPanel.appendChild(gameBoard.drawMoon((screenWidth*4.2/20), 175*screenReduction, 50*screenReduction, 1));
scrollPanel.appendChild(gameBoard.drawMoon((screenWidth*7.8/20), 565*screenReduction, 50*screenReduction, 7));

// Finds the stockDashboard holder in the left hand panel
let stockDashboardNode = document.querySelector('div.stockDashboard');
Expand Down
2 changes: 1 addition & 1 deletion scoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let gameScore = {
// Method to check if score is to be added for move
// ------------------------------------------------
addScore: function(contest, localTeam, subContest, scoreDetail) {
console.log(contest, localTeam, subContest, scoreDetail);
//console.log(contest, localTeam, subContest, scoreDetail);

// Finds array position in score array of current team
let teamPosition = this.scoreArray.findIndex(fI => fI.team == localTeam);
Expand Down