A real-time WebSocket server for a multiplayer word chain game where players take turns creating words that start with the last letter of the previous word.
- Real-time Multiplayer: WebSocket-based communication for instant game updates
- Turn-based Gameplay: Automated turn management with configurable time limits
- Word Validation: Dictionary-based word verification
- Flexible Scoring: Configurable point systems (fixed or length-based)
- Room Management: Create and join game rooms with custom configurations
- Player Management: Automatic handling of disconnections and eliminations
- Progressive Difficulty: Optional time decrease and difficulty increase per turn
- Runtime: Node.js
- Language: TypeScript
- WebSocket:
wslibrary - Build Tool: TypeScript Compiler (
tsc)
- Node.js (v16 or higher)
- npm or yarn
# Install dependencies
npm install
# Run development server
npm run devThe WebSocket server will start on ws://localhost:8080
backend/
├── src/
│ ├── app.ts # WebSocket server entry point
│ ├── db/ # In-memory data stores
│ │ ├── rooms.ts # Active game rooms
│ │ └── users.ts # Connected users
│ ├── func/
│ │ ├── GameManager.ts # Core game logic and turn management
│ │ ├── routeMessage.ts # WebSocket message router
│ │ ├── routeHandler/ # Message handlers
│ │ │ ├── createRoom.ts # Room creation logic
│ │ │ ├── joinRoom.ts # Room joining logic
│ │ │ ├── startGame.ts # Game initialization
│ │ │ ├── playGame.ts # Word submission handler
│ │ │ └── sendChatMessage.ts # Chat functionality
│ │ ├── userFunc/ # User management
│ │ │ └── removeUser.ts # Handle disconnections
│ │ └── utils/ # Utility functions
│ │ ├── generateUniqueRoomId.ts
│ │ ├── isValidWord.ts # Word validation
│ │ ├── jsonParser.ts # Message parsing
│ │ └── validateUsername.ts
│ └── interfaces/ # TypeScript interfaces
├── dist/ # Compiled JavaScript (generated)
├── package.json
├── tsconfig.json
└── readme.md
- Client connects → WebSocket connection established
- Client sends message →
jsonParserparses JSON - Message routed →
routeMessagedirects to appropriate handler - Handler processes → Business logic executed
- Response sent → Results broadcast to relevant clients
- Room Creation: Host creates room with custom configuration
- Players Join: Other players join using room ID
- Game Start: Host starts game when ready
- Turn Management:
GameManagerhandles turns automatically - Word Submission: Players submit words during their turn
- Validation: Words checked for validity and game rules
- Scoring: Points awarded based on configuration
- Game End: Last remaining player wins
All messages follow this structure:
{
"type": "MESSAGE_TYPE",
"payload": { /* message-specific data */ }
}CREATE_ROOM: Create a new game roomJOIN_ROOM: Join an existing roomSTART_GAME: Begin the game (host only)PLAY_GAME: Submit a word during your turnCHAT_MESSAGE: Send a chat message
For detailed API documentation, see api_docs.md
When creating a room, you can configure:
max_players: Maximum number of players (2-8)init_time_per_turn: Initial time limit per turn (seconds)min_word_len: Minimum word length (3-5)diff: Difficulty level (easy/medium/hard)prg_time_dcr: Progressive time decrease (boolean)prg_diff_incr: Progressive difficulty increase (boolean)reward_by_word_length: Award points based on word length (boolean)
- Use TypeScript for all new code
- Follow existing naming conventions
- Add comments for complex logic
- Keep functions focused and single-purpose
- Create a new route handler in
src/func/routeHandler/ - Register the handler in
src/func/routeMessage.ts - Update interfaces if needed in
src/interfaces/ - Test thoroughly with multiple clients
- Define the message type in
routeMessage.ts - Create handler function in
routeHandler/ - Add payload interface if needed
- Document in
api_docs.md
- Start the dev server:
npm run dev - Connect with a WebSocket client
- Test all message types and edge cases
- Verify error handling and disconnection scenarios
Add a new utility function:
- Create file in
src/func/utils/ - Export the function
- Import where needed
Modify game rules:
- Edit
src/func/GameManager.ts - Update validation logic in
validateWord() - Adjust scoring in
calculatePoints()
Change data structures:
- Update interfaces in
src/db/rooms.tsorsrc/db/users.ts - Update all references throughout the codebase
ISC
For issues or questions, please open an issue on the repository.