Skip to content

Commit

Permalink
Merge pull request #7 from juxd/backup
Browse files Browse the repository at this point in the history
Update Chrome extension
  • Loading branch information
dexterleng committed Jan 8, 2021
2 parents ee9e92b + 53328fa commit 1027ddb
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 101 deletions.
59 changes: 3 additions & 56 deletions client/clientMain.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,11 @@
import GameClient from './GameClient'
import instantiateClient from './instantiateClient'

const SECRET_KEY_FORM_ID = 'secret-key-form'
import UI from './runClient';

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')
})
const ui = new UI(container);
ui.start();
}

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

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

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

const SECRET_KEY_FORM_ID = 'secret-key-form'

class UI {
constructor(container) {
this.container = container;
}

start() {
this.container.innerText = '';
this.container.insertAdjacentHTML('beforeend', this.renderSecretKeyForm());
document.addEventListener('submit', e => {
e.preventDefault()

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

if (formId === SECRET_KEY_FORM_ID) {
this.handleSecretKeyFormSubmit(e)
}
}, false)
}

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>
`
}

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

instantiateClient(secretKey)
.then(client => {
console.log("success");
const canvas = '<canvas id=\'main-canvas\' style="height: 100%; width: 100%"></canvas>'
this.container.innerText = ''
this.container.insertAdjacentHTML('beforeend', canvas)
const 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(err => {
console.log(`error: ${err}`);
alert('shit secret key')
})
}
}

export default UI;

2 changes: 1 addition & 1 deletion common/gameConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = {
PLAYER_SPEED: 1200,
PLAYER_CHAT_TIMEOUT: 5000, // 5s

MAP_IMAGE: './images/bg.png',
MAP_IMAGE: 'https://raw.githubusercontent.com/juxd/discord-spatial-layer/master/public/images/bg.png',

NEUTRAL_FACE: './images/tile000.png',
HAPPY_FACE: './images/tile002.png',
Expand Down
48 changes: 4 additions & 44 deletions extension/src/js/inject.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import nengiConfig from '../../../common/nengiConfig'
import main from '../../../client/runClient'

console.log(nengiConfig)
const SECRET_KEY_FORM_ID = 'secret-key-form'
import UI from '../../../client/runClient'

let container;

Expand All @@ -22,45 +18,9 @@ let container;
container.style.top = '60%'
container.style.backgroundColor = 'white'

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 ui = new UI(container);
ui.start();

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

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>
`
}

function handleSecretKeyFormSubmit(e) {
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)
}

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

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

if (formId === SECRET_KEY_FORM_ID) {
handleSecretKeyFormSubmit(e)
}
}, false)
}())
2 changes: 2 additions & 0 deletions server/AuthDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class AuthDatabase {

// Returns the code which the user is added to AuthDatabase with
addUser(user) {
console.log('auth');
console.log(user);
let code = randCode()
while (this.registered.has(code)) code = randCode()
this.registered.set(code, { user, game_status: null })
Expand Down
5 changes: 5 additions & 0 deletions server/GameInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class GameInstance {
this.nids = new Map()
if (process.env.NODE_ENV === 'development') this.authDatabase.addUserWithSecret({ displayName: 'joe' }, 'MAGIC_VALUE')
this.instance.onConnect((client, clientData, callback) => {
this.authDatabase.registered.forEach((k) => {
console.log(k);
})
console.log(clientData.fromClient.secret);
const { user } = this.authDatabase.getUser(clientData.fromClient.secret) || { user: undefined }
if (!user) {
callback({ accepted: false, text: 'Secret not correct!' })
Expand Down Expand Up @@ -62,6 +66,7 @@ class GameInstance {
}

handleMessage(msg) {
console.log(JSON.stringify(msg));
const prefix = '!'
if (msg.author.bot) return
if (msg.content.startsWith(prefix)) {
Expand Down

0 comments on commit 1027ddb

Please sign in to comment.