226 lines
7.3 KiB
JavaScript
226 lines
7.3 KiB
JavaScript
/**
|
||
* 网络收发包工具类
|
||
* 自动处理平台框架提供的基础数据字段,简化子游戏的网络通信
|
||
*
|
||
* 功能特性:
|
||
* - 自动注入平台数据:agentid、gameid、playerid、roomcode
|
||
* - 统一的RPC发送接口
|
||
* - 支持调试模式输出
|
||
* - 遵循数据包协议规范
|
||
*
|
||
* @author GameABC Framework
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
var RpcHelper = {
|
||
|
||
/**
|
||
* 发送RPC请求
|
||
* 自动注入平台框架提供的基础验证字段
|
||
*
|
||
* @param {string} app - 应用名称,通常为 "youle"
|
||
* @param {string} route - 路由名称,如 "room"
|
||
* @param {string} rpc - RPC方法名,如 "play_card"
|
||
* @param {Object} gameData - 子游戏业务数据
|
||
* @param {Object} options - 可选配置参数
|
||
* @param {boolean} options.debug - 是否输出调试信息,默认false
|
||
* @param {Object} options.customData - 自定义的额外数据字段
|
||
*/
|
||
sendRpc: function(app, route, rpc, gameData, options) {
|
||
// 参数验证
|
||
if (!app || typeof app !== 'string') {
|
||
console.error('RpcHelper.sendRpc: app参数必须是非空字符串');
|
||
return;
|
||
}
|
||
if (!route || typeof route !== 'string') {
|
||
console.error('RpcHelper.sendRpc: route参数必须是非空字符串');
|
||
return;
|
||
}
|
||
if (!rpc || typeof rpc !== 'string') {
|
||
console.error('RpcHelper.sendRpc: rpc参数必须是非空字符串');
|
||
return;
|
||
}
|
||
|
||
// 默认选项
|
||
options = options || {};
|
||
var debug = options.debug || false;
|
||
var customData = options.customData || {};
|
||
|
||
// 构建完整的数据包
|
||
var data = {};
|
||
|
||
// 1. 自动注入平台框架提供的基础验证字段
|
||
try {
|
||
data.agentid = GameData.AgentId; // 运营商ID(String类型)
|
||
data.gameid = GameData.GameId; // 游戏类型标识(String类型)
|
||
data.playerid = C_Player.playerid; // 玩家唯一标识(Number类型)
|
||
data.roomcode = Utl.getRoomcode(); // 房间号(String类型)
|
||
} catch (e) {
|
||
console.error('RpcHelper.sendRpc: 获取平台基础数据失败', e);
|
||
return;
|
||
}
|
||
|
||
// 2. 验证必需的平台数据
|
||
if (!data.agentid) {
|
||
console.error('RpcHelper.sendRpc: agentid未设置,请确保平台框架已初始化');
|
||
return;
|
||
}
|
||
if (!data.gameid) {
|
||
console.error('RpcHelper.sendRpc: gameid未设置,请确保平台框架已初始化');
|
||
return;
|
||
}
|
||
if (!data.playerid) {
|
||
console.error('RpcHelper.sendRpc: playerid未设置,请确保玩家已登录');
|
||
return;
|
||
}
|
||
if (!data.roomcode) {
|
||
console.error('RpcHelper.sendRpc: roomcode未设置,请确保已进入房间');
|
||
return;
|
||
}
|
||
|
||
// 3. 合并子游戏业务数据
|
||
if (gameData && typeof gameData === 'object') {
|
||
for (var key in gameData) {
|
||
if (gameData.hasOwnProperty(key)) {
|
||
data[key] = gameData[key];
|
||
}
|
||
}
|
||
}
|
||
|
||
// 4. 合并自定义数据
|
||
for (var key in customData) {
|
||
if (customData.hasOwnProperty(key)) {
|
||
data[key] = customData[key];
|
||
}
|
||
}
|
||
|
||
// 5. 调试信息输出
|
||
if (debug || Game_Config.Debugger.isDebugger) {
|
||
console.log('RpcHelper发送RPC:', {
|
||
app: app,
|
||
route: route,
|
||
rpc: rpc,
|
||
platform_data: {
|
||
agentid: data.agentid,
|
||
gameid: data.gameid,
|
||
playerid: data.playerid,
|
||
roomcode: data.roomcode
|
||
},
|
||
game_data: gameData,
|
||
custom_data: customData
|
||
});
|
||
}
|
||
|
||
// 6. 通过框架发送RPC请求
|
||
Utl.sendData(app, route, rpc, data);
|
||
},
|
||
|
||
/**
|
||
* 发送游戏房间RPC
|
||
* 专门用于游戏房间相关的RPC调用,预设app="youle", route="room"
|
||
*
|
||
* @param {string} rpc - RPC方法名
|
||
* @param {Object} gameData - 子游戏业务数据
|
||
* @param {Object} options - 可选配置参数
|
||
*/
|
||
sendGameRpc: function(rpc, gameData, options) {
|
||
this.sendRpc("youle", "room", rpc, gameData, options);
|
||
},
|
||
|
||
/**
|
||
* 发送代理商RPC
|
||
* 专门用于代理商相关的RPC调用,预设app="youle", route="agent"
|
||
*
|
||
* @param {string} rpc - RPC方法名
|
||
* @param {Object} gameData - 子游戏业务数据
|
||
* @param {Object} options - 可选配置参数
|
||
*/
|
||
sendAgentRpc: function(rpc, gameData, options) {
|
||
this.sendRpc("youle", "agent", rpc, gameData, options);
|
||
},
|
||
|
||
/**
|
||
* 发送平台RPC
|
||
* 专门用于平台相关的RPC调用,预设app="youle", route="platform"
|
||
*
|
||
* @param {string} rpc - RPC方法名
|
||
* @param {Object} gameData - 子游戏业务数据
|
||
* @param {Object} options - 可选配置参数
|
||
*/
|
||
sendPlatformRpc: function(rpc, gameData, options) {
|
||
this.sendRpc("youle", "platform", rpc, gameData, options);
|
||
},
|
||
|
||
/**
|
||
* 获取当前平台数据快照
|
||
* 用于调试和信息查看
|
||
*
|
||
* @returns {Object} 包含当前平台数据的对象
|
||
*/
|
||
getPlatformData: function() {
|
||
try {
|
||
return {
|
||
agentid: GameData.AgentId,
|
||
gameid: GameData.GameId,
|
||
playerid: C_Player.playerid,
|
||
roomcode: Utl.getRoomcode(),
|
||
timestamp: new Date().getTime()
|
||
};
|
||
} catch (e) {
|
||
console.error('RpcHelper.getPlatformData: 获取平台数据失败', e);
|
||
return null;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 验证平台数据完整性
|
||
* 检查所有必需的平台数据是否已正确设置
|
||
*
|
||
* @returns {Object} 验证结果对象 {valid: boolean, missing: Array}
|
||
*/
|
||
validatePlatformData: function() {
|
||
var result = {
|
||
valid: true,
|
||
missing: []
|
||
};
|
||
|
||
try {
|
||
if (!GameData.AgentId) {
|
||
result.valid = false;
|
||
result.missing.push('agentid');
|
||
}
|
||
if (!GameData.GameId) {
|
||
result.valid = false;
|
||
result.missing.push('gameid');
|
||
}
|
||
if (!C_Player.playerid) {
|
||
result.valid = false;
|
||
result.missing.push('playerid');
|
||
}
|
||
if (!Utl.getRoomcode()) {
|
||
result.valid = false;
|
||
result.missing.push('roomcode');
|
||
}
|
||
} catch (e) {
|
||
result.valid = false;
|
||
result.missing.push('platform_error');
|
||
console.error('RpcHelper.validatePlatformData: 验证过程出错', e);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
};
|
||
|
||
// ES5兼容性:确保在旧版本浏览器中也能正常工作
|
||
if (typeof Object.defineProperty === 'function') {
|
||
// 防止意外修改工具类
|
||
Object.defineProperty(window, 'RpcHelper', {
|
||
value: RpcHelper,
|
||
writable: false,
|
||
configurable: false
|
||
});
|
||
} else {
|
||
// 旧版本浏览器的兼容处理
|
||
window.RpcHelper = RpcHelper;
|
||
}
|