diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 924e002..d016388 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -9,22 +9,29 @@ export default function App() { const { token } = useAuth() const { openAuthModal } = useAuthModal() const [screen, setScreen] = useState('menu') // 'menu' | 'search' | 'game' + const [searchMode, setSearchMode] = useState('random') // 'random' | 'invite' | 'join' + const [joinCode, setJoinCode] = useState(null) const [gameSession, setGameSession] = useState(null) const handleRandomBattle = () => { if (!token) return openAuthModal() + setSearchMode('random') + setJoinCode(null) setScreen('search') } const handleInvite = () => { if (!token) return openAuthModal() - // TODO: создание приватной комнаты + setSearchMode('invite') + setJoinCode(null) + setScreen('search') } const handleJoinByCode = (code) => { if (!token) return openAuthModal() - // TODO: присоединение к комнате по коду - console.log('join code:', code) + setSearchMode('join') + setJoinCode(code) + setScreen('search') } const handleMatched = useCallback((match) => { @@ -45,6 +52,8 @@ export default function App() { return ( setScreen('menu')} /> diff --git a/frontend/src/components/CodeInput.jsx b/frontend/src/components/CodeInput.jsx index 6c3d171..95a278e 100644 --- a/frontend/src/components/CodeInput.jsx +++ b/frontend/src/components/CodeInput.jsx @@ -12,7 +12,7 @@ export default function CodeInput({ onSubmit }) { const focusInput = (i) => inputsRef.current[i]?.focus() const handleChange = (i, raw) => { - const chars = raw.toUpperCase().replace(/[^A-Z0-9]/g, '') + const chars = raw.toUpperCase().replace(/[^A-Z]/g, '') if (!chars) return const next = [...values] // Поддержка вставки нескольких символов (paste в отдельное поле тоже) @@ -47,7 +47,7 @@ export default function CodeInput({ onSubmit }) { const handlePaste = (e) => { e.preventDefault() - const text = e.clipboardData.getData('text').toUpperCase().replace(/[^A-Z0-9]/g, '') + const text = e.clipboardData.getData('text').toUpperCase().replace(/[^A-Z]/g, '') if (!text) return const next = Array(CODE_LENGTH).fill('') for (let k = 0; k < Math.min(text.length, CODE_LENGTH); k++) next[k] = text[k] diff --git a/frontend/src/components/SearchScreen.jsx b/frontend/src/components/SearchScreen.jsx index 29f9112..d07d9a9 100644 --- a/frontend/src/components/SearchScreen.jsx +++ b/frontend/src/components/SearchScreen.jsx @@ -5,6 +5,8 @@ import { useCable } from '../cable' const STAGE_LABELS = { connecting: 'Подключение...', searching: 'Подбор соперника...', + waiting_for_opponent: 'Ожидание соперника...', + joining: 'Подключение к игре...', } const ERROR_LABELS = { @@ -13,9 +15,10 @@ const ERROR_LABELS = { rejected: 'Не удалось начать поиск. Попробуйте позже.', } -export default function SearchScreen({ token, onMatched, onCancel }) { +export default function SearchScreen({ token, mode = 'random', joinCode = null, onMatched, onCancel }) { const { consumer } = useCable() - const [stage, setStage] = useState('connecting') // 'connecting' | 'searching' + const [stage, setStage] = useState('connecting') + const [inviteCode, setInviteCode] = useState(null) const [error, setError] = useState(null) const [retryToken, setRetryToken] = useState(0) const attemptRef = useRef(0) @@ -27,15 +30,27 @@ export default function SearchScreen({ token, onMatched, onCancel }) { setStage('connecting') setError(null) + setInviteCode(null) - matchmaking = createMatchmaking(consumer || token, (status) => { - if (!cancelled && attempt === attemptRef.current) setStage(status) + matchmaking = createMatchmaking(consumer || token, { + mode, + code: joinCode, + onStatus: (status) => { + if (!cancelled && attempt === attemptRef.current) setStage(status) + }, + onInviteCreated: (code) => { + if (!cancelled && attempt === attemptRef.current) setInviteCode(code) + }, }) matchmaking.promise.then(onMatched).catch((err) => { if (cancelled || attempt !== attemptRef.current) return if (err instanceof MatchmakingError) { - setError(ERROR_LABELS[err.code] || ERROR_LABELS.connection) + if (err.code === 'invite_error') { + setError(err.message || 'Ошибка приглашения') + } else { + setError(ERROR_LABELS[err.code] || err.message || ERROR_LABELS.connection) + } } else { setError(ERROR_LABELS.connection) } @@ -45,30 +60,34 @@ export default function SearchScreen({ token, onMatched, onCancel }) { cancelled = true matchmaking.cancel() } - }, [consumer, token, onMatched, retryToken]) + }, [consumer, token, mode, joinCode, onMatched, retryToken]) const handleCancel = () => { onCancel() } - return ( -
-
- {error ? ( + const renderContent = () => { + if (error) { + return ( + <> +
{error}
+
+ + +
+ + ) + } + + if (mode === 'invite') { + if (!inviteCode) { + return ( <> -
{error}
-
- - -
- - ) : ( - <> -
{STAGE_LABELS[stage]}
+
Создание приглашения...