internal async Task JoinOrCreate(string roomName, RoomOptions roomOptions, List <string> expectedUserIds) { state = State.Joining; try { LobbyInfo lobbyInfo = await Client.lobbyService.Authorize(); LobbyRoomResult lobbyRoom = await Client.lobbyService.JoinRoom(roomName, null, false, true); gameConn = new GameConnection(); await gameConn.Connect(Client.AppId, lobbyRoom.Url, Client.GameVersion, Client.UserId, lobbyInfo.SessionToken); Protocol.RoomOptions options; if (lobbyRoom.Create) { options = await gameConn.CreateRoom(lobbyRoom.RoomId, roomOptions, expectedUserIds); } else { options = await gameConn.JoinRoom(lobbyRoom.RoomId, null); } Init(options); state = State.Game; } catch (Exception e) { state = State.Closed; throw e; } }
internal async Task <LobbyRoomResult> JoinRoom(string roomName, List <string> expectedUserIds, bool rejoin, bool createOnNotFound) { LobbyInfo lobbyInfo = await gameRouter.Authorize(); string path = $"/1/multiplayer/lobby/room/{roomName}"; string fullUrl = $"{lobbyInfo.Url}{path}"; Dictionary <string, object> body = new Dictionary <string, object> { { "cid", roomName }, { "gameVersion", client.GameVersion }, { "sdkVersion", Config.SDKVersion }, { "protocolVersion", Config.ProtocolVersion }, { "useInsecureAddr", !client.Ssl } }; if (expectedUserIds != null) { body.Add("expectMembers", expectedUserIds); } if (rejoin) { body.Add("rejoin", rejoin); } if (createOnNotFound) { body.Add("createOnNotFound", createOnNotFound); } return(await Request(fullUrl, lobbyInfo.SessionToken, body)); }
async Task <LobbyInfo> AuthorizeFromServer() { AppRouter appRouter = await appRouterController.Get(); HttpRequestMessage request = null; HttpResponseMessage response = null; try { Dictionary <string, object> data = new Dictionary <string, object>(); string dataContent = JsonConvert.SerializeObject(data); string url = $"{appRouter.PlayServer}/1/multiplayer/router/authorize"; if (!Uri.IsWellFormedUriString(appRouter.PlayServer, UriKind.Absolute)) { url = $"https://{appRouter.PlayServer}/1/multiplayer/router/authorize"; } request = new HttpRequestMessage { RequestUri = new Uri(url), Method = HttpMethod.Post, Content = new StringContent(dataContent) }; request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpUtils.PrintRequest(httpClient, request, dataContent); response = await httpClient.SendAsync(request); string content = await response.Content.ReadAsStringAsync(); HttpUtils.PrintResponse(response, content); if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode < HttpStatusCode.Ambiguous) { lobbyInfo = await JsonUtils.DeserializeObjectAsync <LobbyInfo>(content); return(lobbyInfo); } PlayException exception = await JsonUtils.DeserializeObjectAsync <PlayException>(content); throw exception; } finally { if (request != null) { request.Dispose(); } if (response != null) { response.Dispose(); } } }
internal async Task JoinRandom(PlayObject matchProperties, List <string> expectedUserIds) { state = State.Joining; try { LobbyInfo lobbyInfo = await Client.lobbyService.Authorize(); LobbyRoomResult lobbyRoom = await Client.lobbyService.JoinRandomRoom(matchProperties, expectedUserIds); gameConn = new GameConnection(); await gameConn.Connect(Client.AppId, lobbyRoom.Url, Client.GameVersion, Client.UserId, lobbyInfo.SessionToken); Protocol.RoomOptions options = await gameConn.JoinRoom(lobbyRoom.RoomId, expectedUserIds); Init(options); state = State.Game; } catch (Exception e) { state = State.Closed; throw e; } }
internal async Task Rejoin(string roomName) { state = State.Joining; try { LobbyInfo lobbyInfo = await Client.lobbyService.Authorize(); LobbyRoomResult lobbyRoom = await Client.lobbyService.JoinRoom(roomName, null, true, false); gameConn = new GameConnection(); await gameConn.Connect(Client.AppId, lobbyRoom.Url, Client.GameVersion, Client.UserId, lobbyInfo.SessionToken); Protocol.RoomOptions options = await gameConn.JoinRoom(lobbyRoom.RoomId, null); Init(options); state = State.Game; } catch (Exception e) { state = State.Closed; throw e; } }
internal async Task <LobbyRoomResult> CreateRoom(string roomName) { LobbyInfo lobbyInfo = await gameRouter.Authorize(); string path = "/1/multiplayer/lobby/room"; string fullUrl = $"{lobbyInfo.Url}{path}"; Logger.Debug(fullUrl); Dictionary <string, object> body = new Dictionary <string, object> { { "gameVersion", client.GameVersion }, { "sdkVersion", Config.SDKVersion }, { "protocolVersion", Config.ProtocolVersion }, { "useInsecureAddr", !client.Ssl } }; if (!string.IsNullOrEmpty(roomName)) { body.Add("cid", roomName); } return(await Request(fullUrl, lobbyInfo.SessionToken, body)); }
internal async Task <LobbyRoomResult> MatchRandom(string piggybackUserId, PlayObject matchProperties, List <string> expectedUserIds) { LobbyInfo lobbyInfo = await gameRouter.Authorize(); string path = "/1/multiplayer/lobby/match/room"; string fullUrl = $"{lobbyInfo.Url}{path}"; Dictionary <string, object> body = new Dictionary <string, object> { { "gameVersion", client.GameVersion }, { "sdkVersion", Config.SDKVersion }, { "protocolVersion", Config.ProtocolVersion }, { "piggybackPeerId", piggybackUserId } }; if (matchProperties != null) { body.Add("expectAttr", matchProperties.Data); } if (expectedUserIds != null) { body.Add("expectMembers", expectedUserIds); } return(await Request(fullUrl, lobbyInfo.SessionToken, body)); }