-
Notifications
You must be signed in to change notification settings - Fork 32
/
game.js
152 lines (124 loc) · 3.68 KB
/
game.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
game = new Chess();
var socket = io();
var color = "white";
var players;
var roomId;
var play = true;
var room = document.getElementById("room")
var roomNumber = document.getElementById("roomNumbers")
var button = document.getElementById("button")
var state = document.getElementById('state')
var connect = function(){
roomId = room.value;
if (roomId !== "" && parseInt(roomId) <= 100) {
room.remove();
roomNumber.innerHTML = "Room Number " + roomId;
button.remove();
socket.emit('joined', roomId);
}
}
socket.on('full', function (msg) {
if(roomId == msg)
window.location.assign(window.location.href+ 'full.html');
});
socket.on('play', function (msg) {
if (msg == roomId) {
play = false;
state.innerHTML = "Game in progress"
}
// console.log(msg)
});
socket.on('move', function (msg) {
if (msg.room == roomId) {
game.move(msg.move);
board.position(game.fen());
console.log("moved")
}
});
var removeGreySquares = function () {
$('#board .square-55d63').css('background', '');
};
var greySquare = function (square) {
var squareEl = $('#board .square-' + square);
var background = '#a9a9a9';
if (squareEl.hasClass('black-3c85d') === true) {
background = '#696969';
}
squareEl.css('background', background);
};
var onDragStart = function (source, piece) {
// do not pick up pieces if the game is over
// or if it's not that side's turn
if (game.game_over() === true || play ||
(game.turn() === 'w' && piece.search(/^b/) !== -1) ||
(game.turn() === 'b' && piece.search(/^w/) !== -1) ||
(game.turn() === 'w' && color === 'black') ||
(game.turn() === 'b' && color === 'white') ) {
return false;
}
// console.log({play, players});
};
var onDrop = function (source, target) {
removeGreySquares();
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: 'q' // NOTE: always promote to a queen for example simplicity
});
if (game.game_over()) {
state.innerHTML = 'GAME OVER';
socket.emit('gameOver', roomId)
}
// illegal move
if (move === null) return 'snapback';
else
socket.emit('move', { move: move, board: game.fen(), room: roomId });
};
var onMouseoverSquare = function (square, piece) {
// get list of possible moves for this square
var moves = game.moves({
square: square,
verbose: true
});
// exit if there are no moves available for this square
if (moves.length === 0) return;
// highlight the square they moused over
greySquare(square);
// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to);
}
};
var onMouseoutSquare = function (square, piece) {
removeGreySquares();
};
var onSnapEnd = function () {
board.position(game.fen());
};
socket.on('player', (msg) => {
var plno = document.getElementById('player')
color = msg.color;
plno.innerHTML = 'Player ' + msg.players + " : " + color;
players = msg.players;
if(players == 2){
play = false;
socket.emit('play', msg.roomId);
state.innerHTML = "Game in Progress"
}
else
state.innerHTML = "Waiting for Second player";
var cfg = {
orientation: color,
draggable: true,
position: 'start',
onDragStart: onDragStart,
onDrop: onDrop,
onMouseoutSquare: onMouseoutSquare,
onMouseoverSquare: onMouseoverSquare,
onSnapEnd: onSnapEnd
};
board = ChessBoard('board', cfg);
});
// console.log(color)
var board;