示例#1
0
 internal void SetRoom(PlayRoom room)
 {
     Room = room;
     if (room != null)
     {
         this.ActorID = room.GetActorIdByUserId(this.UserID);
     }
 }
示例#2
0
        internal static PlayRoom GetRoomWhenGet(PlayCommand request, PlayResponse response)
        {
            var room = new PlayRoom();

            room.RoomRemoteSecureAddress = response.Body["secureAddr"] as string;
            room.RoomRemoteAddress       = response.Body["addr"] as string;

            var roomProperties = response.Body;

            room.SetProperties(roomProperties);

            return(room);
        }
示例#3
0
 internal static void ConnectRoom(PlayRoom room, bool isRejoin = false)
 {
     lock (roomLock)
     {
         DoConnectToGameSever(room, () =>
         {
             peer.SessionRoomJoin(room, isRejoin: isRejoin, roomJoined: (roomm, response) =>
             {
                 DoSetRoomProperties(roomm, response);
                 Play.Room = roomm;
                 Play.InvokeEvent(PlayEventCode.OnJoinedRoom);
             });
         });
     }
 }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        public void ResetEnumerators()
        {
            listEnumerator         = new PlayQueryEnumerator <List <PlayRoom> >();
            listEnumerator.Command = new PlayCommand()
            {
                RelativeUrl   = "/rooms",
                Method        = "GET",
                UrlParameters = new Dictionary <string, object>()
            };

            listEnumerator.Limit = 20;

            listEnumerator.Decoder = (resp) =>
            {
                if (resp.IsSuccessful)
                {
                    var roomMetaDataObjs = resp.Body["rooms"] as List <object>;
                    if (roomMetaDataObjs != null)
                    {
                        var result = roomMetaDataObjs.Select(roomMetaDataObj =>
                        {
                            var room = new PlayRoom();
                            IDictionary <string, object> roomMetaData = roomMetaDataObj as IDictionary <string, object>;
                            room.MergeFromServer(roomMetaData);
                            return(room);
                        });
                        return(result.ToList());
                    }
                }
                else
                {
                    Play.InvokeEvent(PlayEventCode.OnFetchRoomListFailed, resp.ErrorCode, resp.ErrorReason);
                }
                return(new List <PlayRoom>());
            };

            listEnumerator.Before = () =>
            {
                Play.InvokeEvent(PlayEventCode.OnFetchingRoomList);
            };

            listEnumerator.Callback = (rooms) =>
            {
                AddRooms(rooms);
                Play.InvokeEvent(PlayEventCode.OnFetchedRoomList, rooms);
            };
        }
示例#5
0
        internal static void DoConnectToGameSever(PlayRoom room, Action connected = null)
        {
            if (room.RoomRemoteSecureAddress == null)
            {
                LogError("can NOT connect Room withouth remote addresss");
                return;
            }
            RoomConnection.Open(room.RoomRemoteSecureAddress);
            Action onOpened = null;

            onOpened = () =>
            {
                Play.Player.Room = room;
                Log(string.Format("connected Room(unique id:{0}) at server address:{1}", room.Name, room.RoomRemoteSecureAddress));
                RoomConnection.OnOpened -= onOpened;
                Play.peer.SessionOpen(connected);
            };
            RoomConnection.OnOpened += onOpened;
        }
示例#6
0
        internal void SessionRoomJoin(PlayRoom room, bool isRejoin = false, Action <PlayRoom, PlayResponse> roomJoined = null)
        {
            var joinCommand = new PlayCommand()
            {
                Body = new Dictionary <string, object>()
                {
                    { "cmd", "conv" },
                    { "op", "add" },
                    { "cid", room.Name },
                }
            };

            if (isRejoin)
            {
                joinCommand.Body.Add("rejoin", isRejoin);
            }
            Play.RunSocketCommand(joinCommand, done: (req, response) =>
            {
                if (roomJoined != null)
                {
                    roomJoined(room, response);
                }
            });
        }
示例#7
0
        /// <summary>
        /// create a room with config and name.
        /// </summary>
        /// <param name="roomConfig">config of Room</param>
        /// <param name="roomName">name of Room</param>
        public static void CreateRoom(PlayRoom.RoomConfig roomConfig, string roomName = null)
        {
            var createRoomCommand = new PlayCommand()
            {
                Body = new Dictionary <string, object>()
                {
                    { "cmd", "conv" },
                    { "op", "start" },
                },
            };

            if (string.IsNullOrEmpty(roomName))
            {
                roomName = PlayRoom.RandomRoomName(24);
            }

            RunSocketCommand(createRoomCommand, done: (request, response) =>
            {
                // get game server address from game router
                var roomRemoteSecureAddress = response.Body["secureAddr"] as string;
                var roomRemoteAddress       = response.Body["addr"] as string;

                // open websocket connection with game server
                DoConnectToGameSever(roomRemoteSecureAddress, () =>
                {
                    Play.InvokeEvent(PlayEventCode.OnCreatingRoom);
                    // create room at game server
                    peer.SessionCreateRoom(roomName, roomConfig, (room) =>
                    {
                        Play.Room = room;
                        Play.InvokeEvent(PlayEventCode.OnCreatedRoom);
                        Play.InvokeEvent(PlayEventCode.OnJoinedRoom);
                    });
                });
            });
        }
示例#8
0
        internal void SessionCreateRoom(string roomName, PlayRoom.RoomConfig roomConfig, Action <PlayRoom> roomCreated = null)
        {
            IDictionary <string, object> body = new Dictionary <string, object>();

            if (roomConfig.CustomRoomProperties != null)
            {
                body.Add("attr", roomConfig.CustomRoomProperties.ToDictionary());
            }
            if (roomConfig.MaxPlayerCount > 0 && roomConfig.MaxPlayerCount != PlayRoom.DefaultMaxPlayerCount)
            {
                body.Add("maxMembers", roomConfig.MaxPlayerCount);
            }
            if (roomConfig.EmptyTimeToLive > 0 && roomConfig.EmptyTimeToLive != PlayRoom.DefaultMaxEmptyTimeToLive)
            {
                body.Add("emptyRoomTtl", roomConfig.EmptyTimeToLive);
            }
            if (roomConfig.PlayerTimeToKeep > 0 && roomConfig.PlayerTimeToKeep != PlayRoom.DefaultMaxKeepPlayerTime)
            {
                body.Add("playerTtl", roomConfig.PlayerTimeToKeep);
            }
            if (roomConfig.ExpectedUsers != null)
            {
                body.Add("expectMembers", roomConfig.ExpectedUsers);
            }
            if (!roomConfig.IsVisible)
            {
                body.Add("visible", roomConfig.IsVisible);
            }
            if (!roomConfig.IsOpen)
            {
                body.Add("open", roomConfig.IsOpen);
            }
            if (roomConfig.LobbyMatchKeys != null)
            {
                body.Add("lobbyAttrKeys", roomConfig.LobbyMatchKeys);
            }
            body.Add("cmd", "conv");
            body.Add("op", "start");
            body.Add("cid", roomName);
            var createCommand = new PlayCommand()
            {
                Body = body
            };

            Play.RunSocketCommand(createCommand, done: (req, response) =>
            {
                if (response.IsSuccessful)
                {
                    var room = new PlayRoom(roomConfig, roomName);
                    Play.DoSetRoomProperties(room, response);
                    if (roomCreated != null)
                    {
                        roomCreated(room);
                    }
                }
                else
                {
                    Play.InvokeEvent(PlayEventCode.OnCreateRoomFailed, response.ErrorCode, response.ErrorReason);
                }
            });
        }
示例#9
0
        internal static void DoSetRoomProperties(PlayRoom room, PlayResponse response)
        {
            var invalidKeys = new string[] { "cmd", "peerId", "op", "appId", "i" };

            room.SetProperties(response.Body.Filter(invalidKeys));
        }
示例#10
0
 internal static void DisconnectRoom(PlayRoom room)
 {
     room   = null;
     Player = new Player(peer);
     Play.RoomConnection = ResetConnection();
 }
示例#11
0
 internal Player(PlayPeer peer, PlayRoom room) : this()
 {
     Peer = peer;
     SetRoom(room);
 }