fix boss tank cross brick

This commit is contained in:
jakciehan
2026-06-07 22:08:00 +08:00
parent c3a4aa8f15
commit e4140f073f
29 changed files with 2689 additions and 1240 deletions
+33 -10
View File
@@ -46,6 +46,10 @@ class NetworkManager {
// Generate a unique player ID
this._playerId = this._generatePlayerId();
// Connection mutex: queue of pending connect() callers
/** @type {Array<{resolve: Function, timeoutMs: number}>} */
this._connectQueue = [];
}
/**
@@ -56,8 +60,15 @@ class NetworkManager {
*/
connect(serverUrl, timeoutMs = 10000) {
return new Promise((resolve) => {
if (this._connected || this._connecting) {
resolve(this._connected);
// If already connected, resolve immediately
if (this._connected) {
resolve(true);
return;
}
// If another connect() is in progress, queue this one and wait
if (this._connecting) {
this._connectQueue.push({ resolve, timeoutMs });
return;
}
@@ -85,6 +96,13 @@ class NetworkManager {
console.warn('[NetworkManager] connect() failed:', reason || 'unknown');
}
resolve(ok);
// Resolve all queued connect() calls with the same result
const queue = this._connectQueue;
this._connectQueue = [];
for (const pending of queue) {
pending.resolve(ok);
}
};
// Connection timeout guard (e.g. DNS/TLS hang on cellular).
@@ -96,7 +114,6 @@ class NetworkManager {
this._ws = wx.connectSocket({
url: serverUrl,
header: { 'content-type': 'application/json' },
// Surface wx.connectSocket API-level failures (invalid url / domain not whitelisted / etc.)
success: (res) => { console.log('[NetworkManager] wx.connectSocket invoked:', res && res.errMsg); },
fail: (err) => {
console.error('[NetworkManager] wx.connectSocket API failed:', JSON.stringify(err));
@@ -119,16 +136,13 @@ class NetworkManager {
});
this._ws.onError((err) => {
// Log as much context as possible; wx error objects vary across platforms.
console.error('[NetworkManager] WebSocket error:',
(err && (err.errMsg || err.message)) || err,
'url=', serverUrl);
this._emit('error', err);
// If the error arrives before we ever got onOpen, treat it as a connect failure.
if (!this._connected) {
finish(false, `onError before open: ${err && (err.errMsg || err.message)}`);
} else {
// Runtime error on an established connection — let onClose handle reconnection.
this._connecting = false;
}
});
@@ -144,13 +158,11 @@ class NetworkManager {
this._stopHeartbeat();
this._emit('disconnected', { code, reason });
// If onClose arrives before onOpen, this is a connect failure.
if (!wasConnected) {
finish(false, `onClose before open: code=${code} reason=${reason}`);
return;
}
// Auto-reconnect only for drops on an already-established connection.
if (this._shouldReconnect && this._reconnectAttempts < this._maxReconnectAttempts) {
this._attemptReconnect();
}
@@ -183,6 +195,13 @@ class NetworkManager {
this._connecting = false;
this._roomId = null;
this._playerSlot = 0;
// Clear connect queue
const queue = this._connectQueue;
this._connectQueue = [];
for (const pending of queue) {
pending.resolve(false);
}
}
/**
@@ -272,10 +291,12 @@ class NetworkManager {
/**
* Create a new team for 3v3 mode.
* @param {string} [battleMode='3v3'] - Battle mode ('1v1', '2v2', '3v3').
*/
createTeam() {
createTeam(battleMode = '3v3') {
this.send(NET_MSG.CREATE_TEAM, {
playerId: this._playerId,
battleMode,
});
}
@@ -350,10 +371,12 @@ class NetworkManager {
/**
* Start solo matchmaking for 3v3.
* @param {string} [battleMode='3v3'] - Battle mode ('1v1', '2v2', '3v3').
*/
soloMatch() {
soloMatch(battleMode = '3v3') {
this.send(NET_MSG.SOLO_MATCH, {
playerId: this._playerId,
battleMode,
});
}