Skip to content

Commit bebbe57

Browse files
authored
Merge pull request #16 from minzero31/jimini
Main develop#8 #14
2 parents a5ddb55 + 782c405 commit bebbe57

File tree

4 files changed

+134
-21
lines changed

4 files changed

+134
-21
lines changed

src/img/error.png

69.7 KB
Loading

src/img/pond.png

98.8 KB
Loading

src/page/Main.js

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import React, { useEffect, useState } from "react";
1+
import React, { useEffect, useRef, useState } from "react";
22
import Modal from "./Modal";
33
import computer from "../img/computer.png";
4+
import error from "../img/error.png";
45
import io from "socket.io-client";
6+
import pond from "../img/pond.png";
57

68
const socket = io("http://172.10.7.127");
79

810
const Main = () => {
911
const [bombHolder, setBombHolder] = useState(null);
1012
const [isModalOn, setIsModalOn] = useState(true);
13+
const [ready, setReady] = useState(0);
14+
const bombHolderRef = useRef(null);
15+
16+
useEffect(() => {
17+
bombHolderRef.current = bombHolder;
18+
}, [bombHolder]);
1119

1220
useEffect(() => {
1321
socket.on("connect", () => {
@@ -19,32 +27,107 @@ const Main = () => {
1927
console.log("Bomb is with: ", bombHolderId);
2028
});
2129

30+
socket.on("ready_count", (count) => {
31+
setReady(count);
32+
console.log("Ready count is: ", count);
33+
});
34+
35+
socket.on("limit_time", (limitTime) => {
36+
setTimeout(() => {
37+
if (bombHolderRef.current === socket.id) {
38+
alert("You IDIOT!");
39+
} else {
40+
alert("good");
41+
}
42+
}, limitTime * 1000);
43+
});
44+
45+
// Main 페이지에 진입할 때 서버로 이벤트 전송
46+
socket.emit("enter_game");
47+
2248
return () => {
2349
socket.off("connect");
2450
socket.off("bomb");
51+
socket.off("ready_count");
52+
socket.off("limit_time");
2553
};
2654
}, []);
2755

56+
const ErrorComputer = () => {
57+
return (
58+
<div style={{ position: "relative" }}>
59+
<img
60+
style={{ width: "280px" }}
61+
onClick={passBomb}
62+
src={computer}
63+
alt="img"
64+
/>
65+
<img
66+
style={{
67+
width: "130px",
68+
position: "absolute",
69+
bottom: 100,
70+
left: 68,
71+
}}
72+
src={error}
73+
alt="img"
74+
/>
75+
</div>
76+
);
77+
};
78+
2879
const passBomb = () => {
29-
setBombHolder("other");
30-
socket.emit("pass_bomb");
31-
console.log("Bomb is passed to the other player!");
80+
if (bombHolder === socket.id) {
81+
// 폭탄을 소유한 클라이언트만 폭탄을 전달할 수 있음
82+
socket.emit("pass_bomb");
83+
console.log("Bomb is passed to the other player!");
84+
}
3285
};
3386

3487
return (
35-
<div style={{ backgroundColor: "yellow", height: "100vh" }}>
36-
<div>Main</div>
37-
<div>
38-
{bombHolder === socket.id ? <div>내 차례</div> : <div>네 차례</div>}
39-
</div>
40-
<div>
41-
<div>
42-
<img style={{ width: "280px" }} onClick={passBomb} src={computer} />
43-
</div>
44-
<div>
45-
<img style={{ width: "280px" }} onClick={passBomb} src={computer} />
88+
<div
89+
style={{
90+
height: "100vh",
91+
position: "relative",
92+
backgroundColor: "#64A842",
93+
}}
94+
>
95+
<div style={{ display: "flex", justifyContent: "center" }}>
96+
<div
97+
style={{ fontSize: 20, fontWeight: "bolder", backgroundColor: "" }}
98+
>
99+
{bombHolder === socket.id ? "My Turn" : "Your Turn"}
46100
</div>
47101
</div>
102+
<div
103+
style={{
104+
display: "flex",
105+
justifyContent: "center",
106+
marginTop: "150px",
107+
}}
108+
>
109+
<img src={pond} alt="img" />
110+
</div>
111+
<div
112+
style={{
113+
display: "flex",
114+
justifyContent: "center",
115+
width: "100%",
116+
position: "absolute",
117+
bottom: 20,
118+
}}
119+
>
120+
{bombHolder === socket.id ? (
121+
<ErrorComputer />
122+
) : (
123+
<img
124+
style={{ width: "280px" }}
125+
onClick={passBomb}
126+
src={computer}
127+
alt="img"
128+
/>
129+
)}
130+
</div>
48131
{isModalOn ? (
49132
<div
50133
style={{
@@ -60,7 +143,11 @@ const Main = () => {
60143
alignItems: "center",
61144
}}
62145
>
63-
<Modal state={{ isModalOn, setIsModalOn }} />
146+
<Modal
147+
modalState={{ isModalOn, setIsModalOn }}
148+
readyState={{ ready, setReady }}
149+
socket={socket}
150+
/>
64151
</div>
65152
) : null}
66153
</div>

src/page/Modal.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React, { useEffect, useState } from "react";
22
import caution from "../img/caution.png";
33

4-
const Modal = ({ state }) => {
5-
const { isModalOn, setIsModalOn } = state;
4+
const Modal = ({ modalState, readyState, socket }) => {
5+
const { isModalOn, setIsModalOn } = modalState;
6+
const { ready, setReady } = readyState;
67
const [countDown, setCountDown] = useState(0);
8+
const [click, setClick] = useState(false);
79

810
useEffect(() => {
911
const countdown = setInterval(() => {
@@ -14,6 +16,13 @@ const Modal = ({ state }) => {
1416
return () => clearInterval(countdown);
1517
}, [countDown]);
1618

19+
useEffect(() => {
20+
if (ready === 2) {
21+
setCountDown(5);
22+
setTimeout(() => setIsModalOn(false), 5000);
23+
}
24+
}, [ready, setIsModalOn]);
25+
1726
return (
1827
<div
1928
style={{
@@ -92,6 +101,18 @@ const Modal = ({ state }) => {
92101
</div>
93102
</div>
94103
)}
104+
<div
105+
style={{
106+
display: "flex",
107+
justifyContent: "center",
108+
paddingBottom: "15px",
109+
fontSize: "15px",
110+
}}
111+
>
112+
<div>
113+
Ready <span>{ready}</span>/2
114+
</div>
115+
</div>
95116
<div
96117
style={{
97118
display: "flex",
@@ -106,16 +127,21 @@ const Modal = ({ state }) => {
106127
width: "200px",
107128
height: "35px",
108129
borderRadius: "20px",
130+
backgroundColor: click ? "#F0F0F0" : "white",
109131
fontSize: "15px",
110132
border: "none",
111133
fontWeight: "bold",
112134
}}
135+
disabled={click}
113136
onClick={() => {
114-
setCountDown(5);
115-
setTimeout(() => setIsModalOn(false), 5000);
137+
setClick(true);
138+
socket.emit("ready");
139+
setReady(ready + 1);
140+
// setCountDown(5);
141+
// setTimeout(() => setIsModalOn(false), 5000);
116142
}}
117143
>
118-
Confirm
144+
Ready to Start
119145
</button>
120146
</div>
121147
</div>

0 commit comments

Comments
 (0)