コード例 #1
0
ファイル: Room.cs プロジェクト: leancloud/Play-SDK-CSharp
        void HandleRoomCustomPropertiesChanged(UpdatePropertyNotification updatePropertyNotification)
        {
            var changedProps = CodecUtils.DeserializePlayObject(updatePropertyNotification.Attr);

            // 房间属性变化
            MergeCustomProperties(changedProps);
            Client.OnRoomCustomPropertiesChanged?.Invoke(changedProps);
        }
コード例 #2
0
ファイル: Room.cs プロジェクト: leancloud/Play-SDK-CSharp
        void HandleSendEvent(DirectCommand directCommand)
        {
            var eventId   = (byte)directCommand.EventId;
            var eventData = CodecUtils.DeserializePlayObject(directCommand.Msg);
            var senderId  = directCommand.FromActorId;

            Client.OnCustomEvent?.Invoke(eventId, eventData, senderId);
        }
コード例 #3
0
            public static object Deserialize(byte[] bytes)
            {
                var    playObject = CodecUtils.DeserializePlayObject(bytes);
                Weapon weapon     = new Weapon {
                    Name   = playObject.GetString("name"),
                    Attack = playObject.GetInt("attack")
                };

                return(weapon);
            }
コード例 #4
0
ファイル: Player.cs プロジェクト: leancloud/Play-SDK-CSharp
 internal void Init(Room room, RoomMember playerData)
 {
     Room     = room;
     UserId   = playerData.Pid;
     ActorId  = playerData.ActorId;
     IsActive = !playerData.Inactive;
     if (playerData.Attr != null)
     {
         CustomProperties = CodecUtils.DeserializePlayObject(playerData.Attr);
     }
 }
コード例 #5
0
            public static object Deserialize(byte[] bytes)
            {
                var  playObject = CodecUtils.DeserializePlayObject(bytes);
                Hero hero       = new Hero {
                    Name    = playObject.GetString("name"),
                    Score   = playObject.GetFloat("score"),
                    Hp      = playObject.GetInt("hp"),
                    Mp      = playObject.GetInt("mp"),
                    Weapons = playObject.GetPlayArray("weapons").ToList <Weapon>()
                };

                return(hero);
            }
コード例 #6
0
ファイル: Room.cs プロジェクト: leancloud/Play-SDK-CSharp
        void HandlePlayerCustomPropertiesChanged(UpdatePropertyNotification updatePropertyNotification)
        {
            var changedProps = CodecUtils.DeserializePlayObject(updatePropertyNotification.Attr);
            // 玩家属性变化
            var player = GetPlayer(updatePropertyNotification.ActorId);

            if (player == null)
            {
                Logger.Error("No player id: {0} when player properties changed", updatePropertyNotification);
                return;
            }
            player.MergeCustomProperties(changedProps);
            Client.OnPlayerCustomPropertiesChanged?.Invoke(player, changedProps);
        }
コード例 #7
0
        internal async Task <PlayObject> SetRoomCustomProperties(PlayObject properties, PlayObject expectedValues)
        {
            var request = NewRequest();

            request.UpdateProperty = new UpdatePropertyRequest {
                Attr = ByteString.CopyFrom(CodecUtils.SerializePlayObject(properties))
            };
            if (expectedValues != null)
            {
                request.UpdateProperty.ExpectAttr = ByteString.CopyFrom(CodecUtils.SerializePlayObject(expectedValues));
            }
            var res = await SendRequest(CommandType.Conv, OpType.Update, request);

            var props = CodecUtils.DeserializePlayObject(res.Response.UpdateProperty.Attr);

            return(props);
        }
コード例 #8
0
        internal async Task <Tuple <int, PlayObject> > SetPlayerCustomProperties(int playerId, PlayObject properties, PlayObject expectedValues)
        {
            var request = NewRequest();

            request.UpdateProperty = new UpdatePropertyRequest {
                TargetActorId = playerId,
                Attr          = ByteString.CopyFrom(CodecUtils.SerializePlayObject(properties))
            };
            if (expectedValues != null)
            {
                request.UpdateProperty.ExpectAttr = ByteString.CopyFrom(CodecUtils.SerializePlayObject(expectedValues));
            }
            var res = await SendRequest(CommandType.Conv, OpType.UpdatePlayerProp, request);

            var actorId = res.Response.UpdateProperty.ActorId;
            var props   = CodecUtils.DeserializePlayObject(res.Response.UpdateProperty.Attr);

            return(new Tuple <int, PlayObject>(actorId, props));
        }
コード例 #9
0
        LobbyRoom ConvertToLobbyRoom(Protocol.RoomOptions options)
        {
            var lobbyRoom = new LobbyRoom {
                RoomName       = options.Cid,
                Open           = options.Open == null || options.Open.Value,
                Visible        = options.Visible == null || options.Visible.Value,
                MaxPlayerCount = options.MaxMembers,
                PlayerCount    = options.MemberCount,
                EmptyRoomTtl   = options.EmptyRoomTtl,
                PlayerTtl      = options.PlayerTtl
            };

            if (options.ExpectMembers != null)
            {
                lobbyRoom.ExpectedUserIds = options.ExpectMembers.ToList <string>();
            }
            if (options.Attr != null)
            {
                lobbyRoom.CustomRoomProperties = CodecUtils.DeserializePlayObject(options.Attr);
            }
            return(lobbyRoom);
        }
コード例 #10
0
ファイル: Room.cs プロジェクト: leancloud/Play-SDK-CSharp
        void Init(Protocol.RoomOptions options)
        {
            Name            = options.Cid;
            Open            = options.Open == null || options.Open.Value;
            Visible         = options.Visible == null || options.Visible.Value;
            MaxPlayerCount  = options.MaxMembers;
            MasterActorId   = options.MasterActorId;
            ExpectedUserIds = new List <string>();
            if (options.ExpectMembers != null)
            {
                ExpectedUserIds.AddRange(options.ExpectMembers);
            }
            playerDict = new Dictionary <int, Player>();
            foreach (RoomMember member in options.Members)
            {
                Player player = new Player();
                player.Init(this, member);
                if (player.UserId == Client.UserId)
                {
                    Player = player;
                }
                playerDict.Add(player.ActorId, player);
            }
            // attr
            CustomProperties    = CodecUtils.DeserializePlayObject(options.Attr);
            gameConn.OnMessage += (cmd, op, body) => {
                switch (cmd)
                {
                case CommandType.Conv:
                    switch (op)
                    {
                    case OpType.MembersJoined:
                        HandlePlayerJoinedRoom(body.RoomNotification.JoinRoom);
                        break;

                    case OpType.MembersLeft:
                        HandlePlayerLeftRoom(body.RoomNotification.LeftRoom);
                        break;

                    case OpType.MasterClientChanged:
                        HandleMasterChanged(body.RoomNotification.UpdateMasterClient);
                        break;

                    case OpType.SystemPropertyUpdatedNotify:
                        HandleRoomSystemPropertiesChanged(body.RoomNotification.UpdateSysProperty);
                        break;

                    case OpType.UpdatedNotify:
                        HandleRoomCustomPropertiesChanged(body.RoomNotification.UpdateProperty);
                        break;

                    case OpType.PlayerProps:
                        HandlePlayerCustomPropertiesChanged(body.RoomNotification.UpdateProperty);
                        break;

                    case OpType.MembersOffline:
                        HandlePlayerOffline(body.RoomNotification);
                        break;

                    case OpType.MembersOnline:
                        HandlePlayerOnline(body.RoomNotification);
                        break;

                    case OpType.KickedNotice:
                        HandleRoomKicked(body.RoomNotification);
                        break;

                    default:
                        Logger.Error("unknown msg: {0}/{1} {2}", cmd, op, body);
                        break;
                    }
                    break;

                case CommandType.Events:
                    break;

                case CommandType.Direct:
                    HandleSendEvent(body.Direct);
                    break;

                case CommandType.Error: {
                    Logger.Error("error msg: {0}", body);
                    ErrorInfo errorInfo = body.Error.ErrorInfo;
                    Client.OnError?.Invoke(errorInfo.ReasonCode, errorInfo.Detail);
                }
                break;

                default:
                    Logger.Error("unknown msg: {0}/{1} {2}", cmd, op, body);
                    break;
                }
            };
            gameConn.OnClose += (code, message) => {
                Client.OnDisconnected?.Invoke();
            };
        }