feat: optimize pvp invite
This commit is contained in:
@@ -175,18 +175,25 @@ wx.onShow((res) => {
|
||||
|
||||
console.log(`[game.js] onShow fired: scene=${res && res.scene}, query=${JSON.stringify(res && res.query)}, shareTicket=${res && res.shareTicket}`);
|
||||
|
||||
// Check for teamId from invite card (3v3 mode)
|
||||
// Check for teamId from invite card (3v3 mode) or roomId from invite card (1v1 mode)
|
||||
const teamId = _extractTeamId(res && res.query);
|
||||
const roomId = _extractRoomId(res && res.query);
|
||||
if (teamId) {
|
||||
_handleInviteTeamId(teamId);
|
||||
} else if (roomId) {
|
||||
_handleInviteRoomId(roomId);
|
||||
} else {
|
||||
// Fallback: also check launch options in case onShow query is empty on cold start
|
||||
try {
|
||||
const launchOptions = wx.getLaunchOptionsSync();
|
||||
const fallbackTeamId = _extractTeamId(launchOptions && launchOptions.query);
|
||||
const fallbackRoomId = _extractRoomId(launchOptions && launchOptions.query);
|
||||
if (fallbackTeamId) {
|
||||
console.log(`[game.js] onShow query empty, but found teamId in launchOptions: ${fallbackTeamId}`);
|
||||
_handleInviteTeamId(fallbackTeamId);
|
||||
} else if (fallbackRoomId) {
|
||||
console.log(`[game.js] onShow query empty, but found roomId in launchOptions: ${fallbackRoomId}`);
|
||||
_handleInviteRoomId(fallbackRoomId);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
@@ -235,6 +242,71 @@ function _extractTeamId(query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract roomId from query parameter (1v1 invite card).
|
||||
* Similar to _extractTeamId, but looks for roomId key.
|
||||
* @param {object|string|undefined} query
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function _extractRoomId(query) {
|
||||
if (!query) return null;
|
||||
|
||||
// Case 1: query is already an object with roomId property
|
||||
if (typeof query === 'object' && query.roomId) {
|
||||
return query.roomId;
|
||||
}
|
||||
|
||||
// Case 2: query is a string like 'roomId=R12345' or 'roomId=R12345&foo=bar'
|
||||
if (typeof query === 'string') {
|
||||
const match = query.match(/roomId=([^&]+)/);
|
||||
if (match) return match[1];
|
||||
}
|
||||
|
||||
// Case 3: query is an object but roomId might be nested in a raw string field
|
||||
if (typeof query === 'object') {
|
||||
const keys = Object.keys(query);
|
||||
for (const key of keys) {
|
||||
const combined = key + (query[key] ? '=' + query[key] : '');
|
||||
const match = combined.match(/roomId=([^&]+)/);
|
||||
if (match) return match[1];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle roomId from 1v1 invite card.
|
||||
* Navigates to RoomScene (auto-join) if possible, otherwise stores as pending.
|
||||
* @param {string} roomId
|
||||
*/
|
||||
function _handleInviteRoomId(roomId) {
|
||||
if (!roomId) return;
|
||||
|
||||
// Avoid duplicate processing if already pending the same roomId
|
||||
if (GameGlobal._pendingRoomId === roomId) {
|
||||
console.log(`[game.js] roomId ${roomId} already pending, skipping duplicate`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[game.js] Received roomId from 1v1 invite: ${roomId}, currentScene: ${sceneManager._currentName}`);
|
||||
|
||||
// If already past loading, navigate directly to PVP room scene
|
||||
if (sceneManager._currentScene && sceneManager._currentName !== SCENE.LOADING) {
|
||||
console.log(`[game.js] Navigating directly to RoomScene with roomId: ${roomId}`);
|
||||
if (!sceneManager._scenes.has(SCENE.PVP_ROOM)) {
|
||||
const RoomScene = require('./js/scenes/RoomScene');
|
||||
sceneManager.register(SCENE.PVP_ROOM, RoomScene);
|
||||
}
|
||||
sceneManager.switchTo(SCENE.PVP_ROOM, { roomId });
|
||||
GameGlobal._pendingRoomId = null;
|
||||
} else {
|
||||
// Still loading — store pending roomId for auto-navigation after load
|
||||
console.log(`[game.js] Still loading, storing pendingRoomId: ${roomId}`);
|
||||
GameGlobal._pendingRoomId = roomId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle teamId from invite card (shared between onShow and cold launch).
|
||||
* Navigates to TeamRoomScene if possible, otherwise stores as pending.
|
||||
@@ -267,15 +339,18 @@ function _handleInviteTeamId(teamId) {
|
||||
}
|
||||
}
|
||||
|
||||
// Check for teamId from cold launch (user opened game via invite card)
|
||||
// Check for teamId / roomId from cold launch (user opened game via invite card)
|
||||
try {
|
||||
const launchOptions = wx.getLaunchOptionsSync();
|
||||
console.log(`[game.js] Cold launch options: scene=${launchOptions.scene}, query=${JSON.stringify(launchOptions.query)}, referrerInfo=${JSON.stringify(launchOptions.referrerInfo)}`);
|
||||
const launchTeamId = _extractTeamId(launchOptions && launchOptions.query);
|
||||
const launchRoomId = _extractRoomId(launchOptions && launchOptions.query);
|
||||
if (launchTeamId) {
|
||||
_handleInviteTeamId(launchTeamId);
|
||||
} else if (launchRoomId) {
|
||||
_handleInviteRoomId(launchRoomId);
|
||||
} else {
|
||||
console.log('[game.js] No teamId found in cold launch options');
|
||||
console.log('[game.js] No teamId/roomId found in cold launch options');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[game.js] getLaunchOptionsSync failed:', e);
|
||||
|
||||
Reference in New Issue
Block a user