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.
217 lines
6.4 KiB
217 lines
6.4 KiB
const uuid = require('node-uuid');
|
|
const UrlParser = require('url');
|
|
const DEBUG = (process.env.NODE_ENV !== "production");
|
|
|
|
const STATE = {
|
|
COUNTDOWN: 'COUNTDOWN',
|
|
PLAY: 'PLAY',
|
|
SOLUTION: 'SOLUTION'
|
|
};
|
|
|
|
const Ricochet = function({ messenger }) {
|
|
this.messenger = messenger;
|
|
this.squaresPerSide = 20;
|
|
|
|
// Properties that will be emitted
|
|
this.players = {};
|
|
this.robots = this.freshRobots();
|
|
this.state = STATE.PLAY;
|
|
this.walls = this.freshWalls();
|
|
|
|
// this.id = uuid.v4();
|
|
// this.countdownTimer = null;
|
|
// this.guess = Infinity;
|
|
// this.winningStack;
|
|
};
|
|
|
|
Ricochet.prototype.onConnect = function(ws, req) {
|
|
const url = UrlParser.parse(req.url, true);
|
|
const query = url.query;
|
|
const santizedName = (query.name || 'Unknown').replace(/[^\w ]/g, '');
|
|
|
|
DEBUG && console.log(`Connected: ${santizedName} (${ws.id.substr(0, 8)}) via ${req.url}`);
|
|
|
|
this.addPlayer(ws.id, santizedName);
|
|
|
|
this.messenger.messageAll({ type: 'players', body: this.players });
|
|
this.messenger.messageOne(ws, { type: 'robots', body: this.robots});
|
|
this.messenger.messageOne(ws, { type: 'connected', body: ws.id});
|
|
this.messenger.messageOne(ws, { type: 'state', body: this.state});
|
|
this.messenger.messageOne(ws, { type: 'walls', body: this.walls});
|
|
// this.messenger.messageOne(ws, { type: 'winstate', body: G.getWinState()});
|
|
}
|
|
|
|
Ricochet.prototype.onMessage = function(ws, json) {
|
|
|
|
};
|
|
|
|
Ricochet.prototype.onDisconnect = function(ws) {
|
|
DEBUG && console.log(`Disconnected: ${this.players[ws.id]} (${ws.id.substr(0, 8)})`);
|
|
|
|
this.removePlayer(ws.id);
|
|
|
|
this.messenger.messageAll({ type: 'players', body: this.players });
|
|
};
|
|
|
|
Ricochet.prototype.addPlayer = function(id, name) {
|
|
if (!this.players[id]) {
|
|
this.players[id] = name;
|
|
}
|
|
};
|
|
|
|
Ricochet.prototype.removePlayer = function(id) {
|
|
this.players[id] = undefined;
|
|
delete this.players[id];
|
|
};
|
|
|
|
Ricochet.prototype.freshRobots = function() {
|
|
const robots = ['#E00000', '#00C000', '#0000FF', '#00C0C0', '#F000F0'];
|
|
const icons = ['assets/comet.svg', 'assets/moon.svg', 'assets/planet.svg', 'assets/rocket.svg', 'assets/spacesuit.svg'];
|
|
// // spider.svg, ufo.svg
|
|
|
|
const gen = () => Math.floor(Math.random() * this.squaresPerSide);
|
|
return robots.map((color, idx) => ({ i: gen(), j: gen(), color, id: uuid.v4(), icon: icons[idx] }));
|
|
};
|
|
|
|
Ricochet.prototype.freshWalls = 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 numberOfCorners = Math.ceil(Math.pow((this.squaresPerSide / 10), 2));
|
|
const numberOfWalls = Math.ceil(Math.pow((this.squaresPerSide / 5), 2));
|
|
|
|
const gen = () => Math.floor(Math.random() * this.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.getWinState = function() {
|
|
// // const gen = () => Math.floor(Math.random() * this.squaresPerSide);
|
|
|
|
// return { i: 0, j: 0, id: this.TEMP_ROBOTS[0].id };
|
|
// };
|
|
|
|
// // 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);
|
|
// // });
|
|
// // };
|
|
|
|
// Game.prototype.onSolve = function(rawMoveStack) {
|
|
// const sanitizedStack = rawMoveStack.map(move => {
|
|
// const sanitizedRobotId = move.id.replace(/[^0-9a-zA-Z]/g, '');
|
|
// const sanitizedI = move.i * 1;
|
|
// const sanitizedJ = move.j * 1;
|
|
|
|
// return {
|
|
// i: sanitizedI,
|
|
// j: sanitizedJ,
|
|
// id: sanitizedRobotId
|
|
// };
|
|
// });
|
|
|
|
// this.winningStack = sanitizedStack;
|
|
// return sanitizedStack;
|
|
// }
|
|
|
|
// onMessage: (ws, json) => {
|
|
// const message = JSON.parse(json);
|
|
|
|
// DEBUG && console.log('Received message: ');
|
|
// DEBUG && console.log(message);
|
|
|
|
// if (!message.type) {
|
|
// DEBUG && console.warn("Unprocessable message: ")
|
|
// DEBUG && console.warn(message);
|
|
// return;
|
|
// }
|
|
|
|
// switch (message.type) {
|
|
// case 'robots':
|
|
// Server.messageAll({ type: 'robots', body: G.getRobots()});
|
|
// break;
|
|
// case 'skip':
|
|
// Server.messageAll({ type: 'start' });
|
|
// break;
|
|
// case 'solve':
|
|
// const sanitizedStack = G.onSolve(message.rawBody);
|
|
|
|
// Server.messageAll(ws, { type: 'countdown', body: { id: ws.id, stack: sanitizedStack } });
|
|
// break;
|
|
// case 'start':
|
|
// Server.messageAll({ type: 'start' });
|
|
// break;
|
|
// case 'stop':
|
|
// Server.messageAll({ type: 'stop' });
|
|
// break;
|
|
// case 'walls':
|
|
// Server.messageAll({ type: 'walls', body: G.getWalls()});
|
|
// break;
|
|
// default:
|
|
// console.warn("Unknown message type: ", message.head.type)
|
|
// }
|
|
// },
|
|
|
|
module.exports = Ricochet; |