forked from elephantbeard227/sourceroads_just-the-gmod-addon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullets.js
More file actions
247 lines (199 loc) · 6.89 KB
/
Copy pathbullets.js
File metadata and controls
247 lines (199 loc) · 6.89 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import express from "express";
import http from "http";
import { Server } from "socket.io";
import bodyParser from "body-parser";
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: { origin: "*" }
});
app.use(bodyParser.json());
let latestUpdates = []; // from SourceMod
let latestUpdatesCS = []; // from SourceMod
let latestUpdatesGMod = []; // from GMod players
let latestUpdatesProp = []; // from GMod props
let latestSounds = []; // from sound POSTs
let latestSounds2 = []; // from sound POSTs
// HTTP POST endpoint for player updates
app.post("/update", (req, res) => {
if (req.body && Array.isArray(req.body.players)) {
latestUpdates = req.body;
//console.log(`[SourceMod] Received ${latestUpdates.players.length} players`);
}
res.sendStatus(200);
});
app.post("/update_css", (req, res) => {
if (req.body && Array.isArray(req.body.players)) {
latestUpdatesCS = req.body;
//console.log(`[SourceMod] Received ${latestUpdates.players.length} players`);
}
res.sendStatus(200);
});
// HTTP GET endpoint for browser polling (player updates)
app.get("/update", (req, res) => {
res.json(latestUpdates); // return all stored updates
});
// HTTP GET endpoint for browser polling (player updates)
app.get("/update_css", (req, res) => {
res.json(latestUpdatesCS); // return all stored updates
});
app.post("/update_gmod", (req, res) => {
if (req.body && req.body.data) {
latestUpdatesGMod = req.body.data; // store the data
}
res.sendStatus(200);
});
// HTTP GET endpoint for browser polling (player updates)
app.get("/update_gmod", (req, res) => {
res.json(latestUpdatesGMod); // return all stored updates
});
app.post("/update_props", (req, res) => {
if (req.body && req.body) {
latestUpdatesProp = req.body; // store the data
}
res.sendStatus(200);
});
// HTTP GET endpoint for browser polling (player updates)
app.get("/update_props", (req, res) => {
res.json(latestUpdatesProp); // return all stored updates
});
// --- SOUND EVENTS ---
// POST route: GMod sends when a sound plays
app.post("/sound", (req, res) => {
if (req.body && req.body.sound) {
// Example: { sound: "vo/npc/male01/hi01.wav", pos: { x: 0, y: 0, z: 0 }, volume: 1.0 }
latestSounds.push({
sound: req.body.sound,
pos: req.body.pos || null,
volume: req.body.volume || 1.0,
time: Date.now()
});
// Optional: only keep last 100 sounds
if (latestSounds.length > 100) latestSounds.shift();
io.emit("sound", req.body); // optionally broadcast to web clients
}
res.sendStatus(200);
});
// GET route: returns the latest list of sound events
app.get("/sound", (req, res) => {
res.json(latestSounds);
latestSounds = [];
});
// --- In-memory trace storage ---
var traceAttacks = [];
// POST route// --- In-memory trace storage ---
var traceAttacks = [];
// POST route: accepts single object or array of trace objects
app.post("/traceattacks", (req, res) => {
const body = req.body;
if (!body) return res.status(400).json({ error: "Empty request body" });
const items = Array.isArray(body) ? body : [body];
const storedEntries = [];
for (const item of items) {
if (!item) continue;
const { attacker, victim, damage, hitpos } = item;
// Basic required-field check
if (hitpos == null) {
continue;
}
// Normalize hitpos into [x, y, z] numbers
let hp = hitpos;
// 1) If it's a space-separated string "100 200 300"
if (typeof hp === "string") {
const parts = hp.trim().split(/\s+/).map(Number);
if (parts.length !== 3 || parts.some(isNaN)) continue;
hp = parts;
}
// 2) If it's already an array [x,y,z]
else if (Array.isArray(hp)) {
if (hp.length !== 3 || hp.some(v => typeof v !== "number")) continue;
}
// 3) If it's an object like { x:..., y:..., z:... } or numeric keys
else if (typeof hp === "object" && hp !== null) {
if (hp.x != null && hp.y != null && hp.z != null) {
const x = Number(hp.x), y = Number(hp.y), z = Number(hp.z);
if ([x, y, z].some(isNaN)) continue;
hp = [x, y, z];
} else if (hp[0] != null && hp[1] != null && hp[2] != null) {
const x = Number(hp[0]), y = Number(hp[1]), z = Number(hp[2]);
if ([x, y, z].some(isNaN)) continue;
hp = [x, y, z];
} else {
continue;
}
} else {
continue; // unrecognized hitpos format
}
// Build stored trace object (preserve optional fields)
const traceEntry = {
hitpos: hp[0] + " " + hp[1] + " " + hp[2],
damage: damage
};
traceAttacks.push(traceEntry);
storedEntries.push(traceEntry);
// Keep memory sane
if (traceAttacks.length > 500) traceAttacks.shift();
// Broadcast to connected clients
if (typeof io !== "undefined") {
io.emit("trace", traceEntry);
}
}
if (storedEntries.length === 0) {
return res.status(400).json({ error: "No valid trace entries in request" });
}
return res.status(200).json({ success: true, stored: storedEntries.length });
});
// --- GET /traceattacks ---
// Return all trace attacks or filter by attacker/victim
app.get("/traceattacks", (req, res) => {
let results = traceAttacks;
res.status(200).json(results);
traceAttacks = [];
});
// --- CHAT SYSTEM ---
// In-memory chat message storage
let chatMessages = [];
// POST /chat
// Example body: { name: "Player1", text: "Hello world" }
app.post("/chat", (req, res) => {
const body = req.body;
if (!body || typeof body.name !== "string" || typeof body.text !== "string") {
return res.status(400).json({ error: "Invalid chat message format" });
}
const name = body.name.trim();
const text = body.text.trim();
if (!name || !text) {
return res.status(400).json({ error: "Missing name or text" });
}
// Build message object
const message = {
name,
text,
time: Date.now()
};
chatMessages.push(message);
// Limit to last 100 messages
if (chatMessages.length > 100) chatMessages.shift();
// Broadcast to all connected Socket.IO clients
io.emit("chat", message);
res.status(200).json({ success: true });
});
// GET /chat
// Returns all stored chat messages (and clears them after)
let clearTimeoutId;
app.get("/chat", (req, res) => {
if (chatMessages.length === 0) {
return res.json(null); // or [] if you prefer
}
// Send only the last message
res.json(chatMessages[chatMessages.length - 1]);
// Optional: auto-clear after 2s of inactivity
setTimeout(() => {
chatMessages = [];
}, 1800);
});
io.on("connection", socket => {
console.log("Client connected!");
});
app.use(express.static('./html'));
server.listen(7000, () => console.log("Server running on http://localhost:7000"));