Completed
Push — master ( 38ec3f...d9a118 )
by Jean
55s
created

frames.js ➔ parse   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 9
c 1
b 1
f 1
nc 6
nop 1
dl 0
loc 37
rs 4.909
1
/**
2
 * @file Module 'codingame/parsers/frames'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse response with frames from Codingame.
7
 *
8
 * The typical response in this case if of the following form.
9
 * ```json
10
 * {
11
 * 	"success": {
12
 * 		"frames": [{
13
 * 			"gameInformation": `Landing phase starting\nX=2500m, Y=2700m, HSpeed=0m/s VSpeed=0m/s\nFuel=550l, Angle=0°, Power=0 (0.0m/s2)\n`,
14
 * 			"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`,
15
 * 			"keyframe": true
16
 * 		}],
17
 * 		"gameId": 154447808,
18
 * 		"scores": [ 1 ],
19
 * 		"metadata": {
20
 * 			"fuel": 0
21
 * 		}
22
 * 	}
23
 * }
24
 * ```
25
 * @module codingame/parsers/failexpected
26
 */
27
import colors from 'colors/safe';
28
29
import CodingameError from '../error.js';
30
31
let name = `frames`;
32
33
/**
34
 * Attempt to parse the body of a successful request to Codingame test API.
35
 * This function will try to map a response with frames.
36
 *
37
 * @function parse
38
 * @param {Object} body Body of the response
39
 * @returns {Promise<CodingameError>} Reject with a CodingameError if parsing was successful
40
 * @throws {Error} Throw is parsing failed
41
 * @instance
42
 */
43
let parse = function parse(body) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parse already seems to be declared on line 43. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
44
	try {
45
		let {
46
			"frames": frames,
47
			"gameId": gameId,
48
			"scores": scores,
49
			"metadata": metadata
50
		} = body;
51
		if (Array.isArray(frames) && gameId !== undefined && Array.isArray(scores) && typeof metadata === `object`) {
0 ignored issues
show
Bug introduced by
The variable metadata seems to be never declared. If this is a global, consider adding a /** global: metadata */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable scores seems to be never declared. If this is a global, consider adding a /** global: scores */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Comprehensibility Bug Compatibility introduced by
Using gameId !== undefined to check if a variable is declared may throw an Error. Consider using typeof {name} === "undefined"instead.
Loading history...
Bug introduced by
The variable frames seems to be never declared. If this is a global, consider adding a /** global: frames */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
			if (scores[0] === 1) {
53
				let message = frames[frames.length - 1].gameInformation;
54
				return Promise.resolve(message);
55
			} else {
56
				let message = ``;
57
				for(let frame of frames) {
58
					let gi = frame.gameInformation.trim();
59
					let pattern = /¤RED¤.*§RED§/.exec(gi);
60
					if (pattern !== null) {
61
						let r = colors.red(pattern);
62
						gi = gi.replace(pattern, r);
63
						gi = gi.replace(/(¤|§)RED\1/g, '');
64
					}
65
					message += gi;
66
					message += `\n\n`;
67
				}
68
				let error = new CodingameError(message.trim());
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable error already seems to be declared on line 74. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Comprehensibility introduced by
You are assigning a value to the catch variable error, thereby shadowing the original value.
Loading history...
69
				return Promise.reject(error);
70
			}
71
		} else {
72
			throw `Success value should be false when expected and found properties are provided`;
73
		}
74
	} catch (error) {
75
		let message = `The body is not of response type '${name}'\n`;
76
		message += error.message;
77
		throw new Error(message);
78
	}
79
};
80
81
export default {
82
	"name": name,
83
	"parse": parse
84
};
85