Passed
Branch trunk (3f392c)
by SuperNova.WS
04:59
created

PlayerLevelHelper::calcLevels()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 0
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 13.12.2017 15:45
4
 */
5
6
namespace Player;
7
8
use Common\GlobalContainer;
9
use Bonus\ValueStorage;
10
use classConfig;
11
12
class PlayerLevelHelper {
13
  /**
14
   * @var float[] $playerLevels - [(int)level => (float)maxPointsForLevel]
15
   */
16
  protected $playerLevels = [];
17
18
  /**
19
   * @var GlobalContainer $gc
20
   */
21
  protected $gc;
22
23
  /**
24
   * @var classConfig $config
25
   */
26
  protected $config;
27
28
  /**
29
   * @var ValueStorage $valueStorage
30
   */
31
  protected $valueStorage;
32
33
  /**
34
   * PlayerLevelHelper constructor.
35
   *
36
   * @param GlobalContainer $gc
37
   */
38
  public function __construct(GlobalContainer $gc) {
39
    $this->gc = $gc;
40
    $this->config = $this->gc->config;
41
    $this->valueStorage = $this->gc->valueStorage;
42
  }
43
44
  /**
45
   * Get player level table
46
   *
47
   * @return float[]
48
   */
49
  public function getPlayerLevels() {
50
    if ($this->isLevelExpired() || empty($this->playerLevels)) {
51
      $this->loadLevels();
52
    }
53
54
    return $this->playerLevels;
55
  }
56
57
  /**
58
   * Calculate level by supplied points
59
   *
60
   * Presumed that it's a total points but can be any others
61
   *
62
   * @param float $totalPoints
63
   *
64
   * @return int
65
   */
66
  public function getPointLevel($totalPoints) {
67
    $playerLevels = $this->getPlayerLevels();
68
69
    $theLevel = null;
70
    foreach ($playerLevels as $level => $points) {
71
      if ($totalPoints <= $points) {
72
        $theLevel = $level;
73
        break;
74
      }
75
    }
76
77
    // If no levels found - it means that points is above max calculated level
78
    // We will address it tomorrow - when levels recalculates. For now just give use +1 level - would be ok for a day
79
    if ($theLevel === null) {
80
      end($playerLevels);
81
      $theLevel = key($playerLevels) + 1;
82
    }
83
84
    return $theLevel;
85
  }
86
87
  /**
88
   * Should level be recalculated?
89
   *
90
   * @return bool
91
   */
92
  protected function isLevelExpired() {
93
    return datePart(strtotime($this->config->player_levels_calculated)) < datePart(SN_TIME_NOW);
94
  }
95
96
  /**
97
   * Loading level data from DB
98
   */
99
  protected function loadLevels() {
100
    if ($this->isLevelExpired()) {
101
      $levelArray = [];
102
    } else {
103
      $levelArray = json_decode($this->config->player_levels, true);
104
    }
105
106
    if (empty($levelArray) || !is_array($levelArray)) {
107
      $this->calcLevels();
108
      $this->storeLevels();
109
    } else {
110
      $this->playerLevels = $levelArray;
111
    }
112
  }
113
114
  /**
115
   * Storing level data in DB
116
   */
117
  protected function storeLevels() {
118
    $this->config->pass()->player_levels_calculated = SN_TIME_SQL;
119
    $this->config->pass()->player_levels = json_encode($this->playerLevels);
120
  }
121
122
  /**
123
   * Calculate level table
124
   */
125
  protected function calcLevels() {
126
    $this->playerLevels = [];
127
128
    $multiplier = $this->valueStorage->getValue(UNIT_SERVER_FLEET_NOOB_FACTOR);
0 ignored issues
show
Bug introduced by
Player\UNIT_SERVER_FLEET_NOOB_FACTOR of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueStorage::getValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
    $multiplier = $this->valueStorage->getValue(/** @scrutinizer ignore-type */ UNIT_SERVER_FLEET_NOOB_FACTOR);
Loading history...
129
    !$multiplier ? $multiplier = 5 : false;
130
131
    $levelPoints = $this->valueStorage->getValue(UNIT_SERVER_FLEET_NOOB_POINTS);
132
    !$levelPoints ? $levelPoints = 5000 * $this->valueStorage->getValue(UNIT_SERVER_SPEED_MINING) : false;
133
    $level = 0;
134
    do {
135
      $this->playerLevels[$level++] = $levelPoints;
136
      $levelPoints *= $multiplier;
137
    } while (!empty($gotUser = doquery("SELECT 1 FROM `{{users}}` WHERE `total_points` > {$levelPoints} LIMIT 1", true)));
138
  }
139
140
}
141