-
Notifications
You must be signed in to change notification settings - Fork 2
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: 🎨 Refactor the players array into a map #176
base: development
Are you sure you want to change the base?
Conversation
Began refactoring the players array
…-me-if-you-can into jsalkeld-refactor-players-map
Avatar.ts is refactored. Lobby.ts uses less object.keys
Host works properly with the map rather than an array
Changed to not in rather than in
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
Fixed the most votes logic
@mrjoshua520 some of my comments got resolved but I don't see any new commits am I missing something? |
There are commits waiting to be pushed but I figured I wouldn't until I got a chance to fix some of the bugs that are still in the branch |
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
…-me-if-you-can into jsalkeld-refactor-players-map
const aliveUids: string[] = []; | ||
lobbyData.players.forEach((player, playerIndex) => { | ||
if (player.alive) { | ||
aliveUids.push(lobbyData.uids[playerIndex]); | ||
|
||
for (const uid in lobbyData.players) { | ||
if (lobbyData.players[uid].alive) { | ||
aliveUids.push(uid); | ||
} | ||
}); | ||
} |
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 could be done functionally with filter
but this is ok
functions/src/lobby.ts
Outdated
// get lobby data | ||
const { players, uids } = lobbyInfo.data() as Lobby; | ||
const { players, host } = lobbyInfo.data() as Lobby; | ||
|
||
// Get position of the Player | ||
const playerPos = uids.indexOf(auth.uid); | ||
delete players[auth.uid]; | ||
|
||
// If the last player is leaving delete the document instead | ||
if (uids.length === 1) { | ||
if (Object.keys(players).length === 0) { | ||
transaction.delete(lobby); | ||
} else { | ||
// Remove player from the lobby | ||
transaction.update(lobby, { | ||
players: FieldValue.arrayRemove(players[playerPos]), | ||
uids: FieldValue.arrayRemove(auth.uid), | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Workaround for https://github.com/googleapis/nodejs-firestore/issues/1808 | ||
players: players satisfies Lobby["players"] as any, | ||
host: updateHost(players) ?? host, | ||
}); | ||
} |
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 still looks like it has some duplicaed code with kick and ban
let winner: Lobby["winner"]; | ||
const alteredPlayers: Player[] = JSON.parse(JSON.stringify(currentPlayers)); | ||
const alteredPlayers: Player[] = JSON.parse(JSON.stringify(players)); |
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.
I don't think you can do this anymore because Player now contains a timestamp
…-me-if-you-can into jsalkeld-refactor-players-map
…Time Slightly faster and I don't think it effects readability
Keep in mind this refactor also removes the UID array