-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
131 lines (103 loc) · 3.79 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
129
130
131
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const { Game } = require('./utils/game');
game = new Game();
const {
formatMessage,
botName,
userJoin,
getRoomUsers,
getCurrentUser,
userLeave
} = require('./utils/chat');
io.on('connection', socket => {
socket.on('joinRoom', ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
// Welcome current user
socket.emit('message', formatMessage(botName, 'Welcome to chat!'));
// Broadcast when a user connects
socket.broadcast
.to(user.room)
.emit(
'message',
formatMessage(botName, `${user.username} has joined the chat and the game`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
// Add the new user to the game
if (game.addPlayer(user.username)) {
console.log("[ADD] GAME PLAYERS:", game.getPlayers());
} else {
console.log("[ADD] GAME PLAYERS FAILED:", game.getPlayers())
}
// When we have two players, start and tell players whose turn it is
if (game.correctPlayerCount()) {
console.log("CORRECT PLAYER COUNT!");
// Reset game
game.reset();
// Send turn
io.to(user.room).emit('turn', game.getPlayerTurn());
}
});
// Listen for chatMessage
socket.on('chatMessage', msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
// Allow players to reset the game if the game is done
socket.on('reset', () => {
const user = getCurrentUser(socket.id);
// Reset game
game.reset();
// Send turn
io.to(user.room).emit('turn', game.getPlayerTurn());
});
// Listen for game related event
socket.on('choice', choice => {
const user = getCurrentUser(socket.id);
// Take a step from a player
let res = game.step(user.username, choice);
console.log(user.username, choice, res);
// If the game is done, send the result
if (game.getDone()) {
// Send result if done
io.to(user.room).emit('result', game.getResult());
} else {
// Send turn if not done
io.to(user.room).emit('turn', game.getPlayerTurn());
}
});
// Runs when client disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit(
'message',
formatMessage(botName, `${user.username} has left the chat`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
// Removes the user from the game
if (game.deletePlayer(user.username)) {
console.log("[DEL] GAME PLAYERS:", game.getPlayers());
} else {
console.log("[DEL] GAME PLAYERS FAILED:", game.getPlayers());
}
}
});
});
app.use(express.static(path.join(__dirname, 'public')));
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));