-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (92 loc) · 3.32 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
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const {addUser, removeUser, getUser, getUsersInRoom, everyoneSubmitted, getCzar, addPoints, nextCzar} = require('./utils/users');
const {getWhiteCards, addChosenCards, getCardData, nextCards} = require('./utils/cards');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const port = process.env.PORT || 3000;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
let nameAndRoom = {
name: String,
room: String
};
app.post('/#', (req, res) => {
res.sendFile(__dirname + '/public/game.html');
nameAndRoom = {name: req.body.name, room: req.body.room};
});
io.on('connection', (socket) => {
console.log('New socket connected');
socket.on('join', (callback) => {
const {error, user} = addUser({id: socket.id, username: nameAndRoom.name, room: nameAndRoom.room});
if(error){
return callback(error);
}
socket.join(user.room);
socket.emit('yourCards', getWhiteCards(10, user.room));
io.in(user.room).emit('roomData', {
room: user.room,
users: getUsersInRoom(user.room),
cardData: getCardData(user.room)
});
callback();
});
socket.on('disconnect', () => {
const user = removeUser(socket.id);
console.log(`user disconnected`);
if (user) {
io.to(user.room).emit('roomData', {
room: user.room,
users: getUsersInRoom(user.room),
cardData: getCardData(user.room)
});
}
});
socket.on('sendCards', (cards)=>{
const user = getUser(socket.id);
const {chosenBlackCard: {text, pick}} = getCardData(user.room);
if(cards.length === pick){
addChosenCards(cards, user.room);
socket.emit('cardsSentOK');
user.submitted = true;
io.in(user.room).emit('chosenCards', {
user: user,
cards: cards
});
}else{
socket.emit("error", `cards sent: ${cards.length}. needed cards: ${pick}`)
}
if(everyoneSubmitted(user.room)){
const cardCzar = getCzar(user.room);
io.to(cardCzar.id).emit("chooseBest");
}
});
socket.on("winner", (winnerID, bestText) => {
const room = getUser(socket.id).room;
const users = getUsersInRoom(room);
const winner = users.filter( user => user.username === winnerID)[0];
addPoints(winner);
users.forEach(user => {
if(user.submitted){
io.to(user.id).emit("yourCards", getWhiteCards(1, room));
user.submitted = false;
}
});
io.to(winner.room).emit('winnerInfo', {winnerID, bestText}, users);
});
socket.on('nextRound', () => {
//delete chosen cards and pick a new black card and send to everyones
const room = getUser(socket.id).room;
io.to(room).emit('roomData', {
room: room,
users: nextCzar(room),
cardData: nextCards(room)
});
})
})
server.listen(port, () => {
console.log(`Server is listening on port ${port}`);
})