diff --git a/client/GameClient.js b/client/GameClient.js index d4c5a5c..9e6a2bf 100644 --- a/client/GameClient.js +++ b/client/GameClient.js @@ -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) { diff --git a/client/clientMain.js b/client/clientMain.js index 4adf8ad..be05879 100644 --- a/client/clientMain.js +++ b/client/clientMain.js @@ -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 ` +
+ ` } + +let gameClient + +function handleSecretKeyFormSubmit(e) { + const secretKey = document.getElementById('secret-key').value + + instantiateClient(secretKey) + .then(client => { + const 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) diff --git a/client/instantiateClient.js b/client/instantiateClient.js new file mode 100644 index 0000000..cf07b09 --- /dev/null +++ b/client/instantiateClient.js @@ -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 diff --git a/client/runClient.js b/client/runClient.js deleted file mode 100644 index 4f00b23..0000000 --- a/client/runClient.js +++ /dev/null @@ -1,22 +0,0 @@ -import GameClient from './GameClient' - -function main(secret) { - - // TODO: const secret = document.getElementById('secret-input').value - const gameClient = new GameClient(secret) - 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() -} - -export default main diff --git a/common/gameConfig.js b/common/gameConfig.js index d482795..fe1b441 100644 --- a/common/gameConfig.js +++ b/common/gameConfig.js @@ -10,4 +10,4 @@ const config = { MAP_IMAGE: './images/bg.png', } -export default config \ No newline at end of file +export default config diff --git a/extension/src/js/background.js b/extension/src/js/background.js index ffacd79..2fc4966 100644 --- a/extension/src/js/background.js +++ b/extension/src/js/background.js @@ -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' - }); -}); \ No newline at end of file +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', + }) +}) diff --git a/extension/src/js/inject.js b/extension/src/js/inject.js index ab99206..3f9a7bc 100644 --- a/extension/src/js/inject.js +++ b/extension/src/js/inject.js @@ -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 `