From 6cc708775e0940a891ab991e5b1de56df32690c7 Mon Sep 17 00:00:00 2001 From: ElCholoGamer Date: Mon, 1 Mar 2021 03:41:12 -0500 Subject: [PATCH] Create key codes object and replace body-parser (#20) * Add codes object to key listener * use express.json instead of body-parser * run fix script * fix tab indentation to spaces --- index.js | 45 ++++++---------- package.json | 143 +++++++++++++++++++++++++-------------------------- server.js | 3 +- 3 files changed, 89 insertions(+), 102 deletions(-) diff --git a/index.js b/index.js index cb01ba1..55cdb47 100644 --- a/index.js +++ b/index.js @@ -94,39 +94,28 @@ async function main () { }) document.addEventListener('contextmenu', (e) => e.preventDefault(), false) + + const codes = { + KeyW: 'forward', + KeyS: 'back', + KeyA: 'right', + KeyD: 'left', + Space: 'jump', + ShiftLeft: 'sneak', + ControlLeft: 'sprint' + } + document.addEventListener('keydown', (e) => { console.log(e.code) - if (e.code === 'KeyW') { - bot.setControlState('forward', true) - } else if (e.code === 'KeyS') { - bot.setControlState('back', true) - } else if (e.code === 'KeyA') { - bot.setControlState('right', true) - } else if (e.code === 'KeyD') { - bot.setControlState('left', true) - } else if (e.code === 'Space') { - bot.setControlState('jump', true) - } else if (e.code === 'ShiftLeft') { - bot.setControlState('sneak', true) - } else if (e.code === 'ControlLeft') { - bot.setControlState('sprint', true) + + if (e.code in codes) { + bot.setControlState(codes[e.code], true) } }, false) + document.addEventListener('keyup', (e) => { - if (e.code === 'KeyW') { - bot.setControlState('forward', false) - } else if (e.code === 'KeyS') { - bot.setControlState('back', false) - } else if (e.code === 'KeyA') { - bot.setControlState('right', false) - } else if (e.code === 'KeyD') { - bot.setControlState('left', false) - } else if (e.code === 'Space') { - bot.setControlState('jump', false) - } else if (e.code === 'ShiftLeft') { - bot.setControlState('sneak', false) - } else if (e.code === 'ControlLeft') { - bot.setControlState('sprint', false) + if (e.code in codes) { + bot.setControlState(codes[e.code], false) } }, false) document.addEventListener('mousedown', (e) => { diff --git a/package.json b/package.json index 967dbfc..c326826 100644 --- a/package.json +++ b/package.json @@ -1,76 +1,75 @@ { - "name": "prismarine-web-client", - "version": "1.1.0", - "description": "A minecraft client running in a browser", - "main": "index.js", - "scripts": { - "prepare": "webpack", - "start": "webpack serve", - "prod-start": "node server.js", - "build-start": "npm run prepare && npm run prod-start", - "lint": "standard", - "fix": "standard --fix", - "test": "npm run lint && mocha" + "name": "prismarine-web-client", + "version": "1.1.0", + "description": "A minecraft client running in a browser", + "main": "index.js", + "scripts": { + "prepare": "webpack", + "start": "webpack serve", + "prod-start": "node server.js", + "build-start": "npm run prepare && npm run prod-start", + "lint": "standard", + "fix": "standard --fix", + "test": "npm run lint && mocha" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/PrismarineJS/prismarine-web-client.git" + }, + "keywords": [ + "prismarine", + "web", + "client" + ], + "author": "Romain Beaumont", + "contributors": [ + { + "name": "AwesomestCode" }, - "repository": { - "type": "git", - "url": "git+https://github.com/PrismarineJS/prismarine-web-client.git" - }, - "keywords": [ - "prismarine", - "web", - "client" - ], - "author": "Romain Beaumont", - "contributors": [ - { - "name": "AwesomestCode" - }, - { - "name": "circuit10" - } - ], - "license": "MIT", - "bin": { - "prismarine-web-client": "./server.js" - }, - "bugs": { - "url": "https://github.com/PrismarineJS/prismarine-web-client/issues" - }, - "homepage": "https://github.com/PrismarineJS/prismarine-web-client#readme", - "dependencies": { - "body-parser": "^1.19.0", - "compression": "^1.7.4", - "express": "^4.17.1", - "net-browserify": "^0.2.4", - "request": "^2.88.2" - }, - "devDependencies": { - "assert": "^2.0.0", - "browserify-zlib": "^0.2.0", - "buffer": "^6.0.3", - "clean-webpack-plugin": "^3.0.0", - "constants-browserify": "^1.0.0", - "copy-webpack-plugin": "^7.0.0", - "crypto-browserify": "^3.12.0", - "events": "^3.2.0", - "http-browserify": "^1.7.0", - "http-server": "^0.12.3", - "https-browserify": "^1.0.0", - "lodash-webpack-plugin": "^0.11.6", - "memfs": "^3.2.0", - "mineflayer": "^2.39.2", - "mocha": "^8.3.0", - "os-browserify": "^0.3.0", - "path-browserify": "^1.0.1", - "prismarine-viewer": "^1.14.0", - "process": "^0.11.10", - "standard": "^16.0.3", - "stream-browserify": "^3.0.0", - "three": "^0.124.0", - "timers-browserify": "^2.0.12", - "webpack": "^5.11.0", - "webpack-cli": "^4.2.0", - "webpack-dev-server": "^3.11.0" + { + "name": "circuit10" } + ], + "license": "MIT", + "bin": { + "prismarine-web-client": "./server.js" + }, + "bugs": { + "url": "https://github.com/PrismarineJS/prismarine-web-client/issues" + }, + "homepage": "https://github.com/PrismarineJS/prismarine-web-client#readme", + "dependencies": { + "compression": "^1.7.4", + "express": "^4.17.1", + "net-browserify": "^0.2.4", + "request": "^2.88.2" + }, + "devDependencies": { + "assert": "^2.0.0", + "browserify-zlib": "^0.2.0", + "buffer": "^6.0.3", + "clean-webpack-plugin": "^3.0.0", + "constants-browserify": "^1.0.0", + "copy-webpack-plugin": "^7.0.0", + "crypto-browserify": "^3.12.0", + "events": "^3.2.0", + "http-browserify": "^1.7.0", + "http-server": "^0.12.3", + "https-browserify": "^1.0.0", + "lodash-webpack-plugin": "^0.11.6", + "memfs": "^3.2.0", + "mineflayer": "^2.39.2", + "mocha": "^8.3.0", + "os-browserify": "^0.3.0", + "path-browserify": "^1.0.1", + "prismarine-viewer": "^1.14.0", + "process": "^0.11.10", + "standard": "^16.0.3", + "stream-browserify": "^3.0.0", + "three": "^0.124.0", + "timers-browserify": "^2.0.12", + "webpack": "^5.11.0", + "webpack-cli": "^4.2.0", + "webpack-dev-server": "^3.11.0" + } } diff --git a/server.js b/server.js index 6365ca0..8013fed 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,6 @@ const express = require('express') const netApi = require('net-browserify') -const bodyParser = require('body-parser') const request = require('request') const compression = require('compression') const path = require('path') @@ -30,7 +29,7 @@ app.use(compression()) app.use(netApi()) app.use(express.static(path.join(__dirname, './public'))) -app.use(bodyParser.json({ limit: '100kb' })) +app.use(express.json({ limit: '100kb' })) app.all('*', function (req, res, next) { // Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment