1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 13.12.2017 15:45 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Player; |
7
|
|
|
|
8
|
|
|
use Core\GlobalContainer; |
9
|
|
|
use Bonus\ValueStorage; |
10
|
|
|
use HelperString; |
11
|
|
|
use SN; |
12
|
|
|
use classConfig; |
13
|
|
|
|
14
|
|
|
class PlayerLevelHelper { |
15
|
|
|
/** |
16
|
|
|
* @var float[] $playerLevels - [(int)level => (float)maxPointsForLevel] |
17
|
|
|
*/ |
18
|
|
|
protected $playerLevels = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var GlobalContainer $gc |
22
|
|
|
*/ |
23
|
|
|
protected $gc; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var classConfig $config |
27
|
|
|
*/ |
28
|
|
|
protected $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ValueStorage $valueStorage |
32
|
|
|
*/ |
33
|
|
|
protected $valueStorage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* PlayerLevelHelper constructor. |
37
|
|
|
* |
38
|
|
|
* @param GlobalContainer $gc |
39
|
|
|
*/ |
40
|
|
|
public function __construct(GlobalContainer $gc) { |
41
|
|
|
$this->gc = $gc; |
42
|
|
|
$this->config = $this->gc->config; |
43
|
|
|
$this->valueStorage = $this->gc->valueStorage; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get player level table |
48
|
|
|
* |
49
|
|
|
* @return float[] |
50
|
|
|
*/ |
51
|
|
|
public function getPlayerLevels() { |
52
|
|
|
if ($this->isLevelExpired() || empty($this->playerLevels)) { |
53
|
|
|
$this->loadLevels(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->playerLevels; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Calculate level by supplied points |
61
|
|
|
* |
62
|
|
|
* Presumed that it's a total points but can be any others |
63
|
|
|
* |
64
|
|
|
* @param float $totalPoints |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function getPointLevel($totalPoints, $authLevel = false) { |
69
|
|
|
if ($authLevel && SN::$config->stats_hide_admins) { |
70
|
|
|
return PLAYER_RANK_MAX; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$playerLevels = $this->getPlayerLevels(); |
74
|
|
|
|
75
|
|
|
$theLevel = null; |
76
|
|
|
foreach ($playerLevels as $level => $points) { |
77
|
|
|
if ($totalPoints <= $points) { |
78
|
|
|
$theLevel = $level; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// If no levels found - it means that points is above max calculated level |
84
|
|
|
// We will address it tomorrow - when levels recalculates. For now just give use +1 level - would be ok for a day |
85
|
|
|
if ($theLevel === null) { |
86
|
|
|
end($playerLevels); |
87
|
|
|
$theLevel = key($playerLevels) + 1; |
88
|
|
|
} |
89
|
|
|
if ($theLevel > PLAYER_RANK_MAX) { |
90
|
|
|
$theLevel = PLAYER_RANK_MAX; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $theLevel; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $playerRank |
98
|
|
|
* @param bool $noTitle - do not add title to image |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function renderRank($playerRank, $noTitle = false) { |
103
|
|
|
$title = (empty($noTitle) |
104
|
|
|
? " title='" . HelperString::htmlEncode("[{$playerRank}] " . SN::$lang['ranks'][$playerRank]) . "'" |
105
|
|
|
: '' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return "<div class='rank nick rank-{$playerRank}'{$title}></div>"; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param float $playerPoints |
113
|
|
|
* @param int $authLevel |
114
|
|
|
* @param bool $noTitle - do not add title to image |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function renderRankFromPoints($playerPoints, $authLevel = AUTH_LEVEL_REGISTERED, $noTitle = false) { |
119
|
|
|
return $this->renderRank($this->getPointLevel($playerPoints, $authLevel), $noTitle); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Should level be recalculated? |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
protected function isLevelExpired() { |
128
|
|
|
return datePart(strtotime($this->config->player_levels_calculated)) < datePart(SN_TIME_NOW); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Loading level data from DB |
133
|
|
|
*/ |
134
|
|
|
protected function loadLevels() { |
135
|
|
|
if ($this->isLevelExpired()) { |
136
|
|
|
$levelArray = []; |
137
|
|
|
} else { |
138
|
|
|
$levelArray = json_decode($this->config->player_levels, true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (empty($levelArray) || !is_array($levelArray)) { |
142
|
|
|
$this->calcLevels(); |
143
|
|
|
$this->storeLevels(); |
144
|
|
|
} else { |
145
|
|
|
$this->playerLevels = $levelArray; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Storing level data in DB |
151
|
|
|
*/ |
152
|
|
|
protected function storeLevels() { |
153
|
|
|
$this->config->pass()->player_levels_calculated = SN_TIME_SQL; |
154
|
|
|
$this->config->pass()->player_levels = json_encode($this->playerLevels); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Calculate level table |
159
|
|
|
*/ |
160
|
|
|
protected function calcLevels() { |
161
|
|
|
$this->playerLevels = []; |
162
|
|
|
|
163
|
|
|
$multiplier = $this->valueStorage->getValue(UNIT_SERVER_FLEET_NOOB_FACTOR); |
|
|
|
|
164
|
|
|
!$multiplier ? $multiplier = 5 : false; |
165
|
|
|
|
166
|
|
|
$levelPoints = $this->valueStorage->getValue(UNIT_SERVER_FLEET_NOOB_POINTS); |
167
|
|
|
!$levelPoints ? $levelPoints = 5000 * $this->valueStorage->getValue(UNIT_SERVER_SPEED_MINING) : false; |
168
|
|
|
|
169
|
|
|
for ($level = 0; $level <= PLAYER_RANK_MAX; $level++) { |
170
|
|
|
$this->playerLevels[$level] = $levelPoints; |
171
|
|
|
$levelPoints *= $multiplier; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|