Skip to content

Commit

Permalink
feat(layout): improve layout deck positions
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebkel authored Mar 23, 2024
1 parent 8e916fc commit 57811a0
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions packages/unoenty/src/hooks/useSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ const useSocket = (): UseSocketResponse => {
}

const getLayoutedOtherPlayers = (): Record<GameDeckLayoutPosition, PlayerData> => {
const positionsIndexMap: Record<number, GameDeckLayoutPosition> = {
0: "topLeft",
1: "top",
2: "topRight",
3: "right",
4: "bottomRight",
5: "bottom",
6: "bottomLeft",
7: "left"
const positionsByAmountOfPlayersMap: Record<number, GameDeckLayoutPosition[]> = {
1: ["bottom"],
2: ["bottom", "right"],
3: ["bottom", "right", "left"],
4: ["bottom", "right", "top", "left"],
5: ["bottom", "right", "topRight", "top", "left"],
6: ["bottom", "right", "topRight", "top", "topLeft", "left"],
7: ["bottom", "bottomRight", "right", "topRight", "top", "topLeft", "left"],
8: ["bottom", "bottomRight", "right", "topRight", "top", "topLeft", "left", "bottomLeft"],
}

const layoutedOtherPlayers = {} as Record<GameDeckLayoutPosition, PlayerData>
Expand All @@ -103,29 +103,15 @@ const useSocket = (): UseSocketResponse => {
const playerId = socketStore.player?.id

const currentPlayerIndex = players?.findIndex(player => player.id === playerId) as number
const isThereAnyCurrentPlayer = currentPlayerIndex === -1

if (isThereAnyCurrentPlayer) {
players.forEach((player, index) => {
layoutedOtherPlayers[positionsIndexMap[index]] = player
})
} else {
const currentPlayerPositionIndex = 5 // bottom

const otherPlayersBeforeCurrentPlayer = players?.slice(0, currentPlayerIndex)

otherPlayersBeforeCurrentPlayer?.reverse()?.forEach((player, index) => {
const otherPlayerIndex = getSanitizedValueWithBoundaries(currentPlayerPositionIndex - (index + 1), players.length, 0)
layoutedOtherPlayers[positionsIndexMap[otherPlayerIndex]] = player
})

const otherPlayersAfterCurrentPlayer = players?.slice(currentPlayerIndex + 1, players?.length)

otherPlayersAfterCurrentPlayer?.forEach((player, index) => {
const otherPlayerIndex = getSanitizedValueWithBoundaries(currentPlayerPositionIndex + (index + 1), players.length, 0)
layoutedOtherPlayers[positionsIndexMap[otherPlayerIndex]] = player
})
}
const isSpectator = currentPlayerIndex === -1

players?.forEach((player, index) => {
const diff = isSpectator ? 0 : currentPlayerIndex
const positionIndex = getSanitizedValueWithBoundaries(players.length - diff + index, players.length, 0)
const currentPositions = positionsByAmountOfPlayersMap[players.length]

layoutedOtherPlayers[currentPositions[positionIndex]] = player
})

return layoutedOtherPlayers
}
Expand Down

0 comments on commit 57811a0

Please sign in to comment.