-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/pepero-1/liar-game into …
…feature/#3
- Loading branch information
Showing
5 changed files
with
247 additions
and
9 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,82 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { | ||
Button, | ||
Modal, | ||
ModalOverlay, | ||
ModalContent, | ||
ModalHeader, | ||
ModalCloseButton, | ||
ModalBody, | ||
ModalFooter, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
import useFireFetch from "../../../hooks/useFireFetch"; | ||
|
||
interface CalculateVoteProps { | ||
voteResults: string; | ||
onClose: (finalLiar: string) => void; | ||
gameId: string; | ||
} | ||
|
||
const CalculateVote: React.FC<CalculateVoteProps> = ({ | ||
voteResults, | ||
onClose, | ||
gameId, | ||
}) => { | ||
const [finalLiar, setFinalLiar] = useState<string>(""); | ||
const fireFetch = useFireFetch(); | ||
|
||
// 투표 결과 계산 로직 | ||
useEffect(() => { | ||
const calculateFinalLiar = async () => { | ||
const gameData = await fireFetch.useGetSome("game", "id", gameId); | ||
const { users, votedFor } = gameData.data[0]; | ||
|
||
const votesCount: Record<string, number> = {}; | ||
users.forEach((user: string) => { | ||
if (votedFor.includes(user)) { | ||
votesCount[user] = (votesCount[user] || 0) + 1; | ||
} | ||
}); | ||
|
||
let maxVotes = 0; | ||
for (const user in votesCount) { | ||
if (votesCount[user] > maxVotes) { | ||
maxVotes = votesCount[user]; | ||
setFinalLiar(user); | ||
} | ||
} | ||
}; | ||
|
||
calculateFinalLiar(); | ||
}, [voteResults, gameId, fireFetch]); | ||
|
||
// 투표 결과 계산 후 최종 라이어를 부모 컴포넌트로 전달 | ||
useEffect(() => { | ||
if (finalLiar) { | ||
onClose(finalLiar); | ||
} | ||
}, [finalLiar, onClose]); | ||
|
||
return ( | ||
<Modal isOpen={true} onClose={() => {}}> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>투표 결과 계산 중</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody> | ||
<Text>투표 결과 계산 중입니다...</Text> | ||
</ModalBody> | ||
<ModalFooter> | ||
{finalLiar && ( | ||
<Button colorScheme="blue" onClick={() => onClose(finalLiar)}> | ||
계산 완료 | ||
</Button> | ||
)} | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default CalculateVote; |
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,5 +1,82 @@ | ||
const Vote = () => { | ||
return <div>Vote</div>; | ||
import { useState } from "react"; | ||
import { | ||
Modal, | ||
ModalOverlay, | ||
ModalContent, | ||
ModalHeader, | ||
ModalCloseButton, | ||
ModalBody, | ||
ModalFooter, | ||
Button, | ||
Radio, | ||
RadioGroup, | ||
Stack, | ||
} from "@chakra-ui/react"; | ||
import useFireFetch from "../../../hooks/useFireFetch"; | ||
import { arrayUnion } from "firebase/firestore"; | ||
|
||
interface GameData { | ||
id: string; | ||
users: string[]; | ||
} | ||
|
||
interface VoteProps { | ||
onClose: (selectedUser: string) => void; | ||
gameData: GameData; | ||
} | ||
|
||
const Vote: React.FC<VoteProps> = ({ | ||
onClose, | ||
gameData, | ||
}): React.ReactElement => { | ||
const [selectedUser, setSelectedUser] = useState<string>(""); | ||
const fireFetch = useFireFetch(); | ||
|
||
const storedToken = localStorage.getItem("token"); | ||
const tokenData = storedToken ? JSON.parse(storedToken) : null; | ||
|
||
const handleUserChange = (value: string) => { | ||
setSelectedUser(value); | ||
}; | ||
|
||
const handleVoteSubmit = () => { | ||
if (selectedUser !== null) { | ||
const myId = tokenData.id; | ||
fireFetch.updateData("game", gameData.id, { | ||
votedFor: arrayUnion({ by: myId, liar: selectedUser }), | ||
}); | ||
console.log(selectedUser + "에 투표했습니다."); | ||
onClose(selectedUser); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Modal isOpen={true} onClose={() => onClose(selectedUser)}> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>라이어 투표하기</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody> | ||
<RadioGroup value={selectedUser} onChange={handleUserChange}> | ||
<Stack spacing={2}> | ||
{gameData.users.map((user) => ( | ||
<Radio key={user} value={user}> | ||
{user} | ||
</Radio> | ||
))} | ||
</Stack> | ||
</RadioGroup> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button colorScheme="blue" mr={3} onClick={handleVoteSubmit}> | ||
투표 완료 | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default Vote; |
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,16 @@ | ||
import React from "react"; | ||
import { Box } from "@chakra-ui/react"; | ||
|
||
interface SystemChatProps { | ||
text: string; | ||
} | ||
|
||
const SystemChat: React.FC<SystemChatProps> = ({ text }) => { | ||
return ( | ||
<Box textAlign="center" bgColor="gray.300" p={1} borderRadius={5} my={2}> | ||
{text} | ||
</Box> | ||
); | ||
}; | ||
|
||
export default SystemChat; |
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