Completed
Push — master ( 349ddf...9351ef )
by Andres
33s
created

src/scripts/services/reaction.js   A

Complexity

Total Complexity 22
Complexity/F 5.5

Size

Lines of Code 66
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 22
c 1
b 0
f 0
nc 16
mnd 4
bc 16
fnc 4
dl 0
loc 66
rs 10
bpm 4
cpm 5.5
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A angular.service(ꞌreactionꞌ) 0 60 1
1
/**
2
 reaction
3
 Service that processes a reaction i.e. it converts reactants to products.
4
 Many phenomenons, including decay, redox and reactions, use the react function.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('reaction', ['state','util','data',
13
    function(state, util, data) {
14
      // FIXME: move to util?
15
        function isReactionCostMet (number, reaction, playerData) {
16
          for (let key in reaction.reactant) {
17
            let available = playerData.resources[key].number;
18
            let required = number * reaction.reactant[key];
19
            if (required > available) {
20
              return false;
21
            }
22
          }
23
          return true;
24
        }
25
26
      /* Transforms reactants to products */
27
      this.react = function(number, reaction, playerData) {
28
        if (!Number.isInteger(number) || number <= 0 ||
29
            !reaction.reactant || !reaction.product) {
30
          return;
31
        }
32
        if (isReactionCostMet(number, reaction, playerData)) {
33
          let elements = [];
34
          for (let resource in reaction.reactant) {
35
            let required = number * reaction.reactant[resource];
36
            playerData.resources[resource].number -= required;
37
            playerData.resources[resource].number = playerData.resources[resource].number;
38
            // We track which elements produced the products, for the statistics
39
            for(let elem of Object.keys(data.resources[resource].elements)){
40
              if(elements.indexOf(elem) === -1) elements.push(elem);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
41
            }
42
          }
43
          for (let resource in reaction.product) {
44
            let produced = number * reaction.product[resource];
45
            util.addResource(playerData, elements, resource, produced, state);
46
          }
47
        }
48
      };
49
50
      this.processReactions = function(reactions, player){
51
        let declared = {};
52
        for(let reaction of reactions){
53
          let reactant = reaction.reaction.reactant;
54
          for (let resource in reactant) {
55
            declared[resource] = declared[resource]+reactant[resource]*reaction.number || reactant[resource]*reaction.number;
56
          }
57
        }
58
        for(let reaction of reactions){
59
          let reactant = reaction.reaction.reactant;
60
          for (let resource in reactant) {
61
            if(!declared[resource] || !reactant[resource]) continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
62
            let available = Math.min(declared[resource], player.resources[resource].number);
63
            let ratio = reactant[resource]*reaction.number/declared[resource];
64
            reaction.number = Math.min(reaction.number, Math.floor(available*ratio));
65
          }
66
        }
67
68
        for(let reaction of reactions){
69
          this.react(reaction.number, reaction.reaction, player);
70
        }
71
      }
72
    }
73
  ]);
74