-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
128 lines (114 loc) · 4 KB
/
server.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
/* -----CONFIG----- */
const PORT = process.env.PORT || 3000;
const MAX_PLAYERS = 5;
/* ---------------- */
/* ---GAME STATE--- */
const uuid = require('uuid');
const games = {};
const players = {};
/* ---------------- */
/* -----SERVER----- */
const { Server } = require('socket.io');
const express = require('express');
const path = require('path');
const app = express();
const server = require('http').createServer(app);
const io = new Server(server);
/* ---------------- */
// Server all the files in `out/`
app.use(express.static(path.resolve(__dirname, './client/out')))
// Listen for incoming socket connections
io.on('connection', (socket) => {
console.log(`User#${socket.id} connected!`);
// Subscribe to client's events
socket.on('disconnect', () => {
console.log(`User#${socket.id} disconnected.`);
});
socket.on('create-game', () => {
// Create game ID to send back to player
// Note: This takes the first segment of a UUID
const id = uuid.v4().split('-')[0];
// Register game
games[id] = {
// Clients in game
clients: [socket],
// [Rounds] where Rounds: { (client.id): [track data] }
tracks: [],
// Length of tracks -1
round: -1,
}
// Register host
players[socket.id] = id;
// Send response back to client
socket.emit('create-game-res', id);
});
socket.on('join-game', (id) => {
if (!games[id]) {
return socket.emit('error', 'Game does not exist!');
}
if (games[id].clients.length > MAX_PLAYERS) {
return socket.emit('error', 'Max players reached!');
}
// Register client with game
games[id].clients.push(socket);
players[socket.id] = id;
// TODO: notify client
});
socket.on('start-game', () => {
let game = findGame(socket);
if (game) {
if (game.round >= 0) {
return socket.emit('error', "The game has already started!");
}
// Create round
game.tracks.push({});
game.round = 0;
// Notify everyone
game.clients.forEach((c) => {
c.emit('start-round', [/* Other's track data, starts empty */])
})
}
});
socket.on('submit-track', (track) => {
let game = findGame(socket);
if (game) {
// Put under client id under current round
game.tracks[game.round][socket.id] = track;
console.log(game.tracks);
// Everyone submitted?
if (Object.entries(game.tracks[game.round]).length == game.clients.length) {
// Done with round
game.clients.forEach((c) => {
// Build up previous round's tracks
const prev = [];
// TODO make this deterministic
game.tracks.forEach((round) => {
// Add a random track that isn't from author
prev.push(Object
.entries(round)
.filter(([author, _]) => author != socket.id)
.map(([_, track]) => track)
[Math.floor(Math.random() * game.clients.length)]);
});
console.log(prev);
// Same as (start-game), but with track data.
c.emit('start-round', prev);
});
game.round += 1;
}
}
});
// Find game for the given client
const findGame = (socket) => {
let out = undefined;
if (!players[socket.id]) {
socket.emit('error', 'You are not in a game!');
}
else if (!(out = games[players[socket.id]])) {
socket.emit('error', 'Game does not exist!');
}
return out;
}
})
// Start server
server.listen(PORT, () => console.log(`Listening on ${PORT}`) );