codingame-connector
Version:
An interface for Codingame website
172 lines (153 loc) • 4.85 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _safe = require('colors/safe');
var _safe2 = _interopRequireDefault(_safe);
var _error = require('../error.js');
var _error2 = _interopRequireDefault(_error);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @file Module 'codingame/parsers/frames'
* @author woshilapin <woshilapin@tuziwo.info>
*/
/**
* Parse response with frames from Codingame.
*
* The typical response in this case if of the following form.
* ```json
* {
* "success": {
* "frames": [{
* "gameInformation": `Landing phase starting\nX=2500m, Y=2700m, HSpeed=0m/s VSpeed=0m/s\nFuel=550l, Angle=0°, Power=0 (0.0m/s2)\n`,
* "stdout": "0 0",
* "stderr": "debug: speed 0",
* "view": ` 0\n7000 3000 3.711 1.0 1.0 1 0 4 -90 90\n20 40 10 20 DIRECT 15 1\n7\n0 100\n1000 500\n1500 1500\n3000 1000\n4000 150\n5500 150\n6999 800\n2500 2700 0 0 550 0 0\n`,
* "keyframe": true
* }],
* "gameId": 154447808,
* "scores": [ 1 ],
* "metadata": {
* "fuel": 0
* }
* }
* }
* ```
* @module codingame/parsers/failexpected
*/
var name = 'frames';
/**
* Replace coloring tags from Codingame with ANSI colors for terminal
*
* @name colorize
* @function
* @param {String} message Input message with Codingame formatting
* @returns {String} Output message with ANSI coloring format
*/
var colorize = function colorize(message) {
var colorlist = '(RED|GREEN)';
var beginchar = '\xA4';
var endchar = '\xA7';
var regexp = new RegExp('' + beginchar + colorlist + beginchar + '(.*)' + endchar + colorlist + endchar);
var pattern = regexp.exec(message);
if (pattern !== null) {
var text = pattern[0];
var color = pattern[1].toLowerCase();
var subtext = pattern[2];
message = message.replace(text, _safe2.default[color](subtext));
}
return message;
};
/**
* Format the display of game frames
*
* @name formatFrames
* @function
* @param {Array} frames List of frames to display
* @returns {String} Formatted string to display information about the frames
*/
var formatFrames = function formatFrames(frames) {
var message = '';
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(frames), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var frame = _step.value;
var gameInformation = frame["gameInformation"],
stdout = frame["stdout"],
stderr = frame["stderr"],
view = frame["view"],
keyframe = frame["keyframe"];
if (stdout !== undefined) {
message += _safe2.default.bold('Standard Output\n');
message += stdout.trim();
message += '\n\n';
}
if (stderr !== undefined) {
message += _safe2.default.bold('Standard Error\n');
message += _safe2.default.red(stderr.trim());
message += '\n\n';
}
message += _safe2.default.bold('Game Information\n');
message += colorize(gameInformation.trim());
message += '\n\n';
console.log(message);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return message;
};
/**
* Attempt to parse the body of a successful request to Codingame test API.
* This function will try to map a response with frames.
*
* @function parse
* @param {Object} body Body of the response
* @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful
* @throws {Error} Throw is parsing failed
* @instance
*/
var parse = function parse(body) {
try {
var frames = body["frames"],
gameId = body["gameId"],
scores = body["scores"],
metadata = body["metadata"];
if (Array.isArray(frames) && gameId !== undefined && Array.isArray(scores) && typeof metadata === 'object') {
var message = formatFrames(frames);
if (scores[0] === 1) {
return _promise2.default.resolve(message.trim());
} else {
var error = new _error2.default(message.trim());
return _promise2.default.reject(error);
}
} else {
throw 'Success value should be false when expected and found properties are provided';
}
} catch (error) {
var _message = 'The body is not of response type \'' + name + '\'\n';
_message += error.message;
throw new Error(_message);
}
};
exports.default = {
"name": name,
"parse": parse
};