Example #1
0
        public IEnumerator MatchRandom()
        {
            var f = false;

            var roomName = "jr8_r";
            var c0       = Utils.NewClient("jr8_0");
            var c1       = Utils.NewClient("jr8_1");
            var c2       = Utils.NewClient("jr8_2");
            var c3       = Utils.NewClient("jr8_xxx");

            var props = new PlayObject {
                { "lv", 5 }
            };

            c0.Connect().OnSuccess(_ => {
                var roomOptions = new RoomOptions {
                    MaxPlayerCount                 = 3,
                    CustomRoomProperties           = props,
                    CustomRoomPropertyKeysForLobby = new List <string> {
                        "lv"
                    }
                };
                return(c0.CreateRoom(roomName, roomOptions));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c1.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                Debug.Log("c1 connected");
                return(c1.MatchRandom("jr8_1", new PlayObject {
                    { "lv", 5 }
                }, new List <string> {
                    "jr8_xxx"
                }));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(t => {
                var roomId = t.Result;
                Assert.AreEqual(roomId, roomName);
                return(c1.JoinRoom(roomId));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c2.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c2.JoinRandomRoom(props));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().ContinueWith(t => {
                PlayException e = (PlayException)t.Exception.InnerException;
                Assert.AreEqual(e.Code, 4301);
                _ = c2.Close();
                return(c3.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c3.JoinRandomRoom(props));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(async _ => {
                await c0.Close();
                await c1.Close();
                await c3.Close();
                f = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());

            while (!f)
            {
                yield return(null);
            }
        }
Example #2
0
        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();
                }
            }
        }
Example #3
0
        async Task <LobbyRoomResult> Request(string url, string sessionToken, Dictionary <string, object> body)
        {
            HttpClient          httpClient = null;
            HttpRequestMessage  request    = null;
            HttpResponseMessage response   = null;

            try {
                httpClient = new HttpClient();
                string data = JsonConvert.SerializeObject(body);
                request = new HttpRequestMessage {
                    RequestUri = new Uri(url),
                    Method     = HttpMethod.Post,
                    Content    = new StringContent(data)
                };
                AddHeaders(request.Content.Headers);
                request.Content.Headers.Add(USER_SESSION_TOKEN_KEY, sessionToken);
                HttpUtils.PrintRequest(httpClient, request, data);
                response = await httpClient.SendAsync(request);

                string content = await response.Content.ReadAsStringAsync();

                HttpUtils.PrintResponse(response, content);
                if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode < HttpStatusCode.Ambiguous)
                {
                    return(JsonConvert.DeserializeObject <LobbyRoomResult>(content));
                }
                PlayException exception = JsonConvert.DeserializeObject <PlayException>(content);
                throw exception;
            } finally {
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
                if (request != null)
                {
                    request.Dispose();
                }
                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Example #4
0
        public IEnumerator JoinWithExpectedUserIds()
        {
            var f        = false;
            var roomName = "jrt2_r";
            var c0       = Utils.NewClient("jrt2_0");
            var c1       = Utils.NewClient("jrt2_1");
            var c2       = Utils.NewClient("jrt2_2");

            c0.Connect().OnSuccess(_ => {
                var roomOptions = new RoomOptions {
                    MaxPlayerCount = 2
                };
                return(c0.CreateRoom(roomName, roomOptions, new List <string> {
                    "jrt2_2"
                }));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c1.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c1.JoinRoom(roomName));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().ContinueWith(t => {
                Assert.AreEqual(t.IsFaulted, true);
                PlayException exception = t.Exception.InnerException as PlayException;
                Assert.AreEqual(exception.Code, 4302);
                Debug.Log(exception.Detail);
                return(c2.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c2.JoinRoom(roomName));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(async t => {
                Room room = t.Result;
                Assert.AreEqual(room.Name, roomName);
                await c0.Close();
                await c1.Close();
                await c2.Close();
                f = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());

            while (!f)
            {
                yield return(null);
            }
        }
Example #5
0
        public IEnumerator JoinRandomWithMatchProperties()
        {
            var f        = false;
            var roomName = "jrt7_r";
            var c0       = Utils.NewClient("jrt7_0");
            var c1       = Utils.NewClient("jrt7_1");
            var c2       = Utils.NewClient("jrt7_2");
            var c3       = Utils.NewClient("jrt7_3");
            var c4       = Utils.NewClient("jrt7_2");

            var props = new PlayObject {
                { "lv", 2 }
            };

            c0.Connect().OnSuccess(_ => {
                var roomOptions = new RoomOptions {
                    MaxPlayerCount                 = 3,
                    CustomRoomProperties           = props,
                    CustomRoomPropertyKeysForLobby = new List <string> {
                        "lv"
                    }
                };
                return(c0.CreateRoom(roomName, roomOptions));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c1.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c1.JoinRandomRoom(props, new List <string> {
                    "jrt7_2"
                }));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c2.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c2.JoinRandomRoom(new PlayObject {
                    { "lv", 3 }
                }));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().ContinueWith(async t => {
                PlayException e = (PlayException)t.Exception.InnerException;
                Assert.AreEqual(e.Code, 4301);
                await c2.Close();
                return(c3.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(t => {
                return(c3.JoinRandomRoom(props));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().ContinueWith(async t => {
                PlayException e = (PlayException)t.Exception.InnerException;
                Assert.AreEqual(e.Code, 4301);
                await c3.Close();
                return(c4.Connect());
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(_ => {
                return(c4.JoinRandomRoom(props));
            }, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap().OnSuccess(async _ => {
                await c0.Close();
                await c1.Close();
                await c4.Close();
                f = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());

            while (!f)
            {
                yield return(null);
            }
        }