const uuid = require('node-uuid'); const UrlParser = require('url'); /////////////// // REMEMBER THIS WILL BE IN A COMPLETELY SEPARATE DIRECTORY /////////////// const Ricochet = function({ messenger }) { this.messenger = messenger; console.log("HELLO RICOCHET") // const players = {}; // this.id = uuid.v4(); // this.countdownTimer = null; // this.guess = Infinity; // this.squaresPerSide = 20; // this.winningStack; // 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); // this.TEMP_ROBOTS = robots.map((color, idx) => ({ i: gen(), j: gen(), color, id: uuid.v4(), icon: icons[idx] })); }; Ricochet.prototype.onConnect = function(ws, req) { console.log("CONNECTED TO RICOCHET INSTANCE") // const query = url.query; // const santizedName = (query.name || 'Unknown').replace(/[^\w ]/g, ''); // DEBUG && console.log("Connected:"); // DEBUG && console.log (`${santizedName} ${ws.id} via ${req.url}`); // G.addPlayer(ws.id, santizedName); // Server.messageAll({ type: 'players', body: G.getPlayers() }); // Server.messageOne(ws, { type: 'walls', body: G.getWalls()}); // Server.messageOne(ws, { type: 'robots', body: G.getRobots()}); // Server.messageOne(ws, { type: 'winstate', body: G.getWinState()}); // Server.messageOne(ws, { type: 'connected', body: ws.id}); } Ricochet.prototype.onMessage = function(ws, json) { }; Ricochet.prototype.onDisconnect = function(ws) { // onDisconnect: (ws) => { // DEBUG && console.log("Disconnected:"); // DEBUG && console.log(ws.id); // G.removePlayer(ws.id); // Server.messageOthers(ws, { type: 'players', body: G.getPlayers() }); // }, }; // 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() { // return this.TEMP_ROBOTS; // } // 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 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 = { new: Ricochet };