From 2040c523663951028ac0e4d00b3f2a192c6e498f Mon Sep 17 00:00:00 2001 From: Ruba Al Mahmoud Date: Sun, 8 Dec 2024 18:55:43 +0300 Subject: [PATCH 1/4] Transform players array in findOpenGames to include only id and username --- api/hooks/customGameHook/index.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/api/hooks/customGameHook/index.js b/api/hooks/customGameHook/index.js index afac7416b..201326de2 100644 --- a/api/hooks/customGameHook/index.js +++ b/api/hooks/customGameHook/index.js @@ -30,24 +30,36 @@ module.exports = function gameHook() { }, findOpenGames: function () { return new Promise(function (resolve, reject) { - const recentUpdateThreshhold = dayjs.utc().subtract(1, 'day') - .toDate(); + const recentUpdateThreshold = dayjs.utc().subtract(1, 'day').toDate(); + Game.find({ status: gameService.GameStatus.CREATED, - createdAt: { '>=': recentUpdateThreshhold }, + createdAt: { '>=': recentUpdateThreshold }, }) - .populate('players') + .populate('players') // Populate the players array .exec(function (error, games) { if (error) { return reject(error); - } else if (!games) { + } else if (!games || games.length === 0) { return reject({ message: "Can't find games" }); } - const openGames = games.filter(({ players }) => players.length < 2); - return resolve(openGames); + + // Filter games with fewer than 2 players and transform the players array + const openGames = games + .filter(({ players }) => players.length < 2) // Keep games with fewer than 2 players + .map((game) => { + // Transform players array to include only id and username + game.players = game.players.map((player) => ({ + id: player.id, // Use 'id' since Sails.js generates it automatically + username: player.username, + })); + return game; // Return the updated game object + }); + + return resolve(openGames); // Resolve with transformed open games }); }); - }, + }, findGame: function (id) { return new Promise(function (resolve, reject) { Game.findOne(id) From ac992ff13bcac78fbea268884a12c98833429bf4 Mon Sep 17 00:00:00 2001 From: Ruba Al Mahmoud Date: Tue, 10 Dec 2024 12:39:34 +0300 Subject: [PATCH 2/4] Refactor game-player relationship to use p0 and p1 on game creation --- api/models/Game.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/models/Game.js b/api/models/Game.js index af5b26985..e619b4cfb 100644 --- a/api/models/Game.js +++ b/api/models/Game.js @@ -144,4 +144,11 @@ module.exports = { columnType: 'timestamptz', }, }, + + customToJSON: function () { + return { + ...this, + players: [this.p0, this.p1].filter(Boolean), + }; + }, }; // end exports From af1c8544b860312fca3f540ed88482a0e533b554 Mon Sep 17 00:00:00 2001 From: Ruba Al Mahmoud Date: Tue, 10 Dec 2024 12:50:37 +0300 Subject: [PATCH 3/4] Update customGameHook.js to set p0 and p1 during game creation --- api/hooks/customGameHook/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/hooks/customGameHook/index.js b/api/hooks/customGameHook/index.js index 201326de2..ae95cac0d 100644 --- a/api/hooks/customGameHook/index.js +++ b/api/hooks/customGameHook/index.js @@ -6,12 +6,14 @@ module.exports = function gameHook() { // Game API // ////////////// return { - createGame: function (gameName, isRanked = false, status = gameService.GameStatus.CREATED) { + createGame: function (gameName, isRanked = false, status = gameService.GameStatus.CREATED, player0Id, player1Id) { return new Promise(function (resolve, reject) { Game.create({ name: gameName, isRanked: isRanked === true, status, + p0: player0Id, + p1: player1Id, }) .fetch() .then((game) => { From a89a2e58b0e7b15dbb3caf14c4e6f7622a704f25 Mon Sep 17 00:00:00 2001 From: Ruba Al Mahmoud Date: Tue, 10 Dec 2024 12:52:52 +0300 Subject: [PATCH 4/4] Update create.js to pass player0Id and player1Id when creating a game --- api/controllers/game/create.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/api/controllers/game/create.js b/api/controllers/game/create.js index 358d3eb7a..70d562ca3 100644 --- a/api/controllers/game/create.js +++ b/api/controllers/game/create.js @@ -1,22 +1,25 @@ const gameAPI = sails.hooks['customgamehook']; module.exports = function (req, res) { - const { gameName, isRanked = false } = req.body; + const { gameName, isRanked = false, player0Id, player1Id } = req.body; + if (req.body.gameName) { gameAPI - .createGame(gameName, isRanked) + .createGame(gameName, isRanked, gameService.GameStatus.CREATED, player0Id, player1Id) .then(function (game) { sails.sockets.broadcast('GameList', 'gameCreated', { id: game.id, name: game.name, status: game.status, isRanked: game.isRanked, - players: [], + players: [], // This will now be populated based on p0 and p1 }); return res.ok({ gameId: game.id }); }) .catch(function (reason) { res.badRequest(reason); }); + } else { + res.badRequest({ message: "Game name is required" }); } -}; +}; \ No newline at end of file