Completed
Push — master ( 663c4c...aa9f38 )
by Andres
29s
created

angular.controller(ꞌct_reactionsꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
/**
2
 reactions
3
 Component that handles reactions and molecules.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('reactions', {
10
  templateUrl: 'views/reactions.html',
11
  controller:  'ct_reactions',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_reactions', ['state', 'data', 'visibility', 'util', 'format', 'reaction', 'upgrade',
16
function (state, data, visibility, util, format, reactionService, upgradeService) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.format = format;
22
  ct.upgradeService = upgradeService;
23
  ct.adjustAmount = [1, 10, 25, 100];
24
25
  function update(player) {
26
    for(let slot of player.element_slots){
27
      if(!slot){
28
        continue;
29
      }
30
      for (let reaction of slot.reactions) {
31
        if (!reaction.active) {
32
          continue;
33
        }
34
        reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player);
35
      }
36
    }
37
  }
38
39
  function numberToReact(player, reaction) {
40
    let power = util.calculateValue(data.global_upgrades.reaction_bandwidth.power.base,
41
            data.global_upgrades.reaction_bandwidth.power,
42
            player.global_upgrades_current.reaction_bandwidth);
43
    let number = power;
44
    for(let resource in reaction.reactants){
45
      number = Math.min(number, player.resources[resource].number);
46
    }
47
    return number;
48
  }
49
50
  ct.reactionSize = function (player) {
51
    let size = 0;
52
    for(let slot of player.element_slots){
53
      if(!slot){
54
        continue;
55
      }
56
      size += slot.reactions.length;
57
    }
58
    return size;
59
  };
60
61
  /* Adds a new reaction to the player list */
62
  ct.addReaction = function (player, slot, key) {
63
    if(ct.reactionSize(player) >= util.calculateValue(data.global_upgrades.reaction_slots.power.base,
64
            data.global_upgrades.reaction_slots.power,
65
            player.global_upgrades.reaction_slots)){
66
      return;
67
    }
68
    let reaction = data.reactions[key];
69
    slot.reactions.push({
70
      active: false,
71
      reaction: angular.copy(reaction)
72
    });
73
  };
74
75
  ct.removeReaction = function (slot, item) {
76
    for(let i = 0; i < slot.reactions.length; i++){
77
      if(slot.reactions[i] === item){
78
        slot.reactions.splice(i, 1);
79
      }
80
    }
81
  };
82
83
  ct.visibleReactions = function(slot) {
84
    return slot.reactions;
85
  };
86
87
  ct.availableReactions = function(slot, player) {
88
    return visibility.visible(data.reactions, isReactionAvailable, slot.element, null, player);
89
  };
90
91
  function isReactionAvailable(entry, currentElement, player) {
92
    let available = true;
93
    let reaction = data.reactions[entry];
94
    for(let resource in reaction.reactant){
95
      available = available && player.resources[resource].unlocked;
96
    }
97
    // Workaround to reuse the visibility function. It expects an object with the
98
    // reaction inside
99
    let reactionObject = {reaction:reaction};
100
    return available && isReactionVisible(reactionObject, currentElement);
101
  }
102
103
  function isReactionVisible(entry, currentElement) {
104
    let reaction = entry.reaction;
105
    if(reaction.elements.length === 0){
106
      return true;
107
    }
108
    for(let element of reaction.elements){
109
      if(element === currentElement){
110
        return true;
111
      }
112
    }
113
    return false;
114
  }
115
116
  ct.adjustLevel = function(player, upgrade, amount){
117
    player.global_upgrades_current[upgrade] += amount;
118
    // We cap it between 1 and the current max level
119
    player.global_upgrades_current[upgrade] = Math.max(1, Math.min(player.global_upgrades_current[upgrade], player.global_upgrades[upgrade]));
120
  };
121
122
  ct.visibleUpgrades = function() {
123
    return visibility.visible(data.global_upgrades, upgradeService.filterByTag('reactions'));
124
  };
125
126
  state.registerUpdate('reactions', update);
127
}]);
128