-
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' into feature/#3
- Loading branch information
Showing
5 changed files
with
68 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,82 +1,41 @@ | ||
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; | ||
interface Vote { | ||
by: string; | ||
liar: 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]); | ||
interface GameData { | ||
id: string; | ||
users: string[]; | ||
votedFor: Vote[]; | ||
} | ||
|
||
// 투표 결과 계산 후 최종 라이어를 부모 컴포넌트로 전달 | ||
useEffect(() => { | ||
if (finalLiar) { | ||
onClose(finalLiar); | ||
const calculateVote = (gameData: GameData): string | null => { | ||
if (gameData.votedFor.length !== gameData.users.length) { | ||
console.log( | ||
`투표가 진행중입니다: ${gameData.votedFor.length} / ${gameData.users.length}`, | ||
); | ||
return null; | ||
} | ||
|
||
const voteCount: Record<string, number> = {}; | ||
gameData.votedFor.forEach((vote) => { | ||
const liarId = vote.liar; | ||
console.log("liarId:" + liarId); | ||
voteCount[liarId] = (voteCount[liarId] || 0) + 1; | ||
}); | ||
|
||
let maxCount = 0; | ||
let maxId: string | null = null; | ||
|
||
for (const id in voteCount) { | ||
if (voteCount[id] > maxCount) { | ||
maxCount = voteCount[id]; | ||
maxId = id; | ||
console.log("maxId", maxId, maxCount + "표"); | ||
} | ||
}, [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> | ||
); | ||
return maxId; | ||
}; | ||
|
||
export default CalculateVote; | ||
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