You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.0 KiB

//===== Constructor
const Stack = function() {
// This is the heart of the robot movement architecture.
// Its elements are of the form { robotId, i, j }
this.moves = [];
this.playerId = null;
document.addEventListener('L-arrow', this.msgArrow.bind(this));
document.addEventListener('L-submit', this.msgSubmit.bind(this));
document.addEventListener('L-undo', this.msgUndo.bind(this));
document.addEventListener('L-reset', this.msgReset.bind(this));
document.addEventListener('G-connected', this.msgConnected.bind(this));
document.addEventListener('G-robots', this.msgRobots.bind(this));
};
Stack.prototype.getInitialPositions = function() {
const moves = this.moves.reduce((acc, move) => {
if (!acc[move.id]) {
acc[move.id] = move;
}
return acc;
}, {});
return Object.values(moves);
};
Stack.prototype.msgConnected = function(evt) {
this.playerId = evt.detail.body;
};
Stack.prototype.msgRobots = function(evt) {
this.moves = evt.detail.body.map(({ id, i, j }) => ({ id, i, j }));
const evtStack = new CustomEvent('L-stack', { detail: this.moves });
document.dispatchEvent(evtStack);
const evtShadows = new CustomEvent('L-shadows', { detail: this.moves });
document.dispatchEvent(evtShadows);
};
Stack.prototype.msgArrow = function(evt) {
this.moves.push(evt.detail);
const evtStack = new CustomEvent('L-stack', { detail: this.moves });
document.dispatchEvent(evtStack);
};
Stack.prototype.msgReset = function() {
this.moves = this.getInitialPositions();
const evtStack = new CustomEvent('L-stack', { detail: this.moves });
document.dispatchEvent(evtStack);
};
Stack.prototype.msgSubmit = function() {
const evtSolve = new CustomEvent('L-solve', { detail: { stack: this.moves, id: this.playerId } });
document.dispatchEvent(evtSolve);
};
Stack.prototype.msgUndo = function() {
this.moves.pop();
const evtStack = new CustomEvent('L-stack', { detail: this.moves });
document.dispatchEvent(evtStack);
};