change dir stucture

This commit is contained in:
Zutatensuppe 2021-05-01 00:16:08 +02:00
parent e18b8d3b98
commit 62f8991e11
26 changed files with 8718 additions and 804 deletions

View file

@ -1,77 +0,0 @@
import WsClient from './WsClient.js'
import Protocol from './../common/Protocol.js'
/** @type WsClient */
let conn
let changesCallback = () => {}
function onServerChange(callback) {
changesCallback = callback
}
function send(message) {
conn.send(JSON.stringify(message))
}
let clientSeq
let events
function connect(gameId, clientId) {
clientSeq = 0
events = {}
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
return new Promise(r => {
conn.connect()
send([Protocol.EV_CLIENT_INIT])
conn.onSocket('message', async ({ data }) => {
const msg = JSON.parse(data)
const msgType = msg[0]
if (msgType === Protocol.EV_SERVER_INIT) {
const game = msg[1]
r(game)
} else if (msgType === Protocol.EV_SERVER_EVENT) {
const msgClientId = msg[1]
const msgClientSeq = msg[2]
if (msgClientId === clientId && events[msgClientSeq]) {
delete events[msgClientSeq]
// we have already calculated that change locally. probably
return
}
changesCallback(msg)
}
})
})
}
function connectReplay(gameId, clientId) {
clientSeq = 0
events = {}
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
return new Promise(r => {
conn.connect()
send([Protocol.EV_CLIENT_INIT_REPLAY])
conn.onSocket('message', async ({ data }) => {
const msg = JSON.parse(data)
const msgType = msg[0]
if (msgType === Protocol.EV_SERVER_INIT_REPLAY) {
const game = msg[1]
const log = msg[2]
r({game, log})
}
})
})
}
function sendClientEvent(mouse) {
// when sending event, increase number of sent events
// and add the event locally
clientSeq++;
events[clientSeq] = mouse
send([Protocol.EV_CLIENT_EVENT, clientSeq, events[clientSeq]])
}
export default {
connect,
connectReplay,
onServerChange,
sendClientEvent,
}