Skip to content

Commit

Permalink
MOVE TO RUNCLIENT
Browse files Browse the repository at this point in the history
  • Loading branch information
dexterleng committed Jan 8, 2021
1 parent ee9e92b commit 125a23a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 56 deletions.
58 changes: 2 additions & 56 deletions client/clientMain.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,10 @@
import GameClient from './GameClient'
import instantiateClient from './instantiateClient'

const SECRET_KEY_FORM_ID = 'secret-key-form'
import runClient 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')
})
runClient(container);
}

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

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

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

export default function runClient(container) {
container.innerText = '';
container.insertAdjacentHTML('beforeend', renderSecretKeyForm())
}

const SECRET_KEY_FORM_ID = 'secret-key-form'

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;
console.log(secretKey);

instantiateClient(secretKey)
.then(client => {
console.log("success");
const canvas = '<canvas id=\'main-canvas\' style="height: 100%; width: 100%"></canvas>'
container.innerText = ''
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')
})
}


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 125a23a

Please sign in to comment.