How stefanrudi/darts-counter works
Main Components
• Client (Frontend): Contains React components (GameLobby, GameBoard, Dartboard, etc.), hooks (e.g., useWebSocket, useGame), utilities, and assets. This part is responsible for the user interface and communicating with the server.
• Server (Backend): Built on Node.js/Express. Contains controllers (e.g., wsController), models (Game, Player), services (GameService, WebSocketService), and utilities. This serves as the core gameplay logic handler, real-time communication manager, and state keeper.
• Shared: A module that provides TypeScript types (client, server, game types) ensuring both client and server use a consistent contract.
• WebSocket Layer: Acts as the real-time communication bridge between the client and server.
Relationships and Interactions
• Clients (web browsers running the React app) establish WebSocket connections to the server.
• Frontend components (e.g., GameLobby, GameBoard) translate user interactions into WebSocket messages. Use hooks like useWebSocket and useGame to manage state and communication.
• The Server’s WebSocketService manages connections and routes messages through the wsController.
• GameService on the server handles game-related logic such as score tracking, game session management, and processing throw history.
• Models (Game and Player) represent and persist the game state.
• Both client and server use type definitions from the shared module, ensuring strong typing and consistency.
Architectural Patterns and Design Principles
• Separation of Concerns: The client handles UI using React, while the server handles business logic and real-time communication.
• Event-driven Real-time Communication: WebSocket-based interactions push updates to clients as game events happen.
• Modularization: Different directories (components, hooks, controllers, models, services) indicate a modular approach to both the frontend and backend.
• Consistent Typing: Shared TypeScript types guarantee a unified interface between the frontend and backend.