Repository navigation
Expand file tree
/
Copy pathadmin-lobby-server.js
More file actions
178 lines (151 loc) · 5.41 KB
/
Copy pathadmin-lobby-server.js
File metadata and controls
178 lines (151 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* GGE Warlords - AI-Powered Admin Lobby Server
*
* This Node.js server handles:
* - Socket.io connections from admin clients
* - AI command processing (OpenAI/Anthropic/Gemini)
* - Scene update broadcasting to all connected clients
* - Asset registry management
*/
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const cors = require('cors');
const app = express();
const server = http.createServer(app);
// Configure CORS for Socket.io
const io = new Server(server, {
cors: {
origin: process.env.CLIENT_URL || "http://localhost:5173",
methods: ["GET", "POST"]
}
});
// Middleware
app.use(cors());
app.use(express.json());
// In-memory asset registry (replace with database in production)
const assetRegistry = {
meshes: [
{ id: 'mesh_001', name: 'Generic_Box_01', type: 'box', scene: 'env_1' },
{ id: 'mesh_002', name: 'Player_Capsule', type: 'capsule', scene: 'env_1' },
{ id: 'mesh_003', name: 'Terrain_Chunk', type: 'ground', scene: 'env_1' }
],
scenes: [
{ id: 'env_1', name: 'Main Archipelago', status: 'Loaded' },
{ id: 'env_2', name: 'Underground Lab', status: 'Standby' },
{ id: 'env_3', name: 'Neon City', status: 'Standby' }
],
materials: [],
lights: []
};
// Connected clients tracking
const connectedClients = new Map();
// Socket.io connection handling
io.on('connection', (socket) => {
console.log(`[Admin Connected] Socket ID: ${socket.id}`);
connectedClients.set(socket.id, { connectedAt: new Date() });
// Send initial asset registry to new client
socket.emit('registry-update', assetRegistry);
// Broadcast connection count
io.emit('client-count', connectedClients.size);
// Handle AI command from admin
socket.on('ai-command', async (data) => {
console.log(`[AI Command] From ${socket.id}: "${data.prompt}"`);
try {
// Log to client
socket.emit('log', {
type: 'info',
message: `AI Processing: "${data.prompt}"`
});
// Process AI command (this is where you'd call OpenAI/Anthropic/Gemini)
const babylonUpdate = await processAICommand(data.prompt);
// Log success
socket.emit('log', {
type: 'success',
message: `Success: ${babylonUpdate.description}`
});
// Broadcast scene update to all clients
io.emit('scene-update', babylonUpdate);
} catch (error) {
console.error('[AI Command Error]', error);
socket.emit('log', {
type: 'error',
message: `Error: ${error.message}`
});
}
});
// Handle manual scene updates
socket.on('manual-update', (data) => {
console.log(`[Manual Update] From ${socket.id}:`, data);
io.emit('scene-update', data);
});
// Handle asset registry queries
socket.on('get-registry', () => {
socket.emit('registry-update', assetRegistry);
});
// Handle disconnection
socket.on('disconnect', () => {
console.log(`[Admin Disconnected] Socket ID: ${socket.id}`);
connectedClients.delete(socket.id);
io.emit('client-count', connectedClients.size);
});
});
/**
* Process AI command and convert to Babylon.js update
* This is a simplified version - integrate with your AI service
*/
async function processAICommand(prompt) {
// Simulate AI processing delay
await new Promise(resolve => setTimeout(resolve, 1000));
// Parse simple commands (replace with actual AI integration)
const lowerPrompt = prompt.toLowerCase();
if (lowerPrompt.includes('scale') || lowerPrompt.includes('bigger') || lowerPrompt.includes('larger')) {
return {
action: 'UPDATE_MESH',
target: 'Generic_Box_01',
properties: {
scaling: [2, 2, 2]
},
description: 'Scaled Generic_Box_01 to 2x size'
};
}
if (lowerPrompt.includes('color') || lowerPrompt.includes('yellow') || lowerPrompt.includes('red')) {
const color = lowerPrompt.includes('red') ? '#EF4444' : '#EAB308';
return {
action: 'UPDATE_MATERIAL',
target: 'Generic_Box_01',
properties: {
materialColor: color
},
description: `Changed Generic_Box_01 color to ${color}`
};
}
// Default response
return {
action: 'LOG',
message: 'Command received but not recognized',
description: 'No action taken'
};
}
// REST API endpoints
app.get('/api/health', (req, res) => {
res.json({
status: 'healthy',
clients: connectedClients.size,
uptime: process.uptime()
});
});
app.get('/api/registry', (req, res) => {
res.json(assetRegistry);
});
// Start server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`
╔════════════════════════════════════════════╗
║ GGE Warlords - Admin Lobby Server ║
║ Port: ${PORT} ║
║ Status: Running ║
╚════════════════════════════════════════════╝
`);
});