Completed
Push — master ( 41eb99...0e62c1 )
by Andres
26s
created

angular.service(ꞌsavegameꞌ)   C

Complexity

Conditions 12
Paths 30

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 30
dl 0
loc 30
rs 5.1612
c 0
b 0
f 0
nop 0

How to fix   Complexity   

Complexity

Complex classes like angular.service(ꞌsavegameꞌ) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/* globals versionCompare, alert */
2
/**
3
 savegame
4
 Service that handles save/load related functions.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('savegame', ['$state',
13
    'state',
14
    'data',
15
    function ($state, state, data) {
16
      this.initSave = function () {
17
        state.player = {};
18
        this.versionControl();
19
        state.init();
20
        $state.go('generators');
21
      };
22
23
      this.save = function () {
24
        state.player.last_login = Math.floor(Date.now()/1000);
25
        localStorage.setItem('player', JSON.stringify(state.player));
26
      };
27
28
      this.load = function () {
29
        try {
30
          let storedPlayer = localStorage.getItem('player');
31
          if (!storedPlayer) {
32
            this.initSave();
33
          } else {
34
            state.player = JSON.parse(storedPlayer);
35
            this.versionControl();
36
          }
37
        } catch (err) {
38
          alert('Error loading savegame, reset forced.');
39
          this.initSave();
40
        }
41
      };
42
43
      this.versionControl = function () {
44
        // delete saves older than this version
45
        if (state.player.version && versionCompare(state.player.version, '2.6.0') < 0) {
46
          state.player = {};
47
        }
48
        // we merge the properties of the player with the start player to
49
        // avoid undefined errors with new properties
50
        state.player = angular.merge({}, data.start_player, state.player);
51
52
        for(let resource in state.player.resources){
53
          if(!data.resources[resource]){
54
            delete state.player.resources[resource];
55
          }
56
          if(state.player.resources[resource] !== null && state.player.resources[resource].unlocked){
57
            state.player.resources[resource] = state.player.resources[resource].number;
58
          }else{
59
            state.player.resources[resource] = null;
60
          }
61
        }
62
        for(let slot of state.player.element_slots){
63
          if(!slot){
64
            continue;
65
          }
66
          for(let i in slot.redoxes){
67
            if(slot.redoxes[i].from === -2 || slot.redoxes[i].to === -2){
68
              slot.redoxes.splice(i, 1);
69
            }
70
          }
71
        }
72
      };
73
    }
74
  ]);
75