Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5822177

Browse files
committed
Make mock data a bit more realistic
1 parent 3c31c82 commit 5822177

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

radar/socketserver/mock.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,45 @@ const WebSocket = require('ws')
22

33
const wss = new WebSocket.Server({ port: 9999 })
44

5+
const pseudoObstacle = angle => {
6+
if (angle > 45 && angle < 90) {
7+
return 20
8+
} else if (angle > 110 && angle < 150) {
9+
return 30
10+
} else {
11+
return 45
12+
}
13+
}
14+
15+
const distanceRandomizer = angle =>
16+
Math.floor(Math.random() * 2) + pseudoObstacle(angle)
17+
518
function* gen() {
619
let angle = 0
720
while (angle <= 180) {
8-
yield { angle, distance: Math.floor(Math.random() * 30) }
21+
yield { angle, distance: distanceRandomizer(angle) }
922
angle++
1023
}
24+
yield* invGen()
25+
}
26+
27+
function* invGen() {
28+
let angle = 180
29+
while (angle >= 0) {
30+
yield { angle, distance: distanceRandomizer(angle) }
31+
angle--
32+
}
1133
yield* gen()
1234
}
1335

36+
1437
wss.on('connection', function connection(ws) {
1538

1639
const data = gen()
1740

1841
const id = setInterval(() => {
1942
ws.send(JSON.stringify(data.next().value))
20-
}, 20)
43+
}, 10)
2144

2245
ws.on('close', () => {
2346
clearInterval(id)

0 commit comments

Comments
 (0)