Repository navigation
Expand file tree
/
Copy pathmultiplayer.js
More file actions
271 lines (271 loc) · 7.67 KB
/
Copy pathmultiplayer.js
File metadata and controls
271 lines (271 loc) · 7.67 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
export class MultiplayerManager {
constructor() {
this.ws = null;
this.isConnected = false;
this.isHost = false;
this.reconnectAttempts = 0;
this.maxReconnectAttempts = 3;
this.reconnectDelay = 1000;
this.playerId = this.generatePlayerId();
}
generatePlayerId() {
return 'player_' + Math.random().toString(36).substr(2, 9);
}
// Check if multiplayer server is available (optional)
async checkServerAvailable(serverUrl = this.getDefaultServerUrl()) {
try {
// For relative /ws, use /mp/health
if (serverUrl === '/ws') {
const response = await fetch('/mp/health', {
method: 'GET',
signal: AbortSignal.timeout(2000),
});
return response.ok;
}
// Otherwise, use the old logic
const httpUrl = serverUrl
.replace('ws://', 'http://')
.replace('wss://', 'https://');
let healthUrl;
if (httpUrl.includes('/ws')) {
healthUrl = httpUrl.replace('/ws', '/mp/health');
} else {
healthUrl = `${httpUrl}/health`;
}
const response = await fetch(healthUrl, {
method: 'GET',
signal: AbortSignal.timeout(2000),
});
return response.ok;
} catch (_error) {
console.log(
'Multiplayer server not available, running in single-player mode'
);
return false;
}
}
getDefaultServerUrl() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const host = window.location.host;
const hostname = window.location.hostname;
// Render.com or any production: always use relative /ws
if (
hostname === 'onrender.com' ||
hostname.endsWith('.onrender.com') ||
!host.includes('localhost')
) {
return '/ws';
}
// If running in development (localhost:5173), try local server
if (host.includes('localhost:5173') || host.includes('127.0.0.1:5173')) {
return 'ws://localhost:3001';
}
// If running in Docker container locally, use direct connection to port 3001
if (
host.includes(':8080') ||
(hostname === 'localhost' && host.includes(':80'))
) {
const baseHostname = host.split(':')[0];
return `${protocol}//${baseHostname}:3001`;
}
// Default fallback for HTTP
return `${protocol}//${host}:3001`;
}
// Initialize multiplayer connection (optional)
async initialize(serverUrl = this.getDefaultServerUrl()) {
try {
// First check if server is available
const serverAvailable = await this.checkServerAvailable(serverUrl);
if (!serverAvailable) {
console.log(
'Multiplayer server not available, continuing in single-player mode'
);
return false;
}
this.ws = new WebSocket(serverUrl);
return new Promise((resolve) => {
if (!this.ws) {
resolve(false);
return;
}
const timeout = setTimeout(() => {
console.log('Connection timeout, falling back to single-player mode');
this.disconnect();
resolve(false);
}, 5000);
this.ws.onopen = () => {
clearTimeout(timeout);
this.isConnected = true;
this.reconnectAttempts = 0;
console.log('Connected to multiplayer server');
// Get playerName from localStorage or default
let playerName = '';
try {
playerName = localStorage.getItem('playerName') || '';
} catch {
// Ignore localStorage errors
}
this.send({
type: 'join',
playerId: this.playerId,
name: playerName,
timestamp: Date.now(),
});
resolve(true);
};
this.ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
this.handleMessage(data);
} catch (error) {
console.error('Error parsing message:', error);
}
};
this.ws.onclose = () => {
this.isConnected = false;
console.log('Disconnected from multiplayer server');
this.attemptReconnect(serverUrl);
};
this.ws.onerror = (_error) => {
clearTimeout(timeout);
console.log('WebSocket error, falling back to single-player mode');
this.isConnected = false;
resolve(false);
};
});
} catch (_error) {
console.log(
'Failed to initialize multiplayer, continuing in single-player mode'
);
return false;
}
}
attemptReconnect(serverUrl) {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
console.log(
`Attempting to reconnect (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`
);
setTimeout(() => {
this.initialize(serverUrl);
}, this.reconnectDelay * this.reconnectAttempts);
} else {
console.log(
'Max reconnection attempts reached, continuing in single-player mode'
);
}
}
handleMessage(data) {
switch (data.type) {
case 'gameState':
if (this.onStateUpdate) {
this.onStateUpdate(data.gameState);
}
break;
case 'playerJoined':
if (data.playerId === this.playerId) {
this.isHost = data.isHost || false;
}
if (this.onPlayerJoin) {
this.onPlayerJoin(data.playerId);
}
break;
case 'playerLeft':
if (this.onPlayerLeave) {
this.onPlayerLeave(data.playerId);
}
break;
case 'playerUpdate':
if (this._onPlayerUpdate) {
this._onPlayerUpdate(
data.playerId,
data.position,
data.score,
data.name
);
}
break;
case 'itemCollected':
if (this._onPlayerUpdate) {
this._onPlayerUpdate(data.playerId, {}, data.score, data.name);
}
break;
case 'pong':
// Handle ping/pong for connection health
break;
}
}
send(data) {
if (this.ws && this.isConnected) {
try {
this.ws.send(JSON.stringify(data));
} catch (error) {
console.error('Error sending message:', error);
this.isConnected = false;
}
}
}
// Send player position update
updatePlayerPosition(x, y, width, height, growLevel) {
if (!this.isConnected) return;
this.send({
type: 'playerUpdate',
playerId: this.playerId,
position: { x, y, width, height, growLevel },
timestamp: Date.now(),
});
}
// Send collectible pickup
collectItem(collectibleId) {
if (!this.isConnected) return;
this.send({
type: 'collectItem',
playerId: this.playerId,
collectibleId,
timestamp: Date.now(),
});
}
// Event handlers
onGameStateUpdate(callback) {
this.onStateUpdate = callback;
}
onPlayerJoined(callback) {
this.onPlayerJoin = callback;
}
onPlayerLeft(callback) {
this.onPlayerLeave = callback;
}
/**
* Register a callback for player position updates.
* @param callback (playerId: string, position: any) => void
*/
onPlayerUpdate(callback) {
this._onPlayerUpdate = callback;
}
// Getters
get connected() {
return this.isConnected;
}
get currentPlayerId() {
return this.playerId;
}
get isHostPlayer() {
return this.isHost;
}
// Disconnect
disconnect() {
if (this.ws) {
this.ws.close();
this.ws = null;
}
this.isConnected = false;
}
// Send ping to keep connection alive
ping() {
if (this.isConnected) {
this.send({ type: 'ping', timestamp: Date.now() });
}
}
}
// Export a singleton instance
export const multiplayerManager = new MultiplayerManager();