# OnlineBattleManager.createRoom(Object option)
以 Promise 风格调用:支持
# 功能描述
创建新房间,设置房间配置和玩家配置。创建成功后,创建者自动成为房主。
# 参数
# Object option
| 属性 | 类型 | 默认值 | 必填 | 说明 |
| data | Object | 是 | 创建房间请求数据 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
# data 对象结构
| 属性 | 类型 | 必填 | 说明 |
| roomCfg | Object | 是 | 房间配置 |
| playerCfg | Object | 否 | 玩家配置 |
# roomCfg 对象结构
| 属性 | 类型 | 必填 | 说明 |
| maxPlayerCount | number | 是 | 房间最大人数 |
| type | string | 是 | 房间类型 |
| name | string | 是 | 房间名称 |
| customProperties | string | 否 | 自定义房间属性(最大2048字节) |
| matchParams | Object | 是 | 匹配参数 |
# matchParams 对象结构
| 属性 | 类型 | 必填 | 说明 |
| level | string | 是 | 玩家等级 |
| score | string | 是 | 玩家积分 |
# playerCfg 对象结构
| 属性 | 类型 | 必填 | 说明 |
| customStatus | number | 否 | 自定义玩家状态 |
| customProperties | string | 否 | 自定义玩家属性(最大2048字节) |
# option.success 回调函数
# 参数
# Object res
| 属性 | 类型 | 说明 |
| roomInfo | Object | 完整房间信息(见RoomInfo数据结构) |
| errMsg | string | 成功时为 "createRoom:ok" |
# option.fail 回调函数
# 参数
# Object err
| 属性 | 类型 | 说明 |
| errMsg | string | 错误信息 |
| errno | string | 错误码 |
# RoomInfo 数据结构
| 属性 | 类型 | 说明 |
| createTime | string | 创建时间(时间戳) |
| customProperties | string | 自定义房间属性(JSON字符串) |
| id | string | 房间ID |
| maxPlayerCount | number | 房间最大人数 |
| name | string | 房间名称 |
| ownerId | string | 房主ID |
| players | Array | 房间内玩家列表(PlayerInfo数组) |
| type | string | 房间类型 |
# 示例代码
let tapOnlineBattle = tap.getOnlineBattleManager();
// 回调风格
tapOnlineBattle.createRoom({
data: {
roomCfg: {
maxPlayerCount: 2,
type: "新手区",
matchParams: {
level: "5",
score: "100"
},
name: "1v1",
customProperties: "customRoomProperties"
},
playerCfg: {
customStatus: 0,
customProperties: "customPlayerProperties"
}
},
success: (res) => {
console.log('创建房间成功:', res.roomInfo);
console.log('房间ID:', res.roomInfo.id);
console.log('房主ID:', res.roomInfo.ownerId);
},
fail: ({errMsg, errno}) => {
console.error('创建房间失败:', errMsg);
}
});
// Promise风格
tapOnlineBattle.createRoom({
data: {
roomCfg: {
maxPlayerCount: 2,
type: "新手区",
matchParams: {
level: "5",
score: "100"
},
name: "1v1",
customProperties: "customRoomProperties"
},
playerCfg: {
customStatus: 0,
customProperties: "customPlayerProperties"
}
}
}).then(res => {
console.log('创建房间成功:', res.roomInfo);
}).catch(error => {
console.error('创建房间失败:', error);
});
