-
Notifications
You must be signed in to change notification settings - Fork 0
/
arcade.js
550 lines (376 loc) · 10.6 KB
/
arcade.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// NATIVE PACKAGES
const host = require('os').hostname()
const express = require('express')
const http = require('http')
const fs = require('fs')
const os = require('os')
// LOCAL PACKAGES
const log = require('./server/log.js')
const DB = require('./server/db.js')
// const config = require('./config.js')
const env = require('./server/.env.js')
// const env = require(!fs.existsSync("env.js") ? './default_env.js' : './.env.js')
// NPM
const bodyParser = require('body-parser')
const session = require('express-session')
const cookie = require('cookie')
const cookieParser = require('cookie-parser')
const FormData = require('express-form-data')
const mkdirp = require('mkdirp')
const uuid = require('uuid').v4
const User = require('./server/User.js')
const MemoryStore = require('memorystore')(session)
// const uuid = require('uuid').v4
const GAME = require('./server/GAME.js')
// const ROUTER = require('./server/ROUTER.js')
const SOCKETS = require('./server/SOCKETS.js')
// const lib = require('./lib.js')
const WSS = require('./server/WSS.js')
const MAP = require('./server/MAP.js')
const auth = require('./server/auth.js')
// const readline = require('readline')
log('call', 'mud.js')
const STORE = new MemoryStore({
checkPeriod: 1000 * 60 * 60 * 24 * 2// prune expired entries every 24h
})
const render = require('./client/arcade_html.js')
// CACHED SESSIONS
const lru_session = session({
cookie: { maxAge: 1000 * 60 * 60 * 24 * 2 },
resave: false,
saveUninitialized: true,
store: STORE,
secret: env.SECRET
})
// const version = 14
const gatekeep = function(req, res, next) {
if( req.path.match(/\/resources/) || req.path.match(/\/client/) || req.path.match(/favicon/)){
// log('flag', 'resource: ', req.path )
next()
}else{
// log('flag', 'old blorble: ', req.session.blorble )
// const hhttp_b = 'heres an http blorble: ' + Date.now()
// log('flag', 'saved to http blorble: ', hhttp_b )
// req.session.blorble = hhttp_b
// req.session.USER = new User( req.session.USER )
log('gatekeep', req.path )
next()
}
}
const exp = new express()
const server = http.createServer( exp )
// const upload = multer({
// dest: env.UPLOAD_DIR
// })
const FormData_options = {
uploadDir: os.tmpdir(),
autoClean: true
}
// parse data with connect-multiparty.
exp.use( FormData.parse( FormData_options ) )
// delete from the request all empty files (size == 0)
exp.use( FormData.format() )
// change the file objects to fs.ReadStream
// exp.use( FormData.stream() )
// union the body and the files
// exp.use( FormData.union() )
// HTTP ROUTER
// exp.set( 'port', env.PORT )
exp.use('/client', express.static( './client' )) // __dirname +
exp.use('/resources', express.static( './resources' )) // __dirname +
exp.use('/fs', express.static( '/fs' )) // __dirname +
exp.use('/favicon.ico', express.static( '/resources/favicon.ico') )
exp.use( bodyParser.json({
type: 'application/json'
}))
exp.use( bodyParser.urlencoded({
extended: true
}));
// exp.use( upload.array() )
exp.use( lru_session )
exp.use( gatekeep )
// routing
exp.get('/', function(request, response) {
response.send( render( 'index', request ) )
})
// exp.get('/test', function( request, response) {
// log('flag', Object.keys(h).length )
// })
exp.post('/register', function( request, response ){
auth.register_user( request )
.then( success => {
if( success ){
response.send( render( 'avatar' ) )
}else{
response.send( render( 'index' ) )
}
}).catch( err => {
log('flag', 'err register: ', err )
response.send( render('index') )
})
})
exp.post('/login', function( request, response ){
auth.login_user( request )
.then( success => {
if( success ){
response.send( render( 'avatar' ) )
}else{
response.send( render( 'index' ) )
}
}).catch( err => {
log('flag', 'err login: ', err )
response.send( render('index') )
})
})
exp.post('/arcade', function( request, response ){
response.send( render('arcade') )
// auth.login_user( request )
// .then( success => {
// if( success ){
// response.send( render( 'avatar' ) )
// }else{
// response.send( render( 'index' ) )
// }
// }).catch( err => {
// log('flag', 'err login: ', err )
// response.send( render('index') )
// })
})
exp.post('/logout', function( request, response ){
auth.logout( request )
.then( res => {
// includes success false's :
response.json( res )
}).catch( err => {
log('flag', 'err logout: ', err )
response.json({
success: false,
msg: 'error logging out'
})
})
})
exp.post('/update', function( request, response ){
auth.update( request )
.then( res => {
// includes success false's :
response.json( res )
}).catch( err => {
log('flag', 'err update: ', err )
response.json({
success: false,
msg: 'error updating'
})
})
})
// exp.post('/img_handler', function( request, response ){
// if( request.files && request.session.USER.name ){
// const FILE = request.files.upload
// const slot_arc_id = request.body.slot_arc_id
// const title = request.body.title
// const description = request.body.description
// const tempPath = FILE.path
// const file_URL = `${ Date.now() }__${ FILE.originalFilename }`
// const arc_idPath = env.UPLOAD_DIR + request.session.USER.arc_id
// const finalPath = arc_idPath + '/' + file_URL
// if( '(check valid)' == '(check valid)' ){
// mkdirp( arc_idPath, { mode: '0744' })
// .then( res => {
// // fs.rename( tempPath, finalPath, err => {
// fs.rename( tempPath, finalPath, err => {
// if( err ) {
// log('flag', 'filepath err: ', err)
// response.status(500).end()
// }
// // log('flag', 'request.session.USER.website: ', request.session.USER )
// GAME.add_img_upload( request.session.USER.arc_id, slot_arc_id, file_URL, description, title )
// .then( res => {
// if( res.success ){
// response.json({
// success: true
// })
// }else{
// log('flag', 'new img fail: ', res )
// response.json({
// success: false,
// msg: 'failed to set image'
// })
// }
// }).catch( err => { log('flag', 'error setting image: ', err ) })
// })
// }).catch( err => {
// log('flag', 'err saving upload', err )
// response.status(500).end()
// return false
// })
// }else{
// fs.unlink( tempPath, err => {
// if( err ) return false
// response.status(403).contentType('text/plain').end('invalid file upload')
// })
// }
// }else{
// log('flag', 'no file')
// response.end()
// }
// })
exp.post('*', function(request, response){
log('router', '404 POST: ' + request.url)
if( request.url.match(/\.html$/) ){
response.send( render('gabbagabbahey'))
}else{
response.end()
}
})
exp.get('*', function(request, response){
log('router', '404 GET: ' + request.url)
if( request.url.match(/\.html$/) ){
response.send( render('jargonyjargon'))
}else{
response.end()
}
})
DB.initPool(( err, pool ) => {
if( err ) return console.error( 'no db: ', err )
log('db', 'init:', Date.now() )
server.listen( env.PORT, function() {
log( 'boot', `\n
----------------------\n
----------------------\n
----------------------\n
----------------------\n
----------------------\n
\n ${ host }: ${ env.PORT }
\n ${ Date.now() }
\n` )
})
server.on('upgrade', function( request, socket, head ){
lru_session( request, {}, () => {
// if ( !request.session.USER ) {
// log('flag', 'no user, aborting')
// socket.destroy()
// return
// }
log('wss', 'session parsed')
WSS.handleUpgrade( request, socket, head, function( ws ) {
WSS.emit('connection', ws, request )
})
})
})
WSS.on('connection', function connection( socket, req ) {
// log('wss', 'socket connection ', req.session.USER.name )
socket.request = req
// log('wss', 'stuff...' )///socket.request.session.save
// USER not a User yet...
// const this_save = 'savd from socket: ' + Date.now()
// socket.request.session.blorble = this_save
// socket.request.session.save( function(){
// console.log('saved......', this_save )
// })
if( Object.keys( SOCKETS ).length >= env.MAX_CONNECTIONS ) {
log('flag', 'max capacity')
return false
}
if( !GAME.pulse ) {
if( GAME.opening ){ // extremely unlikely
socket.send(JSON.stringify({
type: 'error',
msg: 'tried to join during server start - try again in a few seconds'
}))
}else{
log('arcade', 'WUNDERBAR, OPENING ARCADE')
GAME.opening = true
GAME.init_async_elements()
.then( res => {
GAME.opening = false
GAME.init_sync_elements()
GAME.init_user( socket )
.catch( err => {
log('flag', 'err init user:', err )
})
})
.catch( err => {
log('flag', 'err opening GAME: ', err )
})
}
}else{
GAME.init_user( socket )
}
})
})
if( env.ACTIVE.serverlog ){
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'serverlog> \n'
})
setTimeout(function(){
rl.prompt()
}, 500)
// let readline_last = []
rl.on('line', ( line ) => {
if( env.READLINE_LITERAL ){ // Mac terminal reads the characters outputted by 'up' literally (?)
// switch ( line.trim() ) {
// case '^[[A':
// log('flag', 'wut')
// try_readline( readline_last[0] )
// break
// default:
// readline_last.unshift( line.trim() )
// break
// }
}else{
try_readline( line.trim() )
}
rl.prompt()
}).on('close', () => {
process.exit( 0 )
})
}
function try_readline( msg ){
try{
log( 'serverlog', eval(`${ msg }`) ) //), '\n(command): ' + String( msg ) )
log( 'serverlog', String( msg ) )
}catch( e ){
log('serverlog', 'fail: ', e )
}
}
function hal( type, msg, time ){
for( const arc_id of Object.keys( SOCKETS ) ){
SOCKETS[ arc_id ].send(JSON.stringify({
type: 'hal',
hal_type: type,
msg: msg,
time: time
}))
}
}
// function heap(){
// const mem = process.memoryUsage()
// for (const key in mem) {
// console.log(`${key} ${Math.round(mem[key] / 1024 / 1024 * 100) / 100} MB`);
// }
// }
// class Mapcell {
// constructor( init ){
// this.id = init.id
// this.x = init.x
// this.y = init.y
// this.z = init.z
// }
// }
// let h = {};
// (function( width, height ){
// const total = width * height
// let id
// for( let i = 0; i < total; i++ ){
// id = uuid()
// h[ id ] = new Number()
// // Mapcell({
// // id: id,
// // x: i % width,
// // y: Math.random(),
// // z: Math.floor( i / width )
// // })
// }
// })( 1000, 1000 )