-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (79 loc) · 2.33 KB
/
index.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
const express = require('express');
const app = express();
const http = require('http');
const { send } = require('process');
const server = http.createServer(app);
const io = require('socket.io')(server);
const { uniqueNamesGenerator, adjectives, colors, animals } = require('unique-names-generator');
const PORT = process.env.PORT || 3000;
// STATE
let users = {};
// IO
app.get('/', (req, res) => {
res.sendFile('index.html', { root: __dirname });
});
app.use(express.static(__dirname + '/public'));
io.on('connection', socket => {
console.log('A user connected ' + socket.id);
users[socket.id] = {
name: uniqueNamesGenerator(nameConfig),
score: 0,
};
sendLeaderboard();
socket.emit('init', {
low: low,
high: high,
name: users[socket.id].name,
});
socket.on('guess', (data) => {
console.log(data);
if (data == num) {
console.log('correct');
io.emit('correct', { user: users[socket.id].name, num: num });
users[socket.id].score++;
sendLeaderboard();
refreshNum();
}
else {
io.emit('guess', {
user: users[socket.id].name,
guess: data, judgement: (data < num) ? 'too low' : 'too high'
});
}
});
socket.on('disconnect', () => {
console.log('A user disconnected');
delete users[socket.id];
// io.emit('count', Object.keys(users).length);
sendLeaderboard();
});
});
server.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});
// LOGIC
let num;
let low;
let high;
function refreshNum() {
let radius = randBetween(100, 500);
let mid = randBetween(radius, 1000-radius);
low = mid - radius;
high = mid + radius;
num = randBetween(low, high);
console.log(`Range: [${low}, ${high}], Number: ${num}`);
io.emit('range', { low: low, high: high });
}
function sendLeaderboard() {
io.emit('leaderboard', Object.values(users).sort( (a, b) => a.score < b.score ? 1 : -1 ) )
}
// random integer between low (inclusive) and high (exclusive)
function randBetween(low, high) {
return Math.floor(Math.random() * (high-low)) + low;
}
const nameConfig = {
dictionaries: [ [...adjectives, ...colors], animals],
separator: '-',
};
// Setup
refreshNum();