Ejemplo n.º 1
0
        public async Task <GameResponse> Post()
        {
            List <Request> requests      = new List <Request>();
            List <Game>    gamesToReturn = new List <Game>();
            GameResponse   gameResponse  = new GameResponse
            {
                StatusCode   = HttpStatusCode.OK,
                ErrorMessage = "",
                Games        = gamesToReturn
            };

            __req = await GetHelper.ReadFromBody <Request>(Request.Body);

            __req.ApiKey = GetApiKey();


            if (__req.ApiKey == null)
            {
                // Handle non-existent API-Key
                gameResponse.StatusCode   = HttpStatusCode.NotFound;
                gameResponse.ErrorMessage = "No API key provided";

                return(gameResponse);
            }

            GetHelper getHelper = new GetHelper(__req.Region.ToLower(), __req.ApiKey);

            // Using https://developer.riotgames.com/apis

            /**
             * (1) Get {encryptedAccountId}
             */
            //TODO: Move these gets into a helper class and have them return an HttpGetRespone
            HttpGetResponse summonerGet = await getHelper.GetAsyncFromRiotApi <SummonerDTO>(".api.riotgames.com/lol/summoner/v4/summoners/by-name/" + __req.SummonerName);

            if (summonerGet.Ex != null)
            {
                return(GetHelper.HandleBadRequest(summonerGet.Ex, GetRequest.Summoner));
            }
            __summoner = summonerGet.Value as SummonerDTO;

            /**
             * (2) Use that information to get the match-lists
             *
             */
            HttpGetResponse matchesGet = await getHelper.GetAsyncFromRiotApi <MatchlistDto>(".api.riotgames.com/lol/match/v4/matchlists/by-account/" + __summoner.AccountId,
                                                                                            new List <Filter>
            {
                new Filter
                {
                    PropertyName = "endIndex",
                    Value        = "10"
                }
            }
                                                                                            );

            if (matchesGet.Ex != null)
            {
                return(GetHelper.HandleBadRequest(matchesGet.Ex, GetRequest.Matches));
            }
            __matches = matchesGet.Value as MatchlistDto;

            // Get Queue.json data - TODO: Maybe only need to do this one time
            HttpGetResponse versionsGet = await GetHelper.GetAsync <List <string> >("https://ddragon.leagueoflegends.com/api/versions.json");

            if (versionsGet.Ex != null)
            {
                return(GetHelper.HandleBadRequest(versionsGet.Ex, GetRequest.Versions));
            }
            __version = (versionsGet.Value as List <string>).First();

            // Get Champs data - TODO: Maybe only need to do this one time
            HttpGetResponse championsGet = await GetHelper.GetAsync <ChampionsJson>("http://ddragon.leagueoflegends.com/cdn/" + __version + "/data/en_US/champion.json");

            if (championsGet.Ex != null)
            {
                return(GetHelper.HandleBadRequest(championsGet.Ex, GetRequest.Champions));
            }
            __champsJson = (championsGet.Value as ChampionsJson);

            foreach (MatchReferenceDto matchRef in __matches.Matches)
            {
                /**
                 * Player stats:
                 *  - 1) Win/Loss = matchId -> MatchDto get request -> get index in partipiantIndentities -> get ParticipantDto from participants via the participantId + 1 ->
                 *       get ParticipantStatsDto -> get win boolean value
                 *  2) Queue type = Queue from MatchRferenceDto -> (TODO: map to the queue json)
                 *  3) Date - get timestamp long from MatchReferenceDto.cs and compare it to epoch in order to get date
                 *  4) ChampionName - for now just return return champion int from MatchReferenceDto (TODO: Also return the image eventually)
                 *  5) ChampionImage - The href link
                 *  6) GameLength - natchId -> MatchDto get request -> gameDuration
                 *  7) Kills
                 *  8) Deaths
                 *  9) Assists
                 *
                 *  TODO:
                 *  1) Items built (w/ icons)
                 *
                 *  Player card:
                 *  1) Overall KDA
                 *  2) Summoner Level
                 *  3) Win rate
                 *  4) Profile icon
                 *  5) Custom report card rating
                 */
                Game            currGame = new Game();
                MatchDto        match    = new MatchDto();
                HttpGetResponse matchGet = await getHelper.GetAsyncFromRiotApi <MatchDto>(".api.riotgames.com/lol/match/v4/matches/" + matchRef.GameId);

                if (matchGet.Ex != null)
                {
                    return(GetHelper.HandleBadRequest(matchGet.Ex, GetRequest.Match));
                }
                match = matchGet.Value as MatchDto;

                ParticipantIdentityDto participantId = match.ParticipantIdentities.Find(IsSummonerMatch);
                ParticipantDto         participant   = match.Participants[participantId.ParticipantId - 1];

                if (participant.Stats.Win) // (1) Result
                {
                    currGame.Result = "Win";
                    __totalWins++;
                }
                else
                {
                    currGame.Result = "Loss";
                }

                // TODO: Maybe actually map to http://http://static.developer.riotgames.com/docs/lol/queues.json
                currGame.QueueType = GetQueueStringMapping(matchRef.Queue); // (2) QueueType

                long timeStamp = matchRef.Timestamp;
                currGame.Date = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(timeStamp - 86400000).ToShortDateString(); // (3) Date

                __currentChampId = matchRef.Champion;
                Tuple <string, string> champTuple = GetChampMappings();
                currGame.ChampionName  = champTuple.Item1;            // (4) ChampionName
                currGame.ChampionImage = champTuple.Item2;            // (5) ChampionImage

                currGame.GameLength = (int)(match.GameDuration / 60); // (6) GameLength

                currGame.Kills   = participant.Stats.Kills;           // (7) Kills
                __totalKills    += currGame.Kills;
                currGame.Deaths  = participant.Stats.Deaths;          // (8) Deaths
                __totalDeaths   += currGame.Deaths;
                currGame.Assists = participant.Stats.Assists;         // (9) Assists
                __totalAssists  += currGame.Assists;

                gamesToReturn.Add(currGame); // Woohoo
            }

            double kda     = (__totalKills + __totalAssists) / (__totalDeaths * 1.0);
            double winRate = (double)__totalWins / gameResponse.Games.Count() * 100;

            gameResponse.CardStats = GetSummonerCardInfo(kda, winRate);

            return(gameResponse);
        }