forked from elephantbeard227/sourceroads_just-the-gmod-addon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (139 loc) · 4.83 KB
/
Copy pathindex.js
File metadata and controls
172 lines (139 loc) · 4.83 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
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
let latestBullets = [];
// 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()
});
latestSounds2.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();
if (latestSounds2.length > 100) latestSounds2.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 = [];
});
app.get("/sound_css", (req, res) => {
res.json(latestSounds);
latestSounds2 = [];
});
app.post("/bullet", (req, res) => {
if (req.body && req.body.bullet) {
latestBullets.push({
bullet: req.body.bullet, // could be object or string
pos: req.body.pos || null,
dir: req.body.dir || null,
shooter: req.body.shooter || null,
weapon: req.body.weapon || null,
time: Date.now()
});
// Keep memory sane
if (latestBullets.length > 200) latestBullets.shift();
io.emit("bullet", req.body); // optional: broadcast to Socket.IO clients
}
res.sendStatus(200);
});
// --- In-memory trace storage ---
const traceAttacks = [];
// POST route
app.post("/traceattacks", (req, res) => {
if (!req.body) {
return res.status(400).json({ error: "Empty request body" });
}
const { attacker, victim, damage, hitpos } = req.body;
if (!attacker || !victim || damage == null || !hitpos) {
return res.status(400).json({ error: "Missing required fields" });
}
// store trace...
res.status(201).json({ success: true });
});
// --- GET /traceattacks ---
// Return all trace attacks or filter by attacker/victim
app.get("/traceattacks", (req, res) => {
const { attacker, victim } = req.query;
let results = traceAttacks;
if (attacker)
results = results.filter(t => t.attacker === attacker);
if (victim)
results = results.filter(t => t.victim === victim);
res.json(results);
});
io.on("connection", socket => {
console.log("Client connected!");
});
app.use(express.static('./html'));
server.listen(2634, () => console.log("Server running on http://localhost:2634"));