Completed
Push — master ( 6e8dba...bda2ec )
by Andres
30s
created

angular.controller(ꞌct_redoxꞌ)   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
nc 4
dl 0
loc 16
rs 8.8571
nop 1
1
/**
2
 redox
3
 Component that handles reduction/oxidation and ions.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('redox', {
10
  templateUrl: 'views/redox.html',
11
  controller: 'ct_redox',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_redox', ['state', 'data', 'visibility', 'util', 'format', 'reaction',
16
  function (state, data, visibility, util, format, reaction) {
17
    let ct = this;
18
    ct.state = state;
19
    ct.data = data;
20
    ct.util = util;
21
    ct.format = format;
22
    ct.reaction = reaction;
23
24
    function update(player) {
25
      for(let slot of player.element_slots){
26
        for (let redox of slot.redoxes) {
27
          if (!redox.resource || !redox.active) {
28
            continue;
29
          }
30
31
          let reactant = ct.generateName(redox.element, redox.from);
32
          let power = ct.redoxPower(player);
33
          let number = Math.min(power, player.resources[reactant].number);
34
          let react = ct.redoxReaction(redox);
35
36
          ct.reaction.react(number, react, player);
37
        }
38
      }
39
    }
40
41
    /* Calculates the redox power based on the redox upgrades */
42
    ct.redoxPower = function(player) {
43
      let level = player.global_upgrades.redox_bandwidth;
44
      let upgrade = data.global_upgrades.redox_bandwidth;
45
      let basePower = upgrade.power;
46
      let polynomial = upgrade.power_poly;
47
      return basePower * Math.floor(Math.pow(level, polynomial));
48
    };
49
50
    /* Writes a redox in the form of a reaction so that we can use the reaction
51
    service to process it */
52
    ct.redoxReaction = function (redox) {
53
      let reactant = ct.generateName(redox.element, redox.from);
54
      let product = ct.generateName(redox.element, redox.to);
55
      let energy = redoxEnergy(redox.from, redox.to, redox.element);
56
57
      let react = {
58
        'reactant': {},
59
        'product': {}
60
      };
61
62
      react.reactant[reactant] = 1;
63
      react.product[product] = 1;
64
      if (energy > 0) {
65
        react.reactant.eV = energy;
66
      } else if (energy < 0) {
67
        react.product.eV = -energy;
68
      }
69
70
      let electron = redox.from - redox.to;
71
      if (electron > 0) {
72
        react.reactant['e-'] = electron;
73
      } else if (electron < 0) {
74
        react.product['e-'] = -electron;
75
      }
76
77
      return react;
78
    };
79
80
    /* Calculates how much energy it takes to go from a redox level to another
81
    for a given element */
82
    function redoxEnergy(from, to, element) {
83
      let energyFrom = cumulativeEnergy(element, from);
84
      let energyTo = cumulativeEnergy(element, to);
85
      let energy = energyTo - energyFrom;
86
87
      return energy;
88
    }
89
90
    /* Calculates the cummulative energy of a redox level.
91
    The logic is the following: the redox array gives how much energy it costs
92
    to go from a level to the next, e.g. from +2 to +3. This function calculates
93
    how much it takes to go from level 0 to x by summing each successive level */
94
    function cumulativeEnergy(element, level) {
95
      let energy = 0;
96
      let start = Math.min(0, level);
97
      let end = Math.max(0, level);
98
      for (let i = start; i <= end; i++) {
99
        energy += data.redox[element][i];
100
      }
101
      if (level < 0) {
102
        energy = -energy;
103
      }
104
      return energy;
105
    }
106
107
    /* Generates the name of a ion, e.g. O3+ */
108
    ct.generateName = function (element, i) {
109
      if (i === 0) {
110
        return data.elements[element].main;
111
      }
112
      let postfix = '';
113
      if (Math.abs(i) > 1) {
114
        postfix = Math.abs(i);
115
      }
116
      postfix += getSign(i);
117
      let name = element + postfix;
118
      // special case!! H+ is just a proton
119
      if (name === 'H+') {
120
        name = 'p';
121
      }
122
      return name;
123
    };
124
125
    function getSign(number) {
126
      return number > 0 ? '+' : '-';
127
    }
128
129
    /* Calculates the number of redox slots based on the redox upgrades */
130
    ct.redoxSlots = function (player) {
131
      let level = player.global_upgrades.redox_slots;
132
      let upgrade = data.global_upgrades.redox_slots;
133
      let basePower = upgrade.power;
134
      let multiplier = upgrade.power_mult;
135
      return basePower * Math.floor(multiplier * level);
136
    };
137
138
    ct.redoxSize = function (player) {
139
      let size = 0;
140
      for(let slot of player.element_slots){
141
        size += slot.redoxes.length;
142
      }
143
      return size;
144
    };
145
146
    /* Adds a new redox to the player list */
147
    ct.addRedox = function (player, slot) {
148
      if(ct.redoxSize(player) >= ct.redoxSlots(player)){
149
        return;
150
      }
151
      slot.redoxes.push({
152
        resource: data.elements[slot.element].main,
153
        active: false,
154
        element: slot.element,
155
        from: 0,
156
        to: 1
157
      });
158
    };
159
160
    ct.removeRedox = function (slot, index) {
161
      slot.redoxes.splice(index, 1);
162
    };
163
164
    ct.visibleRedox = function(slot) {
165
      return slot.redoxes;
166
    };
167
168
    state.registerUpdate('redox', update);
169
  }
170
]);
171