# TapBattleClient.JoinRoom

# 功能描述

加入指定房间,通过房间ID直接加入。适用于好友邀请、房间码加入等场景。

# 方法签名

public static void JoinRoom(JoinRoomOption option)

# 参数说明

参数名 类型 必填 说明
data JoinRoomRequest 加入房间请求数据
success Action<JoinRoomSuccessResponse> 成功回调
fail Action<TapCallbackResult> 失败回调
complete Action<TapCallbackResult> 完成回调

# JoinRoomRequest

字段名 类型 必填 说明
roomId string 房间ID
playerCfg PlayerConfig 玩家配置

# PlayerConfig

字段名 类型 必填 说明
customStatus int 自定义玩家状态(如0=未准备,1=已准备)
customProperties string 自定义玩家属性(JSON字符串,最大2048字节)

# JoinRoomSuccessResponse

字段名 类型 说明
roomInfo RoomInfo 完整房间信息
errMsg string 成功时为 "joinRoom:ok"

# RoomInfo

字段名 类型 说明
id string 房间ID
name string 房间名称
type string 房间类型
ownerId string 房主ID
maxPlayerCount int 房间最大人数
players PlayerInfo[] 房间内玩家列表
customProperties string 自定义房间属性(JSON字符串)
createTime string 创建时间(时间戳)

# 使用说明

# 注意事项

  • 连接后可用 - 需要先连接服务器才能加入房间
  • 房间容量 - 加入前需要确保房间未满
  • 玩家配置 - 可以在加入时设置自己的初始状态和属性

# 代码示例

# 示例1:通过房间ID加入房间

using UnityEngine;

public class RoomJoiner : MonoBehaviour
{
    public void JoinRoomById(string roomId)
    {
        TapBattleClient.JoinRoom(new JoinRoomOption
        {
            data = new JoinRoomRequest
            {
                roomId = roomId
            },
            success = (result) => {
                Debug.Log($"成功加入房间: {result.roomInfo.name}");
                Debug.Log($"房间人数: {result.roomInfo.players.Length}/{result.roomInfo.maxPlayerCount}");
                OnJoinRoomSuccess(result.roomInfo);
            },
            fail = (result) => {
                Debug.LogError($"加入房间失败: {result.errMsg}");
                ShowErrorMessage(result.errMsg);
            }
        });
    }

    private void OnJoinRoomSuccess(RoomInfo roomInfo)
    {
        // 加入成功后的处理逻辑
    }

    private void ShowErrorMessage(string message) { }
}

# 示例2:带玩家配置加入房间

using UnityEngine;

public class RoomJoinerWithConfig : MonoBehaviour
{
    public void JoinRoomWithPlayerInfo(string roomId, string nickname, int level)
    {
        var playerProperties = new {
            nickname = nickname,
            level = level,
            avatarId = "avatar_001"
        };

        TapBattleClient.JoinRoom(new JoinRoomOption
        {
            data = new JoinRoomRequest
            {
                roomId = roomId,
                playerCfg = new PlayerConfig
                {
                    customStatus = 0, // 0=未准备
                    customProperties = JsonUtility.ToJson(playerProperties)
                }
            },
            success = (result) => {
                Debug.Log($"成功加入房间,自定义信息已设置");
                EnterRoomScene(result.roomInfo);
            }
        });
    }

    private void EnterRoomScene(RoomInfo roomInfo) { }
}

# 示例3:从房间列表加入

using UnityEngine;

public class RoomListJoiner : MonoBehaviour
{
    public void JoinFromRoomList(RoomInfo selectedRoom)
    {
        // 检查房间是否已满
        if (selectedRoom.players.Length >= selectedRoom.maxPlayerCount)
        {
            Debug.LogWarning("房间已满,无法加入");
            return;
        }

        TapBattleClient.JoinRoom(new JoinRoomOption
        {
            data = new JoinRoomRequest
            {
                roomId = selectedRoom.id
            },
            success = (result) => {
                Debug.Log($"成功加入房间: {result.roomInfo.name}");
                LoadRoomScene();
            },
            fail = (result) => {
                Debug.LogError($"加入失败: {result.errMsg}");
            }
        });
    }

    private void LoadRoomScene() { }
}

# 示例4:通过房间码加入

using UnityEngine;
using UnityEngine.UI;

public class RoomCodeJoiner : MonoBehaviour
{
    [SerializeField] private InputField roomCodeInput;

    public void JoinByRoomCode()
    {
        string roomCode = roomCodeInput.text.Trim();

        if (string.IsNullOrEmpty(roomCode))
        {
            Debug.LogWarning("请输入房间码");
            return;
        }

        TapBattleClient.JoinRoom(new JoinRoomOption
        {
            data = new JoinRoomRequest
            {
                roomId = roomCode
            },
            success = (result) => {
                Debug.Log($"通过房间码成功加入: {result.roomInfo.name}");
                roomCodeInput.text = "";
                EnterRoom(result.roomInfo);
            },
            fail = (result) => {
                Debug.LogError($"房间码无效或房间已满: {result.errMsg}");
            }
        });
    }

    private void EnterRoom(RoomInfo roomInfo) { }
}

# 相关API