From 1c5b90c5022cfa888df596ce2d2d82b252e6f22b Mon Sep 17 00:00:00 2001
From: Nixinova <42429413+Nixinova@users.noreply.github.com>
Date: Fri, 2 Jul 2021 10:09:44 +1200
Subject: [PATCH] Refactor and fix puzzle move output
Also make file extension of include more accurate
---
includes/{header.njk => header.html} | 0
layouts/menu.njk | 2 +-
pages/home/index.html | 2 +-
pages/play.njk | 2 +-
scripts/play/game-cycle.js | 26 ++-----------------
scripts/play/puzzles.js | 37 ++++++++++++++++++++++++----
styles/common.nvss | 2 +-
7 files changed, 38 insertions(+), 33 deletions(-)
rename includes/{header.njk => header.html} (100%)
diff --git a/includes/header.njk b/includes/header.html
similarity index 100%
rename from includes/header.njk
rename to includes/header.html
diff --git a/layouts/menu.njk b/layouts/menu.njk
index 2d32a0e..b2f3e30 100644
--- a/layouts/menu.njk
+++ b/layouts/menu.njk
@@ -14,7 +14,7 @@
- {% include "header.njk" %}
+ {% include "header.html" %}
{{ content | safe }}
diff --git a/pages/home/index.html b/pages/home/index.html
index 4499528..f3fedea 100644
--- a/pages/home/index.html
+++ b/pages/home/index.html
@@ -3,7 +3,7 @@
layout: menu.njk
---
-Select opponent
+CarbonChess
diff --git a/pages/play.njk b/pages/play.njk
index e83e71f..82ca757 100644
--- a/pages/play.njk
+++ b/pages/play.njk
@@ -21,7 +21,7 @@ permalink: /play/
- {% include "header.njk" %}
+ {% include "header.html" %}
diff --git a/scripts/play/game-cycle.js b/scripts/play/game-cycle.js
index 067c2f6..376204f 100644
--- a/scripts/play/game-cycle.js
+++ b/scripts/play/game-cycle.js
@@ -78,30 +78,8 @@ function hasClicked(cell) {
checkGameEnding();
// check if correct puzzle move has been made
- if (window.gameOptions.puzzles && puzzleColour === global.currentTurn && movesToMake) {
- if (startCell === movesToMake[0].slice(0, 2).toUpperCase() && endCell === movesToMake[0].slice(2, 4).toUpperCase()) {
- movesToMake.shift();
- if (movesToMake.length > 0) {
- $.id('winner').innerHTML = 'Correct, now find the next move'
- setTimeout(puzzleMove, 500);
- }
- else {
- $.id('winner').innerHTML = 'Well done';
- $.id('next-puzzle').classList.remove('hide');
- window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, window.puzzleHintUsed ? 0 : 1);
- saveUserData();
- }
- }
- else {
- undoLastMove();
- $.id('winner').innerHTML = 'Wrong, try again';
- if (window.failedPuzzleAttempts === 0) {
- window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, 0);
- saveUserData();
- }
- window.failedPuzzleAttempts++;
- }
- $.id('puzzle-attempts-value').innerText = window.failedPuzzleAttempts;
+ if (window.gameOptions.puzzles && puzzleColour === global.currentTurn && window.movesToMake) {
+ puzzleMoveOutput(startCell, endCell);
}
// send to server
diff --git a/scripts/play/puzzles.js b/scripts/play/puzzles.js
index 6d0d6d4..2b6eb57 100644
--- a/scripts/play/puzzles.js
+++ b/scripts/play/puzzles.js
@@ -1,9 +1,9 @@
const puzzleCache = 10;
-let savedPuzzles;
-let movesToMake;
-let puzzleColour;
-let puzzlePosition = 0;
-let hasLoadedCustom = false;
+window.savedPuzzles;
+window.movesToMake = [];
+window.puzzleColour;
+window.puzzlePosition = 0;
+window.hasLoadedCustom = false;
function processData(allText) {
const allTextLines = allText.split(/\r\n|\n/);
@@ -75,6 +75,7 @@ function nextPuzzle() {
window.ingame = true;
window.points = { w: 0, b: 0 };
window.failedPuzzleAttempts = 0;
+ window.puzzleHintUsed = false;
$.id('puzzle-attempts-value').innerText = window.failedPuzzleAttempts;
$$('td').forEach(elem => elem.setAttribute('class', ''));
if (puzzlePosition >= puzzleCache - 1) {
@@ -89,6 +90,32 @@ function nextPuzzle() {
$.id('winner').innerHTML = 'Find the best move';
}
+function puzzleMoveOutput(startCell, endCell) {
+ if (startCell === window.movesToMake[0].slice(0, 2).toUpperCase() && endCell === window.movesToMake[0].slice(2, 4).toUpperCase()) {
+ window.movesToMake.shift();
+ if (window.movesToMake.length > 0) {
+ $.id('winner').innerHTML = 'Correct, now find the next move'
+ setTimeout(puzzleMove, 500);
+ }
+ else {
+ $.id('winner').innerHTML = 'Well done';
+ $.id('next-puzzle').classList.remove('hide');
+ window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, window.puzzleHintUsed ? 0 : 1);
+ saveUserData();
+ }
+ }
+ else {
+ undoLastMove();
+ $.id('winner').innerHTML = 'Wrong, try again';
+ if (window.failedPuzzleAttempts === 0) {
+ window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, 0);
+ saveUserData();
+ }
+ window.failedPuzzleAttempts++;
+ }
+ $.id('puzzle-attempts-value').innerText = window.failedPuzzleAttempts;
+}
+
function showPuzzleHint() {
window.puzzleHintUsed = true;
$.id(movesToMake[0].slice(0, 2).toUpperCase())?.classList.add('valid');
diff --git a/styles/common.nvss b/styles/common.nvss
index b47d3cc..2682bce 100644
--- a/styles/common.nvss
+++ b/styles/common.nvss
@@ -37,7 +37,7 @@ summary {
margin: 1em;
padding: 0.5em;
background: #0004;
- text-align: center;
+ text-align: right;
&-elo {
margin-left: 4px;