Skip to content

Instantly share code, notes, and snippets.

@Alonski
Created March 28, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alonski/355dcc02056695e58957c7874d913d4c to your computer and use it in GitHub Desktop.
Save Alonski/355dcc02056695e58957c7874d913d4c to your computer and use it in GitHub Desktop.
Vorpal Maze
const fetch = require("node-fetch");
const vorpal = require("vorpal")();
vorpal.delimiter("maze$").show();
let mazeId = "";
let mazeStart = "";
const ip = "http://139.59.154.221/";
let ipStart = "";
let total = 0;
let codeList = [];
vorpal.command("new", "Starts new maze").action(function(args, callback) {
mazeId = "";
mazeStart = "";
total = 0;
let that = this;
fetch(ip, { method: "POST" }).then(result => result.json()).then(json => {
mazeId = json.maze_id;
mazeStart = json.start;
this.log("Starting new maze", "MazeId: ", mazeId, "Start: ", mazeStart);
});
callback();
});
vorpal
.command("gold", "Counts amount of gold in maze")
.action(function(args, callback) {
traverse(mazeStart);
callback();
});
let traverse = code => {
if (codeList.includes(code)) {
return;
vorpal.log("THIS SHOULDN'T HAPPEN!@#!@#!");
}
let ipTraverse = `${ip}${mazeId}/${code}/`;
codeList.push(code);
fetch(ipTraverse).then(result => result.json()).then(json => {
let exits = json.exits;
let score = json.score;
total += score;
vorpal.log("Total: ", total);
if (exits) {
exits.forEach(exit => {
traverse(exit.code);
});
}
});
};
vorpal.command("final", "Gets Final Info").action(function(args, callback) {
this.log("MazeId: ", mazeId, "Final Score: ", total);
let ipFinal = `${ip}solve/${mazeId}/${total}/`;
this.log(ipFinal);
fetch(ipFinal, { method: "POST" })
.then(result => result.json())
.then(json => {
this.log(json);
});
callback();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment