| 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']; |
| 9 | } |
| 10 | |
| 11 | function pieceColorPrefs(string $userId): array |
| 12 | { |
| 13 | $row = query('SELECT color_mode, preferred_color, alternative_color 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 | query( |
| 34 | 'INSERT INTO user_piece_colors(user_id,color_mode,preferred_color,alternative_color) VALUES(?,?,?,?) |
| 35 | ON CONFLICT(user_id) DO UPDATE SET color_mode=excluded.color_mode, preferred_color=excluded.preferred_color, alternative_color=excluded.alternative_color', |
| 36 | [$userId, $mode, $preferred, $alternative] |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function effectivePreferredColor(array $prefs, string $rulesColour): string |
| 41 | { |
| 42 | return ($prefs['color_mode'] ?? 'default') === 'default' ? $rulesColour : $prefs['preferred_color']; |
| 43 | } |
| 44 | |
| 45 | function nextDistinctPieceColor(string $from, string $skip): string |
| 46 | { |
| 47 | $i = array_search($from, PIECE_COLORS, true); |
| 48 | if ($i === false) { |
| 49 | $i = 0; |
| 50 | } |
| 51 | for ($n = 0; $n < count(PIECE_COLORS); $n++) { |
| 52 | $i = ($i + 1) % count(PIECE_COLORS); |
| 53 | if (PIECE_COLORS[$i] !== $skip) { |
| 54 | return PIECE_COLORS[$i]; |
| 55 | } |
| 56 | } |
| 57 | throw new RuntimeException('Unable to pick a distinct piece colour.'); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Resolve display skins for the white and black rules sides. |
| 62 | * @param null|callable():int $coinFlip returns 0 or 1; injectable for tests |
| 63 | * @return array{white:string,black:string} |
| 64 | */ |
| 65 | function resolvePieceSkins(array $whitePrefs, array $blackPrefs, ?callable $coinFlip = null): array |
| 66 | { |
| 67 | $coinFlip ??= static fn(): int => random_int(0, 1); |
| 68 | $whiteWant = effectivePreferredColor($whitePrefs, 'White'); |
| 69 | $blackWant = effectivePreferredColor($blackPrefs, 'Black'); |
| 70 | if ($whiteWant !== $blackWant) { |
| 71 | return ['white' => $whiteWant, 'black' => $blackWant]; |
| 72 | } |
| 73 | |
| 74 | $whiteMode = $whitePrefs['color_mode'] ?? 'default'; |
| 75 | $blackMode = $blackPrefs['color_mode'] ?? 'default'; |
| 76 | |
| 77 | if ($whiteMode === 'default' && $blackMode === 'custom') { |
| 78 | $alt = $blackPrefs['alternative_color']; |
| 79 | if ($alt === $whiteWant) { |
| 80 | $alt = nextDistinctPieceColor($alt, $whiteWant); |
| 81 | } |
| 82 | return ['white' => $whiteWant, 'black' => $alt]; |
| 83 | } |
| 84 | if ($blackMode === 'default' && $whiteMode === 'custom') { |
| 85 | $alt = $whitePrefs['alternative_color']; |
| 86 | if ($alt === $blackWant) { |
| 87 | $alt = nextDistinctPieceColor($alt, $blackWant); |
| 88 | } |
| 89 | return ['white' => $alt, 'black' => $blackWant]; |
| 90 | } |
| 91 | |
| 92 | // Both custom (or residual conflict): randomly pick who keeps preferred. |
| 93 | if ($coinFlip()) { |
| 94 | $alt = $blackPrefs['alternative_color']; |
| 95 | if ($alt === $whiteWant) { |
| 96 | $alt = nextDistinctPieceColor($alt, $whiteWant); |
| 97 | } |
| 98 | return ['white' => $whiteWant, 'black' => $alt]; |
| 99 | } |
| 100 | $alt = $whitePrefs['alternative_color']; |
| 101 | if ($alt === $blackWant) { |
| 102 | $alt = nextDistinctPieceColor($alt, $blackWant); |
| 103 | } |
| 104 | return ['white' => $alt, 'black' => $blackWant]; |
| 105 | } |
| 106 | |
| 107 | function assignGamePieceSkins(int $gameId, string $whiteId, string $blackId): void |
| 108 | { |
| 109 | $skins = resolvePieceSkins(pieceColorPrefs($whiteId), pieceColorPrefs($blackId)); |
| 110 | query('UPDATE games SET piece_skin_white=?, piece_skin_black=? WHERE id=?', [$skins['white'], $skins['black'], $gameId]); |
| 111 | } |
| 112 | |
| 113 | function gamePieceSkins(array $g): array |
| 114 | { |
| 115 | $white = $g['piece_skin_white'] ?? null; |
| 116 | $black = $g['piece_skin_black'] ?? null; |
| 117 | if (!is_string($white) || $white === '' || !in_array($white, PIECE_COLORS, true)) { |
| 118 | $white = 'White'; |
| 119 | } |
| 120 | if (!is_string($black) || $black === '' || !in_array($black, PIECE_COLORS, true)) { |
| 121 | $black = 'Black'; |
| 122 | } |
| 123 | return ['white' => $white, 'black' => $black]; |
| 124 | } |
projects-timfalken-com microchess