This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.mjs
382 lines (309 loc) · 11.1 KB
/
server.mjs
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import * as alt from 'alt';
import * as color from './colors.mjs';
console.log(`${color.FgYellow}Extended v0.5 is running.`);
const markersToLoad = [];
const callbacks = [];
const registeredCallbacks = new Set();
alt.on('playerConnect', (player) => {
if (markersToLoad.length >= 1) {
for(var i = 0; i < markersToLoad.length; i++) {
// (type, pos, dir, rot, scale, color, enterColor, deleteOnEnter, range, uniqueID)
alt.emitClient(
player,
'createLocalMarker',
markersToLoad[i].markerType,
markersToLoad[i].pos,
{x: 0, y: 0, z: 0},
{x: 0, y: 0, z: 0},
markersToLoad[i].markerScale,
markersToLoad[i].exitMarkerColor,
markersToLoad[i].enterMarkerColor,
false,
markersToLoad[i].markerRange,
undefined
);
}
}
});
alt.on('entityEnterColshape', (colshape, entity) => {
if (!colshape.emitter)
return;
if (!colshape.enterEventName)
return;
if (colshape.isEnterClientside && colshape.isPlayerOnly && entity instanceof alt.Player) {
alt.emitClient(entity, colshape.enterEventName);
return;
}
if (colshape.isPlayerOnly && entity instanceof alt.Player) {
console.log('Called server-side event.');
alt.emit(colshape.enterEventName, entity);
return;
}
alt.emit(colshape.enterEventName, entity);
});
alt.on('entityLeaveColshape', (colshape, entity) => {
if (!colshape.emitter)
return;
if (colshape.exitEventName == undefined || colshape.exitEventName == null)
return;
if (colshape.isExitClientside && colshape.isPlayerOnly && entity instanceof alt.Player) {
alt.emitClient(entity, colshape.exitEventName);
return;
}
if (colshape.isPlayerOnly && entity instanceof alt.Player) {
alt.emit(colshape.exitEventName, entity);
return;
}
alt.emit(colshape.exitEventName, entity);
});
// This emits server or client functions automatically on entering.
export function ColshapeEmitter(pos, enterEventName, exitEventName, markerType, enterMarkerColor, exitMarkerColor, markerScale, markerRange, isEnterClientside = false, isExitClientside = false, isPlayerOnly = true) {
var colshape = new alt.ColshapeCylinder(pos.x, pos.y, pos.z, markerRange, markerScale.z);
colshape.emitter = true;
colshape.isPlayerOnly = isPlayerOnly;
colshape.exitEventName = exitEventName;
colshape.enterEventName = enterEventName;
colshape.isEnterClientside = isEnterClientside;
colshape.isExitClientside = isExitClientside;
colshape.markerType = markerType;
colshape.enterMarkerColor = enterMarkerColor;
colshape.exitMarkerColor = exitMarkerColor;
colshape.markerScale = markerScale;
colshape.markerRange = markerRange;
markersToLoad.push(colshape);
}
// This needs to be called for each player that joins the server.
export function SetupExportsForPlayer(player) {
if (player === undefined) {
throw new Error('SetupExportsForPlayer => player is undefined.');
}
// Returns the ForwardVector of the player.
player.forwardVector = (callback) => {
SetupCallback(player, 'getForwardVector', undefined, (result) => {
callback(result);
});
};
// Returns if the player is near a position.
player.isNearPos = (pos, range) => {
if (pos === undefined || range === undefined) {
throw new Error('isNearPos => pos or range is undefined');
}
var currentDistance = Distance(player.pos, pos);
if (currentDistance <= range)
return true;
return false;
};
// Create a local blip for a player.
player.createLocalBlip = (pos, sprite, color, scale, name, shortRange, uniqueID) => {
if (pos === undefined || sprite === undefined || color === undefined || scale === undefined) {
throw new Error('createLocalBlip => One or more parameters is undefined.');
}
alt.emitClient(player, 'createLocalBlip', pos, sprite, color, scale, name, shortRange, uniqueID);
};
// Delete a local blip by uniqueID.
player.deleteLocalBlip = (uniqueID) => {
if (uniqueID === undefined) {
throw new Error('deleteLocalBlip => uniqueID is undefined.');
}
alt.emitClient(player, 'deleteLocalBlip', uniqueID);
};
// Create a marker for a player.
player.createLocalMarker = (type, pos, dir, rot, scale, color, enterColor, deleteOnEnter, range, uniqueID) => {
if (type === undefined || pos === undefined || scale === undefined || color === undefined || enterColor === undefined || range === undefined) {
throw new Error('createLocalMarker => One or more parameters is undefined.');
}
alt.emitClient(player, 'createLocalMarker', type, pos, dir, rot, scale, color, enterColor, deleteOnEnter, range, uniqueID);
};
// Delete a marker for a player.
player.deleteLocalMarker = (uniqueID) => {
if (uniqueID === undefined) {
throw new Error('deleteLocalMarker => uniqueID is undefined.');
}
alt.emitClient(player, 'deleteLocalMarker', uniqueID);
};
// Show Notification to Player
player.showNotification = (imageName, headerMsg, detailsMsg, message) => {
if (imageName === undefined || headerMsg === undefined || detailsMsg === undefined || message === undefined) {
throw new Error('showNotification => One or more parameters is undefined.');
}
alt.emitClient(player, 'showNotification', imageName, headerMsg, detailsMsg, message);
};
// Freeze Player
player.freeze = (state) => {
alt.emitClient(player, 'freezePlayer', state);
};
// Fade screen to black and back.
player.fadeScreen = (state, timeInMS) => {
if (timeInMS === undefined) {
throw new Error('fadeScreen => state or timeInMS is undefined.');
}
alt.emitClient(player, 'fadeOutScreen', state, timeInMS);
};
// Blur screen.
player.blurScreen = (state, timeInMS) => {
if (timeInMS === undefined) {
throw new Error('blurScreen => state or timeInMS is undefined.');
}
alt.emitClient(player, 'blurOutScreen', state, timeInMS);
};
// Show Cursor
player.showCursor = (state) => {
alt.emitClient(player, 'showCursor', state);
};
// Show Help Text
player.showHelpText = (text, timeInMS) => {
if (text === undefined || timeInMS === undefined) {
throw new Error('showHelpText => text or timeInMS is undefined.');
}
alt.emitClient(player, 'displayHelpText', text, timeInMS);
};
// Show Subtitle Text
player.showSubtitle = (text, timeInMS) => {
if (text === undefined || timeInMS === undefined) {
throw new Error('showSubtitle => text or timeInMS is undefined.');
}
alt.emitClient(player, 'displaySubtitle', text, timeInMS);
};
// Display Loading
player.showLoading = (text, timeInMS, type, toggled) => {
if (text === undefined || timeInMS === undefined) {
throw new Error('showLoading => One or more parameters is undefined.');
}
alt.emitClient(player, 'showLoading', text, timeInMS, type, toggled);
};
}
// Returns the GroundLevel from a 3D Position near a player.
// Returns undefined if ground was not present. Otherwise a number.
export function GetGroundZFrom3DCoord(player, pos, callback) {
if (player === undefined || pos === undefined) {
throw new Error('GetGroundZFrom3DCoord => player or pos is undefined.');
}
SetupCallback(player, 'getGroundZFrom3DCoord', pos, (result) => {
if (result[0] == false)
callback(undefined);
callback(result[1]);
});
}
// Add one vector to another.
export function AddVector3(vector1, vector2) {
if (vector1 === undefined || vector2 === undefined) {
throw new Error('AddVector => vector1 or vector2 is undefined');
}
return {
x: vector1.x + vector2.x,
y: vector1.y + vector2.y,
z: vector1.z + vector2.z
};
}
// Subtract one vector from another.
export function SubVector3(vector1, vector2) {
if (vector1 === undefined || vector2 === undefined) {
throw new Error('AddVector => vector1 or vector2 is undefined');
}
return {
x: vector1.x - vector2.x,
y: vector1.y - vector2.y,
z: vector1.z - vector2.z
};
}
// Returns as {r,g,b}
export function GetRandomColor() {
return {
r: Math.floor(Math.random() * 255) + 1,
g: Math.floor(Math.random() * 255) + 1,
b: Math.floor(Math.random() * 255) + 1
};
}
// Get all of the players in range of a position.
export function GetPlayersInRange(pos, range) {
if (pos === undefined || range === undefined) {
throw new Error('GetPlayersInRange => pos or range is undefined');
}
var inRange = [];
alt.Player.all.forEach((value) => {
if (Distance(pos, value.pos) > range)
return;
inRange.push(value);
});
return inRange;
}
// Get the distance between two vectors.
export function Distance(vector1, vector2) {
if (vector1 === undefined || vector2 === undefined) {
throw new Error('AddVector => vector1 or vector2 is undefined');
}
return Math.sqrt(Math.pow(vector1.x - vector2.x, 2) + Math.pow(vector1.y - vector2.y, 2) + Math.pow(vector1.z - vector2.z, 2));
}
// Get Random Position Around Position
export function RandomPosAround(pos, range) {
if (pos === undefined || range === undefined) {
throw new Error('RandomPosAround => pos or range is undefined');
}
return {
x: pos.x + (Math.random() * (range * 2)) - range,
y: pos.y + (Math.random() * (range * 2)) - range,
z: pos.z
};
}
// Display a message above a player's head.
export function DisplayAboveHead(player, message, timeInMS, rangeToDisplay, r = 255, g = 255, b = 255, a = 255) {
if (message === undefined || timeInMS <= 0) {
throw new Error('DisplayAboveHead => message or timeInMS is undefined.');
}
var players = GetPlayersInRange(player.pos, rangeToDisplay);
for (const target of players) {
if (target === player) {
continue;
}
alt.emitClient(target, 'displayMessageAboveHead', player, message, timeInMS, r, g, b, a);
}
}
async function SetupCallback(player, eventName, args, callback) {
ClientsideCall(player, eventName, args, (result) => {
callback(result);
});
}
async function ClientsideCall(player, eventName, args, callback) {
if (player === undefined)
throw new Error('ClientsideCall => Player is undefined.');
if (eventName === undefined)
throw new Error('ClientsideCall => eventName is undefined.');
alt.emitClient(player, eventName, args);
callbacks.push({player, eventName, startTime: Date.now(), completed: false, callback});
if (registeredCallbacks.has(eventName))
return;
registeredCallbacks.add(eventName);
alt.onClient(eventName, (player, results) => {
let index = -1;
for(var i = 0; i < callbacks.length; i++) {
if (callbacks[i].player !== player && callbacks[i].eventName !== eventName)
continue;
index = i;
break;
}
if (index <= -1)
return;
callbacks[i].callback(results);
callbacks[i].completed = true;
});
}
// Callback Cleanup
setInterval(() => {
var callbacksCleanedUp = 0;
var i = callbacks.length;
while(i--) {
if (callbacks[i].completed) {
callbacks.splice(i, 1);
callbacksCleanedUp += 1;
continue;
}
if ((callbacks[i].startTime + 10000) < Date.now()) {
callbacks.splice(i, 1);
callbacksCleanedUp += 1;
continue;
}
}
if (callbacksCleanedUp <= 0)
return;
console.log(`===> Extended: Cleaned up ${callbacksCleanedUp}`);
}, 5000);