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.
124 lines
3.0 KiB
124 lines
3.0 KiB
const uuid = require('node-uuid');
|
|
|
|
const players = {};
|
|
|
|
const Game = function() {
|
|
this.id = uuid.v4();
|
|
this.countdownTimer = null;
|
|
this.guess = Infinity;
|
|
}
|
|
|
|
Game.prototype.addPlayer = function(id, name) {
|
|
if (!players[id]) {
|
|
players[id] = name;
|
|
}
|
|
}
|
|
|
|
Game.prototype.removePlayer = function(id) {
|
|
players[id] = undefined;
|
|
delete players[id];
|
|
}
|
|
|
|
Game.prototype.getPlayers = function() {
|
|
return players;
|
|
}
|
|
|
|
Game.prototype.getRobots = function() {
|
|
const robots = ['#E00000', '#00C000', '#0000FF', '#00C0C0', '#F000F0'];
|
|
const squaresPerSide = 20;
|
|
const gen = () => Math.floor(Math.random() * squaresPerSide);
|
|
|
|
// Leave here for testing.
|
|
// return [
|
|
// {i: 9, j: 9, color: '#E00000' },
|
|
// {i: 18, j: 9, color: '#00C000' },
|
|
// {i: 1, j: 9, color: '#0000FF' },
|
|
// {i: 9, j: 18, color: '#00C0C0' },
|
|
// {i: 9, j: 1, color: '#F000F0' },
|
|
// ];
|
|
|
|
return robots.map(color => ({ i: gen(), j: gen(), color, id: uuid.v4() }));
|
|
}
|
|
|
|
Game.prototype.getWalls = function() {
|
|
// Edge IDs are of the form [i1-j1-i2-j2]. Top left is 0, 0.
|
|
|
|
// Leave here for testing.
|
|
// return [
|
|
// "1-9-1-10",
|
|
// "9-1-10-1",
|
|
// "9-19-10-19",
|
|
// "19-9-19-10"
|
|
// ];
|
|
|
|
// console.log("Generating walls.");
|
|
|
|
// Squares per side has quadratic relationship with wall/corner requirements.
|
|
const squaresPerSide = 20;
|
|
const numberOfCorners = Math.ceil(Math.pow((squaresPerSide / 10), 2));
|
|
const numberOfWalls = Math.ceil(Math.pow((squaresPerSide / 5), 2));
|
|
|
|
const gen = () => Math.floor(Math.random() * squaresPerSide);
|
|
const edges = [];
|
|
|
|
// DO NUMBER OF CORNERS FIRST AFTER TESTING
|
|
for (let n = 0; n < numberOfWalls; n++) {
|
|
const ri = gen();
|
|
const rj = gen();
|
|
|
|
const isHorizontal = Math.random() < 0.5;
|
|
const isBackward = Math.random() < 0.5;
|
|
|
|
let i1, j1, i2, j2;
|
|
|
|
if (isHorizontal) {
|
|
i1 = isBackward ? ri - 1 : ri;
|
|
i2 = isBackward ? ri : ri + 1;
|
|
|
|
j1 = rj;
|
|
j2 = rj;
|
|
} else {
|
|
i1 = ri;
|
|
i2 = ri;
|
|
|
|
j1 = isBackward ? rj - 1 : rj;
|
|
j2 = isBackward ? rj : rj + 1;
|
|
}
|
|
|
|
const edge = `${i1}-${j1}-${i2}-${j2}`;
|
|
|
|
if (edges.includes(edge)) {
|
|
n--;
|
|
} else {
|
|
edges.push(edge);
|
|
}
|
|
}
|
|
|
|
return edges;
|
|
};
|
|
|
|
Game.prototype.onGuess = function(guess) {
|
|
return new Promise((resolve, reject) => {
|
|
const timeIsUp = () => {
|
|
this.guess = Infinity;
|
|
resolve();
|
|
};
|
|
|
|
if (guess < 1) {
|
|
reject(`${guess} is less than 1.`);
|
|
return;
|
|
}
|
|
|
|
if (guess >= this.guess) {
|
|
reject(`${guess} is greater than ${this.guess}.`);
|
|
return;
|
|
}
|
|
|
|
this.guess = guess;
|
|
|
|
clearTimeout(this.countdownTimer);
|
|
this.countdownTimer = setTimeout(timeIsUp, 5 * 1000);
|
|
});
|
|
};
|
|
|
|
module.exports = Game; |