Comprehensive coding rules for building scalable MMO RPG servers using TypeScript, WebAssembly, and worker threads with SOLID principles
Enforce comprehensive coding standards for MMO RPG server projects using TypeScript, WebAssembly (WASM), Node.js worker threads, and SOLID principles. Ensure clarity, maintainability, scalability, and performance while optimizing for lean code and LLM token efficiency.
1. **WebAssembly for CPU-Intensive Tasks**
- Offload heavy computations (procedural generation, physics) to C++ or Rust compiled to WASM
- Use Emscripten to compile and integrate WASM modules with Node.js via `WebAssembly.instantiate`
- Batch WASM calls to minimize overhead (e.g., generate multiple map chunks at once)
2. **Worker Threads with SharedArrayBuffer**
- Use Node.js worker threads for concurrent game logic (player updates, region events)
- Share memory using `SharedArrayBuffer` for efficient data access across threads
- Use `Atomics` to synchronize access and prevent race conditions
3. **Main Thread Management**
- Keep main Node.js thread free for I/O operations (WebSocket communication)
- Offload CPU-intensive tasks to WASM or worker threads
- Never block the main thread with heavy computations
4. **Data Structures**
- Store game state and map data in typed arrays (`Int32Array`, `Float32Array`) with `SharedArrayBuffer`
- Use `Set` for unique items, `Map` for key-value pairs
- Implement caching strategies for frequently accessed data
5. **Performance Monitoring**
- Regularly profile server using Node.js `--prof` or external tools
- Monitor worker thread performance and memory usage
- Use well-tested LTS versions of Node.js, WASM, and libraries
6. **Mandatory Yarn Usage**
- Use Yarn exclusively for all package management (`yarn add`, `yarn install`)
- Never use npm or other package managers
- Lock dependency versions in `yarn.lock` for consistent installations
7. **TypeScript Exclusively**
- Write all code in TypeScript with `.ts` file extensions
- Configure `tsconfig.json` with strict type checking enabled
- Avoid `any` type; use strict null checks
- Leverage type inference to reduce explicit annotations
8. **Interfaces for Abstraction**
- Define TypeScript interfaces for contracts (e.g., `interface Player { id: string; position: number }`)
- Prefer interfaces over type aliases for better extensibility
- Use generics for reusable type-safe components
- Implement type guards for runtime type safety
9. **Minimize Code Size**
- Avoid code duplication; refactor repeated logic into reusable functions
- Use concise syntax (arrow functions, optional chaining `?.`, destructuring)
- Remove unused code, variables, or imports immediately
- Use short, meaningful variable names where context is clear
10. **Algorithm Efficiency**
- Use smallest, most efficient algorithms and data structures
- Implement appropriate caching strategies
- Optimize for both time and space complexity
11. **Single Responsibility Principle (SRP)**
- Each class/module has one responsibility (e.g., `LobbyManager` only manages lobbies)
- Keep methods focused and small
- Split complex modules into separate handlers
12. **Open-Closed Principle (OCP)**
- Design modules to be extensible without modification
- Use interfaces and polymorphism instead of altering base classes
- Add new functionality via subclasses rather than changing existing code
13. **Liskov Substitution Principle (LSP)**
- Ensure subclasses can replace base classes without breaking functionality
- Maintain consistent behavior across inheritance hierarchies
- Never override methods in ways that violate base class contracts
14. **Interface Segregation Principle (ISP)**
- Define small, specific interfaces (e.g., separate `Movable` and `Damageable`)
- Clients should not depend on unused interfaces
- Split large interfaces into smaller, focused ones
15. **Dependency Inversion Principle (DIP)**
- Depend on abstractions (interfaces) rather than concrete implementations
- Use constructor injection for dependencies
- Consider dependency injection containers for complex applications
16. **Minimize Token Count**
- Use short, meaningful variable names (e.g., `pos` vs `playerPosition`) where clear
- Limit comments to essential explanations
- Leverage TypeScript type inference to reduce explicit annotations
- Remove redundant type annotations
17. **Balance Readability**
- Ensure optimizations don't obscure code intent
- Avoid cryptic abbreviations
- Use consistent naming conventions
- Keep function/class names descriptive but concise
18. **Folder Organization**
```
project-root/
├── src/
│ ├── core/
│ │ ├── lobby/
│ │ │ ├── lobbyManager.ts
│ │ │ └── lobbyWorker.ts
│ │ └── game/
│ │ ├── player.ts
│ │ └── world.ts
│ ├── utils/
│ │ ├── logger.ts
│ │ └── helpers.ts
│ ├── config/
│ │ ├── env.ts
│ │ └── constants.ts
│ ├── wasm/
│ │ ├── procedural_generator.cpp
│ │ ├── lobby_generator.wasm
│ │ └── proceduralGenerator.ts
│ ├── server/
│ │ └── server.ts
│ └── index.ts
├── tests/
├── scripts/
├── docs/
├── package.json
└── tsconfig.json
```
19. **Naming Conventions**
- Files/Folders: `camelCase` (e.g., `lobbyManager.ts`)
- Classes: `PascalCase` (e.g., `LobbyManager`)
- Functions/Variables: `camelCase` (e.g., `createLobby`)
- Constants: `UPPER_CASE` (e.g., `MAX_PLAYERS`)
20. **Documentation**
- Use JSDoc for all public functions and classes
- Add file-level comments describing purpose
- Include clear comments for WASM integration and worker thread logic
- Document complex algorithms and business logic
21. **Error Handling**
- Wrap async operations in `try-catch` blocks
- Log errors centrally using `utils/logger.ts`
- Provide meaningful error messages
- Handle edge cases explicitly
22. **Testing**
- Write unit tests for all new functionality using Jest
- Maintain minimum 80% code coverage
- Use TypeScript for test files
- Mock external dependencies
23. **Linting and Formatting**
- Use ESLint with TypeScript support
- Format code with Prettier
- Run type checking before commits
- Review code for SOLID principles compliance
24. **Git Practices**
- Write clear commit messages (e.g., "feat: add lobby creation logic")
- Follow GitFlow branching strategy (`main`, `develop`, feature branches)
- Keep commits focused and atomic
- Review changes before merging
```typescript
// Good: Lean, type-safe, SOLID-compliant
interface ILobbyManager {
create(config: LobbyConfig): Lobby;
}
class LobbyManager implements ILobbyManager {
private lobbies = new Map<string, Lobby>();
create(cfg: LobbyConfig): Lobby {
const lobby = new Lobby(cfg);
this.lobbies.set(lobby.id, lobby);
return lobby;
}
}
// Bad: Violates SRP, no interface, verbose
class LobbyAndGameManager {
createLobbyAndStartGame(configuration: any) {
// Handles both lobby creation AND game logic
}
}
```
This skill enforces enterprise-grade patterns for high-performance multiplayer game servers. Optimized for scalability from single-instance worker threads to distributed architectures.
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/mmo-rpg-server-development-with-wasm/raw