Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.47% |
17 / 19 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| BoardSelector | |
89.47% |
17 / 19 |
|
33.33% |
1 / 3 |
11.14 | |
0.00% |
0 / 1 |
| filterValidBits | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getBoardInfo | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| selectBoard | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
6.03 | |||
| 1 | <?php |
| 2 | |
| 3 | class BoardSelector |
| 4 | { |
| 5 | // Note: this data is duplicated in BoardSelector.js |
| 6 | // the reason for this duplication is, that on client, I can cache this js file and don't have to send it every refresh |
| 7 | // While on backend, I still need to have access to it, to properly setup the board directly without relying on js reload. |
| 8 | public static array $boards |
| 9 | = [ |
| 10 | ['name' => 'Pine', 'index' => 1, 'texture' => 'texture1', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 11 | ['name' => 'Ash', 'index' => 2, 'texture' => 'texture2', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 12 | ['name' => 'Maple', 'index' => 3, 'texture' => 'texture3', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 13 | ['name' => 'Shin Kaya', 'index' => 4, 'texture' => 'texture4', 'black' => 'black.png', 'white' => 'white.png'], |
| 14 | ['name' => 'Birch', 'index' => 5, 'texture' => 'texture5', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 15 | ['name' => 'Wenge', 'index' => 6, 'texture' => 'texture6', 'black' => 'black.png', 'white' => 'white.png'], |
| 16 | ['name' => 'Walnut', 'index' => 7, 'texture' => 'texture7', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 17 | ['name' => 'Mahogany', 'index' => 8, 'texture' => 'texture8', 'black' => 'black.png', 'white' => 'white.png'], |
| 18 | ['name' => 'Blackwood', 'index' => 9, 'texture' => 'texture9', 'black' => 'black.png', 'white' => 'white.png'], |
| 19 | ['name' => 'Marble 1', 'index' => 10, 'texture' => 'texture10', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 20 | ['name' => 'Marble 2', 'index' => 11, 'texture' => 'texture11', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 21 | ['name' => 'Marble 3', 'index' => 12, 'texture' => 'texture12', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 22 | ['name' => 'Tibet Spruce', 'index' => 13, 'texture' => 'texture13', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 23 | ['name' => 'Marble 4', 'index' => 14, 'texture' => 'texture14', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 24 | ['name' => 'Marble 5', 'index' => 15, 'texture' => 'texture15', 'black' => 'black.png', 'white' => 'white.png'], |
| 25 | ['name' => 'Quarry 1', 'index' => 16, 'texture' => 'texture16', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 26 | ['name' => 'Flowers', 'index' => 17, 'texture' => 'texture17', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 27 | ['name' => 'Nova', 'index' => 18, 'texture' => 'texture18', 'black' => 'black.png', 'white' => 'white.png'], |
| 28 | ['name' => 'Spring', 'index' => 19, 'texture' => 'texture19', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 29 | ['name' => 'Moon', 'index' => 20, 'texture' => 'texture20', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 30 | ['name' => 'Apex', 'index' => 33, 'texture' => 'texture33', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 31 | ['name' => 'Gold 1', 'index' => 21, 'texture' => 'texture21', 'black' => 'black.png', 'white' => 'whiteKo.png'], |
| 32 | ['name' => 'Amber', 'index' => 22, 'texture' => 'texture22', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 33 | ['name' => 'Marble 6', 'index' => 34, 'texture' => 'texture34', 'black' => 'black.png', 'white' => 'white.png'], |
| 34 | ['name' => 'Marble 7', 'index' => 35, 'texture' => 'texture35', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 35 | ['name' => 'Marble 8', 'index' => 36, 'texture' => 'texture36', 'black' => 'black.png', 'white' => 'white.png'], |
| 36 | ['name' => 'Marble 9', 'index' => 37, 'texture' => 'texture37', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 37 | ['name' => 'Marble 10', 'index' => 38, 'texture' => 'texture38', 'black' => 'black38.png', 'white' => 'white34.png'], |
| 38 | ['name' => 'Jade', 'index' => 39, 'texture' => 'texture39', 'black' => 'black.png', 'white' => 'white.png'], |
| 39 | ['name' => 'Quarry 2', 'index' => 40, 'texture' => 'texture40', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 40 | ['name' => 'Black Bricks', 'index' => 41, 'texture' => 'texture41', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 41 | ['name' => 'Wallpaper 1', 'index' => 42, 'texture' => 'texture42', 'black' => 'black34.png', 'white' => 'white42.png'], |
| 42 | ['name' => 'Wallpaper 2', 'index' => 43, 'texture' => 'texture43', 'black' => 'black34.png', 'white' => 'white42.png'], |
| 43 | ['name' => 'Gold & Gray', 'index' => 44, 'texture' => 'texture44', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 44 | ['name' => 'Gold & Pink', 'index' => 45, 'texture' => 'texture45', 'black' => 'black34.png', 'white' => 'white42.png'], |
| 45 | ['name' => 'Veil', 'index' => 47, 'texture' => 'texture47', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 46 | ['name' => 'Tiles', 'index' => 48, 'texture' => 'texture48', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 47 | ['name' => 'Mars', 'index' => 49, 'texture' => 'texture49', 'black' => 'black.png', 'white' => 'white.png'], |
| 48 | ['name' => 'Pink Cloud', 'index' => 50, 'texture' => 'texture50', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 49 | ['name' => 'Reptile', 'index' => 51, 'texture' => 'texture51', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 50 | ['name' => 'Mezmerizing', 'index' => 52, 'texture' => 'texture52', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 51 | ['name' => 'Magenta Sky', 'index' => 53, 'texture' => 'texture53', 'black' => 'black34.png', 'white' => 'white34.png'], |
| 52 | ['name' => 'Tsumego Hero', 'index' => 54, 'texture' => 'texture54', 'black' => 'black54.png', 'white' => 'white54.png'], |
| 53 | ['name' => 'Pretty', 'index' => 23, 'texture' => 'texture23', 'black' => 'black.png', 'white' => 'whiteFlower.png'], |
| 54 | ['name' => 'Hunting', 'index' => 24, 'texture' => 'texture24', 'black' => 'black24.png', 'white' => 'white24.png'], |
| 55 | ['name' => 'Haunted', 'index' => 25, 'texture' => 'texture25', 'black' => 'blackGhost.png', 'white' => 'white.png'], |
| 56 | ['name' => 'Carnage', 'index' => 26, 'texture' => 'texture26', 'black' => 'blackInvis.png', 'white' => 'whiteCarnage.png'], |
| 57 | ['name' => 'Blind Spot', 'index' => 27, 'texture' => 'texture27', 'black' => 'black27.png', 'white' => 'white27.png'], |
| 58 | ['name' => 'Giants', 'index' => 28, 'texture' => 'texture28', 'black' => 'blackGiant.png', 'white' => 'whiteKo.png'], |
| 59 | ['name' => 'Gems', 'index' => 29, 'texture' => 'texture29', 'black' => 'blackKo.png', 'white' => 'whiteKo.png'], |
| 60 | ['name' => 'Grandmaster', 'index' => 30, 'texture' => 'texture55', 'black' => 'blackGalaxy.png', 'white' => 'whiteGalaxy.png'] |
| 61 | ]; |
| 62 | |
| 63 | public static $GOLDEN_BOARD = ['name' => 'Golden', 'texture' => 'textureGolden', 'black' => 'black34.png', 'white' => 'white34.png']; |
| 64 | |
| 65 | public static function filterValidBits($boardsSelection) |
| 66 | { |
| 67 | $allowedBits = 0; |
| 68 | // linear search, but we search once per refresh, so w/e |
| 69 | foreach (self::$boards as $board) |
| 70 | $allowedBits |= (1 << ($board['index'] - 1)); |
| 71 | return $boardsSelection & $allowedBits; |
| 72 | } |
| 73 | |
| 74 | public static function getBoardInfo($index) |
| 75 | { |
| 76 | // linear search, but we search once per refresh, so w/e |
| 77 | foreach (self::$boards as $board) |
| 78 | if ($board['index'] == $index) |
| 79 | return $board; |
| 80 | return self::$boards[0]; |
| 81 | } |
| 82 | |
| 83 | public static function selectBoard($boardsBitmask, $goldenTsumego, $setPreference) |
| 84 | { |
| 85 | if (!is_null($setPreference)) |
| 86 | return self::getBoardInfo($setPreference); |
| 87 | |
| 88 | if ($goldenTsumego) |
| 89 | return self::$GOLDEN_BOARD; |
| 90 | $indexes = []; |
| 91 | |
| 92 | foreach (self::$boards as $board) |
| 93 | if ($boardsBitmask & (1 << ($board['index'] - 1))) |
| 94 | $indexes[] = $board['index']; |
| 95 | |
| 96 | if (empty($indexes)) |
| 97 | return self::getBoardInfo(1); |
| 98 | |
| 99 | return self::getBoardInfo($indexes[array_rand($indexes)]); |
| 100 | } |
| 101 | |
| 102 | public static $DEFAULT_BOARDS_BITMASK = 0b11111111; |
| 103 | } |