|
|
|
@ -4,15 +4,20 @@ const Controls = function() { |
|
|
|
|
this.playerId = null; |
|
|
|
|
this.countdownTimer = null; |
|
|
|
|
|
|
|
|
|
this.currentMoveCount = 0; |
|
|
|
|
this.winningMoveCount = Infinity; |
|
|
|
|
|
|
|
|
|
// "Local" and "Global" messages
|
|
|
|
|
document.addEventListener('L-conn-error', this.msgConnectionError.bind(this)); |
|
|
|
|
document.addEventListener('L-complete', this.msgComplete.bind(this)); |
|
|
|
|
document.addEventListener('L-join', this.msgJoin.bind(this)); |
|
|
|
|
document.addEventListener('L-newround', this.msgNewRound.bind(this)); |
|
|
|
|
document.addEventListener('L-skip', this.msgSkip.bind(this)); |
|
|
|
|
document.addEventListener('L-stack', this.msgStack.bind(this)); |
|
|
|
|
|
|
|
|
|
document.addEventListener('G-connected', this.msgConnected.bind(this));
|
|
|
|
|
document.addEventListener('G-countdown', this.msgCountdown.bind(this));
|
|
|
|
|
document.addEventListener('G-full', this.msgFull.bind(this));
|
|
|
|
|
document.addEventListener('G-players', this.msgPlayers.bind(this));
|
|
|
|
|
document.addEventListener('G-state', this.msgState.bind(this)); |
|
|
|
|
document.addEventListener('G-win', this.msgWin.bind(this)); |
|
|
|
@ -97,30 +102,30 @@ Controls.prototype.drawPlayers = function() { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const keys = Object.keys(this.names); |
|
|
|
|
if (keys.length > 1) { |
|
|
|
|
keys.forEach(connectionId => { |
|
|
|
|
const n = document.createElement('div'); |
|
|
|
|
n.innerHTML = this.playerId === connectionId ? `${this.names[connectionId]} (you)` : this.names[connectionId]; |
|
|
|
|
n.className = 'controls-player'; |
|
|
|
|
container.appendChild(n) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
keys.forEach(connectionId => { |
|
|
|
|
const n = document.createElement('div'); |
|
|
|
|
n.innerHTML = this.playerId === connectionId ? `${this.names[connectionId]} (you)` : this.names[connectionId]; |
|
|
|
|
n.className = 'controls-player'; |
|
|
|
|
container.appendChild(n) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const msg = container.querySelector('.controls-message'); |
|
|
|
|
msg.innerHTML = (keys.length > 1 ? '' : 'Nobody is in the game yet.'); |
|
|
|
|
msg.style.display = (keys.length > 1 ? 'none' : 'block'); |
|
|
|
|
msg.innerHTML = (keys.length > 0 ? '' : 'Nobody is in the game yet.'); |
|
|
|
|
msg.style.display = (keys.length > 0 ? 'none' : 'block'); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//===== Message handlers
|
|
|
|
|
|
|
|
|
|
Controls.prototype.msgJoin = function() { |
|
|
|
|
this.setState('CONNECTING'); |
|
|
|
|
|
|
|
|
|
document.querySelector('#controls-connection .controls-message').innerHTML = 'Connecting...'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgComplete = function(evt) { |
|
|
|
|
document.getElementById('controls-moves-solve').style.display = (evt.detail.complete ? 'block' : 'none'); |
|
|
|
|
if (this.currentMoveCount < this.winningMoveCount) { |
|
|
|
|
document.getElementById('controls-moves-solve').style.display = (evt.detail.complete ? 'block' : 'none'); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgConnected = function(evt) { |
|
|
|
@ -128,12 +133,24 @@ Controls.prototype.msgConnected = function(evt) { |
|
|
|
|
this.drawPlayers(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgConnectionError = function() { |
|
|
|
|
Controls.prototype.msgConnectionError = function(evt) { |
|
|
|
|
this.setState('CONNECTING'); |
|
|
|
|
document.querySelector('#controls-connection .controls-message').innerHTML = "Can't reach the server."; |
|
|
|
|
|
|
|
|
|
const msg = (evt.detail === 'noname'
|
|
|
|
|
? "Didn't get your name - refresh to try again."
|
|
|
|
|
: "Can't reach the server."); |
|
|
|
|
|
|
|
|
|
document.querySelector('#controls-connection .controls-message').innerHTML = msg; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgFull = function() { |
|
|
|
|
this.setState('CONNECTING'); |
|
|
|
|
document.querySelector('#controls-connection .controls-message').innerHTML = "Sorry, the game is full right now. Please try again later."; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgCountdown = function(evt) { |
|
|
|
|
this.winningMoveCount = evt.detail.body.moveCount; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-countdown-moves').innerHTML = evt.detail.body.moveCount; |
|
|
|
|
document.getElementById('controls-countdown-player').innerHTML = `${this.names[evt.detail.body.id]} has a solution!`; |
|
|
|
|
|
|
|
|
@ -152,14 +169,14 @@ Controls.prototype.msgSkip = function() { |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgStack = function(evt) { |
|
|
|
|
const robots = evt.detail.reduce((acc, { id }) => acc.has(id) ? acc : acc.add(id), new Set()); |
|
|
|
|
const moveCount = evt.detail.length - robots.size; |
|
|
|
|
this.currentMoveCount = evt.detail.length - robots.size; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-moves-solve').style.display = 'none'; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-moves-count').innerHTML = moveCount; |
|
|
|
|
document.getElementById('controls-moves-count').innerHTML = this.currentMoveCount; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-moves-reset').style.display = moveCount > 0 ? 'block' : 'none'; |
|
|
|
|
document.getElementById('controls-moves-undo').style.display = moveCount > 0 ? 'block' : 'none'; |
|
|
|
|
document.getElementById('controls-moves-reset').style.display = this.currentMoveCount > 0 ? 'block' : 'none'; |
|
|
|
|
document.getElementById('controls-moves-undo').style.display = this.currentMoveCount > 0 ? 'block' : 'none'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgPlayers = function(evt) { |
|
|
|
@ -176,6 +193,10 @@ Controls.prototype.msgWin = function(evt) { |
|
|
|
|
document.getElementById('controls-win-count').innerHTML = evt.detail.body.moveCount; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgNewRound = function() { |
|
|
|
|
this.winningMoveCount = Infinite; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//===== Click handlers
|
|
|
|
|
|
|
|
|
|
Controls.prototype.dispatch = function(evt, data) { |
|
|
|
|