Lobby Frontend Updates for Global Chat

Overview

The lobby frontend components have been updated to support global chat functionality alongside the existing room-based chat. This allows for seamless integration of global chat features while maintaining backward compatibility with existing room chat implementations.

Updated Components

1. EnhancedChatLobby Component

Location: src/components/game/enhanced-chat-lobby.tsx

New Props:

  • isGlobalChat?: boolean - Enables global chat mode when set to true

Key Changes:

  • Dynamic WebSocket URL: Automatically switches between global chat and room chat endpoints
  • Message Type Handling: Supports both global_message and regular chat_message types
  • Join Message Types: Uses player_join_global for global chat, join for room chat
  • Connection Messages: Different success messages for global vs room chat

Usage Example:

// Room chat (existing behavior)
<EnhancedChatLobby 
  lobbyId="room-123" 
  player={player} 
  isGlobalChat={false} 
/>

// Global chat (new functionality)
<EnhancedChatLobby 
  lobbyId="global-chat" 
  player={player} 
  isGlobalChat={true} 
/>

2. ChatLobby Component

Location: src/components/game/chat-lobby.tsx

New Props:

  • isGlobalChat?: boolean - Enables global chat mode when set to true

Key Changes:

  • WebSocket Endpoint: Routes to /api/global-chat when isGlobalChat is true
  • Message Handling: Supports global chat message types
  • NPC Interactions: Maintains NPC functionality for both modes

Usage Example:

// Room chat
<ChatLobby 
  lobbyId="room-123" 
  player={player} 
  isGlobalChat={false} 
/>

// Global chat
<ChatLobby 
  lobbyId="global-chat" 
  player={player} 
  isGlobalChat={true} 
/>

3. GlobalChatLobby Component (New)

Location: src/components/game/global-chat-lobby.tsx

Features:

  • Dedicated Global Chat UI: Specialized interface for global chat
  • Minimizable Interface: Can be minimized or fully collapsed
  • Global Statistics: Displays platform-wide statistics
  • Responsive Design: Works on both desktop and mobile

Props:

  • player: PlayerInfo - Player information
  • autoConnect?: boolean - Auto-connect to global chat
  • isModal?: boolean - Display as modal overlay
  • hideHeader?: boolean - Hide the header section

Usage Example:

<GlobalChatLobby 
  player={player}
  autoConnect={true}
  isModal={false}
  hideHeader={false}
/>

Message Type Support

Global Chat Message Types

Message TypeDescriptionComponent Support
global_messageRegular global chat messages✅ All components
player_join_globalPlayer joining global chat✅ All components
welcome_globalWelcome message for new users✅ All components
global_statsPlatform statistics updates✅ GlobalChatLobby
global_player_listOnline players list✅ All components
typing_start_globalGlobal typing indicators✅ All components
typing_stop_globalGlobal typing indicators✅ All components
level_up_globalGlobal level up announcements✅ All components

Room Chat Message Types (Existing)

Message TypeDescriptionComponent Support
chat_messageRegular room chat messages✅ All components
joinPlayer joining room✅ All components
npc_messageNPC responses✅ All components
level_upRoom level up announcements✅ All components
player_listRoom players list✅ All components

Implementation Examples

1. Adding Global Chat to Existing Pages

Before (Room-only chat):

<EnhancedChatLobby 
  lobbyId={roomId} 
  player={player} 
/>

After (With global chat option):

<EnhancedChatLobby 
  lobbyId={isGlobalChat ? "global-chat" : roomId} 
  player={player} 
  isGlobalChat={isGlobalChat}
/>

2. Creating a Global Chat Only Page

import { GlobalChatLobby } from '@/components/game/global-chat-lobby';

export default function GlobalChatPage() {
  return (
    <div className="min-h-screen bg-background">
      <GlobalChatLobby 
        player={player}
        autoConnect={true}
        isModal={false}
      />
    </div>
  );
}

3. Toggle Between Room and Global Chat

import { useState } from 'react';
import { EnhancedChatLobby } from '@/components/game/enhanced-chat-lobby';

export function ChatToggle() {
  const [isGlobalChat, setIsGlobalChat] = useState(false);
  const [roomId, setRoomId] = useState('room-123');

  return (
    <div>
      <div className="flex gap-2 mb-4">
        <Button 
          onClick={() => setIsGlobalChat(false)}
          variant={!isGlobalChat ? "default" : "outline"}
        >
          Room Chat
        </Button>
        <Button 
          onClick={() => setIsGlobalChat(true)}
          variant={isGlobalChat ? "default" : "outline"}
        >
          Global Chat
        </Button>
      </div>

      <EnhancedChatLobby 
        lobbyId={isGlobalChat ? "global-chat" : roomId}
        player={player}
        isGlobalChat={isGlobalChat}
      />
    </div>
  );
}

WebSocket Connection Logic

Global Chat Connection

// When isGlobalChat = true
const wsUrl = `${protocol}//${window.location.host}/api/global-chat`;

// Join message
ws.send(JSON.stringify({
  type: 'player_join_global',
  player: player
}));

// Send message
ws.send(JSON.stringify({
  type: 'global_message',
  message: message,
  player: player
}));

Room Chat Connection

// When isGlobalChat = false
const wsUrl = `${protocol}//${window.location.host}/api/lobby/${lobbyId}/websocket`;

// Join message
ws.send(JSON.stringify({
  type: 'join',
  player: player
}));

// Send message
ws.send(JSON.stringify({
  type: 'message',
  message: message,
  player: player
}));

Styling and UI Considerations

Global Chat Visual Indicators

GlobalChatLobby Component:

  • Globe icon (🌐) to distinguish from room chat
  • Blue color scheme for global chat elements
  • Online player count badge
  • Platform statistics display

EnhancedChatLobby/ChatLobby Components:

  • Dynamic connection messages
  • Context-aware join notifications
  • Message type handling for both modes

Responsive Design

Desktop:

  • Global chat can be displayed as sidebar or overlay
  • Minimizable interface for non-intrusive experience
  • Statistics panel for detailed information

Mobile:

  • Full-width or modal display options
  • Collapsible interface for space efficiency
  • Touch-friendly controls

Migration Guide

For Existing Implementations

  1. No Breaking Changes: Existing room chat functionality remains unchanged
  2. Optional Global Chat: Add isGlobalChat prop only when needed
  3. Backward Compatibility: All existing props and functionality preserved

Adding Global Chat Support

  1. Import Updated Components:

    import { EnhancedChatLobby } from '@/components/game/enhanced-chat-lobby';
    import { GlobalChatLobby } from '@/components/game/global-chat-lobby';
  2. Add Global Chat Option:

    <EnhancedChatLobby 
      lobbyId="global-chat"
      player={player}
      isGlobalChat={true}
    />
  3. Handle Global Message Types:

    // Message handling is already included in updated components
    // No additional code needed for basic functionality

Best Practices

1. User Experience

  • Provide clear visual distinction between global and room chat
  • Use appropriate icons and colors for each chat type
  • Implement smooth transitions between chat modes

2. Performance

  • Lazy load global chat components when needed
  • Implement proper cleanup for WebSocket connections
  • Use efficient message handling for high-volume global chat

3. Accessibility

  • Maintain keyboard navigation support
  • Provide screen reader descriptions for chat types
  • Ensure proper focus management

4. Error Handling

  • Graceful fallback for connection failures
  • Clear error messages for users
  • Automatic reconnection for global chat

Future Enhancements

Planned Features

  1. Global Chat Channels: Multiple global chat channels
  2. Moderation Tools: Global chat moderation interface
  3. Advanced Statistics: Real-time analytics dashboard
  4. Integration Features: Cross-room notifications

Scalability Considerations

  1. Message Batching: Efficient handling of high-volume global chat
  2. Rate Limiting: Per-user rate limiting for global messages
  3. Content Filtering: AI-powered content moderation
  4. Performance Optimization: Message compression and caching

Conclusion

The lobby frontend updates provide a seamless integration of global chat functionality while maintaining full backward compatibility with existing room chat implementations. The modular approach allows developers to choose the appropriate component and configuration for their specific use case.

The updated components handle all the complexity of WebSocket connections, message routing, and UI state management, making it easy to add global chat support to any existing application.

PadawanForge v1.4.1