Skip to content

Commit

Permalink
Merge pull request #62 from pepero-1/feature/#53
Browse files Browse the repository at this point in the history
입장메세지 수정, 게임종료시 reload
  • Loading branch information
2YH02 authored Nov 15, 2023
2 parents 7abc135 + 52eed7a commit 1e1a60b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/components/Game/GameChat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface GameChatProps {

interface UserResponse {
users: string[];
joiners?: string[];
joiners?: any[];
leaver?: string;
}

Expand All @@ -61,13 +61,10 @@ const GameChat: React.FC<GameChatProps> = ({
setCurrent,
}) => {
const user = useRecoilValue(userState);
console.log("Chat/ liar:" + liar);
const [message] = useState<Message>({
id: "",
text: "",
});
console.log("current,", current);
// console.log("GameChat/ gameData:", gameData);
const [messages, setMessages]: any = useState([]);
const messageRef = useRef<HTMLInputElement | null>(null);
const [, setUsers] = useState<string[]>([]);
Expand Down Expand Up @@ -147,10 +144,20 @@ const GameChat: React.FC<GameChatProps> = ({
useEffect(() => {
// 유저 입장 메시지 수신
socket.on("join", (responseData: UserResponse) => {
const systemMessage = `${responseData.joiners!.join} 님이 입장했습니다.`;

setMessages([...messages, { id: "system", text: systemMessage }]);
setUsers(responseData.users);
console.log(
"responseData:",
responseData,
"responseData.joiners:",
responseData.joiners,
);
if (responseData.joiners) {
const idParts = responseData.joiners[0].id.split(":");
const joinId =
idParts.length > 1 ? idParts[1] : responseData.joiners[0].id;
const systemMessage = `${joinId} 님이 입장했습니다.`;
setMessages([...messages, { id: "system", text: systemMessage }]);
setUsers(responseData.users);
}
});

// 유저 퇴장 메시지 수신
Expand Down
10 changes: 7 additions & 3 deletions src/components/Game/GameStart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface GameStartProps {
status: string;
users: string[];
host: string;
current: string;
setCurrent: React.Dispatch<React.SetStateAction<string>>;
}

interface UserWithSort {
Expand All @@ -37,6 +39,7 @@ const GameStart: React.FC<GameStartProps> = ({
status,
users,
host,
setCurrent,
}) => {
const user = useRecoilValue(userState);

Expand Down Expand Up @@ -107,7 +110,7 @@ const GameStart: React.FC<GameStartProps> = ({
};

// 게임 종료
const hadleEnd = () => {
const handleEnd = () => {
const gameInfo = JSON.stringify({
category: "",
keyword: "",
Expand All @@ -120,8 +123,9 @@ const GameStart: React.FC<GameStartProps> = ({
fireFetch.updateData("game", gameId as string, { votedFor: [] });
fireFetch.updateData("game", gameId as string, { status: "대기중" });

socketMain.emit("message-to-server", gameId + ":" + "~!a%2@##");
setCurrent("게임종료");

socketMain.emit("message-to-server", gameId + ":" + "~!a%2@##");
// setShowStartModal(false);
};

Expand All @@ -142,7 +146,7 @@ const GameStart: React.FC<GameStartProps> = ({
w="200px"
h="100%"
mr="20px"
onClick={hadleEnd}
onClick={handleEnd}
isDisabled={host !== user.id}
>
게임 종료
Expand Down
8 changes: 8 additions & 0 deletions src/pages/Game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ const Game = () => {
}
}, [gameData.data]);

useEffect(() => {
if (current === "게임종료") {
window.location.reload();
}
}, [current]);

// 게임 나가기 api 선언 (호출 X)
const leave = useFetch({
url: "https://fastcampus-chat.net/chat/leave",
Expand Down Expand Up @@ -188,6 +194,8 @@ const Game = () => {
status={status}
users={users}
host={gameData.data[0].host}
current={current}
setCurrent={setCurrent}
/>
</GridItem>
<GridItem>
Expand Down

0 comments on commit 1e1a60b

Please sign in to comment.