Skip to content

Instantly share code, notes, and snippets.

@linanqiu
Created February 1, 2018 04:05
Show Gist options
  • Save linanqiu/4ea46fd130b29d6341c76a15df46293e to your computer and use it in GitHub Desktop.
Save linanqiu/4ea46fd130b29d6341c76a15df46293e to your computer and use it in GitHub Desktop.
const request = require('request');
const async = require('async');
const _ = require('lodash');
const URL_API = 'http://52.27.140.147:9099/maze';
function getMaze(mazeDefinition, solveCallback) {
let width = mazeDefinition.width;
let height = mazeDefinition.height;
let id = mazeDefinition.id;
let requests = _.flatMap(_.range(width), w => _.range(height).map(h => {
let coords = {
'x': w,
'y': h,
'id': id
};
return coords;
}));
console.log('about to spam server');
async.map(requests, getMazePositionInfo, function (err, results) {
console.log('got results');
console.log('width: ' + width);
console.log('height: ' + height);
console.log('results size: ' + results.length);
let maze = reformatResults(width, height, requests, results);
console.log(maze);
});
}
function getMazePositionInfo(item, callback) {
let urlPosition = URL_API + '/' + item.id + '/check';
request({
url: urlPosition,
qs: item
}, function (err, resp, body) {
console.log(body);
if (err) {
getMazePositionInfo(item, callback);
} else {
if (body.toLowerCase().includes('invalid position')) {
callback(false, 0);
} else if (body.toLowerCase().includes('position')) {
callback(false, 1);
} else {
getMazePositionInfo(item, callback);
}
}
});
}
function reformatResults(width, height, requests, results) {
let maze = (function (width, height, init) {
var arr = []
for (var i = 0; i < height; i++) {
arr[i] = [];
for (var j = 0; j < width; j++) {
arr[i][j] = init;
}
}
return arr;
})(width, height, -1);
for (var i = 0; i < requests.length; i++) {
maze[requests[i].x][requests[i].y] = results[i];
}
return maze;
}
request.post(URL_API, function (err, resp, body) {
if (resp.statusCode == 201) {
getMaze(JSON.parse(body));
} else {
console.err('you got fucked');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment