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: 🎨 Refactor the players array into a map #176

Open
wants to merge 108 commits into
base: development
Choose a base branch
from

Conversation

mrjoshua520
Copy link
Member

Keep in mind this refactor also removes the UID array

Avatar.ts is refactored. Lobby.ts uses less object.keys
Host works properly with the map rather than an array
Fixed the most votes logic
@nstringham
Copy link
Member

@mrjoshua520 some of my comments got resolved but I don't see any new commits am I missing something?

@mrjoshua520
Copy link
Member Author

@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

Comment on lines 14 to +20
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);
}
});
}
Copy link
Member

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/util.ts Outdated Show resolved Hide resolved
Comment on lines 231 to 246
// 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,
});
}
Copy link
Member

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

functions/src/lobby.ts Outdated Show resolved Hide resolved
let winner: Lobby["winner"];
const alteredPlayers: Player[] = JSON.parse(JSON.stringify(currentPlayers));
const alteredPlayers: Player[] = JSON.parse(JSON.stringify(players));
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor players into a map
2 participants