1
1
import './game.scss' ;
2
- import renderWaitingRoom from '@scenes/waitingRoom' ;
2
+ import { renderWaitingRoom , setupWaitingRoomSocket } from '@scenes/waitingRoom' ;
3
3
import socket from '@utils/socket' ;
4
+ import requestHandler from '@utils/requestHandler' ;
5
+
6
+ const scrollToBottom = ( component ) => {
7
+ const scrollOption = {
8
+ top : component . scrollHeight ,
9
+ } ;
10
+ component . scrollTo ( scrollOption ) ;
11
+ } ;
4
12
5
13
const initializeLayout = ( ) => {
6
14
const chatMessageLog = document . getElementById ( 'chat-message-log' ) ;
@@ -9,50 +17,74 @@ const initializeLayout = () => {
9
17
chatForm . addEventListener ( 'submit' , ( e ) => {
10
18
e . preventDefault ( ) ;
11
19
const message = e . target . message . value ;
20
+ if ( ! message . length ) return ;
12
21
e . target . message . value = '' ;
13
22
14
23
// TODO: initialize에서 분리하기
15
24
const sendMessageToServer = ( ) => {
16
25
const messageWrapper = document . createElement ( 'div' ) ;
17
26
const messageBox = document . createElement ( 'div' ) ;
27
+
18
28
messageWrapper . classList . add ( 'chat-mine' ) ;
19
- messageBox . classList . add ( 'chat-message' ) ;
20
29
messageWrapper . appendChild ( messageBox ) ;
21
- chatMessageLog . appendChild ( messageWrapper ) ;
30
+
31
+ messageBox . classList . add ( 'chat-message' ) ;
22
32
messageBox . innerText = message ;
23
- } ;
24
33
25
- // sendMessageToServer();
34
+ chatMessageLog . appendChild ( messageWrapper ) ;
35
+ } ;
26
36
27
- socket . emit ( 'send chat' , { message } , sendMessageToServer ) ;
37
+ socket . emit ( 'send chat' , { message } ) ;
38
+ sendMessageToServer ( ) ;
39
+ scrollToBottom ( chatMessageLog ) ;
28
40
} ) ;
29
41
30
42
// TODO: initialize에서 분리하기
31
43
const getMessageFromServer = ( { nickname, message } ) => {
32
44
const messageWrapper = document . createElement ( 'div' ) ;
33
45
const nicknameBox = document . createElement ( 'div' ) ;
34
46
const messageBox = document . createElement ( 'div' ) ;
47
+
35
48
messageWrapper . classList . add ( 'chat-other-player' ) ;
36
- nicknameBox . classList . add ( 'chat-nickname' ) ;
37
- messageBox . classList . add ( 'chat-message' ) ;
38
49
messageWrapper . appendChild ( nicknameBox ) ;
39
50
messageWrapper . appendChild ( messageBox ) ;
40
- chatMessageLog . appendChild ( messageWrapper ) ;
51
+
52
+ nicknameBox . classList . add ( 'chat-nickname' ) ;
41
53
nicknameBox . innerText = nickname ;
54
+
55
+ messageBox . classList . add ( 'chat-message' ) ;
42
56
messageBox . innerText = message ;
57
+
58
+ chatMessageLog . appendChild ( messageWrapper ) ;
59
+ scrollToBottom ( chatMessageLog ) ;
43
60
} ;
44
61
45
62
socket . on ( 'send chat' , getMessageFromServer ) ;
46
63
} ;
47
64
48
- const initialize = ( ) => {
65
+ const initialize = async ( ) => {
49
66
const urlParams = new URLSearchParams ( window . location . search ) ;
50
67
const roomID = urlParams . get ( 'room' ) ;
68
+ const config = { method : 'GET' , uri : `/rooms/${ roomID } ` } ;
69
+ const { success } = await requestHandler ( config ) ;
70
+ if ( ! success ) {
71
+ window . alert ( '올바르지 않은 코드입니다.' ) ;
72
+ window . location . href = '/' ;
73
+ return ;
74
+ }
51
75
socket . emit ( 'join player' , { roomID } ) ;
52
76
53
77
initializeLayout ( ) ;
54
78
55
- renderWaitingRoom ( roomID ) ;
79
+ const { NicknameInput } = renderWaitingRoom ( roomID ) ;
80
+ // const { PlayerList } = renderLeftTab();
81
+ setupWaitingRoomSocket ( ) ;
82
+
83
+ socket . on ( 'enter room' , ( { nickname, color, players } ) => {
84
+ NicknameInput . setValue ( nickname ) ;
85
+ // NicknameInput.instance.style.backgroundColor = color;
86
+ // PlayerList.setListItems(players);
87
+ } ) ;
56
88
} ;
57
89
58
90
initialize ( ) ;
0 commit comments