-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 2.07 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
function getThrow(max) {
return Math.floor(Math.random() * (max + 1));
}
function bowlingScore() {
let scores = []
for(let frame = 1; frame <= 10; frame++)
{
let throw1 = getThrow(10)
let throw2
scores.push(throw1)
if(throw1 != 10) { // not a strike
throw2 = getThrow( 10 - throw1 )
scores.push(throw2)
}
// bonus throws:
if( frame === 10 ) {
if (throw1 === 10) { // strike
let bonusBall1 = getThrow(10)
scores.push(bonusBall1)
scores.push(bonusBall1 === 10 ? getThrow(10) : getThrow(10-bonusBall1))
}
if (throw1 + throw2 === 10) {
scores.push(getThrow(10))
}
}
}
return scores
}
const http = require("http");
const port = 8080;
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
const requestListener = function (req, res) {
let originalGame = bowlingScore()
console.log(`Sending game: ${JSON.stringify(originalGame)}`)
let game = originalGame.reverse();
res.statusCode = 200;
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("connection", "keep-alive");
res.setHeader("Content-Type", "text/event-stream");
const sendNextScore = () => {
const pins = game.pop();
if (game.length === 0) {
const data = JSON.stringify({
state: 'end',
pins
});
res.end(`data: ${data}\n\n`);
return false;
} else {
const data = JSON.stringify({
state: 'next',
pins
});
res.write(`data: ${data}\n\n`);
return true;
}
};
const next = () => {
if (sendNextScore()) {
const newTimeout = getRandomArbitrary(300, 2000)
setTimeout(next, newTimeout)
}
}
next()
};
const server = http.createServer(requestListener);
server.listen(port, () => {
console.log(`server running on port ${port}`);
});