1
- import React , { useEffect , useState } from "react" ;
1
+ import React , { useEffect , useRef , useState } from "react" ;
2
2
import Modal from "./Modal" ;
3
3
import computer from "../img/computer.png" ;
4
+ import error from "../img/error.png" ;
4
5
import io from "socket.io-client" ;
6
+ import pond from "../img/pond.png" ;
5
7
6
8
const socket = io ( "http://172.10.7.127" ) ;
7
9
8
10
const Main = ( ) => {
9
11
const [ bombHolder , setBombHolder ] = useState ( null ) ;
10
12
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 ] ) ;
11
19
12
20
useEffect ( ( ) => {
13
21
socket . on ( "connect" , ( ) => {
@@ -19,32 +27,107 @@ const Main = () => {
19
27
console . log ( "Bomb is with: " , bombHolderId ) ;
20
28
} ) ;
21
29
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
+
22
48
return ( ) => {
23
49
socket . off ( "connect" ) ;
24
50
socket . off ( "bomb" ) ;
51
+ socket . off ( "ready_count" ) ;
52
+ socket . off ( "limit_time" ) ;
25
53
} ;
26
54
} , [ ] ) ;
27
55
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
+
28
79
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
+ }
32
85
} ;
33
86
34
87
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" }
46
100
</ div >
47
101
</ 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 >
48
131
{ isModalOn ? (
49
132
< div
50
133
style = { {
@@ -60,7 +143,11 @@ const Main = () => {
60
143
alignItems : "center" ,
61
144
} }
62
145
>
63
- < Modal state = { { isModalOn, setIsModalOn } } />
146
+ < Modal
147
+ modalState = { { isModalOn, setIsModalOn } }
148
+ readyState = { { ready, setReady } }
149
+ socket = { socket }
150
+ />
64
151
</ div >
65
152
) : null }
66
153
</ div >
0 commit comments