Update code readability (#370)

This commit is contained in:
andriycraft 2023-04-16 00:54:29 +02:00 committed by GitHub
commit e5b8cc8bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,7 @@ const debug = globalThis.isElectron ? console.debug : require('debug')('minecraf
class Server extends EventEmitter {
constructor (options) {
super()
this.options = { ...Options.defaultOptions, ...options }
this.validateOptions()
@ -27,19 +28,23 @@ class Server extends EventEmitter {
}
setCompressor (algorithm, level = 1, threshold = 256) {
if (algorithm === 'none') {
this.compressionAlgorithm = 'none'
this.compressionLevel = 0
} else if (algorithm === 'deflate') {
this.compressionAlgorithm = 'deflate'
this.compressionLevel = level
this.compressionThreshold = threshold
} else if (algorithm === 'snappy') {
this.compressionAlgorithm = 'snappy'
this.compressionLevel = level
this.compressionThreshold = threshold
} else {
throw new Error(`Unknown compression algorithm ${algorithm}`)
switch (algorithm) {
case 'none':
this.compressionAlgorithm = 'none'
this.compressionLevel = 0
break
case 'deflate':
this.compressionAlgorithm = 'deflate'
this.compressionLevel = level
this.compressionThreshold = threshold
break
case 'snappy':
this.compressionAlgorithm = 'snappy'
this.compressionLevel = level
this.compressionThreshold = threshold
break
default:
throw new Error(`Unknown compression algorithm: ${algorithm}`)
}
}
@ -60,7 +65,8 @@ class Server extends EventEmitter {
}
onOpenConnection = (conn) => {
this.conLog('new connection', conn?.address)
this.conLog('New connection: ', conn?.address)
const player = new Player(this, conn)
this.clients[conn.address] = player
this.clientCount++
@ -68,7 +74,8 @@ class Server extends EventEmitter {
}
onCloseConnection = (inetAddr, reason) => {
this.conLog('close connection', inetAddr?.address, reason)
this.conLog('Connection closed: ', inetAddr?.address, reason)
delete this.clients[inetAddr]?.connection // Prevent close loop
this.clients[inetAddr?.address ?? inetAddr]?.close()
delete this.clients[inetAddr]
@ -79,9 +86,10 @@ class Server extends EventEmitter {
const client = this.clients[address]
if (!client) {
// Ignore packets from clients that are not connected.
debug(`ignoring packet from unknown inet addr: ${address}`)
debug(`Ignoring packet from unknown inet address: ${address}`)
return
}
process.nextTick(() => client.handle(buffer))
}
@ -89,18 +97,21 @@ class Server extends EventEmitter {
if (this.options.advertisementFn) {
return this.options.advertisementFn()
}
this.advertisement.playersOnline = this.clientCount
return this.advertisement
}
async listen (host = this.options.host, port = this.options.port) {
this.raknet = new this.RakServer({ host, port }, this)
try {
await this.raknet.listen()
} catch (e) {
console.warn(`Failed to bind server on [${this.options.host}]/${this.options.port}, is the port free?`)
throw e
}
this.conLog('Listening on', host, port, this.options.version)
this.raknet.onOpenConnection = this.onOpenConnection
this.raknet.onCloseConnection = this.onCloseConnection
@ -114,7 +125,7 @@ class Server extends EventEmitter {
return { host, port }
}
async close (disconnectReason) {
async close (disconnectReason = 'Server closed') {
for (const caddr in this.clients) {
const client = this.clients[caddr]
client.disconnect(disconnectReason)