Skip to content

Commit

Permalink
Merge pull request #56 from Owlvernyte/fiezt/turn-table
Browse files Browse the repository at this point in the history
turned game table with viewer side
  • Loading branch information
fieztazica authored Oct 23, 2023
2 parents 6ce2f2c + 9447076 commit 499d134
Showing 1 changed file with 76 additions and 58 deletions.
134 changes: 76 additions & 58 deletions app/(basic)/rooms/[id]/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ function Game({ roomCode, accessToken, user }: GameProps) {
return false
})()

const isBoardReversed: boolean = !(!isPlayer
? !board.isHostRed
: (board.isHostRed && isHost) || (!board.isHostRed && !isHost))

useEffect(() => {
connection.on('error', (e) => {
console.log('ws error', e)
Expand Down Expand Up @@ -328,27 +332,60 @@ function Game({ roomCode, accessToken, user }: GameProps) {
)
}

const squares = board.squares.map((row, i) =>
row.map((cell, j) => {
if (!cell) {
return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
></Cell>
)
}
const RenderedSquares = () => {
const squares = board.squares.map((row, i) =>
row.map((cell, j) => {
if (!cell) {
return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
></Cell>
)
}

if (!isUserTurn) {
return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
>
<Piece
id={cell.id}
target={cell}
position={cell.coord}
title={'Cant move'}
disabled
draggable={false}
/>
</Cell>
)
}

if (board.isRedTurn === cell.isRed) {
return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
>
<DraggablePiece
id={cell.id}
target={cell}
position={cell.coord}
title={'Movable'}
/>
</Cell>
)
}

if (!isUserTurn) {
return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
>
<Cell key={`cell_${i}_${j}`} id={`${i}_${j}`} x={i} y={j}>
<Piece
id={cell.id}
target={cell}
Expand All @@ -359,45 +396,13 @@ function Game({ roomCode, accessToken, user }: GameProps) {
/>
</Cell>
)
}

if (board.isRedTurn === cell.isRed) {
return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
>
<DraggablePiece
id={cell.id}
target={cell}
position={cell.coord}
title={'Movable'}
/>
</Cell>
)
}
})
)

return (
<Cell
key={`cell_${i}_${j}`}
id={`${i}_${j}`}
x={i}
y={j}
>
<Piece
id={cell.id}
target={cell}
position={cell.coord}
title={'Cant move'}
disabled
draggable={false}
/>
</Cell>
)
})
);
return isBoardReversed
? squares.map((row) => row.reverse()).reverse()
: squares
}

return (
<DndContext
Expand Down Expand Up @@ -434,7 +439,7 @@ function Game({ roomCode, accessToken, user }: GameProps) {
</div>
</div>
<Board>
{(board.isHostRed && isHost) || (!board.isHostRed && !isHost) ? squares : squares.reverse()}
<RenderedSquares />
</Board>
<div
id="right-area"
Expand Down Expand Up @@ -472,10 +477,23 @@ function Game({ roomCode, accessToken, user }: GameProps) {
<PlayerArea
playerIndex={1}
userName={room.hostUser?.userName}
label={
board.isRedTurn && board.isHostRed
? undefined
: !board.isRedTurn &&
!board.isHostRed
? undefined
: 'ĐANG CHỜ TỚI LƯỢT'
}
/>
<PlayerArea
playerIndex={2}
userName={room.opponentUser?.userName}
label={
board.isRedTurn && !board.isHostRed
? undefined
: 'ĐANG CHỜ TỚI LƯỢT'
}
/>
</>
)}
Expand Down

0 comments on commit 499d134

Please sign in to comment.