This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp-client.js
229 lines (225 loc) · 14.5 KB
/
mp-client.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
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
/*
Kiwi's Co-Op Mod for Half-Life: Alyx
Copyright (c) 2022 KiwifruitDev
All rights reserved.
This software is licensed under the MIT License.
-----------------------------------------------------------------------------
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-----------------------------------------------------------------------------
*/
// Modules
import { WebSocket } from 'ws';
import * as fs from 'fs';
import chalk from 'chalk';
import * as alyx from './alyx.js';
// Variables
const margin = 2; // margin of error
// Exports
/**
* Start the client.
* @param {Object} config The configuration object.
*/
export function StartClient(config) {
const ws = new WebSocket(`ws://${config.client_ip}:${config.client_port}`, {
headers: {
Authorization: `AuthID ${config.client_username} ${config.client_authid}`,
Password: config.client_password
}
});
let vconsole_server;
// Client connected.
ws.on('open', () => {
console.log(chalk.green("[CL] Connected to server."));
// Send memo to server.
ws.send(JSON.stringify({
type: 'chat',
message: `(Memo) ${config.client_memo}`
}));
// Connect to VConsole.
vconsole_server = alyx.InitVConsole(ws, config);
});
// Handle messages.
ws.on('message', async (data) => {
const message = JSON.parse(data);
switch(message.type) {
case 'chat':
// Print chat message for non-listen server hosts.
if (config.server_enabled && config.client_ip !== config.server_ip)
console.log(`[CHAT] ${message.username}: ${message.message}`);
// Play sound to game to notify VR users.
if(vconsole_server)
await vconsole_server.WriteCommand("play sounds/ui/hint.vsnd");
break;
case 'status':
// Print status message.
console.log(chalk.magenta(`[STATUS] ${message.message}`));
break;
case 'command':
// Execute command in VConsole.
if(vconsole_server)
await vconsole_server.WriteCommand(message.command);
break;
case 'update':
// Update player.
alyx.UpdateVScript(vconsole_server, message.connectioninfo, config);
break;
case 'map':
if(message.changelevel || message.username !== config.client_username && (config.client_anarchy_mode !== "true" && message.host || config.client_anarchy_mode.toLowerCase() === "true")) {
// Update map.
await vconsole_server.WriteCommand(`addon_play ${message.map};addon_tools_map ${message.map}`);
}
break;
case 'physicsobject':
if(message.username !== config.client_username && (config.client_anarchy_mode !== "true" && message.host || config.client_anarchy_mode.toLowerCase() === "true")) {
// Someone else has moved a physics object.
// This should not fire if we move the object ourselves.
for(let i = 0; i < vconsole_server.physicsObjects.length; i++) {
if(vconsole_server.physicsObjects[i].startLocation.x + margin >= message.startLocation.x && vconsole_server.physicsObjects[i].startLocation.x - margin <= message.startLocation.x
&& vconsole_server.physicsObjects[i].startLocation.y + margin >= message.startLocation.y && vconsole_server.physicsObjects[i].startLocation.y - margin <= message.startLocation.y
&& vconsole_server.physicsObjects[i].startLocation.z + margin >= message.startLocation.z && vconsole_server.physicsObjects[i].startLocation.z - margin <= message.startLocation.z) {
if(!vconsole_server.physicsObjects[i].movingLocally) {
// Name is arbitrary, startLocation is unique.
if(!vconsole_server.physicsObjects[i].motionDisabled) {
await vconsole_server.WriteCommand(`ent_fire ${vconsole_server.physicsObjects[i].name} sleep;ent_fire ${vconsole_server.physicsObjects[i].name} disablemotion;ent_fire ${vconsole_server.physicsObjects[i].name} disableinteraction`);
vconsole_server.physicsObjects[i].motionDisabled = true;
}
// Update location.
vconsole_server.physicsObjects[i].updateTime = Date.now();
await vconsole_server.WriteCommand((vconsole_server.physicsObjects[i].door ? `` : `ent_setpos ${vconsole_server.physicsObjects[i].index} ${message.position.x} ${message.position.y} ${message.position.z};`)+`ent_setang ${vconsole_server.physicsObjects[i].index} ${message.angles.x} ${message.angles.y} ${message.angles.z}`);
// Enable motion after inactivity for a while.
if(vconsole_server.physicsObjects[i].interval !== null)
clearTimeout(vconsole_server.physicsObjects[i].interval);
vconsole_server.physicsObjects[i].interval = setTimeout(() => {
if(Date.now() - vconsole_server.physicsObjects[i].updateTime >= config.client_grace_period) {
vconsole_server.physicsObjects[i].motionDisabled = false;
vconsole_server.WriteCommand(`ent_fire ${vconsole_server.physicsObjects[i].name} wake;ent_fire ${vconsole_server.physicsObjects[i].name} enablemotion;ent_fire ${vconsole_server.physicsObjects[i].name} enableinteraction`);
}
vconsole_server.physicsObjects[i].interval = null;
}, config.client_grace_period);
}
break;
}
}
}
break;
case 'button':
if(message.username !== config.client_username && (config.client_anarchy_mode !== "true" && message.host || config.client_anarchy_mode.toLowerCase() === "true")) {
// Someone has pressed a button.
// This should not fire if we press a button ourselves.
for(let i = 0; i < vconsole_server.buttons.length; i++) {
if(vconsole_server.buttons[i].startLocation.x + margin >= message.startLocation.x && vconsole_server.buttons[i].startLocation.x - margin <= message.startLocation.x
&& vconsole_server.buttons[i].startLocation.y + margin >= message.startLocation.y && vconsole_server.buttons[i].startLocation.y - margin <= message.startLocation.y
&& vconsole_server.buttons[i].startLocation.z + margin >= message.startLocation.z && vconsole_server.buttons[i].startLocation.z - margin <= message.startLocation.z) {
if(!vconsole_server.buttons[i].pressingLocally) {
if(!vconsole_server.buttons[i].pressing) {
await vconsole_server.WriteCommand(`ent_fire ${vconsole_server.buttons[i].name} lock`);
vconsole_server.buttons[i].pressing = true;
}
vconsole_server.buttons[i].updateTime = Date.now();
await vconsole_server.WriteCommand(`button_press ${vconsole_server.buttons[i].index}`);
if(vconsole_server.buttons[i].interval === null)
clearTimeout(vconsole_server.buttons[i].interval);
vconsole_server.buttons[i].interval = setTimeout(() => {
if(Date.now() - vconsole_server.buttons[i].updateTime >= config.client_grace_period) {
vconsole_server.buttons[i].pressing = false;
vconsole_server.WriteCommand(`ent_fire ${vconsole_server.buttons[i].name} unlock`);
}
vconsole_server.buttons[i].interval = null;
}, config.client_grace_period);
}
break;
}
}
}
break;
case 'breakphys':
if(message.username !== config.client_username && (config.client_anarchy_mode !== "true" && message.host || config.client_anarchy_mode.toLowerCase() === "true")) {
// Someone has broken a physics object.
for(let i = 0; i < vconsole_server.physicsObjects.length; i++) {
if(vconsole_server.physicsObjects[i].startLocation.x + margin >= message.startLocation.x && vconsole_server.physicsObjects[i].startLocation.x - margin <= message.startLocation.x
&& vconsole_server.physicsObjects[i].startLocation.y + margin >= message.startLocation.y && vconsole_server.physicsObjects[i].startLocation.y - margin <= message.startLocation.y
&& vconsole_server.physicsObjects[i].startLocation.z + margin >= message.startLocation.z && vconsole_server.physicsObjects[i].startLocation.z - margin <= message.startLocation.z) {
vconsole_server.WriteCommand(`ent_fire ${vconsole_server.physicsObjects[i].name} break`, true);
vconsole_server.physicsObjects.splice(i, 1); // We don't want this to linger.
break;
}
}
}
break;
case 'triggerbrush':
if(message.username !== config.client_username && (config.client_anarchy_mode !== "true" && message.host || config.client_anarchy_mode.toLowerCase() === "true")) {
// Someone has triggered something.
for(let i = 0; i < vconsole_server.triggers.length; i++) {
if(vconsole_server.triggers[i].startLocation.x + margin >= message.startLocation.x && vconsole_server.triggers[i].startLocation.x - margin <= message.startLocation.x
&& vconsole_server.triggers[i].startLocation.y + margin >= message.startLocation.y && vconsole_server.triggers[i].startLocation.y - margin <= message.startLocation.y
&& vconsole_server.triggers[i].startLocation.z + margin >= message.startLocation.z && vconsole_server.triggers[i].startLocation.z - margin <= message.startLocation.z) {
if(!vconsole_server.triggers[i].triggeringLocally) {
if(!vconsole_server.triggers[i].triggering) {
await vconsole_server.WriteCommand(`ent_fire ${vconsole_server.triggers[i].name} disable`);
vconsole_server.triggers[i].triggering = true;
}
vconsole_server.triggers[i].updateTime = Date.now();
if(message.once && !vconsole_server.triggers[i].triggeredOnce || !message.once) {
vconsole_server.triggers[i].triggeredOnce = (message.output == "OnTrigger");
// Yes, you can still 'touch' triggers even if they're disabled.
await vconsole_server.WriteCommand(`trigger_touch ${vconsole_server.triggers[i].index} ${message.output}`, true);
if(!vconsole_server.triggers[i].once) { // Only attempt to re-enable if it's not a trigger_once.
if(vconsole_server.triggers[i].interval !== null)
clearTimeout(vconsole_server.triggers[i].interval);
vconsole_server.triggers[i].interval = setTimeout(() => {
if(Date.now() - vconsole_server.triggers[i].updateTime >= config.client_grace_period) {
clearInterval(vconsole_server.triggers[i].interval);
vconsole_server.triggers[i].interval = null;
vconsole_server.triggers[i].triggering = false;
vconsole_server.WriteCommand(`ent_fire ${vconsole_server.triggers[i].name} enable`);
}
}, config.client_grace_period);
}
}
}
break;
}
}
}
break;
}
});
// Handle errors.
ws.on('error', (error) => {
console.log(chalk.red(`[ERROR] [CL] ${error.stack}`));
});
// Handle close.
ws.on('close', () => {
console.log(chalk.red("[CL] Disconnected from server. The program will now exit."));
process.exit(0);
});
// Chat through the console.
process.stdin.on('data', (data) => {
const message = data.toString().trim();
if(message.length > 0) {
// Check for commands.
if (message.startsWith("/")) {
let command = message.substring(1).split(" ")[0];
let args = message.substring(1).split(" ").slice(1);
// Send command to server.
ws.send(JSON.stringify({
type: 'command',
command: command,
args: args
}));
} else {
// Send chat message to server.
ws.send(JSON.stringify({
type: 'chat',
message: message
}));
}
}
});
}