-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.js
184 lines (168 loc) · 5.77 KB
/
webserver.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const fs = require('fs');
const path = require('path');
const express = require('express');
const WebSocket = require('ws');
const DeathTracker = require('./deathTracker.js');
const QUSB2SNESConnection = require('./2snesbridge.js');
const electron = require('electron');
const app = electron.app || electron.remote.app;
const port = 3000;
let server;
let wss;
let snes;
let statTracker = new DeathTracker();
const userDataPath = app.getPath('userData');
const statsFilePath = path.join(userDataPath, 'saveData.json');
let currentHackName;
// Function to save stats to a JSON file
function saveStats(hackName, statsData) {
console.log('Saving stats:', statsFilePath)
fs.readFile(statsFilePath, (err, data) => {
if (err && err.code !== 'ENOENT') {
console.error('Error reading stats file:', err);
return;
}
let stats = {};
if (data) {
stats = JSON.parse(data.toString() || '{}');
}
stats[hackName] = statsData;
fs.writeFile(statsFilePath, JSON.stringify(stats, null, 2), err => {
if (err) {
console.error('Error writing stats to file:', err);
} else {
console.log('Stats saved successfully.');
}
});
});
}
// Function to load stats from a JSON file
function loadStats(hackName, callback) {
console.log('Loading stats:', statsFilePath)
fs.readFile(statsFilePath, (err, data) => {
if (err && err.code === 'ENOENT') {
console.log('Stats file not found, initializing new stats file.');
callback({});
return;
} else if (err) {
console.error('Error reading stats file:', err);
callback({});
return;
}
const stats = JSON.parse(data.toString() || '{}');
callback(stats[hackName] || {});
});
}
// Start the WebSocket server
function startWebSocketServer() {
wss = new WebSocket.Server({ server });
console.log('WebSocket server started on port 3000');
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('WebSocket connection established');
});
}
// Stop the WebSocket server
function stopWebSocketServer() {
if (wss) {
wss.clients.forEach(client => client.close());
wss.close(() => {
console.log('WebSocket Server stopped');
});
wss = null;
}
}
// Broadcast to all clients
function broadcast(data) {
if (!wss || !wss.clients) {
console.error("WebSocket server is not initialized or clients are undefined.");
return;
}
const jsonData = JSON.stringify(data);
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(jsonData);
}
});
}
// Start the Express server
function startServer(initData) {
const serverApp = express();
let data = initData;
serverApp.use(express.static(path.join(__dirname, 'tracker', 'public')));
serverApp.set('views', path.join(__dirname, 'tracker', 'views'));
serverApp.set('view engine', 'ejs');
serverApp.get('/', (req, res) => {
const hackName = req.query.hackName || data.hackName;
const author = req.query.author || data.author;
currentHackName = hackName;
loadStats(hackName, (statsData) => {
res.render('tracker', { hackName, author, stats: statsData });
});
});
server = serverApp.listen(port, () => {
console.log(`Express webserver started on port ${port}`);
});
startWebSocketServer();
snes = new QUSB2SNESConnection("ws://localhost:8080", {
death: () => {
statTracker.addDeath();
saveStats(data.hackName, {
deathCount: statTracker.getDeaths(),
timer: statTracker.getElapsedTime(),
exitCount: statTracker.getExits()
});
console.log('Player died');
broadcast({ type: 'death', message: statTracker.getDeaths() });
},
timerStart: () => {
console.log('Timer started');
statTracker.startTimer();
if (!global.timerBroadcastInterval) {
global.timerBroadcastInterval = setInterval(() => {
broadcast({ type: 'timer', message: statTracker.getElapsedTime() });
}, 1000);
}
},
timerStop: () => {
console.log('Timer stopped');
saveStats(data.hackName, {
deathCount: statTracker.getDeaths(),
timer: statTracker.getElapsedTime(),
exitCount: statTracker.getExits()
});
clearInterval(global.timerBroadcastInterval);
global.timerBroadcastInterval = null;
statTracker.stopTimer();
broadcast({ type: 'timer', message: statTracker.getElapsedTime() });
},
exitUpdate: (exits) => {
statTracker.setExits(exits);
saveStats(data.hackName, {
deathCount: statTracker.getDeaths(),
timer: statTracker.getElapsedTime(),
exitCount: statTracker.getExits()
});
broadcast({ type: 'exit', message: statTracker.getExits() });
}
});
snes.connect();
}
// Stop the Express server
function stopServer() {
saveStats(currentHackName, {
deathCount: statTracker.getDeaths(),
timer: statTracker.getElapsedTime(),
exitCount: statTracker.getExits()
});
stopWebSocketServer();
if (server) {
server.close(() => {
console.log('Express webserver stopped');
});
server = null;
}
}
module.exports = { startServer, stopServer, broadcast };