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
This commit is contained in:
ElCholoGamer 2021-03-01 03:41:12 -05:00 committed by GitHub
parent 0b2a3e7950
commit 6cc708775e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 102 deletions

View file

@ -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) => {

View file

@ -39,7 +39,6 @@
},
"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",

View file

@ -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