Completed
Push — trunk ( a060bf...055e93 )
by SuperNova.WS
04:11
created

PlayerLevelHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 133
rs 10
ccs 0
cts 63
cp 0
wmc 21

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isLevelExpired() 0 2 1
A getPlayerLevels() 0 6 3
C getPointLevel() 0 26 7
A loadLevels() 0 12 4
A storeLevels() 0 3 1
A __construct() 0 4 1
A calcLevels() 0 13 4
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 classSupernova;
11
use classConfig;
12
13
class PlayerLevelHelper {
14
  /**
15
   * @var float[] $playerLevels - [(int)level => (float)maxPointsForLevel]
16
   */
17
  protected $playerLevels = [];
18
19
  /**
20
   * @var GlobalContainer $gc
21
   */
22
  protected $gc;
23
24
  /**
25
   * @var classConfig $config
26
   */
27
  protected $config;
28
29
  /**
30
   * @var ValueStorage $valueStorage
31
   */
32
  protected $valueStorage;
33
34
  /**
35
   * PlayerLevelHelper constructor.
36
   *
37
   * @param GlobalContainer $gc
38
   */
39
  public function __construct(GlobalContainer $gc) {
40
    $this->gc = $gc;
41
    $this->config = $this->gc->config;
42
    $this->valueStorage = $this->gc->valueStorage;
43
  }
44
45
  /**
46
   * Get player level table
47
   *
48
   * @return float[]
49
   */
50
  public function getPlayerLevels() {
51
    if ($this->isLevelExpired() || empty($this->playerLevels)) {
52
      $this->loadLevels();
53
    }
54
55
    return $this->playerLevels;
56
  }
57
58
  /**
59
   * Calculate level by supplied points
60
   *
61
   * Presumed that it's a total points but can be any others
62
   *
63
   * @param float $totalPoints
64
   *
65
   * @return int
66
   */
67
  public function getPointLevel($totalPoints, $authLevel = false) {
68
    if ($authLevel && classSupernova::$config->stats_hide_admins) {
69
      return PLAYER_RANK_MAX;
70
    }
71
72
    $playerLevels = $this->getPlayerLevels();
73
74
    $theLevel = null;
75
    foreach ($playerLevels as $level => $points) {
76
      if ($totalPoints <= $points) {
77
        $theLevel = $level;
78
        break;
79
      }
80
    }
81
82
    // If no levels found - it means that points is above max calculated level
83
    // We will address it tomorrow - when levels recalculates. For now just give use +1 level - would be ok for a day
84
    if ($theLevel === null) {
85
      end($playerLevels);
86
      $theLevel = key($playerLevels) + 1;
87
    }
88
    if ($theLevel > PLAYER_RANK_MAX) {
89
      $theLevel = PLAYER_RANK_MAX;
90
    }
91
92
    return $theLevel;
93
  }
94
95
  /**
96
   * Should level be recalculated?
97
   *
98
   * @return bool
99
   */
100
  protected function isLevelExpired() {
101
    return datePart(strtotime($this->config->player_levels_calculated)) < datePart(SN_TIME_NOW);
102
  }
103
104
  /**
105
   * Loading level data from DB
106
   */
107
  protected function loadLevels() {
108
    if ($this->isLevelExpired()) {
109
      $levelArray = [];
110
    } else {
111
      $levelArray = json_decode($this->config->player_levels, true);
112
    }
113
114
    if (empty($levelArray) || !is_array($levelArray)) {
115
      $this->calcLevels();
116
      $this->storeLevels();
117
    } else {
118
      $this->playerLevels = $levelArray;
119
    }
120
  }
121
122
  /**
123
   * Storing level data in DB
124
   */
125
  protected function storeLevels() {
126
    $this->config->pass()->player_levels_calculated = SN_TIME_SQL;
127
    $this->config->pass()->player_levels = json_encode($this->playerLevels);
128
  }
129
130
  /**
131
   * Calculate level table
132
   */
133
  protected function calcLevels() {
134
    $this->playerLevels = [];
135
136
    $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

136
    $multiplier = $this->valueStorage->getValue(/** @scrutinizer ignore-type */ UNIT_SERVER_FLEET_NOOB_FACTOR);
Loading history...
137
    !$multiplier ? $multiplier = 5 : false;
138
139
    $levelPoints = $this->valueStorage->getValue(UNIT_SERVER_FLEET_NOOB_POINTS);
140
    !$levelPoints ? $levelPoints = 5000 * $this->valueStorage->getValue(UNIT_SERVER_SPEED_MINING) : false;
141
    $level = 0;
142
    do {
143
      $this->playerLevels[$level++] = $levelPoints;
144
      $gotUser = doquery("SELECT 1 FROM `{{users}}` WHERE `total_points` > {$levelPoints} LIMIT 1", true);
145
      $levelPoints *= $multiplier;
146
    } while (!empty($gotUser));
147
  }
148
149
}
150