This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (81 loc) · 3.05 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
const fs = require('fs')
const ThemeParks = require('themeparks')
const express = require('express')
const socketio = require('socket.io')
const http = require('http')
// Constants
const DB_PATH = './db/times.json' // json file to save to
const POLL_RATE = 1e4 // poll rate for checking the servers
const park = new ThemeParks.Parks.DisneylandResortMagicKingdom()
const lastTimes = {}
const data = JSON.parse(fs.existsSync(DB_PATH) ? fs.readFileSync(DB_PATH, 'utf8') : '{}')
/* Server */
var app = express();
var server = http.Server(app);
var io = socketio.listen(server);
app.get('/times', (req, res) => res.send(data))
app.use(express.static('static'))
app.use(express.static('node_modules'))
io.on('connection', client => client.emit('current-times', lastTimes))
/* Theme Park Polling */
// pad a string with a character
const pad = (str, char, width) => {
if (str.length >= width) return str
return pad(str + char, char, width)
}
// iterative check for ride data
const pollForUpdates = () => {
park.GetWaitTimes((err, rides) => {
if (err) {
console.error('ERROR', err)
return
}
// see if the ride times have changed
const newTimes = []
rides.forEach(ride => {
// print changes
if (lastTimes[ride.id] && lastTimes[ride.id].waitTime !== ride.waitTime) {
console.log(`${pad(ride.name, ' ', 65)} from ${lastTimes[ride.id].waitTime} to ${ride.waitTime}min`)
} else if (!lastTimes[ride.id]) {
console.log(`${pad(ride.name, ' ', 65)} wait is ${ride.waitTime}min`)
}
// save changes to our history
if (!lastTimes[ride.id] || lastTimes[ride.id].waitTime !== ride.waitTime) {
const arr = data[ride.id] || []
const newTime = {
date: new Date(),
name: ride.name,
lastUpdate: ride.lastUpdate,
waitTime: ride.waitTime,
active: ride.active,
status: ride.status,
fastPass: ride.fastPass
}
arr.push(newTime)
newTimes.push(Object.assign({}, newTime, { id: ride.id }))
data[ride.id] = arr
}
lastTimes[ride.id] = ride
})
io.emit('new-times', newTimes)
io.emit('current-times', lastTimes)
setTimeout(pollForUpdates, POLL_RATE)
})
}
/* Application exit */
// save the history on close
const onClose = () => {
fs.writeFileSync(DB_PATH, JSON.stringify(data))
process.exit()
}
//https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits
// do something when app is closing
process.on('exit', onClose)
// catches ctrl+c event
process.on('SIGINT', onClose);
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', onClose);
process.on('SIGUSR2', onClose);
/* Start */
pollForUpdates()
server.listen(8080, () => console.log('listening on 8080'))