# OnlineBattleManager.createRoom(Object option)

以 Promise 风格调用:支持

# 功能描述

创建新房间,设置房间配置和玩家配置。创建成功后,创建者自动成为房主。

# 参数

# Object option

属性类型默认值必填说明
dataObject创建房间请求数据
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

# data 对象结构

属性类型必填说明
roomCfgObject房间配置
playerCfgObject玩家配置

# roomCfg 对象结构

属性类型必填说明
maxPlayerCountnumber房间最大人数
typestring房间类型
namestring房间名称
customPropertiesstring自定义房间属性(最大2048字节)
matchParamsObject匹配参数

# matchParams 对象结构

属性类型必填说明
levelstring玩家等级
scorestring玩家积分

# playerCfg 对象结构

属性类型必填说明
customStatusnumber自定义玩家状态
customPropertiesstring自定义玩家属性(最大2048字节)

# option.success 回调函数

# 参数
# Object res
属性类型说明
roomInfoObject完整房间信息(见RoomInfo数据结构)
errMsgstring成功时为 "createRoom:ok"

# option.fail 回调函数

# 参数
# Object err
属性类型说明
errMsgstring错误信息
errnostring错误码

# RoomInfo 数据结构

属性类型说明
createTimestring创建时间(时间戳)
customPropertiesstring自定义房间属性(JSON字符串)
idstring房间ID
maxPlayerCountnumber房间最大人数
namestring房间名称
ownerIdstring房主ID
playersArray房间内玩家列表(PlayerInfo数组)
typestring房间类型

# 示例代码

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);
});