This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
435 lines (324 loc) · 12.2 KB
/
index.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const fs = require('fs')
const {createClient, constants} = require('oakwood')
const {vehicleModels, playerModels} = require('oakwood')
const {
VISIBILITY_NAME,
VISIBILITY_ICON,
VISIBILITY_RADAR,
VISIBILITY_MODEL,
VISIBILITY_COLLISION,
} = constants
const oak = createClient()
/* get random element from an array */
const rndarr = arr => arr[Math.floor(Math.random()*arr.length)]
oak.event('start', () => {
console.log('[info] connection started')
oak.log('[info] oakwood-node connected')
})
oak.event('stop', () => console.log('[info] connection stopped'))
oak.event("vehicleUse", async (veh, pid, success, seatId, enterOrLeave) => {
if (success) return;
oak.hudMessage(pid, "This vehicle is locked!", 0xff0000)
})
/* chat system */
oak.event('playerChat', async (pid, text) => {
/* skip messages with commands */
if (text.startsWith('/'))
return;
/* get author player name */
const name = await oak.playerNameGet(pid)
const msg = `[chat] ${name}: ${text}`
/* log stuff into our local console */
console.log('[chat]', `${name}:`, text)
/* send messages to each clients' chat windows */
oak.chatBroadcast(msg)
})
oak.event('unknownCommand', (pid, text) => {
oak.chatSend(pid, `[error] unknown command: ${text}`)
})
oak.cmd('clear', (pid) => {
oak.chatSend(pid, '\n'.repeat(12))
})
/* Player system */
const spawnLocs = [
{ name: "pete", pos: [61.4763, 4.72524, 107.708 ]},
{ name: "tommy", pos: [8.62861251831, 22.8868865967, -602.147888184 ]},
{ name: "oakhill", pos: [738.030334473, 106.889381409, -228.563537598 ]},
{ name: "hoboken", pos: [537.296386719, -5.01502513885, 77.8488616943 ]},
{ name: "downtown", pos: [-188.796401978, 18.6846675873, -668.6328125 ]},
{ name: "hospital", pos: [-760.439697266, 12.6204996109, 753.350646973 ]},
{ name: "central", pos: [-1091.47839355, -7.27131414413, 5.55286931992 ]},
{ name: "china", pos: [-1709.24157715, 16.0029373169, 582.041442871 ]},
{ name: "salieri", pos: [-1774.59301758, -4.88487052917, -2.40491962433 ]},
{ name: "work", pos: [-2550.85546875, -3.96487784386, -554.806213379 ]},
{ name: "racing", pos: [-3534.42993164, 7.05113887787, -651.97338867 ]},
]
const spawnplayer = pid => {
const loc = rndarr(spawnLocs)
const model = rndarr(playerModels)
oak.chatSend(pid, `[info] spawning you at location: ${loc.name}`)
oak.playerModelSet(pid, model[1])
oak.playerHealthSet(pid, 200)
oak.playerSpawn(pid, loc.pos, 0.0)
oak.hudFadeout(pid, 1, 500, 0xFFFFFF)
oak.hudFadeout(pid, 0, 500, 0xFFFFFF)
}
oak.event('playerConnect', async pid => {
console.log('[info] player connected', pid)
oak.chatBroadcast(`[info] player ${await oak.playerNameGet(pid)} connected.`)
oak.tempWeaponsSpawn(pid)
spawnplayer(pid)
})
oak.event('playerDeath', async pid => {
setTimeout(() => spawnplayer(pid), 5000)
oak.chatBroadcast(`[info] player ${await oak.playerNameGet(pid)} died.`)
})
oak.event('playerDisconnect', async pid => {
oak.chatBroadcast(`[info] player ${await oak.playerNameGet(pid)} disconnected.`)
console.log(`[info] player ${await oak.playerNameGet(pid)} disconnected.`)
})
oak.event('playerHit', (pid, atkr, dmg) => {
// console.log('[info] playerHit', pid, atkr, dmg)
})
oak.cmd('spawn', async pid => {
spawnplayer(pid)
})
oak.cmd('weapons', pid => {
oak.tempWeaponsSpawn(pid)
})
oak.cmd('despawn', async pid => {
oak.playerDespawn(pid)
})
oak.cmd('kill', async pid => {
oak.playerKill(pid)
})
oak.cmd('help', async (pid) => {
const commands = [
'/help - shows this message',
'/clear - clears chat box',
'/spawn - respawns you at random spawn location',
'/despawn - despawns the local player',
'/heal or /healme - heals your player',
'/id - prints your player id',
'/list - prints current online players',
'/tp ID - teleport to a player with provided id',
'/tele NAME - teleport to a location (use /telelist to get locations)',
'/telelist - prints list of possible locations to teleport to',
'/weapons - gives you new set of weapons',
'/skin SKINID sets a specific skin model for your player',
'/car MODELID - creates car near player with specified model',
'/putcar - creates a car on player position with specified model, and puts him inside',
'/delcar - deletes a car you are curently in (only for cars created by you)',
'/fuel AMOUNT - sets fuel level in the car',
'/lock VALUE - set vehicle lock on or off (0 - to unlock, 1 - to lock)',
'/spectate PLAYERID - enable specator mode, and follow specified player',
'/stop - disable spectating mode',
]
oak.chatSend(pid, `Help:\n----------------\n${commands.join('\n')}`)
})
oak.cmd('heal', async (pid) => {
oak.playerHealthSet(pid, 200.0)
})
oak.cmd('healme', async (pid) => {
oak.playerHealthSet(pid, 200.0)
})
oak.cmd('lock', async (pid, state) => {
const veh = await oak.vehiclePlayerInside(pid)
if (await oak.vehicleInvalid(veh)) return;
state = parseInt(state)
stateMsg = (state === 0) ? "unlocked" : "locked"
oak.chatSend(pid, `Vehicle is now ${stateMsg}!`)
oak.vehicleLockSet(veh, state)
})
oak.cmd('id', pid => {
oak.chatSend(pid, `[info] your ID is: ${pid}`)
})
oak.cmd('tp', async (pid, targetid) => {
const tid = parseInt(targetid)
if (tid === NaN) {
return oak.chatSend(pid, `[error] provided argument should be a valid number`)
}
if (pid == tid) {
return oak.chatSend(pid, `[error] you can't teleport to yourself`)
}
if (await oak.playerInvalid(tid)) {
return oak.chatSend(pid, `[error] player you provided was not found`)
}
/* get target name and position */
const pos = await oak.playerPositionGet(tid)
const name = await oak.playerNameGet(tid)
/* are we in any vehicle */
const veh = await oak.vehiclePlayerInside(pid)
if (!await oak.vehicleInvalid(veh)) {
/* offset by height */
pos[1] += 2;
oak.chatSend(pid, `[info] teleporting your car to player ${name}.`)
/* set our vehicle position */
oak.vehiclePositionSet(veh, pos)
} else {
oak.chatSend(pid, `[info] teleporting you to player ${name}.`)
/* set our player position */
oak.playerPositionSet(pid, pos)
}
})
oak.cmd('list', async pid => {
const players = await oak.playerList()
oak.chatSend(pid, `[info] connected players:`)
players.map(async (tid, i) => oak.chatSend(pid, `ID: ${tid} | ${await oak.playerNameGet(tid)}`))
oak.chatSend(pid, '---------------------------')
})
oak.cmd('skin', async (pid, arg1) => {
if (!arg1) {
return oak.chatSend(pid, '[info] usage: /skin [modelId]')
}
const veh = await oak.vehiclePlayerInside(pid)
if (!await oak.vehicleInvalid(veh)) {
return oak.chatSend(pid, `[error] you can't change skin inside of vehicle!`)
}
const modelid = parseInt(arg1)
oak.playerModelSet(pid, playerModels[modelid][1])
})
oak.cmd('telelist', async (pid) => {
oak.chatSend(pid, `Location names for /tele :`)
spawnLocs.map((a, i) => {
oak.chatSend(pid, `${i}. ${a.name}`)
})
})
oak.cmd('tele', async (pid, name) => {
const location = spawnLocs.find(el => el.name == name)
if (!location) {
return oak.chatSend(pid, `[error] cound't find any locations by given name, use /telelist`)
}
oak.chatSend(pid, `[info] teleporting your car to a location ${location.name}.`)
/* are we in any vehicle */
const veh = await oak.vehiclePlayerInside(pid)
if (!await oak.vehicleInvalid(veh)) {
oak.vehiclePositionSet(veh, location.pos)
} else {
oak.playerPositionSet(pid, location.pos)
}
})
/* Spectating system */
oak.cmd('hideme', async (pid) => {
const nameVisible = await oak.playerVisibilityGet(pid, VISIBILITY_NAME)
const iconVisible = await oak.playerVisibilityGet(pid, VISIBILITY_ICON)
oak.playerVisibilitySet(pid, VISIBILITY_NAME, !nameVisible)
oak.playerVisibilitySet(pid, VISIBILITY_ICON, !iconVisible)
})
oak.cmd('spectate', async (pid, arg1) => {
const tid = parseInt(arg1)
if (await oak.playerInvalid(tid)) {
return oak.chatSend(pid, `[error] unknown player target`)
}
oak.cameraTargetPlayer(pid, tid)
})
oak.cmd('stop', async (pid) => {
oak.cameraTargetUnset(pid)
})
/* Vehicles */
let playerVehicles = {}
let playerVehiclesValid = (pid, vid) => playerVehicles.hasOwnProperty(pid)
? playerVehicles[pid].indexOf(vid) !== -1
: false
let playerVehiclesAdd = (pid, vid) => {
if (!playerVehicles.hasOwnProperty(pid)) {
playerVehicles[pid] = []
}
playerVehicles[pid].push(vid)
}
oak.event('start', async () => {
const existing = await oak.vehicleList()
/* despawn all empty vehicles */
if (existing.length > 0) {
console.log('[info] found', existing.length, 'existing cars on start-up')
for (var i = 0; i < existing.length; i++) {
const veh = existing[i]
const pass = await oak.vehiclePlayerList(veh)
if (pass.length == 0) {
await oak.vehicleDespawn(veh)
} else {
console.log('[info] skipping respawn for occupied vehicle', veh)
}
}
}
let models = [
{pos: [-1991.89, -5.09753, 10.4476], heading: 0.0, model: 148}, // Manta Prototype
{pos: [-1974.2, -4.8862, 22.5578], heading: 0.0, model: 148}, // Manta Prototype
{pos: [-1981.11, -4.98206, 22.7471], heading: 0.0, model: 148}, // Manta Prototype
{pos: [-1991.69, -5.12453, 22.3242], heading: 0.0, model: 148}, // Manta Prototype
]
const data = fs
.readFileSync('./savedcars.txt', 'utf8')
.split('\n')
.filter(line => line.trim().length)
.map(line => line.split(' '))
.map(line => {
const [model, heading, x, y, z] = line;
return {
heading: parseFloat(heading),
model: parseInt(model),
pos: [
parseFloat(x),
parseFloat(y),
parseFloat(z),
]
}
})
models.concat(data).map(async car => {
const {pos, heading, model} = car;
oak.vehicleSpawn(vehicleModels[model][1], pos, heading)
})
})
const spawncar = async (pid, model, adjustPos) => {
const m = parseInt(model)
if (m === NaN) {
return oak.chatSend(pid, `[error] provided argument should be a valid number`)
}
oak.chatSend(pid, `[info] spawning vehicle model ${vehicleModels[m][0]}`)
let pos = await oak.playerPositionGet(pid)
let heading = await oak.playerHeadingGet(pid)
if (adjustPos === true) {
let dir = await oak.playerDirectionGet(pid)
pos = pos.map((p, i) => p + dir[i] * 1.5)
heading -= 90.0
}
const veh = await oak.vehicleSpawn(vehicleModels[m][1], pos, heading)
playerVehiclesAdd(pid, veh)
return veh
}
oak.cmd('car', async (pid, model) => {
spawncar(pid, model, true)
})
oak.cmd('putcar', async (pid, model) => {
const veh = await spawncar(pid, model, false)
oak.vehiclePlayerPut(veh, pid, 0)
})
/*oak.cmd('repair', async pid => {
const veh = await oak.vehiclePlayerInside(pid)
if (await oak.vehicleInvalid(veh)) return;
oak.vehicleRepair(veh)
})*/
oak.cmd('delcar', async (pid) => {
const veh = await oak.vehiclePlayerInside(pid)
if (await oak.vehicleInvalid(veh)) {
return oak.chatSend(pid, '[error] you are not in a vehicle')
}
if (!playerVehiclesValid(pid, veh)) {
return oak.chatSend(pid, `[error] you can't remove car not spawned by you`)
}
oak.vehicleDespawn(veh)
oak.chatSend(pid, `[info] car has been successfully removed`)
})
oak.cmd('fuel', async (pid, arg1 = 10.0) => {
const fuel = parseFloat(arg1)
const veh = await oak.vehiclePlayerInside(pid)
if (await oak.vehicleInvalid(veh)) return;
oak.vehicleFuelSet(veh, fuel)
})
oak.cmd('race', async (pid, flags) => {
const f = parseInt(flags)
if (f === NaN) {
return oak.chatSend(pid, '[error] pakuj do pici')
}
oak.hudCountdown(pid, f)
})