-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Box, Button, Grid, Typography } from "@mui/material"; | ||
import { GameState, Player } from "../../models/state" | ||
import { PlayersView } from "../Game/PlayersView"; | ||
import { usePlayersVoteMessages } from "./hooks/usePlayersVoteMessages"; | ||
|
||
interface VoteViewProps { | ||
state: GameState, | ||
onRoundStartClick: () => void; | ||
} | ||
|
||
export const VoteView = ({ state, onRoundStartClick }: VoteViewProps) => { | ||
//TODO clean up vote messages after round or handle each message | ||
const { voteMessages } = usePlayersVoteMessages(); | ||
|
||
const activePlayers: Player[] = state.players.filter((_player, index) => | ||
state.round?.activePlayersIndexes.includes(index) | ||
); | ||
|
||
return ( | ||
<> | ||
<Grid> | ||
{voteMessages.map(voteMessage => ( | ||
<Typography>{voteMessage.name} ={">"} {voteMessage.votePlayerLabel}</Typography> | ||
))} | ||
<PlayersView players={activePlayers} /> | ||
<Box textAlign={"center"}> | ||
<Button | ||
sx={{ width: 200 }} | ||
size="large" | ||
variant="contained" | ||
color="success" | ||
onClick={() => onRoundStartClick()} | ||
> | ||
НАЧАТЬ РАУНД | ||
</Button> | ||
</Box> | ||
</Grid> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect, useState } from "react"; | ||
import { usePeerContext } from "../../../hooks/usePeerContext"; | ||
import { NetworkMessage, NetworkMessageType, PlayerMessage } from "../../../models/networking"; | ||
|
||
interface UsePlayersVoteData { | ||
voteMessages: PlayerMessage[]; | ||
} | ||
|
||
export const usePlayersVoteMessages = (): UsePlayersVoteData => { | ||
const [voteMessages, setVoteMessages] = useState<PlayerMessage[]>([]); | ||
const { lastMessages } = usePeerContext(); | ||
|
||
useEffect(() => { | ||
setVoteMessages(() => { | ||
const newVoteMessages: PlayerMessage[] = []; | ||
lastMessages.forEach((message: NetworkMessage) => { | ||
if (message && message.type === NetworkMessageType.PLAYER_MESSAGE) { | ||
const playerData = message.data as PlayerMessage; | ||
if (playerData.votePlayerLabel) { | ||
newVoteMessages.push(playerData); | ||
} | ||
} | ||
}); | ||
|
||
return newVoteMessages; | ||
}) | ||
|
||
}, [lastMessages]); | ||
|
||
return { | ||
voteMessages | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,65 @@ | ||
export const PlayerVoteView = () => { | ||
return <>VOTE</>; | ||
import { useEffect, useState } from "react"; | ||
import { GameState, Player } from "../../models/state"; | ||
import { NetworkMessageType } from "../../models/networking"; | ||
import { usePlayerState } from "./hooks/usePlayerState"; | ||
import { PlayersView } from "../Game/PlayersView"; | ||
import { Box, Button } from "@mui/material"; | ||
import { usePeerConnection } from "../../hooks/usePeerConnection"; | ||
|
||
interface PlayerViewProps { | ||
state: GameState; | ||
} | ||
|
||
export const PlayerVoteView = ({ state }: PlayerViewProps) => { | ||
const { connectionLabel } = usePlayerState(); | ||
const [playerToRemove, setPlayerToRemovePlayer] = useState<Player>(); | ||
const { send } = usePeerConnection(connectionLabel); | ||
|
||
useEffect(() => { | ||
if (!playerToRemove) { | ||
return; | ||
} | ||
|
||
const player = state.players.filter(player => player.connectionLabel === connectionLabel)[0]; | ||
send( | ||
{ | ||
type: NetworkMessageType.PLAYER_MESSAGE, | ||
data: { | ||
name: player.name, | ||
votePlayerLabel: playerToRemove.connectionLabel | ||
} | ||
} | ||
) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [playerToRemove]); | ||
|
||
const onVoteClick = () => { | ||
//TODO get selected player to remove | ||
const player = state.players[0]; | ||
setPlayerToRemovePlayer(player); | ||
}; | ||
|
||
const round = state.round; | ||
if (!round) { | ||
return <>WAITING END ROUND</> | ||
} | ||
|
||
const candidatePlayers = state.players.filter((player, index) => | ||
round.activePlayersIndexes.includes(index) && | ||
player.connectionLabel !== connectionLabel); | ||
|
||
return ( | ||
<> | ||
<PlayersView players={candidatePlayers} /> | ||
<Box textAlign={"center"}> | ||
<Button | ||
size="large" | ||
variant="contained" | ||
onClick={() => onVoteClick()} | ||
> | ||
ПРОГОЛОСОВАТЬ | ||
</Button> | ||
</Box> | ||
</> | ||
); | ||
} |