-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (44 loc) · 1.36 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
import express from 'express';
import http from 'http';
import createGame from './public/game.js'
import socketio from 'socket.io'
const app = express();
const server = http.createServer(app);
const sockets = socketio(server);
app.use(express.static('public'));
app.use(express.static('node_modules/font-awesome'));
const game = createGame();
game.subscribe((command) => {
console.log(`> Emmiting ${command.type}`);
sockets.emit(command.type, command);
});
sockets.on('connection', (socket) => {
const playerId = socket.id;
socket.emit('setup', game.state);
socket.on('disconnect', () => {
game.removePlayer({ playerId });
console.log(`Player disconnected ${playerId}`);
});
socket.on('card-click', (command) => {
console.log(`> Player estimate ${playerId}`);
game.estimate({
playerId,
cardId: command.cardId
});
});
socket.on('add-player', (command) => {
console.log(`> Player connected on Server with id: ${playerId}`);
game.addPlayer({
playerId,
playerName: command.playerName
});
});
socket.on('restart-game', () => {
console.log(`> restart-game`);
game.restart();
});
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`> Server listening on port: ${port}`);
});