-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebstack.js
More file actions
208 lines (178 loc) · 5.86 KB
/
Webstack.js
File metadata and controls
208 lines (178 loc) · 5.86 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import express from 'express';
import Redux from 'redux'
import fs from 'fs';
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const _ = require("lodash");
const initVars = require("./leanVars.json");
// Allow persistence path to be overridden by environment variable (e.g., for Render Disks)
const STATE_FILE = process.env.STATE_FILE_PATH || './gameState.json';
class Webstack {
constructor(serverConf) {
this.appIndex = serverConf.appIndex
this.serverConf = serverConf
this.port = serverConf.port;
// Serve story-specific assets at root (for relative paths in CSS/JS)
if (serverConf.storyPath) {
app.use("/", express.static(serverConf.storyPath, { index: false }));
console.log(`[SERVER] Serving story assets from: ${serverConf.storyPath}`);
}
app.use("/static", express.static('./static/'));
app.use("/Twine", express.static('./Twine/'));
app.use("/audio", express.static('./static/audio'));
// serverStore stores the current game state
this.serverStore = Redux.createStore(this.reducer);
this.initIO();
// Load initial state from local file or fallback to leanVars
this.loadState();
// DM Mode: Watch for manual edits to the state file
this.watchStateFile();
http.listen(this.port, () => console.log(`App listening at http://localhost:${this.port}`));
process
.on('SIGTERM', this.shutdown('SIGTERM'))
.on('SIGINT', this.shutdown('SIGINT'))
.on('uncaughtException', this.shutdown('uncaughtException'));
}
loadState() {
try {
if (fs.existsSync(STATE_FILE)) {
console.log(`[SERVER] Loading persistent state from ${STATE_FILE}`);
const data = fs.readFileSync(STATE_FILE, 'utf8');
const state = JSON.parse(data);
this.serverStore.dispatch({
type: 'REPLACE',
payload: state
});
} else {
console.log(`[SERVER] No persistent state found, using ${initVars ? 'leanVars.json' : 'empty state'}`);
this.serverStore.dispatch({
type: 'REPLACE',
payload: initVars || {}
});
}
} catch (err) {
console.error("[SERVER] Error loading state:", err.message);
this.serverStore.dispatch({ type: 'REPLACE', payload: initVars || {} });
}
}
saveState() {
try {
const state = this.serverStore.getState();
console.log(`[SERVER] Saving state to ${STATE_FILE}...`);
// Temporarily ignore the watcher to avoid an infinite loop
this.isSaving = true;
fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 4));
console.log("[SERVER] State saved successfully.");
setTimeout(() => { this.isSaving = false; }, 100);
} catch (err) {
console.error("[SERVER] Error saving state:", err.message);
this.isSaving = false;
}
}
watchStateFile() {
if (!fs.existsSync(STATE_FILE)) return;
console.log(`[DM-MODE] Watching ${STATE_FILE} for live edits...`);
fs.watch(STATE_FILE, (eventType) => {
if (eventType === 'change' && !this.isSaving) {
// Use a small debounce to allow the file write to finish
if (this.watchTimeout) clearTimeout(this.watchTimeout);
this.watchTimeout = setTimeout(() => {
console.log(`[DM-MODE] Manual edit detected in ${STATE_FILE}. Syncing with players...`);
this.loadState();
// Broadcast the fresh state to everyone
io.emit('new connection', this.serverStore.getState());
}, 100);
}
});
}
get() {
return {
app
}
}
shutdown(signal) {
return (err) => {
console.log(`[SERVER] Shutting down (${signal})...`);
if (err && signal === 'uncaughtException') {
console.error(err);
}
this.saveState();
process.exit(err ? 1 : 0);
}
}
// Controller for serverStore
reducer(state, action) {
switch (action.type) {
case 'UPDATE':
return _.merge({}, state, action.payload);
case 'REPLACE':
return action.payload;
default:
return state
}
}
initIO() {
io.on("connect_error", (err) => {
console.log(`connect_error due to ${err.message}`);
});
io.on('connection', (socket) => {
// User connects
socket.once('new user', (id) => {
console.log("SERVER RECEIVES NEW USER:", id);
let gstate = this.serverStore.getState();
socket.emit('new connection', gstate || {})
})
// Handle stateUpdate from th-set macro
socket.on('stateUpdate', (data) => {
const varName = data.variable.startsWith('$') ? data.variable.substring(1) : data.variable;
const diff = {};
_.set(diff, varName, data.value);
this.serverStore.dispatch({
type: 'UPDATE',
payload: diff
})
socket.emit('difference', diff);
socket.broadcast.emit('difference', diff);
})
// Handle Atomic Updates
socket.on('atomicUpdate', (data) => {
const varName = data.variable.startsWith('$') ? data.variable.substring(1) : data.variable;
let currentState = this.serverStore.getState();
let currentValue = _.get(currentState, varName);
if (currentValue === undefined || currentValue === null) currentValue = 0;
currentValue = Number(currentValue);
let operand = Number(data.value);
let newValue = currentValue;
switch(data.operation) {
case 'add': newValue += operand; break;
case 'subtract': newValue -= operand; break;
case 'multiply': newValue *= operand; break;
case 'divide': newValue /= operand; break;
case 'modulus': newValue %= operand; break;
case 'set': newValue = data.value; break;
default: return;
}
const diff = {};
_.set(diff, varName, newValue);
this.serverStore.dispatch({
type: 'UPDATE',
payload: diff
})
socket.emit('difference', diff);
socket.broadcast.emit('difference', diff);
});
socket.on('fullReset', ()=>{
this.serverStore.dispatch({
type: 'REPLACE',
payload: Object.assign({}, initVars)
})
socket.emit('reset',{})
socket.broadcast.emit('reset', {})
})
});
}
}
export default Webstack;