|
|
|
@ -1,34 +1,38 @@ |
|
|
|
|
//===== Constructor
|
|
|
|
|
|
|
|
|
|
const Controls = function() { |
|
|
|
|
this.moves = 0; |
|
|
|
|
this.names = {}; |
|
|
|
|
this.positions = []; |
|
|
|
|
this.timers = {}; |
|
|
|
|
this.currentWinningGuess = Infinity; |
|
|
|
|
|
|
|
|
|
this.drawGuesses(); |
|
|
|
|
// this.drawGuesses();
|
|
|
|
|
|
|
|
|
|
// Message handlers: "Local message" and "Global message"
|
|
|
|
|
document.addEventListener('L-connected', this.msgConnected.bind(this)); |
|
|
|
|
document.addEventListener('L-move', this.msgMove.bind(this)); |
|
|
|
|
document.addEventListener('L-reset', this.msgReset.bind(this)); |
|
|
|
|
document.addEventListener('L-undo', this.msgUndo.bind(this)); |
|
|
|
|
|
|
|
|
|
// Message handlers: "Local message" and "Global message"
|
|
|
|
|
// document.addEventListener('G-move', this.msgMove.bind(this));
|
|
|
|
|
// document.addEventListener('G-win', this.msgWin.bind(this));
|
|
|
|
|
document.addEventListener('G-attempt', this.msgAttempt.bind(this));
|
|
|
|
|
document.addEventListener('G-guess', this.msgGuess.bind(this));
|
|
|
|
|
document.addEventListener('G-players', this.msgPlayers.bind(this));
|
|
|
|
|
document.addEventListener('G-robots', this.msgPlayers.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));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Click handlers
|
|
|
|
|
document.getElementById('controls-reset').addEventListener('click', this.onClickReset.bind(this)); |
|
|
|
|
document.getElementById('controls-robots').addEventListener('click', this.onClickRobots.bind(this)); |
|
|
|
|
document.getElementById('controls-skip').addEventListener('click', this.onClickSkip.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-undo').addEventListener('click', this.onClickUndo.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
|
|
|
|
@ -104,6 +108,14 @@ Controls.prototype.showPanic = function() { |
|
|
|
|
document.getElementById('controls-panic').style.display = ''; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.updateMoves = function() { |
|
|
|
|
// Initial robot placement is first move, to allow undo, but doesn't count as a move.
|
|
|
|
|
const moves = this.positions.length - 1; |
|
|
|
|
|
|
|
|
|
document.getElementById('controls-moves').innerHTML = moves; |
|
|
|
|
document.getElementById('controls-undo').style.display = moves > 0 ? 'auto' : 'none'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//===== Message handlers
|
|
|
|
|
|
|
|
|
|
Controls.prototype.msgAttempt = function() { |
|
|
|
@ -130,9 +142,18 @@ Controls.prototype.msgConnected = function() { |
|
|
|
|
this.showWaiting(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgMove = function() { |
|
|
|
|
this.moves++; |
|
|
|
|
document.getElementById('controls-moves').innerHTML = this.moves; |
|
|
|
|
Controls.prototype.msgUndo = function(evt) { |
|
|
|
|
// Remove most recent move and return to the one before that.
|
|
|
|
|
this.moves.pop();
|
|
|
|
|
const secondToLast = this.moves.pop(); |
|
|
|
|
|
|
|
|
|
const evtMove = new CustomEvent('L-move', { detail: secondToLast }); |
|
|
|
|
document.dispatchEvent(evtMove); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgMove = function(evt) { |
|
|
|
|
this.moves.push(evt.detail); |
|
|
|
|
this.updateMoves(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgPlayers = function(evt) { |
|
|
|
@ -169,6 +190,11 @@ Controls.prototype.msgPlayers = function(evt) { |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgReset = function() { |
|
|
|
|
this.moves = []; |
|
|
|
|
this.updateMoves(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Controls.prototype.msgSkip = function() { |
|
|
|
|
this.coundownComplete(); |
|
|
|
|
}; |
|
|
|
@ -200,31 +226,32 @@ Controls.prototype.onClickGuess = function(evt) { |
|
|
|
|
} else { |
|
|
|
|
alert(`That doesn't beat ${this.currentWinningGuess} - try again!`) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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.onClickUndo = function() { |
|
|
|
|
this.dispatch('L-undo'); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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'); |
|
|
|
|
}; |
|
|
|
|