Donat nezd majd át mert a backenden is lett valtozas nem tudom pontosan hogy kell e vagy sem Köszi

This commit is contained in:
2025-11-14 15:21:59 +01:00
parent a7ce891098
commit 0f85356154
10 changed files with 2007 additions and 71 deletions
@@ -197,9 +197,8 @@ export class GameWebSocketService {
private async handleJoinGame(socket: AuthenticatedSocket, data: any): Promise<void> {
try {
// Simple data extraction - let Socket.IO handle the parsing
const jsdata = JSON.parse(data);
const gameToken = jsdata?.gameToken;
// Socket.IO automatically deserializes JSON - data is already an object
const gameToken = data?.gameToken;
if (!gameToken) {
logError('Game join failed: No game token provided');
@@ -243,6 +242,12 @@ export class GameWebSocketService {
const isGamemaster = game.createdby === userId;
const needsApproval = game.logintype === LoginType.PRIVATE && !isGamemaster;
logOther(`Player joining game: ${playerName}`);
logOther(` - userId: ${userId}`);
logOther(` - game.createdby: ${game.createdby}`);
logOther(` - isGamemaster: ${isGamemaster}`);
logOther(` - needsApproval: ${needsApproval}`);
// Generate dynamic room names (needed for both approval and direct join)
const gameRoomName = `game_${gameCode}`;
const playerRoomName = `game_${gameCode}:${playerName}`;
@@ -275,6 +280,8 @@ export class GameWebSocketService {
await socket.join(gameRoomName);
await socket.join(playerRoomName);
// Update Redis with active player connection FIRST (before getting state)
await this.updatePlayerConnection(gameCode, playerName, true);
// Send success response to the joining player
socket.emit('game:joined', {
@@ -296,13 +303,12 @@ export class GameWebSocketService {
});
// Send current game state to the joining player
// Send current game state to the joining player (now includes this player)
const gameState = await this.getGameState(gameCode);
socket.emit('game:state', gameState);
// Update Redis with active player connection
await this.updatePlayerConnection(gameCode, playerName, true);
// Broadcast updated game state to all other players so they see the new player
socket.to(gameRoomName).emit('game:state-update', gameState);
} catch (error) {
socket.emit('game:error', {
@@ -314,7 +320,7 @@ export class GameWebSocketService {
private async handleLeaveGame(socket: AuthenticatedSocket, data: LeaveGameData): Promise<void> {
try {
const { gameCode } = JSON.parse(data as any);
const { gameCode } = data;
const playerName = socket.playerName;
// Validate we have the required data
@@ -354,7 +360,7 @@ export class GameWebSocketService {
private async handleGameAction(socket: AuthenticatedSocket, data: GameActionData): Promise<void> {
try {
const { gameCode, action, data: actionData } = JSON.parse(data as any);
const { gameCode, action, data: actionData } = data;
if (!socket.gameCode || socket.gameCode !== gameCode) {
socket.emit('game:error', { message: 'You must be in the game to perform actions' });
@@ -396,7 +402,7 @@ export class GameWebSocketService {
private async handleGameChat(socket: AuthenticatedSocket, data: GameChatData): Promise<void> {
try {
const { gameCode, message } = JSON.parse(data as any);
const { gameCode, message } = data;
if (!socket.gameCode || socket.gameCode !== gameCode) {
socket.emit('game:error', { message: 'You must be in the game to chat' });
@@ -422,7 +428,7 @@ export class GameWebSocketService {
private async handlePlayerReady(socket: AuthenticatedSocket, data: { gameCode: string; ready: boolean }): Promise<void> {
try {
const { gameCode, ready } = JSON.parse(data as any);
const { gameCode, ready } = data;
const gameRoomName = `game_${gameCode}`;
// Update player ready status in Redis
@@ -452,7 +458,7 @@ export class GameWebSocketService {
private async handleApprovePlayer(socket: AuthenticatedSocket, data: { gameCode: string; playerName: string }): Promise<void> {
try {
const { gameCode, playerName } = JSON.parse(data as any);
const { gameCode, playerName } = data;
// Verify that the requesting socket is the gamemaster
const game = await this.gameRepository.findByGameCode(gameCode);
@@ -513,7 +519,7 @@ export class GameWebSocketService {
private async handleRejectPlayer(socket: AuthenticatedSocket, data: { gameCode: string; playerName: string; reason?: string }): Promise<void> {
try {
const { gameCode, playerName, reason } = JSON.parse(data as any);
const { gameCode, playerName, reason } = data;
// Verify that the requesting socket is the gamemaster
const game = await this.gameRepository.findByGameCode(gameCode);
@@ -561,7 +567,7 @@ export class GameWebSocketService {
private async handleJoinApproved(socket: AuthenticatedSocket, data: JoinGameData): Promise<void> {
try {
const { gameToken } = JSON.parse(data as any);
const { gameToken } = data;
if (!gameToken) {
socket.emit('game:error', { message: 'Game token is required' });
@@ -606,6 +612,9 @@ export class GameWebSocketService {
logOther(`Approved player ${playerName} joined game room: ${gameRoomName}`);
// Update Redis with active player connection FIRST (before getting state)
await this.updatePlayerConnection(gameCode, playerName, true);
// Send success response to the joining player
socket.emit('game:joined', {
gameCode,
@@ -624,12 +633,12 @@ export class GameWebSocketService {
timestamp: new Date().toISOString()
});
// Send current game state to the joining player
// Send current game state to the joining player (now includes this player)
const gameState = await this.getGameState(gameCode);
socket.emit('game:state', gameState);
// Update Redis with active player connection
await this.updatePlayerConnection(gameCode, playerName, true);
// Broadcast updated game state to all other players so they see the new player
socket.to(gameRoomName).emit('game:state-update', gameState);
} catch (error) {
logError('Error handling approved join', error as Error);
@@ -639,7 +648,7 @@ export class GameWebSocketService {
private async handleDiceRoll(socket: AuthenticatedSocket, data: DiceRollData): Promise<void> {
try {
const { gameCode, diceValue } = JSON.parse(data as any);
const { gameCode, diceValue } = data;
// Validate input
if (!gameCode || !socket.gameCode || socket.gameCode !== gameCode) {
@@ -738,7 +747,7 @@ export class GameWebSocketService {
private async handleCardAnswer(socket: AuthenticatedSocket, data: CardAnswerData): Promise<void> {
try {
const { gameCode, answer } = JSON.parse(data as any);
const { gameCode, answer } = data;
// Validate input
if (!gameCode || !socket.gameCode || socket.gameCode !== gameCode) {
@@ -848,7 +857,7 @@ export class GameWebSocketService {
private async handleGamemasterDecision(socket: AuthenticatedSocket, data: GamemasterDecisionData): Promise<void> {
try {
const { gameCode, requestId, decision } = JSON.parse(data as any);
const { gameCode, requestId, decision } = data;
// Validate input
if (!gameCode || !socket.gameCode || socket.gameCode !== gameCode) {