Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Fix multiplayer move making and free play
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Jun 2, 2021
1 parent 184ced5 commit 1cb4f5f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = off
23 changes: 14 additions & 9 deletions scripts/play/game-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function hasClicked(cell) {
if (
!window.ingame
|| gameOptions.spectating
|| gameOptions.multiplayer && window.currentTurn !== window.playerTurn
|| gameOptions.multiplayer && global.currentTurn !== window.playerTurn[0]
) return;

const $cell = $.id('piece' + cell);
Expand Down Expand Up @@ -46,13 +46,18 @@ function hasClicked(cell) {

// move the piece
const moveOutput = validation.makeMove(startCell, endCell);
if (!moveOutput) {
console.log('I', startCell, '->', endCell);
if (getPieceClasses(endCell)[0] === colour) selectPiece(endCell);
else selectPiece(startCell);
return;
if (window.hasRules) {
if (!moveOutput) {
console.log('I', startCell, '->', endCell);
if (getPieceClasses(endCell)[0] === colour) selectPiece(endCell);
else selectPiece(startCell);
return;
}
createBoardFromFen(moveOutput);
}
else {
movePiece(startCell, endCell);
}
createBoardFromFen(moveOutput);
$$('td').forEach(elem => elem.classList.remove('last-move'));
$startCell.classList.add('last-move');
$endCell.classList.add('last-move');
Expand Down Expand Up @@ -90,7 +95,7 @@ function hasClicked(cell) {
}

// send to server
if (autoPing) sendDB(gameId, createFen());
if (window.autoPing) sendDB(window.gameId, createFen());

}

Expand Down Expand Up @@ -141,7 +146,7 @@ function undoLastMove() {
});
$('#log').removeChild($('#log').lastChild);
$('#winner').innerText = '';
if (window.autoPing) sendDB(gameId, createFen());
if (window.autoPing) sendDB(window.gameId, createFen());
if (gameOptions.bot && global.currentTurn === gameOptions.botColour[0]) undoLastMove();
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function run() {
window.ingame = true;
window.sessionLost = false;
window.totalMoves = 0;
window.lastMove = { start: null, end: null };
window.lastMove = { start: '', end: '' };
window.selectedCell = null;
window.playerTurn = 'white';
window.promotionPiece = 'queen';
Expand Down
8 changes: 3 additions & 5 deletions scripts/play/pagelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ function log({ startCell, endCell, startClasses, endClasses, count, taken, promo

let code = ' ';
if (count % 2 === 1 && hasRules) code += '<br class="desktoponly">' + ((count + 1) / 2) + '. ';
if (castled) {
code += endCell.charCodeAt(0) < 'D'.charCodeAt(0) ? '0-0-0' : '0-0';
}
if (castled) code += endCell.charCodeAt(0) < 'D'.charCodeAt(0) ? '0-0-0' : '0-0';
else {
code += getPieceID(piece);
if (taken && piece === 'pawn') code += startCell[0].toLowerCase();
if (taken || enpassantTaken) code += 'x';
code += endCell.toLowerCase();
if (promoted) code += '=' + getPieceID(promotionPiece);
if (promoted) code += '=' + getPieceID(window.promotionPiece);
}
if (check) code += '+';
$('#log').innerHTML += `<span class="move">` + code + '</span>';
Expand All @@ -31,7 +29,7 @@ function logPoints() {

function logTakenPiece(colour, piece) {
const takenPiece = createPiece(piece, colour);
takenPiece.setAttribute('data-move', totalMoves);
takenPiece.setAttribute('data-move', window.totalMoves);
$.id(colour + '-pieces').appendChild(takenPiece);
logPoints();
}
Expand Down
7 changes: 0 additions & 7 deletions scripts/play/pieces.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ function swapPiece(startCell, endCell) {
addPiece(endClasses[1], endClasses[0], startCell);
if (!startClasses.length) resetCell(endCell);
}
updateKingCells();
}

function movePiece(startCell, endCell) {
if (startCell === endCell) return;
const startClasses = getPieceClasses(startCell);
addPiece(startClasses[1], startClasses[0], endCell);
resetCell(startCell);
updateKingCells();
}

function setPromotion(elem) {
Expand All @@ -62,11 +60,6 @@ function setPromotion(elem) {
window.promotionPiece = piece;
}

function updateKingCells() {
kingCell.b = $('.black.king')?.parentNode.id;
kingCell.w = $('.white.king')?.parentNode.id;
}

function getPieceID(piece) {
switch (piece) {
case 'pawn': return '';
Expand Down
12 changes: 6 additions & 6 deletions styles/play.nvss
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ td {
}
}

td.last-move {background-color: rgb(228, 230, 129) !important;}
td.valid {background-color: rgb(155, 207, 144) !important;}
td.check {background-color: rgb(231, 70, 70) !important;}
td.selected {background-color: rgb(124, 192, 214) !important;}
.last-move {background-color: rgb(228, 230, 129) !important;}
.valid {background-color: rgb(155, 207, 144) !important;}
.check {background-color: rgb(231, 70, 70) !important;}
.selected {background-color: rgb(124, 192, 214) !important;}

:is(tr:nth-child(odd) td:nth-child(even),
tr:nth-child(even) td:nth-child(odd)) {
Expand Down Expand Up @@ -290,6 +290,8 @@ button.disabled {
#taken-white-pieces {grid-area: white;}

#sidebar {
max-height: var(--board-size);
overflow-y: scroll;
border: 2px solid #111;
background-color: #dcdcdc;
color: black;
Expand All @@ -313,8 +315,6 @@ button.disabled {
}

#log {
max-height: 494px;
overflow-wrap: break-word;
text-align: center;

.move {
Expand Down

0 comments on commit 1cb4f5f

Please sign in to comment.