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); |
|
|
|
|
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
|
|
|
|