File size: 10,549 Bytes
ece5841 |
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
import React, { createContext, useReducer, useRef, useState } from 'react'
import PropTypes from 'prop-types';
import { socket } from '../socket';
import { ChessModified, chessInit } from '../utils/chess';
import { DISPATCH_EVENTS, SOCKET_EVENTS } from '../constants';
const { MOVE_PIECE, SELECT_PIECE, JUMP_TO, SET_GAME_HISTORY, END_GAME } = DISPATCH_EVENTS
const { GAME_END } = SOCKET_EVENTS;
export const ChessGameContext = createContext();
const reducer = (state, action) => {
try {
switch (action.type) {
case SELECT_PIECE:
{
if (state.chess.turn() === state.chess.myColor && state.currentIndex < state.gameHistory.length - 1) return { ...state, currentIndex: state.gameHistory.length - 1 }
return { ...state, moveHints: state.chess.getMoves(action.val), selected: action.val };
}
case MOVE_PIECE:
{
let newChessObj = new ChessModified(state.chess.fen());
let updatedGameHistory = state.gameHistory;
let { san, after } = newChessObj.move(action.val);
updatedGameHistory.push({ move: san, fen: after });
let newState;
if (newChessObj.isCheckmate()) {
action.val.callback();
action.val.playAudioCallback("CHECKMATE");
newState = { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1, hasGameEnded: true, gameEndedReason: 'CHECKMATE' };
} else if(newChessObj.isCheck() || newChessObj.inCheck()) {
action.val.playAudioCallback("CHECK");
newState = { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 };
} else if (newChessObj.isStalemate()) {
action.val.callback();
action.val.playAudioCallback("STALEMATE");
newState = { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1, hasGameEnded: true, gameEndedReason: 'STALEMATE' };
}
else {
if(!state.chess.get(action.val.to)) {
action.val.playAudioCallback("MOVE");
} else {
action.val.playAudioCallback("CAPTURE");
}
newState = { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 };
}
return newState;
}
case JUMP_TO:
{
let index = action.val;
return { ...state, currentIndex: index }
}
case SET_GAME_HISTORY:
{
let fetchedGameHistory = action.val;
let newChessObj = new ChessModified();
let updatedGameHistory = [];
for (let i = 0; i < fetchedGameHistory.length; i++) {
let { san, after } = newChessObj.move(fetchedGameHistory[i]);
updatedGameHistory.push({ fen: after, move: san })
}
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(), gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }
}
case END_GAME:
{
return { ...state, hasGameEnded: true, gameEndedReason: action.val }
}
default:
return state;
}
} catch (err) {
console.error(err);
return state;
}
}
function chessGameStateInit(myColor) {
let chess = chessInit(myColor);
let chessBoard = chess.getBoard();
let moveHints = [];
let gameHistory = [];
let selected = null;
let currentIndex = -1;
let hasGameEnded = false;
let gameEndedReason = "";
return { chess, chessBoard, moveHints, selected, gameHistory, currentIndex, hasGameEnded, gameEndedReason };
}
// the ChessGameContextProvider seperates the game logic from the ChessBoard component and exposes
// some functions to update game state.
const ChessGameContextProvider = ({ children }) => {
let myColor = localStorage.getItem('myColor');
let roomID = localStorage.getItem('roomID');
const [{ chess, chessBoard, moveHints, selected, gameHistory, currentIndex, hasGameEnded, gameEndedReason }, dispatch] = useReducer(reducer, myColor, chessGameStateInit);
const [isTimerOn, setIsTimerOn] = useState(true);
const chessRef = useRef(chess);
const moveHintsRef = useRef(moveHints);
const selectedRef = useRef(selected);
const gameHistoryRef = useRef(gameHistory);
const currentIndexRef = useRef(currentIndex);
chessRef.current = chess;
selectedRef.current = selected;
moveHintsRef.current = moveHints;
gameHistoryRef.current = gameHistory;
currentIndexRef.current = currentIndex;
const moveAudioRef = useRef(null);
const captureAudioRef = useRef(null);
const gameEndAudioRef = useRef(null);
const checkAudioRef = useRef(null);
function playAudioCallback(action) {
switch(action) {
case "MOVE":
moveAudioRef.current.play();
break;
case "CAPTURE":
captureAudioRef.current.play();
break;
case "CHECK":
checkAudioRef.current.play();
break;
case "CHECKMATE":
case "STALEMATE":
case "GAME_END":
gameEndAudioRef.current.play();
break;
case "CASTLE":
break;
default:
break;
}
}
// data - received through socket
function handleOpponentMove(data, callback) {
let { from, to } = data;
console.log("Opponent move:",from,to);
dispatch({type:MOVE_PIECE,val: { from, to, callback,playAudioCallback }});
}
// called when user clicks a square
function handleSquareClick(square, emitToSocketCallback, callback) {
let { type, color } = chessRef.current.get(square);
let marked = moveHintsRef.current.includes(square);
if (chessRef.current.turn() === myColor) {
if (type && color === myColor) {
selectPiece({square,color});
return;
} else if(marked) {
dispatch({ type: MOVE_PIECE, val: { from: selectedRef.current, to: square, callback,playAudioCallback } })
console.log("Move:",{ from: selectedRef.current, to: square })
emitToSocketCallback({ from: selectedRef.current, to: square })
}
}
}
function handleDrop(moveData, emitToSocketCallback, callback) {
let { from, to } = moveData;
if (moveHintsRef.current.includes(to)) {
dispatch({ type: MOVE_PIECE, val: { from: from, to: to, callback,playAudioCallback } }); // capture piece
console.log("Move:",{ from,to })
emitToSocketCallback(moveData);
}
}
function selectPiece({ square, color: pieceColor }) {
if (pieceColor === myColor && myColor === chessRef.current.turn()) {
dispatch({ type: SELECT_PIECE, val: square });
}
}
function getSquareColor(square) {
return chessRef.current.squareColor(square) === 'light' ? "w" : "b";
}
function isSquareMarked(square) {
return moveHintsRef.current.includes(square);
}
function isLastMoveSquare(square) {
if (currentIndexRef.current < 0)
return false;
let [lastMove] = chessRef.current.history({verbose: true}).slice(-1);
if (square != lastMove.to && square!=lastMove.from)
return false;
return true;
}
function jumpTo(index) {
dispatch({ type: JUMP_TO, val: index })
}
function getChessBoard() {
if (currentIndexRef.current === -1 || gameHistoryRef.current.length === 0) {
return new ChessModified().getBoard();
} else {
// console.log(chess);
let currentChessBoard = new ChessModified(gameHistoryRef.current[currentIndexRef.current].fen).getBoard();
return currentChessBoard;
}
}
function goBack() {
if (currentIndexRef.current > 0) {
jumpTo(currentIndexRef.current - 1);
}
}
function goAhead() {
if (currentIndexRef.current < gameHistoryRef.current.length - 1) {
jumpTo(currentIndexRef.current + 1);
}
}
// fetchedGameHistory is an array of objects of the form {from,to}
function setGameHistory(fetchedGameHistory) {
dispatch({ type: SET_GAME_HISTORY, val: fetchedGameHistory })
}
function endGame(reason) {
dispatch({ type: END_GAME, val: reason })
socket.emit(GAME_END, roomID);
}
function getPieceColor(square) {
return chessRef.current.get(square).color
}
return (
<ChessGameContext.Provider value={{
myColor, chess, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked, isLastMoveSquare,
selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard, currentIndex, goAhead, goBack, setGameHistory,
isTimerOn, hasGameEnded, gameEndedReason, endGame,getPieceColor
}}>
{children}
<audio src='/assets/audio/move-self.mp3' ref={moveAudioRef} />
<audio src='/assets/audio/capture.mp3' ref={captureAudioRef} />
<audio src='/assets/audio/game-end.webm' ref={gameEndAudioRef} />
<audio src='/assets/audio/move-check.mp3' ref={checkAudioRef} />
</ChessGameContext.Provider>
)
}
ChessGameContextProvider.propTypes = {
children: PropTypes.object
}
export default ChessGameContextProvider |