PageRenderTime 1ms CodeModel.GetById 2ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/game_server.js

https://bitbucket.org/nehar/ti
JavaScript | 168 lines | 134 code | 25 blank | 9 comment | 15 complexity | b765fae653d5b927b3267e44d5dde9ce MD5 | raw file
  1/*jshint node: true */
  2var socketio = require('socket.io'),
  3    usernames = [];
  4
  5
  6exports.listen = function (server, game) {
  7  "use strict";
  8var map = [],
  9     io = socketio.listen(server);
 10
 11
 12    io.sockets.on('connection', function (socket) {
 13	
 14            handleClientAddUser(socket);
 15            handleClientSendChat(socket);
 16            handleClientNewGame(socket);
 17            handleClientSwitchRoom(socket);
 18            handleClientDisconnection(socket);
 19			game.socket(map, socket, io);
 20			socket.on('keep-alive', function () { socket.emit('keep-alive', null); });
 21        });
 22		
 23		
 24    //do??czenie nowego klienta do Lobby, przypisanie nazwy u?ytkownika
 25
 26    function handleClientAddUser(socket) {
 27        socket.on('addUser', function (username) {
 28                if (usernames.indexOf(username) > -1) {
 29                    usernames.push(username = assignName(username, 1));
 30                }
 31                else {
 32                    usernames.push(username);
 33                }
 34                socket.username = username;
 35                socket.set('username', username);
 36                socket.join('Lobby');
 37                socket.room = 'Lobby';
 38                socket.broadcast.to(socket.room).emit('updateChat', 'SERVER', username + ' has connected');
 39                socket.emit('updateRooms', getRooms(), socket.room);
 40                io.sockets.to(socket.room).emit('updateUsers', getUsers(socket.room));
 41            });
 42    }
 43
 44	
 45    //wysy?anie wiadomo?ci
 46
 47    function handleClientSendChat(socket) {
 48        socket.on('sendChat', function (data) {
 49                io.sockets. in (socket.room).emit('updateChat', socket.username, data);
 50            });
 51    }
 52	
 53    //tworzenie nowej gry
 54	
 55    function handleClientNewGame(socket) {
 56        socket.on('newGame', function () {
 57		if(socket.room === 'Lobby'){
 58                socket.leave(socket.room);
 59                io.sockets.to(socket.room).emit('updateUsers', getUsers(socket.room));
 60                socket.room = socket.username;
 61                socket.join(socket.room);
 62                io.sockets.to(socket.room).emit('updateUsers', getUsers(socket.room));
 63                socket.broadcast.to(socket.room).emit('updateChat', 'SERVER', socket.username + ' stworzy? now? gr?');
 64                socket.room = socket.username;
 65                socket.broadcast.to('Lobby').emit('updateRooms', getRooms(), 'Lobby');
 66                game.game(map, socket, io);
 67                socket.emit('updateChat', 'SERVER', 'Stworzono now? gr?');
 68                socket.emit('updateRooms', getRooms(), socket.username);
 69				}
 70            });
 71    }
 72    //do??cznie do gry lub powr�t do Lobby
 73
 74    function handleClientSwitchRoom(socket) {
 75        socket.on('switchRoom', function (newroom) {
 76			if(newroom === 'Lobby'){
 77			socket.stopinterval = 1;
 78				socket.broadcast.to(socket.room).emit('backToLobby');
 79                socket.emit('exitRoom');
 80                socket.leave(socket.room);
 81                io.sockets. in (socket.room).emit('updateUsers', getUsers(socket.room));
 82                socket.join(newroom);
 83                io.sockets. in (socket.room).emit('updateRooms', getRooms(), socket.room); 
 84                socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username + ' has left this room');
 85                socket.room = newroom;
 86				socket.emit('updateChat', 'SERVER', 'you have connected to ' + newroom);
 87                io.sockets.to(socket.room).emit('updateUsers', getUsers(socket.room));
 88                socket.broadcast.to(newroom).emit('updateChat', 'SERVER', socket.username + ' has joined this room');
 89                io.sockets. in (newroom).emit('updateRooms', getRooms(), newroom);
 90
 91				
 92			}
 93			else{
 94					if (io.sockets.manager.rooms['/' + newroom].length < 2){
 95				
 96                socket.leave(socket.room);
 97                io.sockets. in (socket.room).emit('updateUsers', getUsers(socket.room));
 98                socket.join(newroom);
 99                io.sockets. in (socket.room).emit('updateRooms', getRooms(), socket.room); 
100                socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username + ' has left this room');
101                socket.room = newroom;
102			
103                    game.game(map, socket, io);
104 
105				socket.emit('updateChat', 'SERVER', 'you have connected to ' + newroom);
106                io.sockets.to(socket.room).emit('updateUsers', getUsers(socket.room));
107                socket.broadcast.to(newroom).emit('updateChat', 'SERVER', socket.username + ' has joined this room');
108                io.sockets. in (newroom).emit('updateRooms', getRooms(), newroom);
109				}
110			}
111            });
112    }
113    //od??czenie klienta
114
115    function handleClientDisconnection(socket) {
116        socket.on('disconnect', function () {
117                if (socket.username !== undefined) {
118                    var index = usernames.indexOf(socket.username);
119                  if (index > -1) {usernames.splice(index, 1);}
120                    console.log("Disconnected", socket.username);
121					if(socket.room !== 'Lobby'){
122					socket.broadcast.to(socket.room).emit('backToLobby');
123					
124					}
125                    else{
126					socket.broadcast.to(socket.room).emit('updateChat', 'SERVER', socket.username + ' has disconnected');
127					}
128                    socket.leave(socket.room);
129                    io.sockets.to(socket.room).emit('updateUsers', getUsers(socket.room));
130                }
131            });
132    }
133    //funkcja numeruj?ca username
134
135    function assignName(username, number) {
136        var username_tmp = "";
137        username_tmp = username + '_' + number;
138        if (usernames.indexOf(username_tmp) > -1) {
139            return assignName(username, ++number);
140        }
141        else {
142            return username_tmp;
143        }
144    }
145    //pobieranie nazw pokoi
146
147    function getRooms() {
148        var rooms = [];
149        for (var i in io.sockets.manager.rooms) {
150            if (i !== '' && i !== '/Lobby') {
151                if (io.sockets.manager.rooms[i].length < 2) {
152                    rooms.push(i.substring(1));
153                }
154            }
155        }
156        return rooms;
157    }
158    //pobieranie nazw u?ytkownik�w w danym pokoju
159
160    function getUsers(room) {
161        var users = [];
162        for (var i in io.sockets.manager.rooms['/' + room]) {
163            users.push(io.sockets.sockets[io.sockets.manager.rooms['/' + room][i]].username);
164        }
165        return users;
166    }
167
168};