Completed
Push — master ( bfee8a...41eb99 )
by Andres
28s
created

check_isotopes.js ➔ isFusionProduct   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 5
dl 0
loc 19
rs 8.8571
nop 1
1
/* eslint-env node */
2
/*jslint node: true */
3
'use strict';
4
5
let jsonfile = require('jsonfile');
6
7
let resources = jsonfile.readFileSync('build/data/resources.json');
8
let elements = jsonfile.readFileSync('build/data/elements.json');
9
10
let numberToElement = {};
11
for (let element in elements) {
12
  numberToElement[elements[element].number] = element;
13
  let isotopes = elements[element].isotopes;
14
  for (let isotope in isotopes) {
15
    if (isotopes[isotope].ratio === 0) {
16
      if (!isDecayProduct(isotope) && !isFusionProduct(isotope)) {
17
        throw new Error(isotope, 'cannot be obtained.');
18
      }
19
    }
20
  }
21
}
22
23
function isDecayProduct(isotope) {
24
  for (let key in resources) {
25
    let resource = resources[key];
26
    if (resource.decay) {
27
      for (let typeKey in resource.decay.decay_types) {
28
        let type = resource.decay.decay_types[typeKey];
29
        if (type.reaction.product[isotope]) {
30
          return true;
31
        }
32
      }
33
    }
34
  }
35
  return false;
36
}
37
38
function isFusionProduct(isotope) {
39
  let number = parseInt(isotope, 10);
40
  let element = isotope.replace(number, '');
41
  let elementNumber = elements[element].number;
42
  for (let i = 1; i < elementNumber; i++) {
43
    let elem1 = numberToElement[i];
44
    let elem2 = numberToElement[elementNumber - i];
45
    for (let iso1 in elements[elem1].isotopes) {
46
      for (let iso2 in elements[elem2].isotopes) {
47
        let num1 = parseInt(iso1, 10);
48
        let num2 = parseInt(iso2, 10);
49
        if (num1 + num2 === number) {
50
          return true;
51
        }
52
      }
53
    }
54
  }
55
  return false;
56
}
57