|
|
|
@ -4,39 +4,58 @@ const Controls = function() { |
|
|
|
|
this.moves = 0; |
|
|
|
|
this.names = {}; |
|
|
|
|
|
|
|
|
|
// this.buildGuesses();
|
|
|
|
|
// this.setUi('connecting');
|
|
|
|
|
this.drawGuesses(); |
|
|
|
|
|
|
|
|
|
document.addEventListener('L-conn-open', this.msgJoin.bind(this)); |
|
|
|
|
document.addEventListener('L-move', this.msgMove.bind(this)); |
|
|
|
|
|
|
|
|
|
// Message handlers: "Local message" and "Global message"
|
|
|
|
|
// document.addEventListener('G-move', this.onMoveIncrement.bind(this));
|
|
|
|
|
// document.addEventListener('G-win', this.onMoveIncrement.bind(this));
|
|
|
|
|
// document.addEventListener('G-start', this.msgStartGame.bind(this));
|
|
|
|
|
// document.addEventListener('G-stop', this.msgStartGame.bind(this));
|
|
|
|
|
// document.addEventListener('G-players', this.msgStartGame.bind(this)); <--- controls.playersUpdate(data.body)
|
|
|
|
|
// document.addEventListener('G-move', this.msgMove.bind(this));
|
|
|
|
|
// document.addEventListener('G-win', this.msgWin.bind(this));
|
|
|
|
|
document.addEventListener('G-guess', this.msgGuess.bind(this));
|
|
|
|
|
document.addEventListener('G-skip', this.msgSkip.bind(this)); |
|
|
|
|
document.addEventListener('G-start', this.msgStart.bind(this)); |
|
|
|
|
document.addEventListener('G-stop', this.msgStop.bind(this)); |
|
|
|
|
document.addEventListener('G-players', this.msgPlayers.bind(this));
|
|
|
|
|
|
|
|
|
|
// document.addEventListener('G-walls', this.msgStartGame.bind(this));
|
|
|
|
|
// document.addEventListener('G-robots', this.msgStartGame.bind(this));
|
|
|
|
|
// document.addEventListener('G-guess', this.msgStartGame.bind(this)); <--- controls.onReceiveGuess(data.body);
|
|
|
|
|
|
|
|
|
|
// document.addEventListener('L-move', this.onMoveIncrement.bind(this));
|
|
|
|
|
|
|
|
|
|
// document.addEventListener('L-conn-error', this.onConnectionError.bind(this));
|
|
|
|
|
// document.addEventListener('L-skip-timer', this.msgStartGame.bind(this));
|
|
|
|
|
// document.addEventListener('L-skip-timer', this.msgStartGame.bind(this));
|
|
|
|
|
|
|
|
|
|
// Click handlers
|
|
|
|
|
// document.getElementById('controls-moves-reset').addEventListener('click', this.onMoveReset.bind(this));
|
|
|
|
|
// document.getElementById('controls-start-game').addEventListener('click', this.onStartGame.bind(this));
|
|
|
|
|
// document.getElementById('controls-stop-game').addEventListener('click', this.onStopGame.bind(this));
|
|
|
|
|
// document.getElementById('controls-regenerate-walls').addEventListener('click', this.onRegenWalls.bind(this));
|
|
|
|
|
// document.getElementById('controls-reposition-robots').addEventListener('click', this.onRepositionRobots.bind(this));
|
|
|
|
|
// document.getElementById('controls-timer-skip').addEventListener('click', this.onSkipTimer.bind(this));
|
|
|
|
|
document.getElementById('controls-start').addEventListener('click', this.onClickStart.bind(this)); |
|
|
|
|
document.getElementById('controls-stop').addEventListener('click', this.onClickStop.bind(this)); |
|
|
|
|
document.getElementById('controls-walls').addEventListener('click', this.onClickWalls.bind(this)); |
|
|
|
|
document.getElementById('controls-robots').addEventListener('click', this.onClickRobots.bind(this)); |
|
|
|
|
document.getElementById('controls-reset').addEventListener('click', this.onClickReset.bind(this)); |
|
|
|
|
document.getElementById('controls-skip').addEventListener('click', this.onClickSkip.bind(this)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//===== UI
|
|
|
|
|
|
|
|
|
|
Controls.prototype.countdownStart = function(seconds) { |
|
|
|
|
const countdown = document.getElementById('controls-countdown'); |
|
|
|
|
countdown.dataset.tick = seconds - 1; |
|
|
|
|
|
|
|
|
|
setTimeout(this.countdownTick.bind(this), 1000); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.countdownTick = function() { |
|
|
|
|
const countdown = document.getElementById('controls-countdown'); |
|
|
|
|
const tick = countdown.dataset.tick * 1; |
|
|
|
|
countdown.dataset.tick = tick - 1; |
|
|
|
|
|
|
|
|
|
if (tick === 0) { |
|
|
|
|
this.countdownComplete(); |
|
|
|
|
return; |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const s = (tick !== 1) ? 's' : ''; |
|
|
|
|
countdown.innerHTML = `${tick} second${s}!`; |
|
|
|
|
setTimeout(this.countdownTick.bind(this), 1000); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.countdownComplete = function() { |
|
|
|
|
alert("boom") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.drawGuesses = function() { |
|
|
|
|
const container = document.getElementById('controls-guesses'); |
|
|
|
|
container.querySelectorAll('.controls-guess').forEach(el => el.parentNode.removeChild(el)); |
|
|
|
@ -58,117 +77,131 @@ Controls.prototype.showWaiting = function() { |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-stop').parentNode.style.display = 'none'; |
|
|
|
|
// document.getElementById('controls-moves-reset').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-timer-skip').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-guesses').parentNode.style.display = 'none';
|
|
|
|
|
document.getElementById('controls-guesses').style.display = 'none'; |
|
|
|
|
document.getElementById('controls-panic').style.display = 'none'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.showGuessing = function() { |
|
|
|
|
document.getElementById('controls-start').parentNode.style.display = 'none'; |
|
|
|
|
document.getElementById('controls-walls').parentNode.style.display = 'none'; |
|
|
|
|
document.getElementById('controls-robots').parentNode.style.display = 'none'; |
|
|
|
|
document.getElementById('controls-panic').style.display = 'none'; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-stop').parentNode.style.display = ''; |
|
|
|
|
// document.getElementById('controls-moves-reset').parentNode.style.display = '';
|
|
|
|
|
document.getElementById('controls-guesses').style.display = ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.showPanic = function() { |
|
|
|
|
this.showGuessing(); |
|
|
|
|
document.getElementById('controls-panic').style.display = ''; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//===== Message handlers
|
|
|
|
|
|
|
|
|
|
Controls.prototype.msgGuess = function(evt) {
|
|
|
|
|
const blurbs = [ " has a solution: ", " can do it in ", " says, maybe ", " wagers ",
|
|
|
|
|
" reckons ", " is pretty sure it's ", ", confidently: ", " wants it to be ",
|
|
|
|
|
" says ", " hazards ", " guesses ", " thinks it might be "]; |
|
|
|
|
const blurb = blurbs[Math.floor(Math.random() * blurbs.length)]; |
|
|
|
|
|
|
|
|
|
const msg = evt.detail; |
|
|
|
|
const guess = msg.guess; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-panic').querySelector('.controls-alert-urgent').innerHTML = (`${this.names[msg.id]}${blurb}${guess} moves.`); |
|
|
|
|
this.showPanic(); |
|
|
|
|
this.countdownStart(30); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgJoin = function() { |
|
|
|
|
this.showWaiting(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Controls.prototype.playersUpdate = function(names) {
|
|
|
|
|
// const container = document.getElementById('controls-players');
|
|
|
|
|
// const keys = Object.keys(names);
|
|
|
|
|
Controls.prototype.msgMove = function() { |
|
|
|
|
this.moves++; |
|
|
|
|
document.getElementById('controls-moves').innerHTML = this.moves; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgPlayers = function(evt) { |
|
|
|
|
const container = document.getElementById('controls-players'); |
|
|
|
|
const names = evt.detail.body; |
|
|
|
|
const keys = Object.keys(names); |
|
|
|
|
|
|
|
|
|
// if (keys.length > 0) {
|
|
|
|
|
// const nobody = document.getElementById('controls-players-nobody');
|
|
|
|
|
// nobody.parentNode.removeChild(nobody);
|
|
|
|
|
// }
|
|
|
|
|
if (keys.length > 0) { |
|
|
|
|
const nobody = document.getElementById('controls-players-nobody'); |
|
|
|
|
nobody && nobody.parentNode.removeChild(nobody); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// keys.forEach(connectionId => {
|
|
|
|
|
// const id = `player-${connectionId}`;
|
|
|
|
|
|
|
|
|
|
// if (document.getElementById(id)) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const n = document.createElement('div');
|
|
|
|
|
// n.id = id;
|
|
|
|
|
// n.innerHTML = names[connectionId];
|
|
|
|
|
// n.className = 'controls-player';
|
|
|
|
|
// container.appendChild(n)
|
|
|
|
|
// });
|
|
|
|
|
// console.log(names)
|
|
|
|
|
// this.names = names;
|
|
|
|
|
|
|
|
|
|
// // container.querySelectorAll('.controls-player').forEach(el => {
|
|
|
|
|
// // if (!names[el.id]) {
|
|
|
|
|
// // container.removeChild(el);
|
|
|
|
|
// // }
|
|
|
|
|
// // })
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onReceiveGuess = function(message) {
|
|
|
|
|
// console.log(this.names[message.id] + ' has a solution')
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onClickGuess = function(evt) {
|
|
|
|
|
// const e = new CustomEvent('guess', { detail: { moves: evt.currentTarget.dataset.value } });
|
|
|
|
|
// document.dispatchEvent(e);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onMoveIncrement = function() {
|
|
|
|
|
// this.moves++;
|
|
|
|
|
// document.getElementById('controls-moves').innerHTML = this.moves;
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onMoveReset = function() {
|
|
|
|
|
// this.moves = 0;
|
|
|
|
|
// document.getElementById('controls-moves').innerHTML = this.moves;
|
|
|
|
|
|
|
|
|
|
// const event = new Event('board-reset');
|
|
|
|
|
// document.dispatchEvent(event);
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onStartGame = function() {
|
|
|
|
|
// this.setUi('gameStarted');
|
|
|
|
|
|
|
|
|
|
// const evt = new Event('startGame');
|
|
|
|
|
// document.dispatchEvent(evt);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onStopGame = function() {
|
|
|
|
|
// this.setUi('gameStopped');
|
|
|
|
|
|
|
|
|
|
// const evt = new Event('stopGame');
|
|
|
|
|
// document.dispatchEvent(evt);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onRegenWalls = function() {
|
|
|
|
|
// const evt = new Event('regenerateWalls');
|
|
|
|
|
// document.dispatchEvent(evt);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.onRepositionRobots = function() {
|
|
|
|
|
// const evt = new Event('repositionRobots');
|
|
|
|
|
// document.dispatchEvent(evt);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Controls.prototype.setUi = function(state) {
|
|
|
|
|
// switch(state) {
|
|
|
|
|
// case 'gameStarted':
|
|
|
|
|
// document.getElementById('controls-start-round').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-regenerate-walls').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-reposition-robots').parentNode.style.display = 'none';
|
|
|
|
|
|
|
|
|
|
// document.getElementById('controls-stop-round').parentNode.style.display = '';
|
|
|
|
|
// document.getElementById('controls-moves-reset').parentNode.style.display = '';
|
|
|
|
|
// document.getElementById('controls-timer-skip').parentNode.style.display = '';
|
|
|
|
|
// document.getElementById('controls-guesses').parentNode.style.display = '';
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
|
|
// case 'gameStopped':
|
|
|
|
|
// document.getElementById('controls-start-round').parentNode.style.display = '';
|
|
|
|
|
// document.getElementById('controls-regenerate-walls').parentNode.style.display = '';
|
|
|
|
|
// document.getElementById('controls-reposition-robots').parentNode.style.display = '';
|
|
|
|
|
|
|
|
|
|
// document.getElementById('controls-stop-round').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-moves-reset').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-timer-skip').parentNode.style.display = 'none';
|
|
|
|
|
// document.getElementById('controls-guesses').parentNode.style.display = 'none';
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
keys.forEach(connectionId => { |
|
|
|
|
const id = `player-${connectionId}`; |
|
|
|
|
|
|
|
|
|
if (document.getElementById(id)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const n = document.createElement('div'); |
|
|
|
|
n.id = id; |
|
|
|
|
n.innerHTML = names[connectionId]; |
|
|
|
|
n.className = 'controls-player'; |
|
|
|
|
container.appendChild(n) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.names = names; |
|
|
|
|
|
|
|
|
|
container.querySelectorAll('.controls-player').forEach(el => { |
|
|
|
|
const id = el.id.split('player-').pop(); |
|
|
|
|
if (!this.names[id]) { |
|
|
|
|
container.removeChild(el); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgSkip = function() { |
|
|
|
|
this.coundownComplete(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgStart = function() { |
|
|
|
|
this.showGuessing(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgStop = function() { |
|
|
|
|
this.showWaiting(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//===== Click handlers
|
|
|
|
|
|
|
|
|
|
Controls.prototype.dispatch = function(evt, data) { |
|
|
|
|
const e = (data ? new CustomEvent(evt, { detail: data }) : new Event(evt)); |
|
|
|
|
document.dispatchEvent(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickGuess = function(evt) { |
|
|
|
|
this.dispatch('L-guess', { moves: evt.currentTarget.dataset.value }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickRobots = function() { |
|
|
|
|
this.dispatch('L-robots'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickSkip = function() { |
|
|
|
|
this.dispatch('L-skip'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickStart = function() { |
|
|
|
|
this.dispatch('L-start'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickStop = function() { |
|
|
|
|
this.dispatch('L-stop'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickWalls = function() { |
|
|
|
|
this.dispatch('L-walls'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Controls.prototype.onClickReset = function() { |
|
|
|
|
this.moves = 0; |
|
|
|
|
document.getElementById('controls-moves').innerHTML = this.moves; |
|
|
|
|
|
|
|
|
|
this.dispatch('L-reset'); |
|
|
|
|
}; |
|
|
|
|