feat: implement competitive clicker MVP with Colyseus.js
- Add real-time multiplayer game server with Colyseus - Implement unique player naming system with auto-increment - Create lobby system with automatic matchmaking - Build 10-minute competitive clicking game rooms (max 2 players) - Add admin dashboard for game management (pause/resume/restart/kick) - Implement Vue 3 client with professional UI - Add WebSocket communication with state synchronization - Include TypeScript throughout with proper typing - Create REST API for admin operations - Add reconnection support and error handling
This commit is contained in:
46
server/src/utils/nameManager.ts
Normal file
46
server/src/utils/nameManager.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export class NameManager {
|
||||
private static instance: NameManager;
|
||||
private nameCounters: Map<string, number> = new Map();
|
||||
private sessionToName: Map<string, string> = new Map();
|
||||
|
||||
private constructor() {}
|
||||
|
||||
static getInstance(): NameManager {
|
||||
if (!NameManager.instance) {
|
||||
NameManager.instance = new NameManager();
|
||||
}
|
||||
return NameManager.instance;
|
||||
}
|
||||
|
||||
generateUniquePlayerName(baseName: string, sessionId: string): string {
|
||||
const normalizedName = baseName.trim().toLowerCase();
|
||||
|
||||
if (!normalizedName) {
|
||||
return this.generateUniquePlayerName('player', sessionId);
|
||||
}
|
||||
|
||||
const currentCounter = this.nameCounters.get(normalizedName) || 0;
|
||||
const newCounter = currentCounter + 1;
|
||||
this.nameCounters.set(normalizedName, newCounter);
|
||||
|
||||
const uniqueName = newCounter === 1 ? normalizedName : `${normalizedName}-${newCounter}`;
|
||||
this.sessionToName.set(sessionId, uniqueName);
|
||||
|
||||
return uniqueName;
|
||||
}
|
||||
|
||||
releasePlayerName(sessionId: string): void {
|
||||
const name = this.sessionToName.get(sessionId);
|
||||
if (name) {
|
||||
this.sessionToName.delete(sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
getPlayerName(sessionId: string): string | undefined {
|
||||
return this.sessionToName.get(sessionId);
|
||||
}
|
||||
|
||||
getAllActivePlayers(): string[] {
|
||||
return Array.from(this.sessionToName.values());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user