From 57811a02c4221d398ddff57fbb054ea669ff80bf Mon Sep 17 00:00:00 2001 From: Guilherme Mota Date: Sat, 23 Mar 2024 15:06:47 -0300 Subject: [PATCH] feat(layout): improve layout deck positions --- packages/unoenty/src/hooks/useSocket.tsx | 50 +++++++++--------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/packages/unoenty/src/hooks/useSocket.tsx b/packages/unoenty/src/hooks/useSocket.tsx index ca15924..e906fe6 100644 --- a/packages/unoenty/src/hooks/useSocket.tsx +++ b/packages/unoenty/src/hooks/useSocket.tsx @@ -86,15 +86,15 @@ const useSocket = (): UseSocketResponse => { } const getLayoutedOtherPlayers = (): Record => { - const positionsIndexMap: Record = { - 0: "topLeft", - 1: "top", - 2: "topRight", - 3: "right", - 4: "bottomRight", - 5: "bottom", - 6: "bottomLeft", - 7: "left" + const positionsByAmountOfPlayersMap: Record = { + 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 @@ -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 }