fix(web-client): surface nickname rejection and stop scene restart loop

This commit is contained in:
2026-04-10 18:03:56 +07:00
parent 3615310335
commit f66a68b590
2 changed files with 20 additions and 3 deletions
+8 -3
View File
@@ -24,9 +24,14 @@ export class BootScene extends Phaser.Scene {
// Connect to server
connectionService.connect();
// Transition to menu once server prompts for nickname
eventBus.on(ClientEventCode.NICKNAME_SET, () => {
// Transition to menu once server prompts for nickname. Server emits
// NICKNAME_SET both as the initial prompt AND on validation rejection,
// so unsubscribe after the first hit — otherwise later rejections would
// bounce the scene back to MenuScene (restart loop).
const onPrompt = () => {
eventBus.off(ClientEventCode.NICKNAME_SET, onPrompt);
this.scene.start('MenuScene');
});
};
eventBus.on(ClientEventCode.NICKNAME_SET, onPrompt);
}
}
+12
View File
@@ -7,6 +7,7 @@ import { connectionService } from '../services/connection-service.js';
import { eventBus } from '../services/event-bus.js';
import { gameState } from '../services/game-state-service.js';
import { ServerEventCode, ClientEventCode } from '../config/protocol-constants.js';
import { showToast } from './game-ui.js';
const overlay = () => document.getElementById('ui-overlay');
@@ -178,3 +179,14 @@ eventBus.on(ClientEventCode.ROOM_CREATE_SUCCESS, (data) => {
});
eventBus.on(ClientEventCode.CLIENT_EXIT, showLobby);
eventBus.on(ClientEventCode.CLIENT_KICK, showLobby);
// Server emits NICKNAME_SET as rejection when the submitted nickname fails
// length validation (payload: { invalidLength: N }). Surface it as a toast
// and clear the optimistically-stored local nickname so we don't drift.
eventBus.on(ClientEventCode.NICKNAME_SET, (data) => {
if (data && typeof data === 'object' && 'invalidLength' in data) {
showToast(`Nickname must be 110 characters (got ${data.invalidLength})`, 'error');
gameState.nickname = '';
showNicknameScreen();
}
});