-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
203 lines (185 loc) · 5.53 KB
/
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const express = require('express');
const socket = require('socket.io');
const path = require('path');
const port = process.env.PORT || 3000;
const app = express();
const server = app.listen(port, () => console.log(`listening on port ${port}`));
const io = socket(server);
let rooms = {};
app.use(express.static(path.join(__dirname, 'build')));
function endGame(roomName) {
delete rooms[roomName];
io.of('/')
.in(roomName)
.clients((error, socketIds) => {
if (error) throw error;
socketIds.forEach(
(socketId) =>
io.sockets.sockets[socketId] &&
io.sockets.sockets[socketId].leave(roomName)
);
});
}
function shiftNames(players) {
Object.keys(players).forEach((id, i, keys) => {
if (i === Object.keys(players).length - 1) {
players[id].nameToGuess = players[keys[0]].sentName;
players[id].receivedNameFrom = keys[0];
} else {
players[id].receivedNameFrom = keys[i + 1];
players[id].nameToGuess = players[keys[i + 1]].sentName;
}
});
return players;
}
io.on('connection', function (socket) {
// turnMode === false ? consecutive (normal) : concurrent (back and forth);
socket.on('make room', ({ name, totalPlayers, useCategories, turnMode }) => {
totalPlayers = Number(totalPlayers);
if (rooms[name]) {
return socket.emit('message', { type: 'name taken' });
}
socket.emit('message', { type: 'room name ok', name, totalPlayers });
rooms[name] = {
totalPlayers,
name,
playersJoined: 1,
players: { [socket.id]: { nameToGuess: null, sentName: null } },
host: socket.id
};
socket.join(name);
});
socket.on('disconnecting', () => {
const roomName = Object.keys(socket.rooms).filter(
(name) => name != socket.id
)[0];
if (rooms[roomName] && rooms[roomName].host == socket.id) {
io.in(roomName).emit('message', { type: 'host disconnection' });
endGame(roomName);
} else if (rooms[roomName]) {
io.in(roomName).emit('message', { type: 'disconnection', id: socket.id });
rooms[roomName].playersJoined -= 1;
}
});
socket.on('restart game', ({ roomName }) => {
rooms[roomName].turn = 0;
Object.keys(rooms[roomName].players).forEach((key) => {
rooms[roomName].players[key] = {};
});
io.in(roomName).emit('message', { type: 'restart' });
});
socket.on('end game', ({ roomName }) => {
endGame(roomName);
socket.to(roomName).emit('message', { type: 'host disconnection' });
});
socket.on('media on', async ({roomName, fromId}) => {
let roomToJoin = rooms[roomName];
if (roomToJoin && roomToJoin.playersJoined < roomToJoin.totalPlayers) {
const client = require('twilio')(process.env.acct_sid, process.env.auth_token);
const token = await client.tokens.create();
// const token = {iceServers: null};
socket.to(roomName).emit('message', {
type: 'joinRequest',
roomName,
fromId,
iceServers: token.iceServers
});
}
});
socket.on('join room', async ({ roomName, fromId }) => {
let roomToJoin = rooms[roomName];
if (roomToJoin && roomToJoin.playersJoined < roomToJoin.totalPlayers) {
socket.emit('message', {
type: 'joining',
totalPlayers: roomToJoin.totalPlayers,
playersJoined: roomToJoin.playersJoined + 1,
name: roomName,
host: roomToJoin.host
});
socket.join(roomName);
} else {
socket.emit('message', { type: 'room DNE' });
}
});
socket.on('description', async ({ description, toId, fromId }) => {
const client = require('twilio')(process.env.acct_sid, process.env.auth_token);
const token = await client.tokens.create();
// const token = {iceServers: null};
io.sockets.sockets[toId].emit('message', {
type: 'offer',
description,
toId,
fromId,
iceServers: token.iceServers
});
});
socket.on('answer', ({ answer, fromId, toId }) => {
io.sockets.sockets[toId].emit('message', {
type: 'answer',
answer,
fromId
});
});
socket.on('iceCandidate', ({ candidate, fromId, toId }) => {
io.sockets.sockets[toId].emit('message', {
type: 'iceCandidate',
candidate,
fromId
});
});
socket.on('setName', ({ nameToGuess, roomName }) => {
const room = rooms[roomName];
room.players[socket.id].sentName = nameToGuess;
if (Object.values(room.players).every((val) => val.sentName)) {
const shiftedNames = shiftNames(room.players);
io.in(roomName).emit('message', {
type: 'give names',
names: shiftedNames,
turn: room.turnOrder[room.turn]
});
} else {
io.in(roomName).emit('message', {
type: 'update set names',
totalNamesSet: Object.values(room.players).filter(
(player) => player.sentName
).length
});
}
});
socket.on('ready', ({ roomName, joinerId }) => {
const room = rooms[roomName];
//There will be n-1 answers from nth joiner, so add 1/n-1.
room.playersJoined += 1 / Math.floor(room.playersJoined);
room.players[joinerId] = { nameToGuess: null, sentName: null };
if (Math.round(room.playersJoined) == room.totalPlayers) {
room.turn = 0;
room.turnOrder = Object.keys(room.players);
io.in(roomName).emit('message', {
type: 'gameReady'
});
}
});
socket.on('next turn', ({ roomName }) => {
const room = rooms[roomName];
//Last turn over check
if (room.turn === room.turnOrder.length - 1) {
room.turn = 0;
return io
.in(roomName)
.emit('message', {
type: 'game end',
revealed: room.turnOrder,
turn: room.turnOrder[0]
});
}
room.turn += 1;
io.in(roomName).emit('message', {
type: 'new turn',
turn: room.turnOrder[room.turn],
revealed: room.turnOrder.slice(0, room.turn)
});
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/build/index.html'));
});