repl.it
@mwilki7/

Telestrations Server2

Nodejs

old one broke for some reason

fork
loading
Files
  • index.js
  • index.html
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var express  = require('express')
    , app    = express()
    , http   = require('http')
    , server = http.createServer(app)
    , io     = require('socket.io').listen(server);

server.listen(6969);

// routing
app.get('/', function (req, res)
{
    res.sendfile(/*__dirname +*/ 'index.html');
});

// usernames which are currently connected to the chat
var usernames = {};
var usernames2= {};
var msgType = { CHAT: 0, IMG: 1 };
var roomleader = null;
var gamePhase = {
    JOIN  : 0, // waiting for players to join
    WRITE : 1, // write phase
    DRAW  : 2, // draw phase
    FINISH: 3  // game is finished
};
var currentPhase = gamePhase.JOIN;
// rooms which are currently available in chat
var rooms = [
    'room1', // waiting lobby for people to join
    'room2', // 
    'room3'  // will think of a use for this later
];

io.sockets.on('connection', function (socket)
{
    // when the client emits 'adduser', this listens and executes
    socket.on('adduser', function (username)
    {
        socket.username = username;     // store the username in the socket session for this client 
        if (currentPhase == gamePhase.JOIN)
        {                   
            socket.room = 'room1';          // store the room name in the socket session for this client        
            usernames[username] = username; // add the client's username to the global list    

            socket.join('room1');           // send client to room 1        
            socket.emit('updatechat', 'SERVER', 'you have connected to room1'); // echo to client they've connected        
            socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room'); // echo to room 1 that a person has connected to their room
            socket.emit('updaterooms', rooms, 'room1');
        }
        else
        {       
            socket.room = 'room2';          // store the room name in the socket session for this client        
            usernames2[username] = username; // add the client's username to the global list    

            socket.join('room2');           // send client to room 1        
            socket.emit('updatechat', 'SERVER', 'you have connected to room2'); // echo to client they've connected
            socket.emit('updatechat', 'SERVER', 'a game is going on right now');  
            socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room'); // echo to room 1 that a person has connected to their room
            socket.emit('updaterooms', rooms, 'room2');
        }
        if (roomleader == null)
        {
            roomleader = socket.username;
            socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', name + ' is the room leader');
        }
    });
    
    // when the client emits 'sendchat', this listens and executes
    socket.on('sendchat', function (data)
    {
        // we tell the client to execute 'updatechat' with 2 parameters
        console.log(data);
        if (data.type == msgType.CHAT)
        {
            io.sockets.in(socket.room).emit('updatechat', socket.username, data.data);
        }
        else if (data.type == msgType.IMG)
        {
            io.sockets.in(socket.room).emit('updatecanvas', data.data);
        }
    });

    //
    // Event:
    //     When a player switches rooms, a 'player left' message is sent to the old room
    //     and a 'player joined' message will be sent to the new room
    //     Room lists are updated with proper usernames
    socket.on('switchRoom', function (newroom)
    {
        socket.leave(socket.room);
        socket.join(newroom);

        socket.emit('updatechat', 'SERVER', 'you have connected to ' + newroom);

        // sent message to OLD room
        socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username + ' has left this room');

        // update socket session room title
        socket.room = newroom;
        socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username + ' has joined this room');
        socket.emit('updaterooms', rooms, newroom);
    });


    // when the user disconnects.. perform this
    socket.on('disconnect', function () 
    {
        var deadroomleader = false;
        if (socket.username == roomleader)
        {
            deadroomleader = true;
        }
        if (socket.room == 'room1')
        {
            delete usernames[socket.username];
            io.sockets.emit('updateusers', usernames);
            if (deadroomleader)
            {
                for (var clientsocket in io.sockets)
                {
                    socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', name + ' is the room leader');
                    roomleader = name;
                    break;
                }
            }
        }
        else if (socket.room == 'room2')
        {
            delete usernames2[socket.username];
            io.sockets.emit('updateusers', usernames2);
            if (deadroomleader)
            {
                for (var name in usernames2)
                {
                    socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', name + ' is the room leader');
                    roomleader = name;
                    break;
                }
            }
        }
        // echo globally that this client has left
        socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
        socket.leave(socket.room);
    });
});
node v10.16.0