How to use Lite.Common.PropertyBag ?

Options
lpp168
lpp168
edited August 2012 in Photon Server
/// <summary>
        /// 处理获取房间列表操作请求方法
        /// </summary>
        /// <param name="operationRequest"></param>
        /// <param name="sendParameters"></param>
        private void HandleGetRoomInfoOperation(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var response = new GetRoomInfoResponse();
            response.MessageCode = (byte)GetRoomInfoOperationEnum.Success;
            response.Message = "Success";
            response.RoomProperties = new Hashtable();  //Room properties.
            foreach (RoomInfo roomInfo in GetRoomInfosDemo())
            {
                // How to use Lite.Common.PropertyBag ?
                Hashtable roomInfoProperties = GetProperties(roomInfo); //Get all properties.
                response.RoomProperties.Add(roomInfo.RoomId, roomInfoProperties);
            }
            this.SendOperationResponse(new OperationResponse(operationRequest.OperationCode, response), sendParameters);
        }
        /// <summary>
        /// Get all properties.
        /// </summary>
        /// <returns>
        /// A copy of all properties with keys
        /// </returns>
        private Hashtable GetProperties(object atype)
        {
            if (atype == null) return new Hashtable();
            System.Type t = atype.GetType();
            System.Reflection.PropertyInfo[] props = t.GetProperties();
            Hashtable hashtable = new Hashtable();
            foreach (System.Reflection.PropertyInfo prp in props)
            {
                object value = prp.GetValue(atype, new object[] { });
                hashtable.Add(prp.Name, value);
            }
            return hashtable;
        }
        private List<RoomInfo> GetRoomInfosDemo()
        {
            List<RoomInfo> RoomInfos = new List<RoomInfo>();
            for (int i = 1; i <= 10; i++)
            {
                RoomInfo room = new RoomInfo()
                {
                    RoomId = i,
                    RoomName = "GameRoom" + i.ToString(),
                    JoinLevel = i * 2,
                    Description = "游戏的房间说明" + i.ToString()
                };
                RoomInfos.Add(room);
            }
            return RoomInfos;
        }

Server you want to send a List <T> to the client,
Must be referred to as the Hashtable <key,Hashtable> format?
I want to use the Lite.Common.PropertyBag T convert a Hashtabel, to how to code?

Comments

  • [Deleted User]
    Options
    Hi Ipp168,

    sorry, but I'm not sure if understand completely what it is , you want to achieve. A PropertyBag is merely a wrapper for the Dictionary type.

    In Lite the Request for GameProperties is handled by the Room itself (LiteGame.cs):
    protected virtual void HandleGetPropertiesOperation(LitePeer peer, GetPropertiesRequest getPropertiesRequest, SendParameters sendParameters)
    {
       var response = new GetPropertiesResponse();
    
       // check if game properties should be returned
       if ((getPropertiesRequest.PropertyType & (byte)PropertyType.Game) == (byte)PropertyType.
       {
          response.GameProperties = this.Properties.GetProperties(getPropertiesRequest.GamePropertyKeys);
       }
    ...
    

    Could provide more information about, what you are planning to do?


    Tim
  • lpp168
    Options
    For example, a List <T>
                List&lt;RoomInfo&gt; RoomInfos = new List&lt;RoomInfo&gt;();
                for (int i = 1; i &lt;= 10; i++)
                {
                    RoomInfo room = new RoomInfo()
                    {
                        RoomId = i,
                        RoomName = "GameRoom" + i.ToString(),
                        JoinLevel = i * 2,
                        Description = "游戏的房间说明" + i.ToString()
                    };
                    RoomInfos.Add(room);
                }
    

    I want the server to send a RoomInfos to client
  • Tobias
    Options
    The RoomList is something all PUN clients get from the server. They don't have to send it back or to other clients.
    Let's assume you want to send a List of values of some type that's not supported by Photon.

    You will have to register serialize and deserialize methods for that type. Use Protocol.TryRegisterType. Client Example code is in Photon Unity Networking: CustomTypes.cs

    You can't send List<T> at the moment. But the client can send T[].
    Convert your List to array to send it.