예제 #1
0
        public async Task <AkinatorQuestion> StartNewGame(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var apiKey = await GetSession().ConfigureAwait(false);

            var url = AkiUrlBuilder.NewGame(apiKey, m_usedLanguage, m_usedServerType);

            var response = await m_webClient.GetAsync(url, cancellationToken).ConfigureAwait(false);

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

            var match = m_regexStartGameResult.Match(content);

            if (!match.Success && match.Groups.Count != 2)
            {
                throw new InvalidCastException($"Invalid result received from Akinator. Result was {response}");
            }

            var result = JsonConvert.DeserializeObject <BaseResponse <NewGameParameters> >(match.Groups[1].Value,
                                                                                           new JsonSerializerSettings()
            {
                MissingMemberHandling = MissingMemberHandling.Ignore
            });

            m_session   = result.Parameters.Identification.Session;
            m_signature = result.Parameters.Identification.Signature;
            m_step      = result.Parameters.StepInformation.Step;
            return(ToAkinatorQuestion(result.Parameters.StepInformation));
        }
예제 #2
0
        public async Task <AkinatorQuestion> StartNewGame(CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var apiKey = await GetSession(cancellationToken).ConfigureAwait(false);

            var url      = AkiUrlBuilder.NewGame(apiKey, _mServer, _mChildMode);
            var response = await _mWebClient.GetAsync(url, cancellationToken).ConfigureAwait(false);

            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var match = _mRegexStartGameResult.Match(content);

            if (!match.Success && match.Groups.Count != 2)
            {
                throw new ApiErrorException(url, content, "Invalid response while creating new game");
            }

            var result = EnsureNoError <NewGameParameters>(url, match.Groups[1].Value);

            _mSession       = result.Identification.Session;
            _mSignature     = result.Identification.Signature;
            _mStep          = result.StepInformation.Step;
            CurrentQuestion = ToAkinatorQuestion(result.StepInformation);

            return(CurrentQuestion);
        }