'default', 'preferred_color' => 'White', 'alternative_color' => 'Black', 'disable_custom_colours' => 0]; } function pieceColorPrefs(string $userId): array { $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(); return $row ?: defaultPieceColorPrefs(); } function validatePieceColors(string $mode, string $preferred, string $alternative): void { if (!in_array($mode, ['default', 'custom'], true)) { throw new DomainException('Choose default or custom colours.'); } if (!in_array($preferred, PIECE_COLORS, true) || !in_array($alternative, PIECE_COLORS, true)) { throw new DomainException('Choose colours from White, Black, Red, Blue, Green or Yellow.'); } if ($preferred === $alternative) { throw new DomainException('Preferred and alternative colours must differ.'); } } function savePieceColorPrefs(string $userId, string $mode, string $preferred, string $alternative): void { validatePieceColors($mode, $preferred, $alternative); $existing = query('SELECT disable_custom_colours FROM user_piece_colors WHERE user_id=?', [$userId])->fetchColumn(); $disable = $existing === false ? 0 : (int)$existing; query( 'INSERT INTO user_piece_colors(user_id,color_mode,preferred_color,alternative_color,disable_custom_colours) VALUES(?,?,?,?,?) ON CONFLICT(user_id) DO UPDATE SET color_mode=excluded.color_mode, preferred_color=excluded.preferred_color, alternative_color=excluded.alternative_color', [$userId, $mode, $preferred, $alternative, $disable] ); } function saveDisableCustomColours(string $userId, bool $disabled): void { $row = pieceColorPrefs($userId); query( 'INSERT INTO user_piece_colors(user_id,color_mode,preferred_color,alternative_color,disable_custom_colours) VALUES(?,?,?,?,?) ON CONFLICT(user_id) DO UPDATE SET disable_custom_colours=excluded.disable_custom_colours', [$userId, $row['color_mode'], $row['preferred_color'], $row['alternative_color'], $disabled ? 1 : 0] ); } function effectivePreferredColor(array $prefs, string $rulesColour): string { return ($prefs['color_mode'] ?? 'default') === 'default' ? $rulesColour : $prefs['preferred_color']; } function nextDistinctPieceColor(string $from, string $skip): string { $i = array_search($from, PIECE_COLORS, true); if ($i === false) { $i = 0; } for ($n = 0; $n < count(PIECE_COLORS); $n++) { $i = ($i + 1) % count(PIECE_COLORS); if (PIECE_COLORS[$i] !== $skip) { return PIECE_COLORS[$i]; } } throw new RuntimeException('Unable to pick a distinct piece colour.'); } /** * Resolve display skins for the white and black rules sides. * @param null|callable():int $coinFlip returns 0 or 1; injectable for tests * @return array{white:string,black:string} */ function resolvePieceSkins(array $whitePrefs, array $blackPrefs, ?callable $coinFlip = null): array { $coinFlip ??= static fn(): int => random_int(0, 1); $whiteWant = effectivePreferredColor($whitePrefs, 'White'); $blackWant = effectivePreferredColor($blackPrefs, 'Black'); if ($whiteWant !== $blackWant) { return ['white' => $whiteWant, 'black' => $blackWant]; } $whiteMode = $whitePrefs['color_mode'] ?? 'default'; $blackMode = $blackPrefs['color_mode'] ?? 'default'; if ($whiteMode === 'default' && $blackMode === 'custom') { $alt = $blackPrefs['alternative_color']; if ($alt === $whiteWant) { $alt = nextDistinctPieceColor($alt, $whiteWant); } return ['white' => $whiteWant, 'black' => $alt]; } if ($blackMode === 'default' && $whiteMode === 'custom') { $alt = $whitePrefs['alternative_color']; if ($alt === $blackWant) { $alt = nextDistinctPieceColor($alt, $blackWant); } return ['white' => $alt, 'black' => $blackWant]; } // Both custom (or residual conflict): randomly pick who keeps preferred. if ($coinFlip()) { $alt = $blackPrefs['alternative_color']; if ($alt === $whiteWant) { $alt = nextDistinctPieceColor($alt, $whiteWant); } return ['white' => $whiteWant, 'black' => $alt]; } $alt = $whitePrefs['alternative_color']; if ($alt === $blackWant) { $alt = nextDistinctPieceColor($alt, $blackWant); } return ['white' => $alt, 'black' => $blackWant]; } function assignGamePieceSkins(int $gameId, string $whiteId, string $blackId): void { $skins = resolvePieceSkins(pieceColorPrefs($whiteId), pieceColorPrefs($blackId)); query('UPDATE games SET piece_skin_white=?, piece_skin_black=? WHERE id=?', [$skins['white'], $skins['black'], $gameId]); } function gamePieceSkins(array $g): array { $white = $g['piece_skin_white'] ?? null; $black = $g['piece_skin_black'] ?? null; if (!is_string($white) || $white === '' || !in_array($white, PIECE_COLORS, true)) { $white = 'White'; } if (!is_string($black) || $black === '' || !in_array($black, PIECE_COLORS, true)) { $black = 'Black'; } return ['white' => $white, 'black' => $black]; }