Skip to content

Commit

Permalink
Secret key form (#5)
Browse files Browse the repository at this point in the history
* Secret key form

* lets hope its not broken this time

Co-authored-by: Dexter Leng <dexterlengcs@gmail.com>
  • Loading branch information
juxd and dexterleng committed Jan 8, 2021
1 parent f3c8381 commit d5f20e0
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 206 deletions.
10 changes: 2 additions & 8 deletions client/GameClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ import PIXIRenderer from './graphics/PIXIRenderer'
const ALLOW_ROTATION = false

class GameClient {
constructor(secret) {
this.client = new nengi.Client(nengiConfig)
constructor(client) {
this.client = client
this.renderer = new PIXIRenderer()
this.input = new InputSystem()

this.client.onConnect(res => {
console.log('onConnect response:', res)
})

this.client.onClose(() => {
console.log('connection closed')
})

this.client.connect('ws://localhost:8079', { secret })
}

update(delta, tick, now) {
Expand Down
67 changes: 62 additions & 5 deletions client/clientMain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
import main from './runClient';
import GameClient from './GameClient'
import instantiateClient from './instantiateClient'

window.onload = function() {
console.log('window loaded')
const secret = 'MAGIC_VALUE';
main(secret);
const SECRET_KEY_FORM_ID = 'secret-key-form'

window.onload = function () {
const container = document.getElementById('container')
container.style.height = '100%'
container.style.width = '100%'
container.style.position = 'absolute'

container.insertAdjacentHTML('beforeend', renderSecretKeyForm())
}

function renderSecretKeyForm() {
return `
<form id="${SECRET_KEY_FORM_ID}">
<label for="secret-key">Secret Key:</label><br>
<input type="text" id="secret-key" name="secret-key"><br>
<input type="submit" value="Submit">
</form>
`
}

let gameClient

function handleSecretKeyFormSubmit(e) {
const secretKey = document.getElementById('secret-key').value

instantiateClient(secretKey)
.then(client => {
const canvas = '<canvas id=\'main-canvas\' style="height: 100%; width: 100%"></canvas>'
container.innerText = ''
container.insertAdjacentHTML('beforeend', canvas)
gameClient = new GameClient(client)
let tick = 0
let previous = performance.now()
const loop = function () {
window.requestAnimationFrame(loop)
const now = performance.now()
const delta = (now - previous) / 1000
previous = now
tick++

gameClient.update(delta, tick, now)
}

loop()
})
.catch(res => {
alert('shit secret key')
})
}

document.addEventListener('submit', e => {
e.preventDefault()

const form = e.target
const formId = form.getAttribute('id')

if (formId === SECRET_KEY_FORM_ID) {
handleSecretKeyFormSubmit(e)
}
}, false)
21 changes: 21 additions & 0 deletions client/instantiateClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import nengi from 'nengi'
import nengiConfig from '../common/nengiConfig'

function instantiateClient(secret) {
return new Promise((resolve, reject) => {
const client = new nengi.Client(nengiConfig)

client.onConnect(res => {
console.log('onConnect response:', res)
if (!res.accepted) {
reject(res)
} else {
resolve(client)
}
})

client.connect('ws://localhost:8079', { secret })
})
}

export default instantiateClient
22 changes: 0 additions & 22 deletions client/runClient.js

This file was deleted.

2 changes: 1 addition & 1 deletion common/gameConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const config = {
MAP_IMAGE: './images/bg.png',
}

export default config
export default config
12 changes: 6 additions & 6 deletions extension/src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.bundle.js'
});
});
chrome.browserAction.onClicked.addListener(tab => {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.bundle.js',
})
})
83 changes: 39 additions & 44 deletions extension/src/js/inject.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import nengiConfig from '../../../common/nengiConfig';
import main from '../../../client/runClient';
import nengiConfig from '../../../common/nengiConfig'
import main from '../../../client/runClient'

console.log(nengiConfig);
const SECRET_KEY_FORM_ID = 'secret-key-form';
console.log(nengiConfig)
const SECRET_KEY_FORM_ID = 'secret-key-form'

let container;

(function() {
const discordElement = document.getElementById("app-mount");
(function () {
const discordElement = document.getElementById('app-mount')

const body = discordElement.parentElement;
const body = discordElement.parentElement

discordElement.style.height = '60%';
discordElement.style.float = 'top';
discordElement.style.height = '60%'
discordElement.style.float = 'top'

container = document.createElement("div");
container.id = "game-container";
container.style.height = '40%';
container.style.width = '100%';
container.style.position = 'absolute';
container.style.top = '60%';
container.style.backgroundColor = 'white';
container = document.createElement('div')
container.id = 'game-container'
container.style.height = '40%'
container.style.width = '100%'
container.style.position = 'absolute'
container.style.top = '60%'
container.style.backgroundColor = 'white'

container.insertAdjacentHTML('beforeend', renderSecretKeyForm());
container.insertAdjacentHTML('beforeend', renderSecretKeyForm())

// const canvas = document.createElement('canvas');
// canvas.id = 'game-canvas';
// canvas.style.height = '100%';
// canvas.style.width = '100%';
// canvas.style.backgroundColor = 'red';
// const canvas = document.createElement('canvas');
// canvas.id = 'game-canvas';
// canvas.style.height = '100%';
// canvas.style.width = '100%';
// canvas.style.backgroundColor = 'red';

body.appendChild(container);
// container.appendChild(canvas)
})();
body.appendChild(container)
// container.appendChild(canvas)
}())

function renderSecretKeyForm() {
return `
return `
<form id="${SECRET_KEY_FORM_ID}">
<label for="secret-key">Secret Key:</label><br>
<input type="text" id="secret-key" name="secret-key"><br>
Expand All @@ -45,27 +45,22 @@ function renderSecretKeyForm() {
}

function handleSecretKeyFormSubmit(e) {
const secretKey = document.getElementById('secret-key').value
const secretKey = document.getElementById('secret-key').value

const canvas = `<canvas id='main-canvas' style="height: 100%; width: 100%"></canvas>`;
container.innerText = "";
container.insertAdjacentHTML('beforeend', canvas);

main(secretKey);
const canvas = '<canvas id=\'main-canvas\' style="height: 100%; width: 100%"></canvas>'
container.innerText = ''
container.insertAdjacentHTML('beforeend', canvas)

main(secretKey)
}

document.addEventListener('submit', function (e) {
e.preventDefault();

const form = e.target;
const formId = form.getAttribute('id');

if (formId === SECRET_KEY_FORM_ID) {
handleSecretKeyFormSubmit(e);
return;
}
document.addEventListener('submit', e => {
e.preventDefault()

}, false);
const form = e.target
const formId = form.getAttribute('id')


if (formId === SECRET_KEY_FORM_ID) {
handleSecretKeyFormSubmit(e)
}
}, false)
12 changes: 6 additions & 6 deletions extension/utils/build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var webpack = require("webpack"),
config = require("../webpack.config");
const webpack = require('webpack')
const config = require('../webpack.config')

delete config.chromeExtensionBoilerplate;
delete config.chromeExtensionBoilerplate

webpack(
config,
function (err) { if (err) throw err; }
);
config,
err => { if (err) throw err },
)
6 changes: 3 additions & 3 deletions extension/utils/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tiny wrapper with default env vars
module.exports = {
NODE_ENV: (process.env.NODE_ENV || "development"),
PORT: (process.env.PORT || 3000)
};
NODE_ENV: (process.env.NODE_ENV || 'development'),
PORT: (process.env.PORT || 3000),
}
49 changes: 23 additions & 26 deletions extension/utils/webserver.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
var WebpackDevServer = require("webpack-dev-server"),
webpack = require("webpack"),
config = require("../webpack.config"),
env = require("./env"),
path = require("path");
const WebpackDevServer = require('webpack-dev-server')
const webpack = require('webpack')
const path = require('path')
const config = require('../webpack.config')
const env = require('./env')

var options = (config.chromeExtensionBoilerplate || {});
var excludeEntriesToHotReload = (options.notHotReload || []);
const options = (config.chromeExtensionBoilerplate || {})
const excludeEntriesToHotReload = (options.notHotReload || [])

for (var entryName in config.entry) {
if (excludeEntriesToHotReload.indexOf(entryName) === -1) {
config.entry[entryName] =
[
("webpack-dev-server/client?http://localhost:" + env.PORT),
"webpack/hot/dev-server"
].concat(config.entry[entryName]);
}
for (const entryName in config.entry) {
if (excludeEntriesToHotReload.indexOf(entryName) === -1) {
config.entry[entryName] = [
(`webpack-dev-server/client?http://localhost:${env.PORT}`),
'webpack/hot/dev-server',
].concat(config.entry[entryName])
}
}

config.plugins =
[new webpack.HotModuleReplacementPlugin()].concat(config.plugins || []);
config.plugins = [new webpack.HotModuleReplacementPlugin()].concat(config.plugins || [])

delete config.chromeExtensionBoilerplate;
delete config.chromeExtensionBoilerplate

var compiler = webpack(config);
const compiler = webpack(config)

var server =
new WebpackDevServer(compiler, {
const server = new WebpackDevServer(compiler, {
hot: true,
contentBase: path.join(__dirname, "../build"),
contentBase: path.join(__dirname, '../build'),
sockPort: env.PORT,
headers: {
"Access-Control-Allow-Origin": "*"
'Access-Control-Allow-Origin': '*',
},
disableHostCheck: true
});
disableHostCheck: true,
})

server.listen(env.PORT);
server.listen(env.PORT)
Loading

0 comments on commit d5f20e0

Please sign in to comment.