-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
332 lines (269 loc) · 7.8 KB
/
server.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
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
/**
* @namespace MRP_SERVER
*/
MRP = {
/**
* @memberof MRP_SERVER
*/
playerSpawnedCharacters: {}
};
ENTITIES = 0;
const config = require('./config/default.json');
let baseItems = require('./config/db/items.json'); //not a constant we will delete this from memory after insert into DB
require('./shared/debug.js');
const logger = mrp_logger;
const db = require('./server/db.js');
const commands = require('./server/commands.js');
var connectedUsers = {};
/**
* MRP - description
*
* @memberof MRP_SERVER
*
* @param {type} source description
* @return {type} description
*/
MRP.getUserId = function(source) {
let numOfIdentifiers = GetNumPlayerIdentifiers(source);
let userID;
for (let i = 0; i < numOfIdentifiers; i++) {
const identifier = GetPlayerIdentifier(source, i);
if (identifier.includes('fivem:')) {
userID = identifier.slice(6);
}
}
if (!userID) {
//don't have fivem suplement with steam
for (let i = 0; i < numOfIdentifiers; i++) {
const identifier = GetPlayerIdentifier(source, i);
if (identifier.includes('steam:')) {
userID = identifier.slice(6);
}
}
}
if (!userID) {
//don't have fivem suplement with license
for (let i = 0; i < numOfIdentifiers; i++) {
const identifier = GetPlayerIdentifier(source, i);
if (identifier.includes('license:')) {
userID = identifier.slice(8);
}
}
}
return userID;
};
/**
* MRP - description
*
* @memberof MRP_SERVER
* @return {type} description
*/
MRP.getPlayersServer = function() {
let num = GetNumPlayerIndices();
let players = [];
for (i = 0; i < num; i++) {
players.push({
id: num,
identifier: GetPlayerIdentifier(num),
name: GetPlayerName(num)
});
}
return players;
};
/**
* MRP - description
*
* @memberof MRP_SERVER
* @return {type} description
*/
MRP.getSpawnedCharacters = function() {
return MRP.playerSpawnedCharacters;
};
/**
* MRP - description
*
* @memberof MRP_SERVER
* @param {type} source description
* @return {type} description
*/
MRP.getSpawnedCharacter = function(source) {
return MRP.playerSpawnedCharacters[source];
};
/**
* MRP - description
*
* @memberof MRP_SERVER
* @param {type} source description
* @param {type} char description
* @return {type} description
*/
MRP.updateSpawnedChar = function(source, char) {
MRP.playerSpawnedCharacters[source] = char;
};
/**
* MRP - description
*
* @memberof MRP_SERVER
* @param {type} source description
* @return {type} description
*/
MRP.getEntityPosition = function(source) {
let retVal = [];
let plyPed = GetPlayerPed(source);
let plyPos = GetEntityCoords(plyPed);
let plyHeading = GetEntityHeading(plyPed);
if (plyPos && plyHeading) {
retVal = [plyPos[0], plyPos[1], plyPos[2], plyHeading];
}
return retVal;
};
/**
* MRP - description
*
* @memberof MRP_SERVER
* @param {type} id1 description
* @param {type} id2 description
* @return {type} description
*/
MRP.isObjectIDEqual = function(id1, id2) {
if (!id1 || !id2 || !id1.id || !id2.id)
return false;
let bufferArr = [];
for (let i in id1.id) {
bufferArr.push(id1.id[i]);
}
let idHash1 = Buffer.from(bufferArr).toString();
bufferArr = [];
for (let i in id2.id) {
bufferArr.push(id2.id[i]);
}
let idHash2 = Buffer.from(bufferArr).toString();
if (idHash1 == idHash2)
return true;
return false;
};
/**
* getConnectedUsers - returns all users connected
* @memberof MRP_SERVER
*/
var getConnectedUsers = () => connectedUsers;
on('mrp:getSharedObject', (cb) => {
cb(MRP);
});
onNet('mrp:characterSpawned', (char) => {
//TODO
});
on('onResourceStart', (resource) => {
let resName = GetCurrentResourceName();
if (resName != resource)
return;
let players = MRP.getPlayersServer();
for (let player of players) {
let userID = MRP.getUserId(player.id + "");
if (userID) {
emit('mrp:userLogin', player.name, player, userID);
}
}
});
on('mrp:db:connected', () => {
const inserBaseItems = async function() {
console.log(`Updating base items in DB`);
let connection = db.client;
const collection = connection.collection('item');
//prefill DB with default values
for (let k in baseItems) {
let baseItem = baseItems[k];
await collection.updateOne({
name: baseItem.name
}, {
$set: baseItem
}, {
upsert: true
});
logger.log(`Item [${baseItem.name}] updated`);
}
//remove loaded items from memory don't need them any more
baseItems = null;
}
inserBaseItems();
});
on('playerConnecting', (playerName, setKickReason, deferrals) => {
deferrals.defer();
let player = global.source;
deferrals.update(`Hello ${playerName}. Your steam ID is being checked.`)
logger.log(`Player connecting: ${playerName}`);
let userID = MRP.getUserId(player);
deferrals.done();
if (userID) {
emit('mrp:userLogin', playerName, player, userID);
}
});
on('onResourceStop', (resource) => {
if (resource == GetCurrentResourceName()) {
//TODO despawn characters to prevent "ghost" characters from running around
}
});
on('mrp:userJoined', (user) => {
connectedUsers[user._id] = user;
});
on("playerDropped", (reason) => {
let source = global.source;
logger.log(`Player ${GetPlayerName(source)} dropped (Reason: ${reason}).`)
let userID = MRP.getUserId(source);
if (userID) {
delete connectedUsers[userID];
}
});
onNet('mrp:server:tacklePlayer', (tackled, forvardVector, tackler) => {
console.log(`SERVER: tackled [${tackled}] forvardVector [${forvardVector}] tackler [${tackler}]`);
emitNet('mrp:client:tacklePlayer', tackled, forvardVector, tackler);
});
onNet('mrp:fetchCharacters', (source) => {
let execute = async function() {
let characters = await MRP.getCharacters(source);
emitNet('mrp:client:fetchCharacters', source, characters);
};
execute();
});
/**
* Character spawn event
* @event MRP_SERVER#mrp:spawn
* @type {object}
* @property {Character} char spawned character
* @property {Location} spawn spawn location
*/
onNet('mrp:useCharacter', (source, characterToUse) => {
if (!characterToUse)
return;
let objId = MRP.toObjectId(characterToUse._id.id);
characterToUse._id = objId;
//convert timestamp
characterToUse.birthday = MRP.toMongoTimestamp(characterToUse.birthday);
characterToUse.entityID = ENTITIES++;
let update = async function() {
let updatedUser = await MRP.setLastUsedCharacter(source, characterToUse);
let users = MRP.getConnectedUsers();
users[updatedUser._id] = updatedUser;
};
update();
let spawnPoint = {};
Object.assign(spawnPoint, config.spawnPoints[0]);
spawnPoint.model = characterToUse.model;
MRP.playerSpawnedCharacters[source] = characterToUse;
emitNet('mrp:spawn', source, characterToUse, spawnPoint);
emit('mrp:spawn', source, characterToUse, spawnPoint);
});
onNet('mrp:server:read:item', (source, name, uuid) => {
MRP.read('item', {
name: name
}, (res) => {
emitNet('mrp:server:read:item:response', source, res, uuid);
})
});
MRP.log = logger.log;
MRP.getConnectedUsers = getConnectedUsers;
exports('log', logger.log);
exports('DBCreate', MRP.create);
exports('DBRead', MRP.read);
exports('toObjectId', MRP.toObjectId);
//exports('getConnectedUsers', getConnectedUsers);