maybe slight code improvements..

This commit is contained in:
Zutatensuppe 2021-05-09 21:43:35 +02:00
parent 53e97cd58b
commit 68a51ce322
3 changed files with 30 additions and 20 deletions

View file

@ -476,36 +476,42 @@ const getPuzzleHeight = (gameId) => {
function handleInput(gameId, playerId, input, ts) { function handleInput(gameId, playerId, input, ts) {
const puzzle = GAMES[gameId].puzzle const puzzle = GAMES[gameId].puzzle
let evtInfo = getEvtInfo(gameId, playerId) const evtInfo = getEvtInfo(gameId, playerId)
let changes = [] const changes = []
const _dataChange = () => { const _dataChange = () => {
changes.push([Protocol.CHANGE_DATA, puzzle.data]) changes.push([Protocol.CHANGE_DATA, puzzle.data])
} }
const _tileChange = (tileIdx) => { const _tileChange = (tileIdx) => {
changes.push([Protocol.CHANGE_TILE, getTile(gameId, tileIdx)]) changes.push([
Protocol.CHANGE_TILE,
Util.encodeTile(getTile(gameId, tileIdx)),
])
} }
const _tileChanges = (tileIdxs) => { const _tileChanges = (tileIdxs) => {
for (let tileIdx of tileIdxs) { for (const tileIdx of tileIdxs) {
_tileChange(tileIdx) _tileChange(tileIdx)
} }
} }
const _playerChange = () => { const _playerChange = () => {
changes.push([Protocol.CHANGE_PLAYER, Util.encodePlayer(getPlayer(gameId, playerId))]) changes.push([
Protocol.CHANGE_PLAYER,
Util.encodePlayer(getPlayer(gameId, playerId)),
])
} }
// put both tiles (and their grouped tiles) in the same group // put both tiles (and their grouped tiles) in the same group
const groupTiles = (gameId, tileIdx1, tileIdx2) => { const groupTiles = (gameId, tileIdx1, tileIdx2) => {
let tiles = GAMES[gameId].puzzle.tiles const tiles = GAMES[gameId].puzzle.tiles
let group1 = getTileGroup(gameId, tileIdx1) const group1 = getTileGroup(gameId, tileIdx1)
let group2 = getTileGroup(gameId, tileIdx2) const group2 = getTileGroup(gameId, tileIdx2)
let group let group
let searchGroups = [] const searchGroups = []
if (group1) { if (group1) {
searchGroups.push(group1) searchGroups.push(group1)
} }
@ -517,7 +523,7 @@ function handleInput(gameId, playerId, input, ts) {
} else if (group2) { } else if (group2) {
group = group2 group = group2
} else { } else {
let maxGroup = getMaxGroup(gameId) + 1 const maxGroup = getMaxGroup(gameId) + 1
changeData(gameId, { maxGroup }) changeData(gameId, { maxGroup })
_dataChange() _dataChange()
group = getMaxGroup(gameId) group = getMaxGroup(gameId)
@ -530,7 +536,7 @@ function handleInput(gameId, playerId, input, ts) {
// TODO: strange // TODO: strange
if (searchGroups.length > 0) { if (searchGroups.length > 0) {
for (let t of tiles) { for (const t of tiles) {
const tile = Util.decodeTile(t) const tile = Util.decodeTile(t)
if (searchGroups.includes(tile.group)) { if (searchGroups.includes(tile.group)) {
changeTile(gameId, tile.idx, { group }) changeTile(gameId, tile.idx, { group })

View file

@ -21,15 +21,14 @@ function connect(gameId, clientId) {
clientSeq = 0 clientSeq = 0
events = {} events = {}
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId) conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
return new Promise(r => { return new Promise(resolve => {
conn.connect() conn.connect()
send([Protocol.EV_CLIENT_INIT])
conn.onSocket('message', async ({ data }) => { conn.onSocket('message', async ({ data }) => {
const msg = JSON.parse(data) const msg = JSON.parse(data)
const msgType = msg[0] const msgType = msg[0]
if (msgType === Protocol.EV_SERVER_INIT) { if (msgType === Protocol.EV_SERVER_INIT) {
const game = msg[1] const game = msg[1]
r(game) resolve(game)
} else if (msgType === Protocol.EV_SERVER_EVENT) { } else if (msgType === Protocol.EV_SERVER_EVENT) {
const msgClientId = msg[1] const msgClientId = msg[1]
const msgClientSeq = msg[2] const msgClientSeq = msg[2]
@ -39,8 +38,11 @@ function connect(gameId, clientId) {
return return
} }
changesCallback(msg) changesCallback(msg)
} else {
throw `[ 2021-05-09 invalid connect msgType ${msgType} ]`
} }
}) })
send([Protocol.EV_CLIENT_INIT])
}) })
} }
@ -48,26 +50,28 @@ function connectReplay(gameId, clientId) {
clientSeq = 0 clientSeq = 0
events = {} events = {}
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId) conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
return new Promise(r => { return new Promise(resolve => {
conn.connect() conn.connect()
send([Protocol.EV_CLIENT_INIT_REPLAY])
conn.onSocket('message', async ({ data }) => { conn.onSocket('message', async ({ data }) => {
const msg = JSON.parse(data) const msg = JSON.parse(data)
const msgType = msg[0] const msgType = msg[0]
if (msgType === Protocol.EV_SERVER_INIT_REPLAY) { if (msgType === Protocol.EV_SERVER_INIT_REPLAY) {
const game = msg[1] const game = msg[1]
const log = msg[2] const log = msg[2]
r({game, log}) resolve({game, log})
} else {
throw `[ 2021-05-09 invalid connectReplay msgType ${msgType} ]`
} }
}) })
send([Protocol.EV_CLIENT_INIT_REPLAY])
}) })
} }
function sendClientEvent(mouse) { function sendClientEvent(evt) {
// when sending event, increase number of sent events // when sending event, increase number of sent events
// and add the event locally // and add the event locally
clientSeq++; clientSeq++;
events[clientSeq] = mouse events[clientSeq] = evt
send([Protocol.EV_CLIENT_EVENT, clientSeq, events[clientSeq]]) send([Protocol.EV_CLIENT_EVENT, clientSeq, events[clientSeq]])
} }

View file

@ -744,7 +744,7 @@ async function main() {
// LOCAL + SERVER CHANGES // LOCAL + SERVER CHANGES
// ------------------------------------------------------------- // -------------------------------------------------------------
const ts = TIME() const ts = TIME()
const changes = Game.handleInput(GAME_ID, CLIENT_ID, evt, ts) const changes = Game.handleInput(gameId, CLIENT_ID, evt, ts)
if (changes.length > 0) { if (changes.length > 0) {
RERENDER = true RERENDER = true
} }