The code returns the following error:
Error fetching game state: TypeError: fetch failed
at async GET (app/api/game/state/route.ts:19:22)
17 | // Call the parent route to get game state
18 | const baseUrl = request.url.split('/api/game/state')[0];
> 19 | const response = await fetch(`${baseUrl}/api/game?gameId=${gameId}&playerId=${playerId}`);
| ^
20 | const gameState = await response.json();
21 |
22 | return new Response(JSON.stringify(gameState), { {
[cause]: [Error: C0419FD51F7F0000:error:0A0000C6:SSL routines:tls_get_more_records:packet length too long:../deps/openssl/openssl/ssl/record/methods/tls_common.c:662:
] {
library: 'SSL routines',
reason: 'packet length too long',
code: 'ERR_SSL_PACKET_LENGTH_TOO_LONG'
}
}
Revise the code to address the error.
I see the problem. The state route is trying to fetch from the parent route using an internal fetch with an incorrect URL construction. The issue is that internal API calls within the same server should not use fetch() - they should directly access the data. Let me fix the state route to export the game state data directly instead of trying to fetch it:
The issue is that the state route is trying to fetch internally which is causing SSL errors. The simplest fix is to replace the state route with a direct implementation that mimics what the game route returns. Let me rewrite the state route to be independent:
Actually, the real issue is that the Game component is polling the state route, but the state route can't communicate back with the main game route properly. The better solution is to refactor so the Game component polls the main /api/game route directly instead of using the state route. Let me update the Game component:
Now I can delete the problematic state route file since it's no longer needed:
I've fixed the SSL error by removing the problematic state route that was trying to internally fetch from the game route, which caused SSL packet length errors. The Game component now polls directly from /api/game for state updates, which is simpler and avoids the internal fetch issue. The game should now work correctly for all modes including AI vs AI.
I'll add a feature to let the AI play as the second player in multiplayer mode, so players don't have to wait for another human. Let me first check the current Lobby and Game logic:
I'll add a "Create Game with AI" option for multiplayer mode. Let me update the Lobby to add this feature:
Now let me update the page.tsx to handle the aiSecondPlayer flag:
Now let me update the Game component to pass this flag to the API:
Now let me update the API route to handle the aiSecondPlayer mode:
Now let me find and update the GET handler:
Now let me find the game start logic and add aiSecondPlayer handling:
Now let me update the game loop to handle AI second player as well:
I've successfully added the AI second player feature to the multiplayer mode. Here's what I implemented:
AI Second Player Feature:
The implementation flows through the Lobby → page.tsx → Game component → API route, with the aiSecondPlayer flag controlling whether AI fills the second player slot. This gives players flexible multiplayer options without needing to wait for others to join.