internal void SetRoom(PlayRoom room) { Room = room; if (room != null) { this.ActorID = room.GetActorIdByUserId(this.UserID); } }
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); }
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); }); }); } }
/// <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); }; }
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; }
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); } }); }
/// <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); }); }); }); }
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); } }); }
internal static void DoSetRoomProperties(PlayRoom room, PlayResponse response) { var invalidKeys = new string[] { "cmd", "peerId", "op", "appId", "i" }; room.SetProperties(response.Body.Filter(invalidKeys)); }
internal static void DisconnectRoom(PlayRoom room) { room = null; Player = new Player(peer); Play.RoomConnection = ResetConnection(); }
internal Player(PlayPeer peer, PlayRoom room) : this() { Peer = peer; SetRoom(room); }