maybe slight code improvements..
This commit is contained in:
parent
53e97cd58b
commit
68a51ce322
3 changed files with 30 additions and 20 deletions
|
|
@ -476,36 +476,42 @@ const getPuzzleHeight = (gameId) => {
|
|||
|
||||
function handleInput(gameId, playerId, input, ts) {
|
||||
const puzzle = GAMES[gameId].puzzle
|
||||
let evtInfo = getEvtInfo(gameId, playerId)
|
||||
const evtInfo = getEvtInfo(gameId, playerId)
|
||||
|
||||
let changes = []
|
||||
const changes = []
|
||||
|
||||
const _dataChange = () => {
|
||||
changes.push([Protocol.CHANGE_DATA, puzzle.data])
|
||||
}
|
||||
|
||||
const _tileChange = (tileIdx) => {
|
||||
changes.push([Protocol.CHANGE_TILE, getTile(gameId, tileIdx)])
|
||||
changes.push([
|
||||
Protocol.CHANGE_TILE,
|
||||
Util.encodeTile(getTile(gameId, tileIdx)),
|
||||
])
|
||||
}
|
||||
|
||||
const _tileChanges = (tileIdxs) => {
|
||||
for (let tileIdx of tileIdxs) {
|
||||
for (const tileIdx of tileIdxs) {
|
||||
_tileChange(tileIdx)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
const groupTiles = (gameId, tileIdx1, tileIdx2) => {
|
||||
let tiles = GAMES[gameId].puzzle.tiles
|
||||
let group1 = getTileGroup(gameId, tileIdx1)
|
||||
let group2 = getTileGroup(gameId, tileIdx2)
|
||||
const tiles = GAMES[gameId].puzzle.tiles
|
||||
const group1 = getTileGroup(gameId, tileIdx1)
|
||||
const group2 = getTileGroup(gameId, tileIdx2)
|
||||
|
||||
let group
|
||||
let searchGroups = []
|
||||
const searchGroups = []
|
||||
if (group1) {
|
||||
searchGroups.push(group1)
|
||||
}
|
||||
|
|
@ -517,7 +523,7 @@ function handleInput(gameId, playerId, input, ts) {
|
|||
} else if (group2) {
|
||||
group = group2
|
||||
} else {
|
||||
let maxGroup = getMaxGroup(gameId) + 1
|
||||
const maxGroup = getMaxGroup(gameId) + 1
|
||||
changeData(gameId, { maxGroup })
|
||||
_dataChange()
|
||||
group = getMaxGroup(gameId)
|
||||
|
|
@ -530,7 +536,7 @@ function handleInput(gameId, playerId, input, ts) {
|
|||
|
||||
// TODO: strange
|
||||
if (searchGroups.length > 0) {
|
||||
for (let t of tiles) {
|
||||
for (const t of tiles) {
|
||||
const tile = Util.decodeTile(t)
|
||||
if (searchGroups.includes(tile.group)) {
|
||||
changeTile(gameId, tile.idx, { group })
|
||||
|
|
|
|||
|
|
@ -21,15 +21,14 @@ function connect(gameId, clientId) {
|
|||
clientSeq = 0
|
||||
events = {}
|
||||
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
|
||||
return new Promise(r => {
|
||||
return new Promise(resolve => {
|
||||
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)
|
||||
resolve(game)
|
||||
} else if (msgType === Protocol.EV_SERVER_EVENT) {
|
||||
const msgClientId = msg[1]
|
||||
const msgClientSeq = msg[2]
|
||||
|
|
@ -39,8 +38,11 @@ function connect(gameId, clientId) {
|
|||
return
|
||||
}
|
||||
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
|
||||
events = {}
|
||||
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
|
||||
return new Promise(r => {
|
||||
return new Promise(resolve => {
|
||||
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})
|
||||
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
|
||||
// and add the event locally
|
||||
clientSeq++;
|
||||
events[clientSeq] = mouse
|
||||
events[clientSeq] = evt
|
||||
send([Protocol.EV_CLIENT_EVENT, clientSeq, events[clientSeq]])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -744,7 +744,7 @@ async function main() {
|
|||
// LOCAL + SERVER CHANGES
|
||||
// -------------------------------------------------------------
|
||||
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) {
|
||||
RERENDER = true
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue