Skip to content

Commit 205f5ff

Browse files
committed
Fix game restart to properly reset moves
1 parent bd73b56 commit 205f5ff

File tree

8 files changed

+57
-58
lines changed

8 files changed

+57
-58
lines changed

client/app/_components/Board.tsx

+16-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import reverseOpponentStones from "@/_utilities/reverseOpponentStones";
88
import graphQLError from "@/_utilities/graphQLError";
99
import { GET_GAME_BY_ID, GET_MOVES_BY_GAME_ID } from "@/_graphql/queries";
1010
import { CREATE_MOVES, UPDATE_GAME } from "@/_graphql/mutations";
11-
import { GAME_MOVED, GAME_RESTARTED, GAME_UPDATED } from "@/_graphql/subscriptions";
11+
import { GAME_MOVED, GAME_UPDATED } from "@/_graphql/subscriptions";
1212
import getActiveGamerData, { IActiveGamerData } from "@/_utilities/getActiveGamerData";
1313
import useDeepCompareMemoize from "@/_hooks/useDeepCompareMemoize";
1414
import useGameResultModal from "@/_store/useGameResultModal";
@@ -21,7 +21,6 @@ import {
2121
IStone,
2222
IStones,
2323
SubscriptionGameMovedData,
24-
SubscriptionGameRestartedData,
2524
SubscriptionGameUpdatedData
2625
} from "@/_types";
2726
import GameResultModal from "./modal/GameResultModal";
@@ -95,32 +94,27 @@ export default function Board() {
9594
onError: graphQLError
9695
});
9796

98-
useSubscription<SubscriptionGameMovedData>(GAME_MOVED, {
99-
variables: {
100-
gameID: id
101-
},
102-
onData: ({ data: { data } }) => {
103-
if (data?.moves) {
104-
const moves = data.moves;
105-
setBoard(prevState => prevState.map(cell => {
106-
const move = moves.find(move => move.row == cell.row && move.col == cell.col);
107-
return move ? move : cell
108-
}))
109-
}
110-
},
111-
onError: graphQLError
112-
});
113-
114-
useSubscription<SubscriptionGameRestartedData>(GAME_RESTARTED, {
97+
useSubscription<SubscriptionGameMovedData>(GAME_MOVED, {
11598
variables: {
11699
gameID: id
117100
},
118101
onData: ({ data: { data } }) => {
119102
if (data?.game) {
120-
setBoard(createBoard)
103+
const game = data.game;
104+
if (game.isGameRestarted) {
105+
setBoard(prevState => prevState.map(cell => {
106+
const move = game.moves.find(move => move.row == cell.row && move.col == cell.col);
107+
return move ? move : { ...cell, gamer: null, gamerID: null }
108+
}))
109+
} else {
110+
setBoard(prevState => prevState.map(cell => {
111+
const move = game.moves.find(move => move.row == cell.row && move.col == cell.col);
112+
return move ? move : cell
113+
}))
114+
}
121115
}
122116
},
123-
onError: graphQLError
117+
onError: graphQLError
124118
});
125119

126120
const handleHint = useCallback(async (move: IStone) => {
@@ -197,7 +191,7 @@ export default function Board() {
197191
variables: {
198192
data: {
199193
_id: id as string,
200-
moveOrder: nextMoveOrderID(memoizedGame as IGame),
194+
moveOrder: nextMoveOrderID(memoizedGame as IGame),
201195
gamers: updatedGamers
202196
}
203197
}

client/app/_graphql/subscriptions.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { gql } from '@apollo/client';
22

33
export const GAME_MOVED = gql`
44
subscription gameMoved($gameID: ID!) {
5-
moves:gameMoved(gameID: $gameID) {
6-
col
7-
gameID
8-
gamer
9-
row
5+
game:gameMoved(gameID: $gameID) {
6+
isGameRestarted
7+
moves {
8+
row
9+
gamer
10+
gameID
11+
col
12+
}
1013
}
1114
}
1215
`;
@@ -31,14 +34,6 @@ export const GAME_UPDATED = gql`
3134
}
3235
`;
3336

34-
export const GAME_RESTARTED = gql`
35-
subscription gameRestarted($gameID: ID!) {
36-
game:gameRestarted(gameID: $gameID) {
37-
_id
38-
}
39-
}
40-
`;
41-
4237
export const GAMERS_STONE_COUNT_UPDATED = gql`
4338
subscription gamersStoneCountUpdated($gameID: ID!) {
4439
game:gamersStoneCountUpdated(gameID: $gameID) {

client/app/_providers/ApolloProviderWrapper.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { ReactNode } from "react";
22
import { ApolloClient, ApolloProvider, createHttpLink, from, InMemoryCache, split } from "@apollo/client";
33
import { removeTypenameFromVariables } from "@apollo/client/link/remove-typename";
4+
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
45
import { createClient } from "graphql-ws";
56
import { getMainDefinition } from "@apollo/client/utilities";
6-
import { IMove } from "@/_types";
77
import { useGame } from "@/_providers/GameProvider";
8-
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
8+
import { SubscriptionGameMovedData } from "@/_types";
99
import { ELocalStorage } from "@/_enums";
1010

1111
function createWsLink(gameID: string) {
1212
return new GraphQLWsLink(createClient({
1313
url: process.env.NEXT_PUBLIC_WS_URL as string,
14+
1415
connectionParams: {
1516
userID: localStorage.getItem(ELocalStorage.USERID),
1617
gameID
@@ -44,16 +45,18 @@ function getClient(gameID: string) {
4445
fields: {
4546
gameMoved: {
4647
keyArgs: [ "gameID" ],
47-
merge(existing: IMove[] = [], incoming: IMove[]) {
48-
const merged = [ ...existing ];
48+
merge(existing: SubscriptionGameMovedData, incoming: SubscriptionGameMovedData) {
49+
const existingMoves = existing?.moves || [];
50+
const incomingMoves = incoming?.moves || [];
51+
const merged = [ ...existingMoves ];
4952

5053
// Create a map to keep track of unique row+col combinations
5154
const map = new Map();
5255
for (const move of merged) {
5356
map.set(`${move.row}:${move.col}`, move);
5457
}
5558

56-
for (const move of incoming) {
59+
for (const move of incomingMoves) {
5760
map.set(`${move.row}:${move.col}`, move);
5861
}
5962

client/app/_types/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@ export type IGamersStoneCount = {
4444
}
4545

4646
export interface SubscriptionGameMovedData {
47-
moves: IMove[] | undefined
47+
game: {
48+
moves: IMove[],
49+
isGameRestarted?: boolean
50+
} | undefined
4851
}
4952

5053
export interface SubscriptionGameUpdatedData {
5154
game: IGame | undefined
5255
}
5356

54-
export interface SubscriptionGameRestartedData {
55-
game: IGame | undefined
56-
}
57-
5857
export interface SubscriptionGamersStoneCountUpdatedData {
5958
game: IGamersStoneCount | undefined
6059
}
File renamed without changes.

server/src/enums/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export enum EGamerStatus {
1111
export enum ESubscriptionMessages {
1212
GAME_MOVED = "GAME_MOVED",
1313
GAME_STARTED = "GAME_STARTED",
14-
GAME_RESTARTED = "GAME_STARTED",
1514
GAME_UPDATED = "GAME_UPDATED",
1615
GAMERS_STONE_COUNT_UPDATED = "GAMERS_STONE_COUNT_UPDATED",
1716
GAMER_CONNECTION = "GAMER_CONNECTION",

server/src/graphql/resolvers.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,13 @@ export default {
195195

196196

197197
// sent initial moves data to subscribers
198-
const allMoves = await Move.find({ gameID: data.gameID });
198+
const moves = await Move.find({ gameID: data.gameID });
199199

200-
pubsub.publish(`${ESubscriptionMessages.GAME_MOVED}_${data.gameID}`, { gameMoved: allMoves });
200+
pubsub.publish(`${ESubscriptionMessages.GAME_MOVED}_${data.gameID}`, {
201+
gameMoved: {
202+
moves
203+
}
204+
});
201205

202206
if (game.gamers.length == MAX_GAMER_COUNT) {
203207
// start game with black gamer
@@ -305,7 +309,7 @@ export default {
305309
const game: IGameDocument = await isAllStoneReversed(gameID) ? await handleGameFinish() : await updateGameMoveOrder(gameID, gamer)
306310
const allMoves = await Move.find({ gameID });
307311

308-
pubsub.publish(`${ESubscriptionMessages.GAME_MOVED}_${gameID}`, { gameMoved: moves });
312+
pubsub.publish(`${ESubscriptionMessages.GAME_MOVED}_${gameID}`, { gameMoved: { moves: allMoves } });
309313
pubsub.publish(`${ESubscriptionMessages.GAME_UPDATED}_${gameID}`, { gameUpdated: game });
310314
pubsub.publish(`${ESubscriptionMessages.GAMERS_STONE_COUNT_UPDATED}_${gameID}`, {
311315
gamersStoneCountUpdated: {
@@ -351,16 +355,20 @@ export default {
351355
if (game.gamers.length != MAX_GAMER_COUNT) return game;
352356

353357
await restartGame(game);
354-
const allMoves = await restartMoves(_id);
355-
pubsub.publish(`${ESubscriptionMessages.GAME_MOVED}_${_id}`, { gameMoved: allMoves });
358+
const moves = await restartMoves(_id);
359+
pubsub.publish(`${ESubscriptionMessages.GAME_MOVED}_${_id}`, {
360+
gameMoved: {
361+
moves,
362+
isGameRestarted: true,
363+
}
364+
});
356365
pubsub.publish(`${ESubscriptionMessages.GAMERS_STONE_COUNT_UPDATED}_${_id}`, {
357366
gamersStoneCountUpdated: {
358367
game,
359-
count: getGamersStoneCount(allMoves)
368+
count: getGamersStoneCount(moves)
360369
}
361370
});
362371
pubsub.publish(`${ESubscriptionMessages.GAME_UPDATED}_${_id}`, { gameUpdated: game });
363-
pubsub.publish(`${ESubscriptionMessages.GAME_RESTARTED}_${_id}`, { gameRestarted: game });
364372

365373
return game;
366374
} catch (error) {
@@ -384,9 +392,6 @@ export default {
384392
gameStarted: {
385393
subscribe: (parent, { gameID }) => pubsub.asyncIterator([ `${ESubscriptionMessages.GAME_STARTED}_${gameID}` ])
386394
},
387-
gameRestarted: {
388-
subscribe: (parent, { gameID }) => pubsub.asyncIterator([ `${ESubscriptionMessages.GAME_RESTARTED}_${gameID}` ])
389-
},
390395
gameUpdated: {
391396
subscribe: (parent, { gameID }) => pubsub.asyncIterator([ `${ESubscriptionMessages.GAME_UPDATED}_${gameID}` ])
392397
},

server/src/graphql/typeDefs.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ type Mutation {
2525
2626
type Subscription {
2727
gameStarted(gameID:ID!):Game!
28-
gameRestarted(gameID:ID!):Game!
2928
gameUpdated(gameID:ID!):Game!
30-
gameMoved(gameID: ID!): [Move]
29+
gameMoved(gameID: ID!): GameMoved!
3130
gamersStoneCountUpdated(gameID:ID!):GamersStoneCount
3231
gamerConnection(gameID:ID!):GamerConnection!
3332
}
@@ -57,6 +56,11 @@ type Move {
5756
gameID:ID!
5857
}
5958
59+
type GameMoved {
60+
isGameRestarted:Boolean,
61+
moves:[Move]!
62+
}
63+
6064
type GamerConnection {
6165
gameID:String!
6266
userID:String!

0 commit comments

Comments
 (0)