# OnlineBattleManager.matchRoom(Object option)
以 Promise 风格调用:支持
# 功能描述
自动匹配房间,根据匹配参数查找或创建房间。如果找到符合条件的房间则加入,否则自动创建新房间。
# 参数
# Object option
| 属性 | 类型 | 默认值 | 必填 | 说明 |
| data | Object | 是 | 匹配房间请求数据 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
# data 对象结构
| 属性 | 类型 | 必填 | 说明 |
| roomCfg | Object | 是 | 房间配置(不需要name字段) |
| playerCfg | Object | 否 | 玩家配置 |
# roomCfg 对象结构
| 属性 | 类型 | 必填 | 说明 |
| maxPlayerCount | number | 是 | 房间最大人数 |
| type | string | 是 | 房间类型 |
| customProperties | string | 否 | 自定义房间属性(最大2048字节) |
| matchParams | Object | 是 | 匹配参数 |
# option.success 回调函数
# 参数
# Object res
| 属性 | 类型 | 说明 |
| roomInfo | Object | 完整房间信息(见RoomInfo数据结构) |
| errMsg | string | 成功时为 "matchRoom:ok" |
# 注意事项
- matchRoom的roomCfg不需要设置name字段,房间名称会自动生成
- 匹配规则基于type和matchParams参数
- 如果没有找到合适的房间会自动创建新房间
# 示例代码
let tapOnlineBattle = tap.getOnlineBattleManager();
// 回调风格
tapOnlineBattle.matchRoom({
data: {
roomCfg: {
maxPlayerCount: 2,
type: "新手区",
matchParams: {
level: "5",
score: "100"
},
customProperties: "customRoomProperties"
},
playerCfg: {
customStatus: 0,
customProperties: "customPlayerProperties"
}
},
success: (res) => {
console.log('匹配房间成功:', res.roomInfo);
},
fail: ({errMsg, errno}) => {
console.error('匹配房间失败:', errMsg);
}
});
// Promise风格
tapOnlineBattle.matchRoom({
data: {
roomCfg: {
maxPlayerCount: 2,
type: "新手区",
matchParams: {
level: "5",
score: "100"
},
customProperties: "customRoomProperties"
},
playerCfg: {
customStatus: 0,
customProperties: "customPlayerProperties"
}
}
}).then(res => {
console.log('匹配房间成功:', res.roomInfo);
}).catch(error => {
console.error('匹配房间失败:', error);
});
