Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor game-player relationship to initialize p0 and p1 at game creation #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions api/controllers/game/create.js
Original file line number Diff line number Diff line change
@@ -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" });
}
};
};
32 changes: 23 additions & 9 deletions api/hooks/customGameHook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -30,24 +32,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)
Expand Down
7 changes: 7 additions & 0 deletions api/models/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,11 @@ module.exports = {
columnType: 'timestamptz',
},
},

customToJSON: function () {
return {
...this,
players: [this.p0, this.p1].filter(Boolean),
};
},
}; // end exports