コード例 #1
0
        /// <summary>
        /// Gets invoked to handle the incoming request
        /// </summary>
        /// <param name="context"></param>
        public async Task Invoke(HttpContext context)
        {
            string path = context.Request.Path.Value;

            string gameShortname = _botManager.BotGameOptions
                                   .SingleOrDefault(g =>
                                                    _botManager.ReplaceGameUrlTokens(g.ScoresUrl, g.ShortName).EndsWith(path)
                                                    )
                                   ?.ShortName;

            if (string.IsNullOrWhiteSpace(gameShortname) ||
                !new[] { HttpMethods.Post, HttpMethods.Get }.Contains(context.Request.Method))
            {
                await _next.Invoke(context);

                return;
            }

            var gameHandlerTuple = _botManager.TryFindGameHandler(gameShortname);

            if (!gameHandlerTuple.Success)
            {
                await _next.Invoke(context);

                return;
            }

            IGameHandler gameHandler = gameHandlerTuple.GameHandler;

            if (context.Request.Method == HttpMethods.Get)
            {
                string playerid = context.Request.Query["id"];
                if (string.IsNullOrWhiteSpace(playerid) || playerid.Length < 20)
                {
                    _logger.LogError("Invalid player id passed. id=`{0}`", playerid);
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    return;
                }

                var highScores = await gameHandler.GetHighestScoresAsync(_botManager.Bot, playerid);

                var responseData = JsonConvert.SerializeObject(highScores);
                context.Response.StatusCode  = StatusCodes.Status200OK;
                context.Response.ContentType = "application/json; charset=utf-8";
                await context.Response.WriteAsync(responseData);
            }
            else if (context.Request.Method == HttpMethods.Post)
            {
                string dataContent;
                using (var reader = new StreamReader(context.Request.Body))
                {
                    dataContent = await reader.ReadToEndAsync();
                }

                SetGameScoreDto scoreData;
                try
                {
                    scoreData = JsonConvert.DeserializeObject <SetGameScoreDto>(dataContent);
                    if (scoreData == null)
                    {
                        throw new NullReferenceException();
                    }
                }
                catch (Exception e)
                    when(e is JsonSerializationException || e is NullReferenceException)
                    {
                        _logger.LogError("Unable to deserialize score data. {0}.{1}Content: `{2}`",
                                         e.Message, Environment.NewLine, dataContent);
                        context.Response.StatusCode = StatusCodes.Status400BadRequest;
                        return;
                    }

                await gameHandler.SetGameScoreAsync(_botManager.Bot, scoreData.PlayerId, scoreData.Score);

                context.Response.StatusCode = StatusCodes.Status201Created;
            }
            else
            {
                await _next.Invoke(context);
            }
        }