forked from Dregu/s2tracker
/
index.js
166 lines (140 loc) · 4.09 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const express = require('express')
const open = require('open')
const WebSocket = require('ws');
const { ArgumentParser } = require('argparse');
const { version } = require('./package.json');
var app = express()
const expressWs = require('express-ws')(app)
const numEntries = 0xec;
app.use(express.urlencoded())
app.use('/', express.static(__dirname + '/static'))
app.set('views', './views')
app.set('view engine', 'pug')
const initState = () => {
let journal = Array(numEntries).fill(0)
journal[0] = 1
journal[0x5e] = 1
journal[0x5f] = 1
journal[0x60] = 1
journal[0x61] = 1
return { 'journal': journal };
}
var state = initState();
var roomCode = null;
var fyiConnection = null;
const tryInt = (value) => {
return isNaN(+value) ? value : +value
}
const updateJournal = (newState) => {
console.log('Updating state from other player.')
newState.map((entry, idx) => {
var authoritative = (fyiConnection == null)
if (authoritative || entry) {
state['journal'][idx] = entry;
}
});
}
const connectFyi = (connectRoomCode) => {
if (!connectRoomCode) {
return null;
}
console.log(`Connecting to spelunky.fyi with room code ${connectRoomCode}`)
var ws = new WebSocket(`wss://spelunky.fyi/ws/tools/coop-aje-tracker/${connectRoomCode}/`)
ws.on('open', () => {
console.log('Connected to Spelunky.fyi')
})
ws.on('message', (data) => {
data = JSON.parse(data);
if (data['action'] == 'update') {
updateJournal(data['data'])
broadcast(state);
} else if (data['action'] == 'clear') {
state = initState();
broadcast(state);
}
})
ws.on('close', (e) => {
console.log('Socket is closed.')
fyiConnection = null;
if (roomCode) {
console.log('Reconnect will be attempted in 1 second.', e.reason)
setTimeout(() => {
fyiConnection = connectFyi(roomCode);
}, 1000)
}
})
ws.on('error', (err) => {
console.error(`Socket encountered error: ${err.message} Closing socket`)
ws.close()
})
return ws
}
const broadcast = (status) => {
for (let client of expressWs.getWss().clients) {
client.send(JSON.stringify({ update: status }))
}
}
app.get('/join-room', function (req, res) {
let newRoomCode = req.query.roomCode
if (fyiConnection !== null && newRoomCode != roomCode) {
fyiConnection.close();
fyiConnection = null;
}
if (newRoomCode && newRoomCode != roomCode) {
fyiConnection = connectFyi(newRoomCode);
}
roomCode = newRoomCode
res.render('join-room', { 'roomCode': roomCode })
})
app.post('/', (req, res) => {
res.sendStatus(200)
Object.entries(req.body).map(([key, value], idx) => {
if (key === "journal") {
let newState = value.split(',').map((value, idx) => parseInt(value, 10))
updateJournal(newState)
} else {
state[key] = tryInt(value)
}
})
console.log('Got journal data from livesplit')
if (fyiConnection !== null) {
console.log('Relaying state to spelunky.fyi')
fyiConnection.send(JSON.stringify({
'action': 'update',
'data': state['journal']
}))
}
broadcast(state)
})
app.post('/clear', (req, res) => {
res.sendStatus(200)
console.log('Got request to clear journal')
state = initState();
broadcast(state);
if (fyiConnection !== null) {
console.log('Clearing state to spelunky.fyi')
fyiConnection.send(JSON.stringify({
'action': 'clear',
}))
}
})
app.ws('/', (ws, req) => {
ws.on('message', (msg) => {
console.log('Client is asking for', msg)
if (msg == 'state') {
ws.send(JSON.stringify({ state: state }))
}
})
})
const parser = new ArgumentParser({
description: 'Spelunky 2 RTA Tracker'
});
parser.add_argument('-v', '--version', { action: 'version', version });
parser.add_argument('-p', '--port', { type: 'int', help: 'Port to listen on', default: 27122 });
const args = parser.parse_args();
var server = app.listen(args.port, function () {
var port = server.address().port
console.log(`Tracker is listening on http://localhost:${port}`)
console.log(`If playing co-op join a room at http://localhost:${port}/join-room`)
open(`http://localhost:${port}`)
})