// Start is called before the first frame update
    void Start()
    {
        UIManager.Instance.BeginLoading();

        base.Start();

        //网络
        Completed += OnComplete;
        Received  += OnReceiveMsg;
        RoomLogic.Init();

        if (ClientManager.Instance != null)
        { // 数据都有了,可以直连房间服务器
            Log($"GameRoomManager.Start()! 开始链接RoomServer - {_address}:{_port}");
            Connect();
        }
    }
Exemplo n.º 2
0
    private static void ENTER_ROOM(byte[] bytes)
    {
        bool      ret       = false;
        string    errMsg    = "";
        EnterRoom input     = EnterRoom.Parser.ParseFrom(bytes);
        RoomLogic roomLogic = ServerRoomManager.Instance.GetRoomLogic(input.RoomId);

        if (roomLogic == null)
        { // 房间没有开启,需要开启并进入
            roomLogic = new RoomLogic();
            if (roomLogic != null)
            {
                string tableName = $"MAP:{input.RoomId}";
                if (ServerRoomManager.Instance.Redis.CSRedis.Exists(tableName))
                {
                    long        createrId = ServerRoomManager.Instance.Redis.CSRedis.HGet <long>(tableName, "Creator");
                    NetRoomInfo roomInfo  = new NetRoomInfo()
                    {
                        RoomId         = ServerRoomManager.Instance.Redis.CSRedis.HGet <long>(tableName, "RoomId"),
                        MaxPlayerCount =
                            ServerRoomManager.Instance.Redis.CSRedis.HGet <int>(tableName, "MaxPlayerCount"),
                        RoomName = ServerRoomManager.Instance.Redis.CSRedis.HGet <string>(tableName, "RoomName"),
                        Creator  = createrId,
                    };
                    // 初始化
                    roomLogic.Init(roomInfo);
                    ServerRoomManager.Instance.AddRoomLogic(roomInfo.RoomId, roomLogic);
                }
                else
                {// 房间地图数据没有找到,等于没有创建房间
                    roomLogic = null;
                }
            }
        }

        PlayerInfo       pi   = null;
        PlayerInfoInRoom piir = null;

        if (roomLogic == null)
        {
            errMsg = $"Battlefield is not found! RoomId:{input.RoomId}"; // 战场没有找到!
        }
        else
        {
            pi = ServerRoomManager.Instance.GetPlayer(_args);
            if (pi == null)
            {
                errMsg = "PlayerInfo is not found!"; // 玩家没有找到!
            }
            else
            {
                pi.RoomId = input.RoomId;
                piir      = roomLogic.GetPlayerInRoom(pi.Enter.TokenId);
                if (piir == null)
                {
                    errMsg = "PlayerInfoInRoom is not found!"; // 玩家没有找到!
                    roomLogic.Online(_args, pi.Enter, pi.RoomId);
                    piir = roomLogic.GetPlayerInRoom(pi.Enter.TokenId);
                }
            }
        }

        if (piir != null)
        {
            // 把当前玩家设置为在线(所有玩家信息在房间创建的时候(RoomLogic.Init)就存在了, 只是不在线)
            roomLogic.Online(_args, pi.Enter, input.RoomId);

            // 通知大厅
            ServerRoomManager.Instance.UpdateRoomInfoToLobby(roomLogic);

            // 返回成功
            EnterRoomReply output = new EnterRoomReply()
            {
                Ret      = true,
                RoomId   = roomLogic.RoomId,
                RoomName = roomLogic.RoomName,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.EnterRoomReply, output.ToByteArray());
            ServerRoomManager.Instance.Log($"MSG: ENTER_ROOM OK - Player enters the battlefield! Account:{pi.Enter.Account} - Room:{roomLogic.RoomName}"); // 玩家进入战场!
        }
        else
        {   // 返回失败
            EnterRoomReply output = new EnterRoomReply()
            {
                Ret    = false,
                ErrMsg = errMsg,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.EnterRoomReply, output.ToByteArray());
            ServerRoomManager.Instance.Log("MSG: ENTER_ROOM Error - " + errMsg);
        }
    }