-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (107 loc) · 3.73 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
const { Console } = require('console');
const express = require('express');
const socketio = require('socket.io');
const {
checkDuplicateUser,
checkHost,
createUser,
createHost,
joinHost,
generateSocketIdToUser,
getUserThruSocket,
deleteUser,
deleteHost,
cleanChallenger,
findChallenger,
updateUserAsHost,
updateHost
} = require('./utils/users');
const bodyParser = require('body-parser')
const PORT = 3000 || process.env.port;
// http module to create server
const http = require('http');
const app = express();
// server that'll handle socket io
const server = http.createServer(app);
const io = socketio(server);
// port
// app.use((req, res, next) => {
// let { method, path, ip } = req;
// console.log( method + " " + path + " - " + ip );
// next();
// });
app.get('/', (req, res) => {
res.sendFile( __dirname + "/public/index.html");
});
app.get('/game', async function(req, res) {
let { username, host } = req.query;
const dupTest = await checkDuplicateUser(username);
const hostTest = await checkHost(host);
if(dupTest) {
res.sendFile( __dirname + `/utils/re-user.html`)
} else if(!dupTest && !host) {
createUser(username, username);
createHost(username);
res.sendFile( __dirname + `/public/game.html` );
} else if(!dupTest && hostTest) {
createUser(username, host);
await joinHost(username, host);
res.sendFile( __dirname + `/public/game.html` );
} else if(!dupTest && !hostTest) {
res.sendFile( __dirname + `/utils/re-host.html`)
}
});
io.on('connection', socket => {
socket.on('joinRoom', ({username, host}) => {
generateSocketIdToUser(username, socket.id)
console.log(`${username} is joining ${host}...`)
socket.join(host);
socket.broadcast.to(host).emit('userjoin', username);
socket.on('serverInstruct', (num, etc) => {
if(num == 1) {
io.to(host).emit('gameInstruct', num);
}
})
socket.on('serverUpdate', ({appleEaten, stillAlive}) => {
if(stillAlive) {
socket.broadcast.to(host).emit('appleOpponent', appleEaten)
} else {
socket.broadcast.to(host).emit('deadOpponent', true)
}
})
socket.on('gameHash', divs => {
socket.broadcast.to(host).emit('gameOpponentHash', divs);
});
})
socket.on('refreshRoom', ({username, host}) => {
console.log(username, ' will leave ',host)
socket.leave(host);
})
socket.on('disconnect', async function() {
const user = await getUserThruSocket(socket.id);
if(user !== null) {
console.log(user.username, ' disconnected');
socket.broadcast.to(user.host).emit('userdisconnect', user.username);
// update host/challenger
if(user.username === user.host) {
const toBeHost = await findChallenger(user.host)
if(toBeHost) {
updateUserAsHost(toBeHost);
updateHost(user.host, toBeHost);
deleteUser(user.username);
} else {
deleteHost(user.username);
deleteUser(user.username);
}
} else {
cleanChallenger(user.host);
deleteUser(user.username);
}
}
})
});
// makes public static for static files
app.use(express.static( __dirname + '/public'));
app.use(bodyParser.urlencoded({extended: false}));
// server instead of app, to handle socket io
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));