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

Complexity

Total Complexity 20
Complexity/F 1.43

Size

Lines of Code 84
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 20
c 1
b 0
f 0
nc 1
mnd 2
bc 17
fnc 14
dl 0
loc 84
rs 10
bpm 1.2142
cpm 1.4285
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.service(ꞌstateꞌ) 0 78 1
1
/* globals performance */
2
/**
3
 state
4
 This service handles all the mutable data of the game, most importantly the player data.
5
 It also binds together the main-loop with each component by keeping a list of update
6
 functions where components can register themselves.
7
8
 @namespace Services
9
 */
10
'use strict';
11
12
angular
13
  .module('game')
14
  .service('state', ['data',
15
  'util',
16
  function(data, util) {
17
    let sv = this;
18
    sv.hoverElement = '';
19
    sv.export = '';
20
    sv.player = {};
21
    sv.loading = true;
22
    sv.fasterTicks = false;
23
    sv.toast = [];
24
    sv.isToastVisible = false;
25
    let newElements = [];
26
    let updateFunctions = {};
27
    sv.reactionsCache = {};
28
    sv.redoxesCache = {};
29
    sv.reactions = [];
30
31
    sv.deleteToast = function() {
32
      sv.toast.shift();
33
      if (sv.toast.length > 0) {
34
        sv.isToastVisible = true;
35
        util.delayedExec(performance.now(),performance.now(), 2500, () => sv.removeToast());
36
      }
37
    };
38
39
    sv.removeToast = function() {
40
      sv.isToastVisible = false;
41
      util.delayedExec(performance.now(),performance.now(), 350, () => sv.deleteToast());
42
    };
43
44
    sv.addToast = function (t) {
45
      sv.toast.push(t);
46
      if (sv.toast.length === 1) {
47
        sv.isToastVisible = true;
48
        util.delayedExec(performance.now(),performance.now(), 2500, () => sv.removeToast());
49
      }
50
    };
51
52
    sv.init = function() {
53
      sv.hoverElement = '';
54
      sv.export = '';
55
      sv.toast = [];
56
      sv.isToastVisible = false;
57
      newElements = [];
58
    };
59
60
    sv.hasNew = function(entry) {
61
      return newElements.indexOf(entry) !== -1;
62
    };
63
64
    sv.addNew = function(entry) {
65
      newElements.push(entry);
66
    };
67
68
    sv.removeNew = function(entry) {
69
      if (newElements.indexOf(entry) !== -1) {
70
        newElements.splice(newElements.indexOf(entry), 1);
71
      }
72
    };
73
74
    sv.elementHasNew = function(element) {
75
      let includes = data.elements[element].includes;
76
      for (let key in includes) {
77
        if (sv.hasNew(includes[key])) {
78
          return true;
79
        }
80
      }
81
      return false;
82
    };
83
84
    sv.registerUpdate = function(name, func){
85
      updateFunctions[name] = func;
86
    };
87
88
    sv.update = function(player){
89
      for(let func in updateFunctions){
90
        updateFunctions[func](player);
91
      }
92
    };
93
  }]);
94