Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.48% |
65 / 66 |
|
91.67% |
11 / 12 |
CRAP | |
50.00% |
1 / 2 |
| XPForNextCalculator | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| section | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| Level | |
98.31% |
58 / 59 |
|
90.00% |
9 / 10 |
24 | |
0.00% |
0 / 1 |
| getSections | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| getXPForNext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sectionSum | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| getXpSumToGetLevel | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| XPAndRatingIsGainedInTsumegoStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getOverallXPGained | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addXP | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addXPAsResultOfTsumegoSolving | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| oldXPSumCode | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
9 | |||
| checkLevelUp | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | class XPForNextCalculator |
| 4 | { |
| 5 | public function __construct($level) |
| 6 | { |
| 7 | foreach (Level::getSections() as $section) |
| 8 | if ($this->section($level, $section[0], $section[1])) |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | public function section($level, $to, $jump): bool |
| 13 | { |
| 14 | $steps = min($to, $level) - $this->from; |
| 15 | $this->result += $steps * $jump; |
| 16 | $this->from = $to; |
| 17 | return $level <= $to; |
| 18 | } |
| 19 | |
| 20 | public int $from = 1; |
| 21 | public int $result = 50; |
| 22 | } |
| 23 | |
| 24 | class Level |
| 25 | { |
| 26 | // this needs to be up to date with level code in util.js |
| 27 | public static function getSections(): array |
| 28 | { |
| 29 | static $sections = [ |
| 30 | [11, 10], |
| 31 | [19, 25], |
| 32 | [39, 50], |
| 33 | [69, 100], |
| 34 | [99, 150], |
| 35 | [100, 50000], |
| 36 | [101, 1150], |
| 37 | [10000, 0]]; |
| 38 | return $sections; |
| 39 | } |
| 40 | |
| 41 | public static function getXPForNext($level): int |
| 42 | { |
| 43 | return new XPForNextCalculator($level)->result; |
| 44 | } |
| 45 | |
| 46 | private static function sectionSum($level, &$from, $to, $jump, &$result, &$xpIncrease): bool |
| 47 | { |
| 48 | $steps = min($to, $level) - $from; |
| 49 | $result += $steps * $xpIncrease; |
| 50 | if ($steps > 1) |
| 51 | $result += (($steps * ($steps - 1)) / 2) * $jump; |
| 52 | $from = $to; |
| 53 | $xpIncrease += $steps * $jump; |
| 54 | return $level <= $to; |
| 55 | } |
| 56 | |
| 57 | public static function getXpSumToGetLevel($level): int |
| 58 | { |
| 59 | $result = 0; |
| 60 | $from = 1; |
| 61 | $xpIncrease = 50; |
| 62 | |
| 63 | foreach (self::getSections() as $section) |
| 64 | if (self::sectionSum($level, $from, $section[0], $section[1], $result, $xpIncrease)) |
| 65 | return $result; |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | public static function XPAndRatingIsGainedInTsumegoStatus($status) |
| 70 | { |
| 71 | return $status != 'S' && $status != 'C'; // solved or doulbe solved is already rewarded, otherwise ok |
| 72 | } |
| 73 | |
| 74 | public static function getOverallXPGained($user) |
| 75 | { |
| 76 | return Level::getXpSumToGetLevel($user['level']) + $user['xp']; |
| 77 | } |
| 78 | |
| 79 | public static function addXP(&$user, $value) |
| 80 | { |
| 81 | $user['xp'] += $value; |
| 82 | Level::checkLevelUp($user); |
| 83 | } |
| 84 | |
| 85 | public static function addXPAsResultOfTsumegoSolving(&$user, $value) |
| 86 | { |
| 87 | Level::addXP($user, $value); |
| 88 | $user['daily_xp'] += $value; |
| 89 | $user['daily_solved']++; |
| 90 | } |
| 91 | |
| 92 | public static function oldXPSumCode($level): int |
| 93 | { |
| 94 | $startxp = 50; |
| 95 | $sumx = 0; |
| 96 | $xpJump = 10; |
| 97 | |
| 98 | for ($i = 1; $i < $level; $i++) |
| 99 | { |
| 100 | if ($i >= 11) |
| 101 | $xpJump = 25; |
| 102 | if ($i >= 19) |
| 103 | $xpJump = 50; |
| 104 | if ($i >= 39) |
| 105 | $xpJump = 100; |
| 106 | if ($i >= 69) |
| 107 | $xpJump = 150; |
| 108 | if ($i >= 99) |
| 109 | $xpJump = 50000; |
| 110 | if ($i == 100) |
| 111 | $xpJump = 1150; |
| 112 | if ($i >= 101) |
| 113 | $xpJump = 0; |
| 114 | $sumx += $startxp; |
| 115 | $startxp += $xpJump; |
| 116 | } |
| 117 | return $sumx; |
| 118 | } |
| 119 | |
| 120 | public static function checkLevelUp(&$user) |
| 121 | { |
| 122 | while (true) |
| 123 | { |
| 124 | $nextLevel = Level::getXPForNext($user['level']); |
| 125 | if ($user['xp'] < $nextLevel) |
| 126 | return; |
| 127 | $user['xp'] -= $nextLevel; |
| 128 | $user['level']++; |
| 129 | } |
| 130 | } |
| 131 | } |