Skip to content

Commit f32dc76

Browse files
adngdbdelucis
andauthored
fix(api): Expose gameover metadata in rooms endpoints. (#666)
* fix(api): Expose gameover metadata in rooms endpoints. This adds the `gameover` metadata, if it exists, to both the all rooms and single room query endpoints. Fixes #665. * Refactor game metadata redacting into a function. Co-authored-by: Chris Swithinbank <[email protected]>
1 parent d76beda commit f32dc76

2 files changed

Lines changed: 38 additions & 18 deletions

File tree

src/server/api.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ describe('.createRouter', () => {
11461146
},
11471147
},
11481148
unlisted: gameID === 'bar-4',
1149+
gameover: gameID === 'bar-3' ? { winner: 0 } : undefined,
11491150
},
11501151
};
11511152
},
@@ -1165,6 +1166,7 @@ describe('.createRouter', () => {
11651166
},
11661167
});
11671168
});
1169+
11681170
describe('when given 2 rooms', () => {
11691171
let response;
11701172
let rooms;
@@ -1188,6 +1190,11 @@ describe('.createRouter', () => {
11881190
expect(rooms[0].players).toEqual([{ id: 0 }, { id: 1 }]);
11891191
expect(rooms[1].players).toEqual([{ id: 0 }, { id: 1 }]);
11901192
});
1193+
1194+
test('returns gameover data for ended game', async () => {
1195+
expect(rooms[0].gameover).toBeUndefined();
1196+
expect(rooms[1].gameover).toEqual({ winner: 0 });
1197+
});
11911198
});
11921199
});
11931200

@@ -1209,6 +1216,7 @@ describe('.createRouter', () => {
12091216
credentials: 'SECRET2',
12101217
},
12111218
},
1219+
gameover: { winner: 1 },
12121220
},
12131221
};
12141222
},
@@ -1235,6 +1243,10 @@ describe('.createRouter', () => {
12351243
test('returns player names', async () => {
12361244
expect(room.players).toEqual([{ id: 0 }, { id: 1 }]);
12371245
});
1246+
1247+
test('returns gameover data for ended game', async () => {
1248+
expect(room.gameover).toEqual({ winner: 1 });
1249+
});
12381250
});
12391251

12401252
describe('when given a non-existent room ID', () => {

src/server/api.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ export const CreateGame = async ({
6262
return gameID;
6363
};
6464

65+
/**
66+
* Create a metadata object without secret credentials to return to the client.
67+
*
68+
* @param {string} gameID - The identifier of the game the metadata belongs to.
69+
* @param {object} metadata - The game metadata object to strip credentials from.
70+
* @return - A metadata object without player credentials.
71+
*/
72+
const createClientGameMetadata = (
73+
gameID: string,
74+
metadata: Server.GameMetadata
75+
) => {
76+
return {
77+
...metadata,
78+
gameID,
79+
roomID: gameID,
80+
matchID: gameID,
81+
players: Object.values(metadata.players).map(player => {
82+
// strip away credentials
83+
const { credentials, ...strippedInfo } = player;
84+
return strippedInfo;
85+
}),
86+
};
87+
};
88+
6589
export const createRouter = ({
6690
db,
6791
games,
@@ -117,15 +141,7 @@ export const createRouter = ({
117141
metadata: true,
118142
});
119143
if (!metadata.unlisted) {
120-
rooms.push({
121-
gameID,
122-
players: Object.values(metadata.players).map(player => {
123-
// strip away credentials
124-
const { credentials, ...strippedInfo } = player;
125-
return strippedInfo;
126-
}),
127-
setupData: metadata.setupData,
128-
});
144+
rooms.push(createClientGameMetadata(gameID, metadata));
129145
}
130146
}
131147
ctx.body = {
@@ -141,15 +157,7 @@ export const createRouter = ({
141157
if (!metadata) {
142158
ctx.throw(404, 'Room ' + gameID + ' not found');
143159
}
144-
const strippedRoom = {
145-
roomID: gameID,
146-
players: Object.values(metadata.players).map(player => {
147-
const { credentials, ...strippedInfo } = player;
148-
return strippedInfo;
149-
}),
150-
setupData: metadata.setupData,
151-
};
152-
ctx.body = strippedRoom;
160+
ctx.body = createClientGameMetadata(gameID, metadata);
153161
});
154162

155163
router.post('/games/:name/:id/join', koaBody(), async ctx => {

0 commit comments

Comments
 (0)