MCPcopy Create free account
hub / github.com/code100x/chess / makeMove

Method makeMove

apps/ws/src/Game.ts:211–284  ·  view source on GitHub ↗
(
    user: User,
    move: Move
  )

Source from the content-addressed store, hash-verified

209 }
210
211 async makeMove(
212 user: User,
213 move: Move
214 ) {
215
216 // validate the type of move using zod
217 if (this.board.turn() === 'w' && user.userId !== this.player1UserId) {
218 return;
219 }
220
221 if (this.board.turn() === 'b' && user.userId !== this.player2UserId) {
222 return;
223 }
224
225 if (this.result) {
226 console.error(`User ${user.userId} is making a move post game completion`);
227 return;
228 }
229
230 const moveTimestamp = new Date(Date.now());
231
232 try {
233 if (isPromoting(this.board, move.from, move.to)) {
234 this.board.move({
235 from: move.from,
236 to: move.to,
237 promotion: 'q',
238 });
239 } else {
240 this.board.move({
241 from: move.from,
242 to: move.to,
243 });
244 }
245 } catch (e) {
246 console.error("Error while making move");
247 return;
248 }
249
250 // flipped because move has already happened
251 if (this.board.turn() === 'b') {
252 this.player1TimeConsumed = this.player1TimeConsumed + (moveTimestamp.getTime() - this.lastMoveTime.getTime());
253 }
254
255 if (this.board.turn() === 'w') {
256 this.player2TimeConsumed = this.player2TimeConsumed + (moveTimestamp.getTime() - this.lastMoveTime.getTime());
257 }
258
259 await this.addMoveToDb(move, moveTimestamp);
260 this.resetAbandonTimer()
261 this.resetMoveTimer();
262
263 this.lastMoveTime = moveTimestamp;
264
265 socketManager.broadcast(
266 this.gameId,
267 JSON.stringify({
268 type: MOVE,

Callers 1

addHandlerMethod · 0.95

Calls 6

addMoveToDbMethod · 0.95
resetAbandonTimerMethod · 0.95
resetMoveTimerMethod · 0.95
endGameMethod · 0.95
broadcastMethod · 0.80
isPromotingFunction · 0.70

Tested by

no test coverage detected