-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Updating Game List to Display Players Username #1130
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,24 +30,37 @@ module.exports = function gameHook() { | |||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
findOpenGames: function () { | ||||||||||||||||||||||||||||||
return new Promise(function (resolve, reject) { | ||||||||||||||||||||||||||||||
const recentUpdateThreshhold = dayjs.utc().subtract(1, 'day') | ||||||||||||||||||||||||||||||
const recentUpdateThreshold = dayjs.utc().subtract(1, 'day') | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch; thank you |
||||||||||||||||||||||||||||||
.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) { | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can combine these with optional chaining and utilize the fact that
Suggested change
|
||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this comment is sufficiently explained by the one above the
Suggested change
|
||||||||||||||||||||||||||||||
.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 | ||||||||||||||||||||||||||||||
Comment on lines
+54
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good. I think the above comments are helpful but the ones on the same line are not adding much clarity beyond what the line already says. Let's remove these. Generally, our approach to commenting is to either:
Suggested change
|
||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
findGame: function (id) { | ||||||||||||||||||||||||||||||
return new Promise(function (resolve, reject) { | ||||||||||||||||||||||||||||||
Game.findOne(id) | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
{{ name }} | ||
</p> | ||
<p v-if="!isSpectatable" class="text-surface-1"> | ||
{{ readyText }} {{ t('home.players') }} | ||
{{ playersText }} | ||
</p> | ||
</v-col> | ||
<v-col lg="6" class="list-item__button pr-md-0"> | ||
|
@@ -81,6 +81,10 @@ export default { | |
type: Number, | ||
required: true, | ||
}, | ||
players: { | ||
type: Array, | ||
required: true, | ||
}, | ||
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also remove |
||
isRanked: { | ||
type: Boolean, | ||
default: false, | ||
|
@@ -109,8 +113,18 @@ export default { | |
numPlayersReady() { | ||
return this.p0ready + this.p1ready; | ||
}, | ||
readyText() { | ||
return `${this.numPlayers} / 2`; | ||
playersText() { | ||
const numPlayers = this.players.length; | ||
switch (numPlayers) { | ||
case 0: | ||
return 'Empty'; | ||
case 1: | ||
return `vs ${this.players[0].username}`; | ||
case 2: | ||
return `${this.players[0].username} vs ${this.players[1].username}`; | ||
default: | ||
return ''; | ||
} | ||
}, | ||
joinButtonText() { | ||
return `${this.t('home.join')} ${this.isRanked ? this.t('global.ranked') : this.t('global.casual')}`; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ class GameSummary { | |
this.id = obj.id ? obj.id : null; | ||
this.name = obj.name ? obj.name : null; | ||
this.numPlayers = Object.prototype.hasOwnProperty.call(obj, 'players') ? obj.players.length : 0; | ||
this.players = Object.prototype.hasOwnProperty.call(obj, 'players') ? obj.players : []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will additionally need to update the places |
||
this.status = Object.prototype.hasOwnProperty.call(obj, 'status') ? obj.status : GameStatus.ARCHIVED; | ||
this.isRanked = Object.prototype.hasOwnProperty.call(obj, 'isRanked') ? obj.isRanked : false; | ||
this.isOver = false; | ||
|
@@ -53,13 +54,15 @@ export const useGameListStore = defineStore('gameList', { | |
const updatedGame = this.openGames.find((game) => game.id === data.gameId); | ||
if (updatedGame) { | ||
updatedGame.numPlayers = Math.min(2, updatedGame.numPlayers + 1); | ||
updatedGame.players.push(data.newPlayer); | ||
updatedGame.status = data.newStatus; | ||
} | ||
}, | ||
otherLeftGame(gameId) { | ||
const updatedGame = this.openGames.find((game) => game.id === gameId); | ||
if (updatedGame) { | ||
updatedGame.numPlayers = Math.max(0, updatedGame.numPlayers - 1); | ||
updatedGame.players.pop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually a little trickier than you have it here and will require a full stack change. Specifically, we don't know which player left, so we can't just assume it was the one at the end of the array. We need to update
So that its
|
||
} | ||
}, | ||
setIsRanked({ gameId, isRanked }) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works and does what the issue described, but can we actually refactor it to use the single-valued attributes of
p0
andp1
and to then combine them and transform them into an array?Right now the
Game
model has:p0
: the user who is player 0p1
: the user who is player 1players
: the collection of[p0, p1]
.players
is redundant and being deprecated for removal. The main reason for this is that at the database level, theplayers
collection works by having theuser
table use a foreign key to thegame
table, which therefore imposes the constraint that a user can only be in one game at a time (something we'd like to change). Conversely,p0
andp1
are columns in thegame
table which are foreign keys to theuser
table, allowing any given user to be in as many games as desired. We plan to refactor the entire codebase to remove the use of.players
from the backend and replace it with.p0
and.p1
.As one additional note, when using
Array.map()
, we generally avoid mutating the original array as this can have unintended consequences. In this case we are producing the list within the function and transforming it just to return so it's not a horrible thing to do, but generally it's preferred to copy the object instead (usually with spread) rather than doing something likegame.players =
inside the.map()
.In this situation, that would look like this