-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverDraw.js
140 lines (133 loc) · 4.63 KB
/
serverDraw.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
//Author: Ethan Pavolik [email protected]
module.exports = function(io, hashmap) {
var currentGames = ['Default'];
var sockets = new hashmap(); //Nickname : Socket
var IdMap = new hashmap();
var servers = new hashmap(); //Server Name : Array of sockets
servers.set('Default', []);
var numPlayers = 0;
var currentImg = 0;
var players = [];
var clientObj = function(socket, name) {
this.socket = socket;
this.nickname = name;
};
io.on('connection', function(socket) {
socket.on('reset', function(data) {
currentGames = ['Default'];
sockets = new hashmap(); //Nickname : Socket
IdMap = new hashmap();
servers = new hashmap(); //Server Name : Array of sockets
servers.set('Default', []);
numPlayers = 0;
currentImg = 0;
players = [];
});
var client;
var nickname;
var socketServer;
//send player current games TODO
socket.emit('currentGames', {currentGames});
socket.on('createServer', function(data) {
if(servers.keys().indexOf(data.serverName) != -1) {
socket.emit('invalidServerName');
}
else {
servers.set(data.serverName, []);
socket.emit('validServerName');
}
currentGames = servers.keys();
io.emit('currentGames', {currentGames});
});
//on player join save their name and insert into hashmap with their socket
//then send them the other players, the current canvas, and let everyone know there is a new player
socket.on('playerJoin', function(data) {
client = new clientObj(socket, data.nickname);
socketServer = data.serverName;
nickname = data.nickname;
var canvasReq;
sockets.set(nickname, socket);
servers.get(socketServer).forEach(function(client) {
players.push(client.nickname);
});
var canv, otherPlay = servers.get(socketServer);
canv = servers.get(socketServer);
otherPlay.push(client);
servers.set(socketServer, otherPlay);
socket.emit('id', servers.get(socketServer).length);
socket.emit('otherPlayers', {players});
servers.get(socketServer).forEach(function(client) {
client.socket.emit('newPlayer', {numPlayers, nickname});
});
if(players.length > 0) {
servers.get(socketServer).forEach(function(client, i) {
if(client.socket != socket) {
client.socket.emit('getCanvas', {nickname});
return;
}
});
}
numPlayers++;
});
//send the canvas to the new player
socket.on('Canvas', function(data) {
currentImg = data.dataUrl;
sockets.get(data.data.nickname).emit('currentCanvas', currentImg);
});
//someone wants to clear the screen
socket.on('clearScreen', function() {
servers.get(socketServer).forEach(function(client) {
client.socket.emit('clrScrn');
});
});
//someone moved their mouse, let everyone else know who and where
socket.on('mousemove', function(data) {
var name = data.clientName;
var leftPos = data.leftPos;
var topPos = data.topPos;
servers.get(socketServer).forEach(function(client) {
client.socket.emit('updateMouse', {name, leftPos, topPos});
});
});
//someone is drawing, let everyone else know
socket.on('isDrawing', function(data) {
servers.get(socketServer).forEach(function(client) {
client.socket.emit('updateScreen', {data});
});
});
//someone disconnected, try to remove them from hashmap and let everyone know who d/c'd
socket.on('disconnect', function(s) {
if(socketServer != null) {
sockets.remove(nickname);
var tArray = [];
var removeI;
if(servers.get(socketServer) != null) {
tArray = servers.get(socketServer);
tArray.forEach(function(ele, i) {
if(ele.nickname == nickname) {
removeI = i;
}
});
if(removeI != null) {
tArray.splice(removeI, 1);
servers.set(socketServer, tArray);
}
servers.get(socketServer).forEach(function(client) {
client.socket.emit('dicon', {nickname});
});
if(servers.get(socketServer).length === 0 && socketServer != 'Default') {
servers.remove(socketServer);
currentGames = servers.keys();
io.emit('currentGames', {currentGames});
}
}
if(servers.get(socketServer) != null) { //check again after potential delete
servers.get(socketServer).forEach(function(client) {
players.push(client.nickname);
});
socket.emit('otherPlayers', {players});
}
}
});
});
}