Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 'use client' import { useEffect, useState } from 'react' import type { Socket } from 'socket.io-client' import { createSocket } from '@/lib/socket' export default function TestArcadePage() { const [socket, setSocket] = useState<Socket | null>(null) const [connected, setConnected] = useState(false) const [userId] = useState('evybhb5o0v4t76e7qnrx3x1t') // Use real user ID from DB const [logs, setLogs] = useState<string[]>([]) const addLog = (message: string) => { setLogs((prev) => [...prev, `${new Date().toLocaleTimeString()}: ${message}`]) } useEffect(() => { // Initialize socket connection const socketInstance = createSocket() socketInstance.on('connect', () => { setConnected(true) addLog('â Connected to Socket.IO') }) socketInstance.on('disconnect', () => { setConnected(false) addLog('â Disconnected from Socket.IO') }) socketInstance.on('session-state', (data) => { addLog(`đĻ Received session state: ${JSON.stringify(data, null, 2)}`) }) socketInstance.on('no-active-session', () => { addLog('âšī¸ No active session found') }) socketInstance.on('move-accepted', (data) => { addLog(`â Move accepted: ${JSON.stringify(data, null, 2)}`) }) socketInstance.on('move-rejected', (data) => { addLog(`â Move rejected: ${JSON.stringify(data, null, 2)}`) }) socketInstance.on('session-ended', () => { addLog('đĒ Session ended') }) socketInstance.on('session-error', (data) => { addLog(`â ī¸ Session error: ${data.error}`) }) setSocket(socketInstance) return () => { socketInstance.disconnect() } }, [addLog]) const joinSession = () => { if (!socket) return addLog(`Joining session as user: ${userId}`) socket.emit('join-arcade-session', { userId }) } const startGame = () => { if (!socket) return const move = { type: 'START_GAME', playerId: userId, timestamp: Date.now(), data: { activePlayers: [1], }, } addLog(`Sending START_GAME move: ${JSON.stringify(move)}`) socket.emit('game-move', { userId, move }) } const testFlipCard = () => { if (!socket) return const move = { type: 'FLIP_CARD', playerId: userId, timestamp: Date.now(), data: { cardId: 'test-card-1', }, } addLog(`Sending move: ${JSON.stringify(move)}`) socket.emit('game-move', { userId, move }) } const createSession = async () => { addLog('Creating test session via API...') // First, delete any existing session addLog('Deleting any existing session first...') try { await fetch(`/api/arcade-session?userId=${userId}`, { method: 'DELETE', }) addLog('â Old session deleted (if existed)') } catch (error) { addLog(`â ī¸ Could not delete old session: ${error}`) } // Now create new session try { const response = await fetch('/api/arcade-session', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId, gameName: 'matching', gameUrl: '/arcade/matching', initialState: { cards: [], gameCards: [], flippedCards: [], gameType: 'abacus-numeral', difficulty: 6, turnTimer: 30, gamePhase: 'setup', currentPlayer: 1, matchedPairs: 0, totalPairs: 6, moves: 0, scores: {}, activePlayers: [1], consecutiveMatches: {}, gameStartTime: null, gameEndTime: null, currentMoveStartTime: null, timerInterval: null, celebrationAnimations: [], isProcessingMove: false, showMismatchFeedback: false, lastMatchedPair: null, }, activePlayers: [1], }), }) const data = await response.json() if (response.ok) { addLog(`â Session created successfully`) } else { addLog(`â Failed to create session: ${data.error}`) } } catch (error) { addLog(`â Error creating session: ${error}`) } } const exitSession = () => { if (!socket) return addLog(`Exiting session for user: ${userId}`) socket.emit('exit-arcade-session', { userId }) } return ( <div style={{ padding: '20px', fontFamily: 'monospace' }}> <h1>Arcade Session Test</h1> <div style={{ marginBottom: '20px', padding: '10px', backgroundColor: '#fff3cd', borderRadius: '4px', }} > <strong>Test Cross-Tab Sync:</strong> <ol style={{ marginLeft: '20px', marginTop: '10px' }}> <li>Open this page in TWO browser tabs</li> <li>In Tab 1: Click buttons 1 â 2</li> <li>In Tab 2: Click button 2 only (session already exists)</li> <li>In Tab 1: Click button 3 (Start Game)</li> <li>Watch Tab 2's event log - it should show "â Move accepted" instantly!</li> </ol> </div> <div style={{ marginBottom: '20px' }}> <strong>Connection Status:</strong>{' '} <span style={{ color: connected ? 'green' : 'red' }}> {connected ? 'đĸ Connected' : 'đ´ Disconnected'} </span> </div> <div style={{ marginBottom: '20px' }}> <strong>User ID:</strong> {userId} </div> <div style={{ marginBottom: '20px', display: 'flex', gap: '10px', flexWrap: 'wrap', }} > <button onClick={createSession} style={{ padding: '10px 20px', backgroundColor: '#6f42c1', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer', }} > 1. Create Session </button> <button onClick={joinSession} disabled={!connected} style={{ padding: '10px 20px', backgroundColor: connected ? '#007bff' : '#ccc', color: 'white', border: 'none', borderRadius: '4px', cursor: connected ? 'pointer' : 'not-allowed', }} > 2. Join Session </button> <button onClick={startGame} disabled={!connected} style={{ padding: '10px 20px', backgroundColor: connected ? '#17a2b8' : '#ccc', color: 'white', border: 'none', borderRadius: '4px', cursor: connected ? 'pointer' : 'not-allowed', }} > 3. Start Game (broadcasts!) </button> <button onClick={testFlipCard} disabled={!connected} style={{ padding: '10px 20px', backgroundColor: connected ? '#28a745' : '#ccc', color: 'white', border: 'none', borderRadius: '4px', cursor: connected ? 'pointer' : 'not-allowed', }} > 4. Test Flip Card </button> <button onClick={exitSession} disabled={!connected} style={{ padding: '10px 20px', backgroundColor: connected ? '#dc3545' : '#ccc', color: 'white', border: 'none', borderRadius: '4px', cursor: connected ? 'pointer' : 'not-allowed', }} > 5. Exit Session </button> </div> <div style={{ marginTop: '20px', padding: '10px', backgroundColor: '#f5f5f5', borderRadius: '4px', maxHeight: '400px', overflow: 'auto', }} > <h3>Event Log:</h3> {logs.length === 0 ? ( <p style={{ color: '#666' }}>No events yet...</p> ) : ( <div> {logs.map((log, i) => ( <div key={i} style={{ padding: '5px', borderBottom: '1px solid #ddd', fontSize: '12px', }} > {log} </div> ))} </div> )} </div> </div> ) } |