Ejemplo n.º 1
0
    private static void LEAVE_ROOM(byte[] bytes)
    {
        LeaveRoom input = LeaveRoom.Parser.ParseFrom(bytes);

        bool      ret       = false;
        RoomLogic roomLogic = ServerRoomManager.Instance.GetRoomLogic(input.RoomId);

        if (roomLogic != null)
        {
            var pi = ServerRoomManager.Instance.GetPlayer(_args);
            if (pi != null)
            { // 把当前玩家在房间的状态设置为离线
                roomLogic.Offline(pi.Enter.TokenId);

                string account = pi.Enter.Account;
                ServerRoomManager.Instance.Log($"MSG: LEAVE_ROOM OK - Player leaves the battlefield! Account:{account} - Room:{roomLogic.RoomName}"); // 玩家离开战场!
            }

            // 通知大厅
            ServerRoomManager.Instance.UpdateRoomInfoToLobby(roomLogic);
            ret = true;
        }
        else
        {
            ServerRoomManager.Instance.Log($"MSG: LEAVE_ROOM Error - Battlefield is not found! RoomId:{input.RoomId}"); // 战场没有找到
        }

        LeaveRoomReply output = new LeaveRoomReply()
        {
            Ret = ret,
        };

        ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.LeaveRoomReply, output.ToByteArray());
    }
    public void RemovePlayer(SocketAsyncEventArgs args, bool bCloseRoomIfNoUser)
    {
        bool       ret = false;
        PlayerInfo pi  = GetPlayer(args);

        if (pi == null)
        {
            Log($"RoomManager RemovePlayer Error - Player not found!");
            return;
        }

        // 如果该玩家还没有离开房间, 则要离开这个房间
        RoomLogic roomLogic = Rooms[pi.RoomId];

        if (roomLogic.IsOnline(pi.Enter.TokenId))
        {
            roomLogic.Offline(pi.Enter.TokenId);

            // 通知大厅
            UpdateRoomInfoToLobby(roomLogic);
        }

        // 最后看是否需要关闭房间
        if (roomLogic.CurPlayerCount == 0 && bCloseRoomIfNoUser)
        {                     // 关闭房间
            roomLogic.Fini(); // 结束化
            Rooms.Remove(pi.RoomId);
            // 通知大厅:删除房间
            Protobuf.Lobby.UpdateRoomInfo output2 = new Protobuf.Lobby.UpdateRoomInfo()
            {
                RoomId   = roomLogic.RoomId,
                IsRemove = true,
            };
            MixedManager.Instance.LobbyManager.SendMsg(Protobuf.Lobby.LOBBY.UpdateRoomInfo, output2.ToByteArray());
        }
        Players.Remove(args);
    }