some more moving code around

This commit is contained in:
Zutatensuppe 2020-11-09 01:54:05 +01:00
parent d4ddf91259
commit 73872ebb6f
3 changed files with 564 additions and 535 deletions

49
game/Communication.js Normal file
View file

@ -0,0 +1,49 @@
import WsClient from './WsClient.js'
let conn
let changesCallback = () => {}
function onChanges(callback) {
changesCallback = callback
}
function connect(gameId, playerId) {
conn = new WsClient(WS_ADDRESS, playerId + '|' + gameId)
return new Promise(r => {
conn.connect()
conn.send(JSON.stringify({ type: 'init' }))
conn.onSocket('message', async ({ data }) => {
const d = JSON.parse(data)
if (d.type === 'init') {
r(d.game)
} else if (d.type === 'state_changed' && d.origin !== playerId) {
changesCallback(d.changes)
}
})
})
}
const _STATE = {
changed: false,
changes: [],
}
function addChange(change) {
_STATE.changes.push(change)
_STATE.changed = true
}
function sendChanges() {
if (_STATE.changed) {
conn.send(JSON.stringify({ type: 'state', state: _STATE }))
_STATE.changes = []
_STATE.changed = false
}
}
export default {
connect,
onChanges,
addChange,
sendChanges,
}

21
game/Debug.js Normal file
View file

@ -0,0 +1,21 @@
let _pt = 0
let _mindiff = 0
const checkpoint_start = (mindiff) => {
_pt = performance.now()
_mindiff = mindiff
}
const checkpoint = (label) => {
const now = performance.now();
const diff = now - _pt
if (diff > _mindiff) {
console.log(label + ': ' + (diff));
}
_pt = now;
}
export default {
checkpoint_start,
checkpoint,
}

File diff suppressed because it is too large Load diff