문서 모음

NodeJS용 html 관련 파일 116

고수석 2019. 4. 18. 18:57

 /////////////////////////////////////////////////////////////////

                socket.on('room', function(input) {

                    if(input.command === 'list') {

                        $(input.command).html('');

                        

                        for(var i = 0; i < input.rooms.length; i++ ) {

                            $('roomList').append('<p>방 #' + i + '->' + input.rooms[i].id + ',' + input.rooms[i].name + ',' + input.rooms[i].owner); 

                        }

                    }

                });


                $game.submit(function(e) {

                    e.preventDefault();

                    var choice = $('input[name=choice]:checked').val();


                    if(!submitted)

                    {

                        submitted = true;

                        socket.emit('player choice', $username.val(), choice);

                        $info.html('Waiting for other player...');

                    }

                    else $info.html('You have already made a choice!');

                });


                socket.on('new message', function(data) {

                    var enableAutoScroll = enableAutoScroll = ($($chat)[0].scrollHeight - $chat.scrollTop()) === $chat.outerHeight();

                    $chat.append('<div class="well"><strong>' + data.user + '</strong>: ' + data.msg + '</div>');


                    if(enableAutoScroll)

                    {

                        $chat.stop().animate({ scrollTop: $chat[0].scrollHeight}, 500);

                    }

                });


                $userFormArea.submit(function(e) {

                    e.preventDefault();


                    socket.emit('add user', $username.val(), function(data) {

                        if(data) 

                        {

                            $userFormArea.hide();

                            $gameArea.show();

                            $game.hide();

                        }

                        else

                        {

                            alert($username.val() + " username is already in use.");

                        }

                    });


                    $username.val();

                });


                socket.on('get user', function(data) {

                    var html = '';


                    for (i = 0; i < data.length; i++) 

                    {

                        html += '<li class="list-group-item">' + data[i] + '</li>';

                    }


                    $users.html(html);

                });


                socket.on('disconnected', function (username) {

                    $info.append('<br />' + username + ' left the room.');

                });


                socket.on('connected', function (username) {

                    $info.append('<br />' + username + ' joined the room.');

                });


                socket.on('game start', function() {

                    $game.show();

                    $info.append('<br />Make your choice.');

                });


                socket.on('tie', function (choices) {

                    countdown(choices);


                    setTimeout(function() {

                        $info.append('<br />A tie!');

                    }, 5000);


                    submitted = false;

                });


                socket.on('player 1 win', function (choices) {

                    countdown(choices);


                    setTimeout(function () {

                        $info.append('<br />' + choices[0]['user'] + ' wins!');

                    }, 5000);


                    submitted = false;

                });

                

                socket.on('player 2 win', function (choices) {

                    countdown(choices);


                    setTimeout(function() {

                        $info.append('<br />' + choices[1]['user'] + ' wins!');

                    }, 5000);


                    submitted = false;

                });

                /////////////////////////////////////////////////////////////////

                


                function countdown(choices) {      

                    setTimeout(function() {

                        $info.html('3...'); 

                    }, 0);

                    setTimeout(function() {

                        $info.html('2...'); 

                    }, 1000);

                    setTimeout(function() {

                        $info.html('1...'); 

                    }, 2000);

                    setTimeout(function() {

                        $info.html(choices[0]['user'] + ' picked ' + choices[0]['choice'] + '.');

                    }, 3000);

                    setTimeout(function() {

                        $info.append('<br />' + choices[1]['user'] + ' picked ' + choices[1]['choice'] + '.');

                    }, 4000);

                }

            });

        </script>

    </body>

</html>