2020-11-25 22:03:35 +01:00
|
|
|
|
"use strict"
|
|
|
|
|
|
import {run} from './gameloop.js'
|
|
|
|
|
|
import Camera from './Camera.js'
|
|
|
|
|
|
import Graphics from './Graphics.js'
|
|
|
|
|
|
import Debug from './Debug.js'
|
|
|
|
|
|
import Communication from './Communication.js'
|
|
|
|
|
|
import Util from './../common/Util.js'
|
|
|
|
|
|
import PuzzleGraphics from './PuzzleGraphics.js'
|
|
|
|
|
|
import Game from './Game.js'
|
2020-12-09 01:27:47 +01:00
|
|
|
|
import fireworksController from './Fireworks.js'
|
2020-12-23 01:19:30 +01:00
|
|
|
|
import Protocol from '../common/Protocol.js'
|
2021-04-14 19:30:45 +02:00
|
|
|
|
import Time from '../common/Time.js'
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
|
|
if (typeof GAME_ID === 'undefined') throw '[ GAME_ID not set ]'
|
|
|
|
|
|
if (typeof WS_ADDRESS === 'undefined') throw '[ WS_ADDRESS not set ]'
|
2020-12-22 22:35:09 +01:00
|
|
|
|
if (typeof MODE === 'undefined') throw '[ MODE not set ]'
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
|
|
if (typeof DEBUG === 'undefined') window.DEBUG = false
|
|
|
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
|
const MODE_PLAY = 'play'
|
|
|
|
|
|
const MODE_REPLAY = 'replay'
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
let RERENDER = true
|
|
|
|
|
|
|
2021-04-14 19:30:45 +02:00
|
|
|
|
let TIME = () => Time.timestamp()
|
2020-12-22 22:35:09 +01:00
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
function addCanvasToDom(canvas) {
|
|
|
|
|
|
canvas.width = window.innerWidth
|
|
|
|
|
|
canvas.height = window.innerHeight
|
|
|
|
|
|
document.body.appendChild(canvas)
|
|
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
|
|
canvas.width = window.innerWidth
|
|
|
|
|
|
canvas.height = window.innerHeight
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
})
|
|
|
|
|
|
return canvas
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const ELEMENTS = {
|
|
|
|
|
|
TABLE: document.createElement('table'),
|
|
|
|
|
|
TR: document.createElement('tr'),
|
|
|
|
|
|
TD: document.createElement('td'),
|
|
|
|
|
|
BUTTON: document.createElement('button'),
|
|
|
|
|
|
INPUT: document.createElement('input'),
|
|
|
|
|
|
LABEL: document.createElement('label'),
|
|
|
|
|
|
DIV: document.createElement('div'),
|
|
|
|
|
|
A: document.createElement('a'),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-27 09:46:39 +02:00
|
|
|
|
let KEY_LISTENER_OFF = false
|
|
|
|
|
|
|
2021-04-27 22:56:50 +02:00
|
|
|
|
let PIECE_VIEW_FIXED = true
|
|
|
|
|
|
let PIECE_VIEW_LOOSE = true
|
|
|
|
|
|
|
2020-12-07 02:38:07 +01:00
|
|
|
|
function addMenuToDom(gameId) {
|
|
|
|
|
|
const previewImageUrl = Game.getImageUrl(gameId)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
function row (...elements) {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const row = ELEMENTS.TR.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
for (let el of elements) {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const td = ELEMENTS.TD.cloneNode(true)
|
2021-04-27 09:46:39 +02:00
|
|
|
|
if (typeof el === 'string') {
|
|
|
|
|
|
td.appendChild(document.createTextNode(el))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
td.appendChild(el)
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
row.appendChild(td)
|
|
|
|
|
|
}
|
|
|
|
|
|
return row
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
function btn(txt) {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const btn = ELEMENTS.BUTTON.cloneNode(true)
|
2020-12-22 22:35:09 +01:00
|
|
|
|
btn.classList.add('btn')
|
|
|
|
|
|
btn.innerText = txt
|
|
|
|
|
|
return btn
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
function colorinput() {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const input = ELEMENTS.INPUT.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
input.type = 'color'
|
|
|
|
|
|
return input
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function textinput(maxLength) {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const input = ELEMENTS.INPUT.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
input.type = 'text'
|
|
|
|
|
|
input.maxLength = maxLength
|
|
|
|
|
|
return input
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function label(text) {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const label = ELEMENTS.LABEL.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
label.innerText = text
|
|
|
|
|
|
return label
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bgColorPickerEl = colorinput()
|
|
|
|
|
|
const bgColorPickerRow = row(
|
|
|
|
|
|
label('Background: '),
|
|
|
|
|
|
bgColorPickerEl
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const playerColorPickerEl = colorinput()
|
|
|
|
|
|
const playerColorPickerRow = row(
|
|
|
|
|
|
label('Color: '),
|
|
|
|
|
|
playerColorPickerEl
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const nameChangeEl = textinput(16)
|
|
|
|
|
|
const nameChangeRow = row(
|
|
|
|
|
|
label('Name: '),
|
|
|
|
|
|
nameChangeEl
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-04-27 09:46:39 +02:00
|
|
|
|
const kbd = function(txt) {
|
|
|
|
|
|
const el = document.createElement('kbd')
|
|
|
|
|
|
el.appendChild(document.createTextNode(txt))
|
|
|
|
|
|
return el
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const h = function(...els) {
|
|
|
|
|
|
const el = ELEMENTS.DIV.cloneNode(true)
|
|
|
|
|
|
for (const other of els) {
|
|
|
|
|
|
if (typeof other === 'string') {
|
|
|
|
|
|
el.appendChild(document.createTextNode(other))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
el.appendChild(other)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return el
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const helpEl = ELEMENTS.TABLE.cloneNode(true)
|
|
|
|
|
|
helpEl.classList.add('help')
|
|
|
|
|
|
helpEl.appendChild(row('⬆️ Move up:', h(kbd('W'), '/', kbd('↑'), '/🖱️')))
|
|
|
|
|
|
helpEl.appendChild(row('⬇️ Move down:', h(kbd('S'), '/', kbd('↓'), '/🖱️')))
|
|
|
|
|
|
helpEl.appendChild(row('⬅️ Move left:', h(kbd('A'), '/', kbd('←'), '/🖱️')))
|
|
|
|
|
|
helpEl.appendChild(row('➡️ Move right:', h(kbd('D'), '/', kbd('→'), '/🖱️')))
|
|
|
|
|
|
helpEl.appendChild(row('', h('Move faster by holding ', kbd('Shift'))))
|
|
|
|
|
|
helpEl.appendChild(row('🔍+ Zoom in:', h(kbd('E'), '/🖱️-Wheel')))
|
|
|
|
|
|
helpEl.appendChild(row('🔍- Zoom out:', h(kbd('Q'), '/🖱️-Wheel')))
|
|
|
|
|
|
helpEl.appendChild(row('🖼️ Toggle preview:', h(kbd('Space'))))
|
2021-04-27 22:56:50 +02:00
|
|
|
|
helpEl.appendChild(row('🧩✔️ Toggle fixed pieces:', h(kbd('F'))))
|
|
|
|
|
|
helpEl.appendChild(row('🧩❓ Toggle loose pieces:', h(kbd('G'))))
|
2021-04-27 09:46:39 +02:00
|
|
|
|
helpEl.addEventListener('click', (e) => {
|
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const toggle = (el, disableHotkeys = true) => {
|
|
|
|
|
|
el.classList.toggle('closed')
|
|
|
|
|
|
if (disableHotkeys) {
|
|
|
|
|
|
KEY_LISTENER_OFF = !el.classList.contains('closed')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const helpOverlay = ELEMENTS.DIV.cloneNode(true)
|
|
|
|
|
|
helpOverlay.classList.add('overlay', 'transparent', 'closed')
|
|
|
|
|
|
helpOverlay.appendChild(helpEl)
|
|
|
|
|
|
helpOverlay.addEventListener('click', () => {
|
|
|
|
|
|
toggle(helpOverlay)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const settingsEl = ELEMENTS.TABLE.cloneNode(true)
|
2020-12-06 21:55:23 +01:00
|
|
|
|
settingsEl.classList.add('settings')
|
2020-11-25 22:03:35 +01:00
|
|
|
|
settingsEl.appendChild(bgColorPickerRow)
|
|
|
|
|
|
settingsEl.appendChild(playerColorPickerRow)
|
|
|
|
|
|
settingsEl.appendChild(nameChangeRow)
|
2020-12-06 21:55:23 +01:00
|
|
|
|
settingsEl.addEventListener('click', (e) => {
|
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const settingsOverlay = ELEMENTS.DIV.cloneNode(true)
|
2020-12-06 21:55:23 +01:00
|
|
|
|
settingsOverlay.classList.add('overlay', 'transparent', 'closed')
|
|
|
|
|
|
settingsOverlay.appendChild(settingsEl)
|
|
|
|
|
|
settingsOverlay.addEventListener('click', () => {
|
2021-04-27 09:46:39 +02:00
|
|
|
|
toggle(settingsOverlay)
|
2020-12-06 21:55:23 +01:00
|
|
|
|
})
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const previewEl = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
previewEl.classList.add('preview')
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const imgEl = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
imgEl.classList.add('img')
|
|
|
|
|
|
imgEl.style.backgroundImage = `url(${previewImageUrl})`
|
|
|
|
|
|
previewEl.appendChild(imgEl)
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const previewOverlay = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
previewOverlay.classList.add('overlay', 'closed')
|
|
|
|
|
|
previewOverlay.appendChild(previewEl)
|
2021-04-19 23:59:14 +02:00
|
|
|
|
const togglePreview = () => {
|
2020-11-25 22:03:35 +01:00
|
|
|
|
previewOverlay.classList.toggle('closed')
|
2021-04-19 23:59:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
previewOverlay.addEventListener('click', () => {
|
|
|
|
|
|
togglePreview()
|
2020-11-25 22:03:35 +01:00
|
|
|
|
})
|
|
|
|
|
|
|
2021-04-27 09:46:39 +02:00
|
|
|
|
const opener = (txt, overlay, disableHotkeys = true) => {
|
|
|
|
|
|
const el = ELEMENTS.DIV.cloneNode(true)
|
|
|
|
|
|
el.classList.add('opener')
|
|
|
|
|
|
el.appendChild(document.createTextNode(txt))
|
|
|
|
|
|
el.addEventListener('click', () => {
|
|
|
|
|
|
toggle(overlay, disableHotkeys)
|
|
|
|
|
|
})
|
|
|
|
|
|
return el
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const homeEl = ELEMENTS.A.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
homeEl.classList.add('opener')
|
|
|
|
|
|
homeEl.appendChild(document.createTextNode('🧩 Puzzles'))
|
2021-04-15 20:50:38 +02:00
|
|
|
|
homeEl.target = '_blank'
|
|
|
|
|
|
homeEl.href = '/'
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-27 09:46:39 +02:00
|
|
|
|
const settingsOpenerEl = opener('🛠️ Settings', settingsOverlay)
|
|
|
|
|
|
const previewOpenerEl = opener('🖼️ Preview', previewOverlay, false)
|
|
|
|
|
|
const helpOpenerEl = opener('ℹ️ Help', helpOverlay)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const tabsEl = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
tabsEl.classList.add('tabs')
|
|
|
|
|
|
tabsEl.appendChild(homeEl)
|
|
|
|
|
|
tabsEl.appendChild(previewOpenerEl)
|
|
|
|
|
|
tabsEl.appendChild(settingsOpenerEl)
|
2021-04-27 09:46:39 +02:00
|
|
|
|
tabsEl.appendChild(helpOpenerEl)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const menuEl = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
menuEl.classList.add('menu')
|
|
|
|
|
|
menuEl.appendChild(tabsEl)
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const scoresTitleEl = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
scoresTitleEl.appendChild(document.createTextNode('Scores'))
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const scoresListEl = ELEMENTS.TABLE.cloneNode(true)
|
|
|
|
|
|
const updateScoreBoard = (ts) => {
|
2021-04-14 19:30:45 +02:00
|
|
|
|
const minTs = ts - 30 * Time.SEC
|
2020-12-21 02:29:14 +01:00
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const players = Game.getRelevantPlayers(gameId, ts)
|
2020-12-21 02:29:14 +01:00
|
|
|
|
const actives = players.filter(player => player.ts >= minTs)
|
|
|
|
|
|
const nonActives = players.filter(player => player.ts < minTs)
|
|
|
|
|
|
|
|
|
|
|
|
actives.sort((a, b) => b.points - a.points)
|
|
|
|
|
|
nonActives.sort((a, b) => b.points - a.points)
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
scoresListEl.innerHTML = ''
|
2020-12-21 02:29:14 +01:00
|
|
|
|
for (let player of actives) {
|
|
|
|
|
|
const r = row(
|
|
|
|
|
|
document.createTextNode('⚡'),
|
|
|
|
|
|
document.createTextNode(player.name),
|
|
|
|
|
|
document.createTextNode(player.points)
|
|
|
|
|
|
)
|
|
|
|
|
|
r.style.color = player.color
|
|
|
|
|
|
scoresListEl.appendChild(r)
|
|
|
|
|
|
}
|
|
|
|
|
|
for (let player of nonActives) {
|
2020-11-25 22:03:35 +01:00
|
|
|
|
const r = row(
|
2020-12-21 02:29:14 +01:00
|
|
|
|
document.createTextNode('💤'),
|
|
|
|
|
|
document.createTextNode(player.name),
|
|
|
|
|
|
document.createTextNode(player.points)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
)
|
2020-12-21 02:29:14 +01:00
|
|
|
|
r.style.color = player.color
|
2020-11-25 22:03:35 +01:00
|
|
|
|
scoresListEl.appendChild(r)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-07 02:38:07 +01:00
|
|
|
|
const timerStr = () => {
|
|
|
|
|
|
const started = Game.getStartTs(gameId)
|
|
|
|
|
|
const ended = Game.getFinishTs(gameId)
|
|
|
|
|
|
const icon = ended ? '🏁' : '⏳'
|
|
|
|
|
|
const from = started;
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const to = ended || TIME()
|
2021-04-14 19:30:45 +02:00
|
|
|
|
const timeDiffStr = Time.timeDiffStr(from, to)
|
|
|
|
|
|
return `${icon} ${timeDiffStr}`
|
2020-12-07 02:38:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const timerCountdownEl = ELEMENTS.DIV.cloneNode(true)
|
2021-04-17 16:09:20 +02:00
|
|
|
|
const updateTimer = () => {
|
2020-12-07 02:38:07 +01:00
|
|
|
|
timerCountdownEl.innerText = timerStr()
|
2021-04-17 16:09:20 +02:00
|
|
|
|
}
|
2021-04-20 23:15:29 +02:00
|
|
|
|
const tilesDoneEl = ELEMENTS.DIV.cloneNode(true)
|
|
|
|
|
|
const udateTilesDone = () => {
|
|
|
|
|
|
const tilesFinished = Game.getFinishedTileCount(gameId)
|
|
|
|
|
|
const tilesTotal = Game.getTileCount(gameId)
|
|
|
|
|
|
tilesDoneEl.innerText = `🧩 ${tilesFinished}/${tilesTotal}`
|
|
|
|
|
|
}
|
2020-12-07 02:38:07 +01:00
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const timerEl = ELEMENTS.DIV.cloneNode(true)
|
2020-12-07 02:38:07 +01:00
|
|
|
|
timerEl.classList.add('timer')
|
2021-04-20 23:15:29 +02:00
|
|
|
|
timerEl.appendChild(tilesDoneEl)
|
2020-12-07 02:38:07 +01:00
|
|
|
|
timerEl.appendChild(timerCountdownEl)
|
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
let replayControl = null
|
2021-04-17 17:23:32 +02:00
|
|
|
|
if (MODE === MODE_REPLAY) {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const replayControlEl = ELEMENTS.DIV.cloneNode(true)
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const speedUp = btn('⏫')
|
|
|
|
|
|
const speedDown = btn('⏬')
|
|
|
|
|
|
const pause = btn('⏸️')
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const speed = ELEMENTS.DIV.cloneNode(true)
|
2020-12-22 22:35:09 +01:00
|
|
|
|
replayControlEl.appendChild(speed)
|
|
|
|
|
|
replayControlEl.appendChild(speedUp)
|
|
|
|
|
|
replayControlEl.appendChild(speedDown)
|
|
|
|
|
|
replayControlEl.appendChild(pause)
|
|
|
|
|
|
timerEl.appendChild(replayControlEl)
|
|
|
|
|
|
replayControl = { speedUp, speedDown, pause, speed }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const scoresEl = ELEMENTS.DIV.cloneNode(true)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
scoresEl.classList.add('scores')
|
|
|
|
|
|
scoresEl.appendChild(scoresTitleEl)
|
|
|
|
|
|
scoresEl.appendChild(scoresListEl)
|
|
|
|
|
|
|
2020-12-06 21:55:23 +01:00
|
|
|
|
document.body.appendChild(settingsOverlay)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
document.body.appendChild(previewOverlay)
|
2021-04-27 09:46:39 +02:00
|
|
|
|
document.body.appendChild(helpOverlay)
|
2020-12-07 02:38:07 +01:00
|
|
|
|
document.body.appendChild(timerEl)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
document.body.appendChild(menuEl)
|
|
|
|
|
|
document.body.appendChild(scoresEl)
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
bgColorPickerEl,
|
|
|
|
|
|
playerColorPickerEl,
|
|
|
|
|
|
nameChangeEl,
|
2021-04-14 21:04:07 +02:00
|
|
|
|
updateScoreBoard,
|
2021-04-17 16:09:20 +02:00
|
|
|
|
updateTimer,
|
2021-04-20 23:15:29 +02:00
|
|
|
|
udateTilesDone,
|
2021-04-19 23:59:14 +02:00
|
|
|
|
togglePreview,
|
2020-12-22 22:35:09 +01:00
|
|
|
|
replayControl,
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function initme() {
|
|
|
|
|
|
// return uniqId()
|
|
|
|
|
|
let ID = localStorage.getItem('ID')
|
|
|
|
|
|
if (!ID) {
|
|
|
|
|
|
ID = Util.uniqId()
|
|
|
|
|
|
localStorage.setItem('ID', ID)
|
|
|
|
|
|
}
|
|
|
|
|
|
return ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default class EventAdapter {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
constructor(canvas, window, viewport) {
|
2020-11-25 22:03:35 +01:00
|
|
|
|
this.events = []
|
|
|
|
|
|
this._viewport = viewport
|
2021-04-27 08:53:17 +02:00
|
|
|
|
this._canvas = canvas
|
2021-04-19 23:59:14 +02:00
|
|
|
|
|
|
|
|
|
|
this.LEFT = false
|
|
|
|
|
|
this.RIGHT = false
|
|
|
|
|
|
this.UP = false
|
|
|
|
|
|
this.DOWN = false
|
2021-04-27 08:53:17 +02:00
|
|
|
|
this.ZOOM_IN = false
|
|
|
|
|
|
this.ZOOM_OUT = false
|
|
|
|
|
|
this.SHIFT = false
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
canvas.addEventListener('mousedown', this._mouseDown.bind(this))
|
|
|
|
|
|
canvas.addEventListener('mouseup', this._mouseUp.bind(this))
|
|
|
|
|
|
canvas.addEventListener('mousemove', this._mouseMove.bind(this))
|
|
|
|
|
|
canvas.addEventListener('wheel', this._wheel.bind(this))
|
2021-04-19 23:59:14 +02:00
|
|
|
|
|
|
|
|
|
|
window.addEventListener('keydown', (ev) => {
|
2021-04-27 09:46:39 +02:00
|
|
|
|
if (KEY_LISTENER_OFF) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-04-27 08:53:17 +02:00
|
|
|
|
if (ev.key === 'Shift') {
|
|
|
|
|
|
this.SHIFT = true
|
|
|
|
|
|
} else if (ev.key === 'ArrowUp' || ev.key === 'w' || ev.key === 'W') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.UP = true
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'ArrowDown' || ev.key === 's' || ev.key === 'S') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.DOWN = true
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'ArrowLeft' || ev.key === 'a' || ev.key === 'A') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.LEFT = true
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'ArrowRight' || ev.key === 'd' || ev.key === 'D') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.RIGHT = true
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'q') {
|
|
|
|
|
|
this.ZOOM_OUT = true
|
|
|
|
|
|
} else if (ev.key === 'e') {
|
|
|
|
|
|
this.ZOOM_IN = true
|
2021-04-19 23:59:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
window.addEventListener('keyup', (ev) => {
|
2021-04-27 09:46:39 +02:00
|
|
|
|
if (KEY_LISTENER_OFF) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-04-27 08:53:17 +02:00
|
|
|
|
if (ev.key === 'Shift') {
|
|
|
|
|
|
this.SHIFT = false
|
|
|
|
|
|
} else if (ev.key === 'ArrowUp' || ev.key === 'w' || ev.key === 'W') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.UP = false
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'ArrowDown' || ev.key === 's' || ev.key === 'S') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.DOWN = false
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'ArrowLeft' || ev.key === 'a' || ev.key === 'A') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.LEFT = false
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'ArrowRight' || ev.key === 'd' || ev.key === 'D') {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
this.RIGHT = false
|
2021-04-27 08:53:17 +02:00
|
|
|
|
} else if (ev.key === 'q') {
|
|
|
|
|
|
this.ZOOM_OUT = false
|
|
|
|
|
|
} else if (ev.key === 'e') {
|
|
|
|
|
|
this.ZOOM_IN = false
|
2021-04-19 23:59:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addEvent(event) {
|
|
|
|
|
|
this.events.push(event)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
consumeAll() {
|
|
|
|
|
|
if (this.events.length === 0) {
|
|
|
|
|
|
return []
|
|
|
|
|
|
}
|
|
|
|
|
|
const all = this.events.slice()
|
|
|
|
|
|
this.events = []
|
|
|
|
|
|
return all
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-19 23:59:14 +02:00
|
|
|
|
_keydowns() {
|
2021-04-27 08:53:17 +02:00
|
|
|
|
let amount = this.SHIFT ? 20 : 10
|
2021-04-19 23:59:14 +02:00
|
|
|
|
let x = 0
|
|
|
|
|
|
let y = 0
|
|
|
|
|
|
if (this.UP) {
|
|
|
|
|
|
y += amount
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.DOWN) {
|
|
|
|
|
|
y -= amount
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.LEFT) {
|
|
|
|
|
|
x += amount
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.RIGHT) {
|
|
|
|
|
|
x -= amount
|
|
|
|
|
|
}
|
2021-04-27 08:53:17 +02:00
|
|
|
|
|
2021-04-19 23:59:14 +02:00
|
|
|
|
if (x !== 0 || y !== 0) {
|
|
|
|
|
|
this.addEvent([Protocol.INPUT_EV_MOVE, x, y])
|
|
|
|
|
|
}
|
2021-04-27 08:53:17 +02:00
|
|
|
|
|
|
|
|
|
|
// zoom keys
|
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
|
x: this._canvas.width / 2,
|
|
|
|
|
|
y: this._canvas.height / 2,
|
|
|
|
|
|
})
|
|
|
|
|
|
if (this.ZOOM_IN && this.ZOOM_OUT) {
|
|
|
|
|
|
// cancel each other out
|
|
|
|
|
|
} else if (this.ZOOM_IN) {
|
|
|
|
|
|
this.addEvent([Protocol.INPUT_EV_ZOOM_IN, pos.x, pos.y])
|
|
|
|
|
|
} else if (this.ZOOM_OUT) {
|
|
|
|
|
|
this.addEvent([Protocol.INPUT_EV_ZOOM_OUT, pos.x, pos.y])
|
|
|
|
|
|
}
|
2021-04-19 23:59:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
_mouseDown(e) {
|
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
|
})
|
2020-12-23 01:19:30 +01:00
|
|
|
|
this.addEvent([Protocol.INPUT_EV_MOUSE_DOWN, pos.x, pos.y])
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_mouseUp(e) {
|
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
|
})
|
2020-12-23 01:19:30 +01:00
|
|
|
|
this.addEvent([Protocol.INPUT_EV_MOUSE_UP, pos.x, pos.y])
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_mouseMove(e) {
|
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
|
})
|
2020-12-23 01:19:30 +01:00
|
|
|
|
this.addEvent([Protocol.INPUT_EV_MOUSE_MOVE, pos.x, pos.y])
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_wheel(e) {
|
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
|
})
|
2020-12-23 01:19:30 +01:00
|
|
|
|
const evt = e.deltaY < 0 ? Protocol.INPUT_EV_ZOOM_IN : Protocol.INPUT_EV_ZOOM_OUT
|
2020-11-25 22:03:35 +01:00
|
|
|
|
this.addEvent([evt, pos.x, pos.y])
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
|
let gameId = GAME_ID
|
|
|
|
|
|
let CLIENT_ID = initme()
|
|
|
|
|
|
|
|
|
|
|
|
const cursorGrab = await Graphics.loadImageToBitmap('/grab.png')
|
|
|
|
|
|
const cursorHand = await Graphics.loadImageToBitmap('/hand.png')
|
|
|
|
|
|
const cursorGrabMask = await Graphics.loadImageToBitmap('/grab_mask.png')
|
|
|
|
|
|
const cursorHandMask = await Graphics.loadImageToBitmap('/hand_mask.png')
|
|
|
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
|
// all cursors must be of the same dimensions
|
|
|
|
|
|
const CURSOR_W = cursorGrab.width
|
|
|
|
|
|
const CURSOR_W_2 = Math.round(CURSOR_W / 2)
|
|
|
|
|
|
const CURSOR_H = cursorGrab.height
|
|
|
|
|
|
const CURSOR_H_2 = Math.round(CURSOR_H / 2)
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
const cursors = {}
|
|
|
|
|
|
const getPlayerCursor = async (p) => {
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const key = p.color + ' ' + p.d
|
2020-11-25 22:03:35 +01:00
|
|
|
|
if (!cursors[key]) {
|
|
|
|
|
|
const cursor = p.d ? cursorGrab : cursorHand
|
|
|
|
|
|
const mask = p.d ? cursorGrabMask : cursorHandMask
|
|
|
|
|
|
cursors[key] = await Graphics.colorize(cursor, mask, p.color)
|
|
|
|
|
|
}
|
|
|
|
|
|
return cursors[key]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
// Create a canvas and attach adapters to it so we can work with it
|
|
|
|
|
|
const canvas = addCanvasToDom(Graphics.createCanvas())
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
// stuff only available in replay mode...
|
|
|
|
|
|
// TODO: refactor
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const REPLAY = {
|
|
|
|
|
|
log: null,
|
|
|
|
|
|
logIdx: 0,
|
|
|
|
|
|
speeds: [0.5, 1, 2, 5, 10, 20, 50],
|
|
|
|
|
|
speedIdx: 1,
|
|
|
|
|
|
paused: false,
|
|
|
|
|
|
lastRealTs: null,
|
|
|
|
|
|
lastGameTs: null,
|
|
|
|
|
|
gameStartTs: null,
|
|
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
|
if (MODE === MODE_PLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const game = await Communication.connect(gameId, CLIENT_ID)
|
2021-04-30 01:03:43 +02:00
|
|
|
|
const gameObject = Util.decodeGame(game)
|
|
|
|
|
|
Game.setGame(gameObject.id, gameObject)
|
2021-04-17 17:23:32 +02:00
|
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const {game, log} = await Communication.connectReplay(gameId, CLIENT_ID)
|
2021-04-30 01:03:43 +02:00
|
|
|
|
const gameObject = Util.decodeGame(game)
|
|
|
|
|
|
Game.setGame(gameObject.id, gameObject)
|
2021-04-15 12:16:14 +02:00
|
|
|
|
REPLAY.log = log
|
|
|
|
|
|
REPLAY.lastRealTs = Time.timestamp()
|
|
|
|
|
|
REPLAY.gameStartTs = REPLAY.log[0][REPLAY.log[0].length - 1]
|
|
|
|
|
|
REPLAY.lastGameTs = REPLAY.gameStartTs
|
|
|
|
|
|
TIME = () => REPLAY.lastGameTs
|
2020-12-22 22:35:09 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
throw '[ 2020-12-22 MODE invalid, must be play|replay ]'
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-14 21:04:07 +02:00
|
|
|
|
const TILE_DRAW_OFFSET = Game.getTileDrawOffset(gameId)
|
|
|
|
|
|
const TILE_DRAW_SIZE = Game.getTileDrawSize(gameId)
|
|
|
|
|
|
const PUZZLE_WIDTH = Game.getPuzzleWidth(gameId)
|
|
|
|
|
|
const PUZZLE_HEIGHT = Game.getPuzzleHeight(gameId)
|
|
|
|
|
|
const TABLE_WIDTH = Game.getTableWidth(gameId)
|
|
|
|
|
|
const TABLE_HEIGHT = Game.getTableHeight(gameId)
|
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(Game.getPuzzle(gameId))
|
|
|
|
|
|
|
2021-04-19 23:59:14 +02:00
|
|
|
|
const {
|
|
|
|
|
|
bgColorPickerEl,
|
|
|
|
|
|
playerColorPickerEl,
|
|
|
|
|
|
nameChangeEl,
|
|
|
|
|
|
updateScoreBoard,
|
|
|
|
|
|
updateTimer,
|
2021-04-20 23:15:29 +02:00
|
|
|
|
udateTilesDone,
|
2021-04-19 23:59:14 +02:00
|
|
|
|
togglePreview,
|
|
|
|
|
|
replayControl,
|
|
|
|
|
|
} = addMenuToDom(gameId)
|
2021-04-17 16:09:20 +02:00
|
|
|
|
updateTimer()
|
2021-04-20 23:15:29 +02:00
|
|
|
|
udateTilesDone()
|
2021-04-14 21:04:07 +02:00
|
|
|
|
updateScoreBoard(TIME())
|
2020-12-09 01:27:47 +01:00
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const longFinished = !! Game.getFinishTs(gameId)
|
|
|
|
|
|
let finished = longFinished
|
|
|
|
|
|
const justFinished = () => finished && !longFinished
|
2020-12-09 01:27:47 +01:00
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const fireworks = new fireworksController(canvas, Game.getRng(gameId))
|
2020-12-09 01:27:47 +01:00
|
|
|
|
fireworks.init(canvas)
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
const ctx = canvas.getContext('2d')
|
2021-04-14 19:39:26 +02:00
|
|
|
|
canvas.classList.add('loaded')
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
|
|
// initialize some view data
|
|
|
|
|
|
// this global data will change according to input events
|
2021-04-17 19:11:43 +02:00
|
|
|
|
const viewport = new Camera()
|
2020-11-25 22:03:35 +01:00
|
|
|
|
// center viewport
|
|
|
|
|
|
viewport.move(
|
2021-04-17 19:11:43 +02:00
|
|
|
|
-(TABLE_WIDTH - canvas.width) /2,
|
|
|
|
|
|
-(TABLE_HEIGHT - canvas.height) /2
|
2020-11-25 22:03:35 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-12-07 12:20:09 +01:00
|
|
|
|
const playerBgColor = () => {
|
|
|
|
|
|
return (Game.getPlayerBgColor(gameId, CLIENT_ID)
|
|
|
|
|
|
|| localStorage.getItem('bg_color')
|
|
|
|
|
|
|| '#222222')
|
|
|
|
|
|
}
|
|
|
|
|
|
const playerColor = () => {
|
|
|
|
|
|
return (Game.getPlayerColor(gameId, CLIENT_ID)
|
|
|
|
|
|
|| localStorage.getItem('player_color')
|
|
|
|
|
|
|| '#ffffff')
|
|
|
|
|
|
}
|
|
|
|
|
|
const playerName = () => {
|
|
|
|
|
|
return (Game.getPlayerName(gameId, CLIENT_ID)
|
|
|
|
|
|
|| localStorage.getItem('player_name')
|
|
|
|
|
|
|| 'anon')
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-19 23:59:14 +02:00
|
|
|
|
window.addEventListener('keypress', (ev) => {
|
2021-04-27 09:46:39 +02:00
|
|
|
|
if (KEY_LISTENER_OFF) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-04-19 23:59:14 +02:00
|
|
|
|
if (ev.key === ' ') {
|
|
|
|
|
|
togglePreview()
|
|
|
|
|
|
}
|
2021-04-27 22:56:50 +02:00
|
|
|
|
if (ev.key === 'F' || ev.key === 'f') {
|
|
|
|
|
|
PIECE_VIEW_FIXED = !PIECE_VIEW_FIXED
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ev.key === 'G' || ev.key === 'g') {
|
|
|
|
|
|
PIECE_VIEW_LOOSE = !PIECE_VIEW_LOOSE
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
2021-04-19 23:59:14 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const evts = new EventAdapter(canvas, window, viewport)
|
|
|
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
|
if (MODE === MODE_PLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
bgColorPickerEl.value = playerBgColor()
|
2020-12-23 01:19:30 +01:00
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_BG_COLOR, bgColorPickerEl.value])
|
2020-12-22 22:35:09 +01:00
|
|
|
|
bgColorPickerEl.addEventListener('change', () => {
|
|
|
|
|
|
localStorage.setItem('bg_color', bgColorPickerEl.value)
|
2020-12-23 01:19:30 +01:00
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_BG_COLOR, bgColorPickerEl.value])
|
2020-12-22 22:35:09 +01:00
|
|
|
|
})
|
|
|
|
|
|
playerColorPickerEl.value = playerColor()
|
2020-12-23 01:19:30 +01:00
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_COLOR, playerColorPickerEl.value])
|
2020-12-22 22:35:09 +01:00
|
|
|
|
playerColorPickerEl.addEventListener('change', () => {
|
|
|
|
|
|
localStorage.setItem('player_color', playerColorPickerEl.value)
|
2020-12-23 01:19:30 +01:00
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_COLOR, playerColorPickerEl.value])
|
2020-12-22 22:35:09 +01:00
|
|
|
|
})
|
|
|
|
|
|
nameChangeEl.value = playerName()
|
2020-12-23 01:19:30 +01:00
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_NAME, nameChangeEl.value])
|
2020-12-22 22:35:09 +01:00
|
|
|
|
nameChangeEl.addEventListener('change', () => {
|
|
|
|
|
|
localStorage.setItem('player_name', nameChangeEl.value)
|
2020-12-23 01:19:30 +01:00
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_NAME, nameChangeEl.value])
|
2020-12-22 22:35:09 +01:00
|
|
|
|
})
|
2021-04-17 16:09:20 +02:00
|
|
|
|
setInterval(updateTimer, 1000)
|
2021-04-17 17:23:32 +02:00
|
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2021-04-14 19:30:45 +02:00
|
|
|
|
const setSpeedStatus = () => {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
replayControl.speed.innerText = 'Replay-Speed: ' +
|
|
|
|
|
|
(REPLAY.speeds[REPLAY.speedIdx] + 'x') +
|
|
|
|
|
|
(REPLAY.paused ? ' Paused' : '')
|
2020-12-22 22:35:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
setSpeedStatus()
|
|
|
|
|
|
replayControl.speedUp.addEventListener('click', () => {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
if (REPLAY.speedIdx + 1 < REPLAY.speeds.length) {
|
|
|
|
|
|
REPLAY.speedIdx++
|
2020-12-22 22:35:09 +01:00
|
|
|
|
setSpeedStatus()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
replayControl.speedDown.addEventListener('click', () => {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
if (REPLAY.speedIdx >= 1) {
|
|
|
|
|
|
REPLAY.speedIdx--
|
2020-12-22 22:35:09 +01:00
|
|
|
|
setSpeedStatus()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
replayControl.pause.addEventListener('click', () => {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
REPLAY.paused = !REPLAY.paused
|
2020-12-22 22:35:09 +01:00
|
|
|
|
setSpeedStatus()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
|
if (MODE === MODE_PLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
Communication.onServerChange((msg) => {
|
|
|
|
|
|
const msgType = msg[0]
|
|
|
|
|
|
const evClientId = msg[1]
|
|
|
|
|
|
const evClientSeq = msg[2]
|
|
|
|
|
|
const evChanges = msg[3]
|
|
|
|
|
|
for(let [changeType, changeData] of evChanges) {
|
|
|
|
|
|
switch (changeType) {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
case Protocol.CHANGE_PLAYER: {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const p = Util.decodePlayer(changeData)
|
|
|
|
|
|
if (p.id !== CLIENT_ID) {
|
|
|
|
|
|
Game.setPlayer(gameId, p.id, p)
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
|
|
|
|
|
} break;
|
2021-04-19 23:59:14 +02:00
|
|
|
|
case Protocol.CHANGE_TILE: {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const t = Util.decodeTile(changeData)
|
|
|
|
|
|
Game.setTile(gameId, t.idx, t)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
RERENDER = true
|
2020-12-22 22:35:09 +01:00
|
|
|
|
} break;
|
2021-04-19 23:59:14 +02:00
|
|
|
|
case Protocol.CHANGE_DATA: {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
Game.setPuzzleData(gameId, changeData)
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
} break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-15 12:16:14 +02:00
|
|
|
|
finished = !! Game.getFinishTs(gameId)
|
2020-12-22 22:35:09 +01:00
|
|
|
|
})
|
2021-04-17 17:23:32 +02:00
|
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
// no external communication for replay mode,
|
2021-04-15 12:16:14 +02:00
|
|
|
|
// only the REPLAY.log is relevant
|
2020-12-22 22:35:09 +01:00
|
|
|
|
let inter = setInterval(() => {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const realTs = Time.timestamp()
|
|
|
|
|
|
if (REPLAY.paused) {
|
|
|
|
|
|
REPLAY.lastRealTs = realTs
|
2020-12-22 22:35:09 +01:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const timePassedReal = realTs - REPLAY.lastRealTs
|
|
|
|
|
|
const timePassedGame = timePassedReal * REPLAY.speeds[REPLAY.speedIdx]
|
|
|
|
|
|
const maxGameTs = REPLAY.lastGameTs + timePassedGame
|
2020-12-22 22:35:09 +01:00
|
|
|
|
do {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
if (REPLAY.paused) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
break
|
|
|
|
|
|
}
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const nextIdx = REPLAY.logIdx + 1
|
|
|
|
|
|
if (nextIdx >= REPLAY.log.length) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
clearInterval(inter)
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const logEntry = REPLAY.log[nextIdx]
|
|
|
|
|
|
const nextTs = REPLAY.gameStartTs + logEntry[logEntry.length - 1]
|
2020-12-22 22:35:09 +01:00
|
|
|
|
if (nextTs > maxGameTs) {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const entryWithTs = logEntry.slice()
|
2020-12-23 01:19:30 +01:00
|
|
|
|
entryWithTs[entryWithTs.length - 1] = nextTs
|
|
|
|
|
|
if (entryWithTs[0] === Protocol.LOG_ADD_PLAYER) {
|
|
|
|
|
|
Game.addPlayer(gameId, ...entryWithTs.slice(1))
|
2020-11-25 22:03:35 +01:00
|
|
|
|
RERENDER = true
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (entryWithTs[0] === Protocol.LOG_UPDATE_PLAYER) {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
2020-12-23 01:19:30 +01:00
|
|
|
|
Game.addPlayer(gameId, playerId, ...entryWithTs.slice(2))
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
} else if (entryWithTs[0] === Protocol.LOG_HANDLE_INPUT) {
|
2021-04-15 12:16:14 +02:00
|
|
|
|
const playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
2020-12-23 01:19:30 +01:00
|
|
|
|
Game.handleInput(gameId, playerId, ...entryWithTs.slice(2))
|
2020-11-25 22:03:35 +01:00
|
|
|
|
RERENDER = true
|
2020-12-22 22:35:09 +01:00
|
|
|
|
}
|
2021-04-15 12:16:14 +02:00
|
|
|
|
REPLAY.logIdx = nextIdx
|
2020-12-22 22:35:09 +01:00
|
|
|
|
} while (true)
|
2021-04-15 12:16:14 +02:00
|
|
|
|
REPLAY.lastRealTs = realTs
|
|
|
|
|
|
REPLAY.lastGameTs = maxGameTs
|
2021-04-17 16:09:20 +02:00
|
|
|
|
updateTimer()
|
2020-12-22 22:35:09 +01:00
|
|
|
|
}, 50)
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
|
|
let _last_mouse_down = null
|
|
|
|
|
|
const onUpdate = () => {
|
2021-04-19 23:59:14 +02:00
|
|
|
|
// handle key downs once per onUpdate
|
|
|
|
|
|
// this will create Protocol.INPUT_EV_MOVE events if something
|
|
|
|
|
|
// relevant is pressed
|
|
|
|
|
|
evts._keydowns()
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
for (let evt of evts.consumeAll()) {
|
2021-04-17 17:23:32 +02:00
|
|
|
|
if (MODE === MODE_PLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
// LOCAL ONLY CHANGES
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
|
const type = evt[0]
|
2021-04-19 23:59:14 +02:00
|
|
|
|
if (type === Protocol.INPUT_EV_MOVE) {
|
|
|
|
|
|
const diffX = evt[1]
|
|
|
|
|
|
const diffY = evt[2]
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
viewport.move(diffX, diffY)
|
|
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_MOVE) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
if (_last_mouse_down && !Game.getFirstOwnedTile(gameId, CLIENT_ID)) {
|
|
|
|
|
|
// move the cam
|
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
|
const mouse = viewport.worldToViewport(pos)
|
|
|
|
|
|
const diffX = Math.round(mouse.x - _last_mouse_down.x)
|
|
|
|
|
|
const diffY = Math.round(mouse.y - _last_mouse_down.y)
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
viewport.move(diffX, diffY)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
|
_last_mouse_down = mouse
|
|
|
|
|
|
}
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_DOWN) {
|
2020-12-05 19:45:34 +01:00
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2020-12-22 22:35:09 +01:00
|
|
|
|
_last_mouse_down = viewport.worldToViewport(pos)
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_UP) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
_last_mouse_down = null
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_IN) {
|
2021-04-15 10:01:27 +02:00
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
|
if (viewport.zoomIn(viewport.worldToViewport(pos))) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
RERENDER = true
|
|
|
|
|
|
Game.changePlayer(gameId, CLIENT_ID, pos)
|
|
|
|
|
|
}
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_OUT) {
|
2021-04-15 10:01:27 +02:00
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
|
if (viewport.zoomOut(viewport.worldToViewport(pos))) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
RERENDER = true
|
|
|
|
|
|
Game.changePlayer(gameId, CLIENT_ID, pos)
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
|
|
|
|
|
|
|
// LOCAL + SERVER CHANGES
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
|
const ts = TIME()
|
|
|
|
|
|
const changes = Game.handleInput(GAME_ID, CLIENT_ID, evt, ts)
|
|
|
|
|
|
if (changes.length > 0) {
|
2020-11-25 22:03:35 +01:00
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
|
Communication.sendClientEvent(evt)
|
2021-04-17 17:23:32 +02:00
|
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
// LOCAL ONLY CHANGES
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
|
const type = evt[0]
|
2020-12-23 01:19:30 +01:00
|
|
|
|
if (type === Protocol.INPUT_EV_MOUSE_MOVE) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
if (_last_mouse_down) {
|
|
|
|
|
|
// move the cam
|
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
|
const mouse = viewport.worldToViewport(pos)
|
|
|
|
|
|
const diffX = Math.round(mouse.x - _last_mouse_down.x)
|
|
|
|
|
|
const diffY = Math.round(mouse.y - _last_mouse_down.y)
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
viewport.move(diffX, diffY)
|
|
|
|
|
|
|
|
|
|
|
|
_last_mouse_down = mouse
|
|
|
|
|
|
}
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_DOWN) {
|
2020-11-25 22:03:35 +01:00
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2020-12-22 22:35:09 +01:00
|
|
|
|
_last_mouse_down = viewport.worldToViewport(pos)
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_UP) {
|
2020-12-22 22:35:09 +01:00
|
|
|
|
_last_mouse_down = null
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_IN) {
|
2021-04-15 10:01:27 +02:00
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2021-04-15 10:06:20 +02:00
|
|
|
|
if (viewport.zoomIn(viewport.worldToViewport(pos))) {
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
2020-12-23 01:19:30 +01:00
|
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_OUT) {
|
2021-04-15 10:01:27 +02:00
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2021-04-15 10:06:20 +02:00
|
|
|
|
if (viewport.zoomOut(viewport.worldToViewport(pos))) {
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-12-09 01:27:47 +01:00
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
|
finished = !! Game.getFinishTs(gameId)
|
2020-12-09 01:27:47 +01:00
|
|
|
|
if (justFinished()) {
|
|
|
|
|
|
fireworks.update()
|
|
|
|
|
|
RERENDER = true
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const onRender = async () => {
|
|
|
|
|
|
if (!RERENDER) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let pos
|
|
|
|
|
|
let dim
|
|
|
|
|
|
|
|
|
|
|
|
if (DEBUG) Debug.checkpoint_start(0)
|
|
|
|
|
|
|
|
|
|
|
|
// CLEAR CTX
|
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-07 12:20:09 +01:00
|
|
|
|
ctx.fillStyle = playerBgColor()
|
2020-11-25 22:03:35 +01:00
|
|
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
|
|
|
|
if (DEBUG) Debug.checkpoint('clear done')
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW BOARD
|
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-24 20:17:01 +01:00
|
|
|
|
pos = viewport.worldToViewportRaw({
|
2021-04-14 21:04:07 +02:00
|
|
|
|
x: (TABLE_WIDTH - PUZZLE_WIDTH) / 2,
|
|
|
|
|
|
y: (TABLE_HEIGHT - PUZZLE_HEIGHT) / 2
|
2020-11-25 22:03:35 +01:00
|
|
|
|
})
|
2020-12-24 20:17:01 +01:00
|
|
|
|
dim = viewport.worldDimToViewportRaw({
|
2021-04-14 21:04:07 +02:00
|
|
|
|
w: PUZZLE_WIDTH,
|
|
|
|
|
|
h: PUZZLE_HEIGHT,
|
2020-11-25 22:03:35 +01:00
|
|
|
|
})
|
2020-12-24 20:17:01 +01:00
|
|
|
|
ctx.fillStyle = 'rgba(255, 255, 255, .3)'
|
2020-11-25 22:03:35 +01:00
|
|
|
|
ctx.fillRect(pos.x, pos.y, dim.w, dim.h)
|
|
|
|
|
|
if (DEBUG) Debug.checkpoint('board done')
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW TILES
|
|
|
|
|
|
// ---------------------------------------------------------------
|
2021-04-15 12:42:47 +02:00
|
|
|
|
const tiles = Game.getTilesSortedByZIndex(gameId)
|
|
|
|
|
|
if (DEBUG) Debug.checkpoint('get tiles done')
|
|
|
|
|
|
|
|
|
|
|
|
for (let tile of tiles) {
|
2021-04-27 22:56:50 +02:00
|
|
|
|
if (tile.owner === -1) {
|
|
|
|
|
|
if (!PIECE_VIEW_FIXED) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (!PIECE_VIEW_LOOSE) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
const bmp = bitmaps[tile.idx]
|
2020-12-24 20:17:01 +01:00
|
|
|
|
pos = viewport.worldToViewportRaw({
|
2021-04-14 21:04:07 +02:00
|
|
|
|
x: TILE_DRAW_OFFSET + tile.pos.x,
|
|
|
|
|
|
y: TILE_DRAW_OFFSET + tile.pos.y,
|
2020-11-25 22:03:35 +01:00
|
|
|
|
})
|
2020-12-24 20:17:01 +01:00
|
|
|
|
dim = viewport.worldDimToViewportRaw({
|
2021-04-14 21:04:07 +02:00
|
|
|
|
w: TILE_DRAW_SIZE,
|
|
|
|
|
|
h: TILE_DRAW_SIZE,
|
2020-11-25 22:03:35 +01:00
|
|
|
|
})
|
|
|
|
|
|
ctx.drawImage(bmp,
|
|
|
|
|
|
0, 0, bmp.width, bmp.height,
|
|
|
|
|
|
pos.x, pos.y, dim.w, dim.h
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (DEBUG) Debug.checkpoint('tiles done')
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW PLAYERS
|
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-22 22:35:09 +01:00
|
|
|
|
const ts = TIME()
|
2021-04-17 17:23:32 +02:00
|
|
|
|
const texts = []
|
|
|
|
|
|
// Cursors
|
2020-12-22 22:35:09 +01:00
|
|
|
|
for (let player of Game.getActivePlayers(gameId, ts)) {
|
2020-12-05 19:45:34 +01:00
|
|
|
|
const cursor = await getPlayerCursor(player)
|
2021-04-17 17:23:32 +02:00
|
|
|
|
const pos = viewport.worldToViewport(player)
|
|
|
|
|
|
ctx.drawImage(cursor, pos.x - CURSOR_W_2, pos.y - CURSOR_H_2)
|
|
|
|
|
|
if (
|
|
|
|
|
|
(MODE === MODE_PLAY && player.id !== CLIENT_ID)
|
|
|
|
|
|
|| (MODE === MODE_REPLAY)
|
|
|
|
|
|
) {
|
|
|
|
|
|
// performance:
|
|
|
|
|
|
// not drawing text directly here, to have less ctx
|
|
|
|
|
|
// switches between drawImage and fillTxt
|
|
|
|
|
|
texts.push([
|
|
|
|
|
|
`${player.name} (${player.points})`,
|
|
|
|
|
|
pos.x,
|
|
|
|
|
|
pos.y + CURSOR_H,
|
|
|
|
|
|
])
|
2020-11-25 22:03:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-17 17:23:32 +02:00
|
|
|
|
|
|
|
|
|
|
// Names
|
|
|
|
|
|
ctx.fillStyle = 'white'
|
|
|
|
|
|
ctx.textAlign = 'center'
|
|
|
|
|
|
for (let [txt, x, y] of texts) {
|
|
|
|
|
|
ctx.fillText(txt, x, y)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
|
if (DEBUG) Debug.checkpoint('players done')
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW PLAYERS
|
|
|
|
|
|
// ---------------------------------------------------------------
|
2021-04-14 21:04:07 +02:00
|
|
|
|
updateScoreBoard(ts)
|
2021-04-20 23:15:29 +02:00
|
|
|
|
udateTilesDone()
|
2020-11-25 22:03:35 +01:00
|
|
|
|
if (DEBUG) Debug.checkpoint('scores done')
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
2020-12-09 01:27:47 +01:00
|
|
|
|
if (justFinished()) {
|
|
|
|
|
|
fireworks.render()
|
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
|
|
RERENDER = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
run({
|
|
|
|
|
|
update: onUpdate,
|
|
|
|
|
|
render: onRender,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
main()
|