src/scripts/services/util.js   A
last analyzed

Complexity

Total Complexity 32
Complexity/F 2.67

Size

Lines of Code 118
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 0
wmc 32
nc 128
mnd 1
bc 31
fnc 12
dl 0
loc 118
rs 9.6
bpm 2.5833
cpm 2.6666
noi 0
c 4
b 1
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.service(ꞌutilꞌ) 0 111 1
1
/* globals numberformat,window */
2
/**
3
 util
4
 Utility service with misc. functions.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('util', ['$sce',
13
    'data',
14
    function($sce, data) {
15
      let sv = this;
16
      /* Return the HTML representation of an element, or the element itself
17
      if it doesn't have one */
18
      sv.getHTML = function(resource) {
19
        let html = data.html[resource];
20
        if (typeof html === 'undefined' && data.resources[resource]) {
21
          html = data.resources[resource].html;
22
        }
23
        if (typeof html === 'undefined') {
24
          return resource;
25
        }
26
        return html;
27
      };
28
29
      sv.prettifyNumber = function(number, player) {
30
        if (typeof number === 'undefined' || number === null){
31
           return null;
32
        }
33
        if (number === ''){
34
           return '';
35
         }
36
        if (number === Infinity){
37
           return '∞';
38
         }
39
        if (number === 0){
40
           return '0';
41
         }
42
        return numberformat.format(number, player.options.numberformat);
43
      };
44
45
      sv.addResource = function(player, scope, key, quantity, state){
46
        if(quantity === 0){
47
          return;
48
        }
49
        sv.addStatistic(player, scope, key, quantity);
50
        if (player.resources[key] === null) {
51
          player.resources[key] = quantity;
52
          state.addNew(key);
53
        }else{
54
          player.resources[key] += quantity;
55
        }
56
      };
57
58
      /* Adds an statistic. Scope can be only all time, dark run,
59
        only for specific elements, or for all elements at once
60
      */
61
      sv.addStatistic = function(player, scope, key, value){
62
        setStatistic(player.statistics.all_time, key, value);
63
        if(scope === 'all_time'){
64
          return;
65
        }
66
        setStatistic(player.statistics.dark_run, key, value);
67
        if(scope === 'dark'){
68
          return;
69
        }
70
        if(scope === 'all_elements') {
71
          scope = Object.keys(data.elements);
72
        }
73
        for(let element of scope){
74
          player.statistics.exotic_run[element] = player.statistics.exotic_run[element] || {};
75
          setStatistic(player.statistics.exotic_run[element], key, value);
76
        }
77
      };
78
79
      function setStatistic(bucket, key, value){
80
        // If it is numeric, add it up
81
        if(!isNaN(parseFloat(value)) && isFinite(value)){
82
          bucket[key] = bucket[key]+value || value;
83
        // otherwise, replace
84
        }else{
85
          bucket[key] = value;
86
        }
87
      }
88
89
      sv.trustHTML = function(html) {
90
        return $sce.trustAsHtml(html);
91
      };
92
93
      sv.nextAmount = function (player, index, array) {
94
        player.options[index] = (player.options[index] + 1) % array.length;
95
      };
96
97
      sv.delayedExec = function(currentTs, eventTs, delay, callback) {
98
        if(currentTs-eventTs >= delay){
99
          callback();
100
        }else{
101
          window.requestAnimationFrame((ts) => sv.delayedExec(ts, eventTs, delay, callback));
102
        }
103
      };
104
105
      sv.prestigeProduction = function(number, start, power){
106
        number = number || 0;
107
        let production = Math.pow(Math.E,(-0.5+Math.sqrt(0.25+0.8686*Math.log(number/start)))/(2/Math.log(power*power))) || 0;
108
        return Math.round(Math.max(0, production));
109
      };
110
111
      sv.calculateValue = function(number, value, level){
112
        let result = number;
113
        if(value.linear){
114
           result *= level*value.linear;
115
         }
116
        if(value.poly){
117
           result *= Math.floor(Math.pow(level, value.poly));
118
         }
119
        if(value.exp){
120
           result *= Math.floor(Math.pow(value.exp, level));
121
         }
122
        return result;
123
      }
124
    }
125
  ]);
126