-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchessClient.js
107 lines (96 loc) · 3.13 KB
/
chessClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const pieceToText = {
"K": "♔",
"Q": "♕",
"R": "♖",
"B": "♗",
"N": "♘",
"P": "♙",
"k": "♚",
"q": "♛",
"r": "♜",
"b": "♝",
"n": "♞",
"p": "♟",
};
const initialFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0";
var board;
let nextMoves = [];
let selectedPiece;
function boardToHTML(blackPerspective=false){
$("#board").html("");
let table = document.createElement("table");
let currentRow;
let trueIndex = blackPerspective ? 63 : 0;
let trueIndexModifier = blackPerspective ? -1 : 1;
for(i=0; i<64; i++){
if(i % 8 == 0){
if(currentRow)
table.appendChild(currentRow);
currentRow = document.createElement("tr");
}
let currentColumn = document.createElement("td");
if((Math.floor(i/8) + (i % 8)) % 2 == 0)
currentColumn.classList.add("light-cell");
else
currentColumn.classList.add("dark-cell");
if(board.array[trueIndex])
currentColumn.innerHTML = pieceToText[board.array[trueIndex].type];
currentColumn.setAttribute("id", trueIndex);
currentRow.appendChild(currentColumn);
trueIndex += trueIndexModifier;
}
table.appendChild(currentRow);
$("#board").append(table);
nextMoves = board.getNextMoves();
}
function highlightMoves(index){
let moves = nextMoves[index];
if(moves)
for(move of moves) // jshint ignore:line
$("#"+String(move)).addClass("highlight");
}
function unselect(){
$("#" + selectedPiece).removeClass("selected");
selectedPiece = null;
$(".highlight").removeClass("highlight");
}
function getThisPlayer(){
return sessionStorage.getItem("player");
}
function select(index){
unselect();
$("#" + index).addClass("selected");
selectedPiece = index;
highlightMoves(index);
}
function processClick(socket, index, Players){
// jshint ignore:start
if(selectedPiece === index)
unselect();
else if(selectedPiece == null && board.array[index] && board.player == getThisPlayer() && board.player == board.array[index].player)
select(index);
else if(selectedPiece == null && board.array[index]?.player == changePlayer(getThisPlayer()))
unselect();
else if(board.array[index]?.player == getThisPlayer() && board.player == getThisPlayer())
select(index);
else if(selectedPiece != null){
if(nextMoves[selectedPiece].includes(Number.parseInt(index))){
// Make Move in client early
board.makeMove(selectedPiece, index);
postMove(Players, true);
socket.emit("makeMove", sessionStorage.getItem("gameId"), selectedPiece, index);
}
else
unselect();
selectedPiece = null;
}
// jshint ignore:end
}
function postMove(Players, early=false){
boardToHTML(getThisPlayer() == Players.black);
if(!early){
nextMoves = board.getNextMoves();
if(board.isCheck(sessionStorage.getItem("player")))
alert("You're in check");
}
}