| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | const PIECE_COLORS = ['White', 'Black', 'Red', 'Blue', 'Green', 'Yellow']; |
| 5 | |
| 6 | function defaultPieceColorPrefs(): array |
| 7 | { |
| 8 | return ['color_mode' => 'default', 'preferred_color' => 'White', 'alternative_color' => 'Black', 'disable_custom_colours' => 0]; |
| 9 | } |
| 10 | |
| 11 | function pieceColorPrefs(string $userId): array |
| 12 | { |
| 13 | $row = query('SELECT color_mode, preferred_color, alternative_color, COALESCE(disable_custom_colours, 0) AS disable_custom_colours FROM user_piece_colors WHERE user_id=?', [$userId])->fetch(); |
| 14 | return $row ?: defaultPieceColorPrefs(); |
| 15 | } |
| 16 | |
| 17 | function validatePieceColors(string $mode, string $preferred, string $alternative): void |
| 18 | { |
| 19 | if (!in_array($mode, ['default', 'custom'], true)) { |
| 20 | throw new DomainException('Choose default or custom colours.'); |
| 21 | } |
| 22 | if (!in_array($preferred, PIECE_COLORS, true) || !in_array($alternative, PIECE_COLORS, true)) { |
| 23 | throw new DomainException('Choose colours from White, Black, Red, Blue, Green or Yellow.'); |
| 24 | } |
| 25 | if ($preferred === $alternative) { |
| 26 | throw new DomainException('Preferred and alternative colours must differ.'); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | function savePieceColorPrefs(string $userId, string $mode, string $preferred, string $alternative): void |
| 31 | { |
| 32 | validatePieceColors($mode, $preferred, $alternative); |
| 33 | $existing = query('SELECT disable_custom_colours FROM user_piece_colors WHERE user_id=?', [$userId])->fetchColumn(); |
| 34 | $disable = $existing === false ? 0 : (int)$existing; |
| 35 | query( |
| 36 | 'INSERT INTO user_piece_colors(user_id,color_mode,preferred_color,alternative_color,disable_custom_colours) VALUES(?,?,?,?,?) |
| 37 | ON CONFLICT(user_id) DO UPDATE SET color_mode=excluded.color_mode, preferred_color=excluded.preferred_color, alternative_color=excluded.alternative_color', |
| 38 | [$userId, $mode, $preferred, $alternative, $disable] |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | function saveDisableCustomColours(string $userId, bool $disabled): void |
| 43 | { |
| 44 | $row = pieceColorPrefs($userId); |
| 45 | query( |
| 46 | 'INSERT INTO user_piece_colors(user_id,color_mode,preferred_color,alternative_color,disable_custom_colours) VALUES(?,?,?,?,?) |
| 47 | ON CONFLICT(user_id) DO UPDATE SET disable_custom_colours=excluded.disable_custom_colours', |
| 48 | [$userId, $row['color_mode'], $row['preferred_color'], $row['alternative_color'], $disabled ? 1 : 0] |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | function effectivePreferredColor(array $prefs, string $rulesColour): string |
| 53 | { |
| 54 | return ($prefs['color_mode'] ?? 'default') === 'default' ? $rulesColour : $prefs['preferred_color']; |
| 55 | } |
| 56 | |
| 57 | function nextDistinctPieceColor(string $from, string $skip): string |
| 58 | { |
| 59 | $i = array_search($from, PIECE_COLORS, true); |
| 60 | if ($i === false) { |
| 61 | $i = 0; |
| 62 | } |
| 63 | for ($n = 0; $n < count(PIECE_COLORS); $n++) { |
| 64 | $i = ($i + 1) % count(PIECE_COLORS); |
| 65 | if (PIECE_COLORS[$i] !== $skip) { |
| 66 | return PIECE_COLORS[$i]; |
| 67 | } |
| 68 | } |
| 69 | throw new RuntimeException('Unable to pick a distinct piece colour.'); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Resolve display skins for the white and black rules sides. |
| 74 | * @param null|callable():int $coinFlip returns 0 or 1; injectable for tests |
| 75 | * @return array{white:string,black:string} |
| 76 | */ |
| 77 | function resolvePieceSkins(array $whitePrefs, array $blackPrefs, ?callable $coinFlip = null): array |
| 78 | { |
| 79 | $coinFlip ??= static fn(): int => random_int(0, 1); |
| 80 | $whiteWant = effectivePreferredColor($whitePrefs, 'White'); |
| 81 | $blackWant = effectivePreferredColor($blackPrefs, 'Black'); |
| 82 | if ($whiteWant !== $blackWant) { |
| 83 | return ['white' => $whiteWant, 'black' => $blackWant]; |
| 84 | } |
| 85 | |
| 86 | $whiteMode = $whitePrefs['color_mode'] ?? 'default'; |
| 87 | $blackMode = $blackPrefs['color_mode'] ?? 'default'; |
| 88 | |
| 89 | if ($whiteMode === 'default' && $blackMode === 'custom') { |
| 90 | $alt = $blackPrefs['alternative_color']; |
| 91 | if ($alt === $whiteWant) { |
| 92 | $alt = nextDistinctPieceColor($alt, $whiteWant); |
| 93 | } |
| 94 | return ['white' => $whiteWant, 'black' => $alt]; |
| 95 | } |
| 96 | if ($blackMode === 'default' && $whiteMode === 'custom') { |
| 97 | $alt = $whitePrefs['alternative_color']; |
| 98 | if ($alt === $blackWant) { |
| 99 | $alt = nextDistinctPieceColor($alt, $blackWant); |
| 100 | } |
| 101 | return ['white' => $alt, 'black' => $blackWant]; |
| 102 | } |
| 103 | |
| 104 | // Both custom (or residual conflict): randomly pick who keeps preferred. |
| 105 | if ($coinFlip()) { |
| 106 | $alt = $blackPrefs['alternative_color']; |
| 107 | if ($alt === $whiteWant) { |
| 108 | $alt = nextDistinctPieceColor($alt, $whiteWant); |
| 109 | } |
| 110 | return ['white' => $whiteWant, 'black' => $alt]; |
| 111 | } |
| 112 | $alt = $whitePrefs['alternative_color']; |
| 113 | if ($alt === $blackWant) { |
| 114 | $alt = nextDistinctPieceColor($alt, $blackWant); |
| 115 | } |
| 116 | return ['white' => $alt, 'black' => $blackWant]; |
| 117 | } |
| 118 | |
| 119 | function assignGamePieceSkins(int $gameId, string $whiteId, string $blackId): void |
| 120 | { |
| 121 | $skins = resolvePieceSkins(pieceColorPrefs($whiteId), pieceColorPrefs($blackId)); |
| 122 | query('UPDATE games SET piece_skin_white=?, piece_skin_black=? WHERE id=?', [$skins['white'], $skins['black'], $gameId]); |
| 123 | } |
| 124 | |
| 125 | function gamePieceSkins(array $g): array |
| 126 | { |
| 127 | $white = $g['piece_skin_white'] ?? null; |
| 128 | $black = $g['piece_skin_black'] ?? null; |
| 129 | if (!is_string($white) || $white === '' || !in_array($white, PIECE_COLORS, true)) { |
| 130 | $white = 'White'; |
| 131 | } |
| 132 | if (!is_string($black) || $black === '' || !in_array($black, PIECE_COLORS, true)) { |
| 133 | $black = 'Black'; |
| 134 | } |
| 135 | return ['white' => $white, 'black' => $black]; |
| 136 | } |
projects-timfalken-com microchess