Completed
Push — master ( bc469b...d8f999 )
by Andres
01:15
created

src/scripts/services/util.js   A

Complexity

Total Complexity 17
Complexity/F 2.83

Size

Lines of Code 95
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 0
wmc 17
c 3
b 0
f 0
nc 4
mnd 1
bc 17
fnc 6
dl 0
loc 95
rs 10
bpm 2.8333
cpm 2.8333
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.service(ꞌutilꞌ) 0 86 1
1
/* globals Ziggurat */
2
'use strict';
3
4
angular
5
  .module('game')
6
  .service('util', ['numberFilter',
7
    '$sce',
8
    '$locale',
9
    'data',
10
    function(numberFilter, $sce, $locale, data) {
11
      let formats = $locale.NUMBER_FORMATS;
12
      this.gaussian = new Ziggurat();
13
      this.poisson = new Poisson();
0 ignored issues
show
Bug introduced by
The variable Poisson seems to be never declared. If this is a global, consider adding a /** global: Poisson */ 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...
14
15
      this.getHTML = function(resource) {
16
        let html = data.html[resource];
17
        if (typeof html === 'undefined') {
18
          html = data.resources[resource].html;
19
        }
20
        if (typeof html === 'undefined') {
21
          return resource;
22
        }
23
        return html;
24
      };
25
26
      this.prettifyNumber = function(number) {
27
        if (typeof number === 'undefined' || number === null) {
28
          return null;
29
        }
30
        if (number === '') {
31
          return '';
32
        }
33
        if (number === Infinity) {
34
          return '∞';
35
        }
36
        if (number > 1e6) {
37
          // Very ugly way to extract the mantisa and exponent from an exponential string
38
          let exponential = number.toPrecision(6).split('e');
39
          let exponent = parseFloat(exponential[1].split('+')[1]);
40
          // And it is displayed in with superscript
41
          return numberFilter(exponential[0], 4) +
42
            ' &#215; 10<sup>' +
43
            this.prettifyNumber(exponent) +
44
            '</sup>';
45
        }
46
        return mangleCeroes(number, 4);
47
      };
48
49
      // FIXME: it turns out we need this abomination since default numberFilter
50
      // uses 3 decimals and we need 4 (for the isotopes proportions precision)
51
      // and if you use decimals, it attaches 0's at the end
52
      function mangleCeroes(input, fractionSize) {
53
        //Get formatted value
54
55
        let formattedValue = numberFilter(input, fractionSize);
56
        //get the decimalSepPosition
57
        let decimalIdx = formattedValue.indexOf(formats.DECIMAL_SEP);
58
        //If no decimal just return
59
        if (decimalIdx === -1) {
60
          return formattedValue;
61
        }
62
63
        let whole = formattedValue.substring(0, decimalIdx);
64
        let decimal = (Number(formattedValue.substring(decimalIdx)) || '').toString();
65
66
        return whole + decimal.substring(1);
67
      }
68
69
      this.randomDraw = function(number, p) {
70
        let production;
71
        let mean = number * p;
72
        if (p < 0.01) {
73
          // using Poisson distribution (would get slow for large numbers. there are fast formulas but I don't know
74
          // how good they are)
75
          production = this.poisson.getPoisson(mean);
76
        } else {
77
          // Gaussian distribution
78
          let q = 1 - p;
79
          let variance = number * p * q;
80
          let std = Math.sqrt(variance);
81
          production = Math.round(this.gaussian.nextGaussian() * std + mean);
82
        }
83
        if (production > number) {
84
          production = number;
85
        }
86
        if (production < 0) {
87
          production = 0;
88
        }
89
        return production;
90
      };
91
92
      this.trustHTML = function(html) {
93
        return $sce.trustAsHtml(html);
94
      };
95
    }
96
  ]);
97