2021-05-09 13:49:40 +02:00
|
|
|
"use strict"
|
|
|
|
|
|
2020-11-09 01:54:05 +01:00
|
|
|
import WsClient from './WsClient.js'
|
2020-11-17 21:46:56 +01:00
|
|
|
import Protocol from './../common/Protocol.js'
|
2020-11-09 01:54:05 +01:00
|
|
|
|
2020-11-17 21:46:56 +01:00
|
|
|
/** @type WsClient */
|
2020-11-09 01:54:05 +01:00
|
|
|
let conn
|
|
|
|
|
let changesCallback = () => {}
|
|
|
|
|
|
2020-11-17 21:46:56 +01:00
|
|
|
function onServerChange(callback) {
|
2020-11-09 01:54:05 +01:00
|
|
|
changesCallback = callback
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 21:46:56 +01:00
|
|
|
function send(message) {
|
|
|
|
|
conn.send(JSON.stringify(message))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let clientSeq
|
|
|
|
|
let events
|
2021-05-13 22:45:55 +02:00
|
|
|
function connect(address, gameId, clientId) {
|
2020-11-17 21:46:56 +01:00
|
|
|
clientSeq = 0
|
|
|
|
|
events = {}
|
2021-05-13 22:45:55 +02:00
|
|
|
conn = new WsClient(address, clientId + '|' + gameId)
|
2021-05-09 21:43:35 +02:00
|
|
|
return new Promise(resolve => {
|
2020-11-09 01:54:05 +01:00
|
|
|
conn.connect()
|
|
|
|
|
conn.onSocket('message', async ({ data }) => {
|
2020-11-17 21:46:56 +01:00
|
|
|
const msg = JSON.parse(data)
|
|
|
|
|
const msgType = msg[0]
|
|
|
|
|
if (msgType === Protocol.EV_SERVER_INIT) {
|
|
|
|
|
const game = msg[1]
|
2021-05-09 21:43:35 +02:00
|
|
|
resolve(game)
|
2020-11-17 21:46:56 +01:00
|
|
|
} else if (msgType === Protocol.EV_SERVER_EVENT) {
|
2020-11-17 22:34:15 +01:00
|
|
|
const msgClientId = msg[1]
|
|
|
|
|
const msgClientSeq = msg[2]
|
|
|
|
|
if (msgClientId === clientId && events[msgClientSeq]) {
|
|
|
|
|
delete events[msgClientSeq]
|
2020-11-17 22:47:55 +01:00
|
|
|
// we have already calculated that change locally. probably
|
|
|
|
|
return
|
2020-11-17 22:34:15 +01:00
|
|
|
}
|
2020-11-17 21:46:56 +01:00
|
|
|
changesCallback(msg)
|
2021-05-09 21:43:35 +02:00
|
|
|
} else {
|
|
|
|
|
throw `[ 2021-05-09 invalid connect msgType ${msgType} ]`
|
2020-11-09 01:54:05 +01:00
|
|
|
}
|
|
|
|
|
})
|
2021-05-09 21:43:35 +02:00
|
|
|
send([Protocol.EV_CLIENT_INIT])
|
2020-11-09 01:54:05 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
function connectReplay(address, gameId, clientId) {
|
2020-12-22 22:35:09 +01:00
|
|
|
clientSeq = 0
|
|
|
|
|
events = {}
|
2021-05-13 22:45:55 +02:00
|
|
|
conn = new WsClient(address, clientId + '|' + gameId)
|
2021-05-09 21:43:35 +02:00
|
|
|
return new Promise(resolve => {
|
2020-12-22 22:35:09 +01:00
|
|
|
conn.connect()
|
|
|
|
|
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]
|
2021-05-09 21:43:35 +02:00
|
|
|
resolve({game, log})
|
|
|
|
|
} else {
|
|
|
|
|
throw `[ 2021-05-09 invalid connectReplay msgType ${msgType} ]`
|
2020-12-22 22:35:09 +01:00
|
|
|
}
|
|
|
|
|
})
|
2021-05-09 21:43:35 +02:00
|
|
|
send([Protocol.EV_CLIENT_INIT_REPLAY])
|
2020-12-22 22:35:09 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
function disconnect() {
|
|
|
|
|
if (conn) {
|
|
|
|
|
conn.disconnect()
|
|
|
|
|
}
|
|
|
|
|
clientSeq = 0
|
|
|
|
|
events = {}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 21:43:35 +02:00
|
|
|
function sendClientEvent(evt) {
|
2020-11-17 21:46:56 +01:00
|
|
|
// when sending event, increase number of sent events
|
|
|
|
|
// and add the event locally
|
|
|
|
|
clientSeq++;
|
2021-05-09 21:43:35 +02:00
|
|
|
events[clientSeq] = evt
|
2020-11-17 21:46:56 +01:00
|
|
|
send([Protocol.EV_CLIENT_EVENT, clientSeq, events[clientSeq]])
|
2020-11-09 01:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
connect,
|
2020-12-22 22:35:09 +01:00
|
|
|
connectReplay,
|
2021-05-13 22:45:55 +02:00
|
|
|
disconnect,
|
2020-11-17 21:46:56 +01:00
|
|
|
onServerChange,
|
|
|
|
|
sendClientEvent,
|
2020-11-09 01:54:05 +01:00
|
|
|
}
|