예제 #1
0
        public static Invocateur ConvertJsonSummoner(JObject jsonSummonner)
        {
            Invocateur invocateur = new Invocateur
            {
                Id               = jsonSummonner["id"].ToString(),
                AccountId        = jsonSummonner["accountId"].ToString(),
                Name             = jsonSummonner["name"].ToString(),
                ImageProfil      = Int32.Parse(jsonSummonner["profileIconId"].ToString()),
                DernieresParties = new List <Partie>()
            };

            return(invocateur);
        }
예제 #2
0
        //// GET: Invocateur
        public async Task <ActionResult> Profil(string summonerId)
        {
            Invocateur invocateur = new Invocateur();

            //Utilisateur en base ?
            if (db.Invocateurs.Where(x => x.Id == summonerId).Count() > 0)
            {
                invocateur = db.Invocateurs.Where(x => x.Id == summonerId).FirstOrDefault();
            }
            //Sinon on récupère l'invocateur et on le sauvegarde en base
            else
            {
                invocateur = ConvertJson.ConvertJsonSummoner(await ApiCall.GetJsonSummoner(summonerId));
                db.Invocateurs.Add(invocateur);
                db.SaveChanges();
            }

            //On récupère les parties
            List <Partie> lastMatchs = new List <Partie>();

            //L'utilisateur a des parties en base ?
            if (invocateur.DernieresParties.Count > 0)
            {
                db.Parties.Where(x => x.Joueur.Invocateur.Id == invocateur.Id).ToList();
            }
            //Sinon on les récupères avec l'api et les stock en base
            else
            {
                lastMatchs = await ApiCall.GetGameHistory(invocateur.AccountId);

                db.Parties.AddRange(lastMatchs);
                db.SaveChanges();
            }

            return(View(lastMatchs));
        }
예제 #3
0
        public async static Task <List <Partie> > GetGameHistory(string accountId)
        {
            List <Partie> history = new List <Partie>();
            HttpClient    client  = new HttpClient();

            client.DefaultRequestHeaders.Add("X-Riot-Token", api_key);

            var response = await client.GetAsync("https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/" + accountId + "?endIndex=10");

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

            if (response.IsSuccessStatusCode)
            {
                JObject jsonMatchList = JObject.Parse(content);

                foreach (JObject match in (JArray)jsonMatchList["matches"])
                {
                    var responseMatch = await client.GetAsync("https://euw1.api.riotgames.com/lol/match/v4/matches/"
                                                              + match["gameId"].ToString());

                    string contentMatch = await responseMatch.Content.ReadAsStringAsync();

                    if (responseMatch.IsSuccessStatusCode)
                    {
                        JObject jsonMatch = JObject.Parse(contentMatch);
                        Partie  partie    = new Partie();

                        //Liste des joueurs
                        partie.AutreJoueurs = new List <Joueur>();

                        //Joueur du profil
                        partie.Joueur             = new Joueur();
                        partie.Joueur.Equipements = new List <Equipement>();

                        partie.DatePartie = UnixTimeStampToDateTime(Double.Parse(jsonMatch["gameCreation"].ToString()) / 1000);
                        partie.Duree      = Int32.Parse(jsonMatch["gameDuration"].ToString());
                        partie.TypePartie = jsonMatch["gameMode"].ToString();

                        //Récupération des identités des joueurs de la game
                        foreach (JObject playersIdentities in (JArray)jsonMatch["participantIdentities"])
                        {
                            Joueur     j = new Joueur();
                            Invocateur invocateurJoueur
                                = new Invocateur
                                {
                                Id        = playersIdentities["player"]["summonerId"].ToString(),
                                Name      = playersIdentities["player"]["summonerName"].ToString(),
                                AccountId = playersIdentities["player"]["accountId"].ToString(),
                                };

                            j.IdParticipant = Int32.Parse(playersIdentities["participantId"].ToString());
                            j.Invocateur    = invocateurJoueur;

                            if (j.Invocateur.AccountId == accountId)
                            {
                                partie.Joueur = j;
                            }
                            partie.AutreJoueurs.Add(j);
                        }

                        //Récupération des stats invocateur principal et teams
                        foreach (JObject playersStats in (JArray)jsonMatch["participants"])
                        {
                            int    idParticipant = Int32.Parse(playersStats["participantId"].ToString());
                            string nomChampion   = await Helpers.IdToName.GetChampionName(Int32.Parse(playersStats["championId"].ToString()));

                            Champion champion = new Champion
                            {
                                Id    = Int32.Parse(playersStats["championId"].ToString()),
                                Nom   = nomChampion,
                                Image = "http://ddragon.leagueoflegends.com/cdn/10.6.1/img/champion/"
                                        + nomChampion + ".png"
                            };

                            //Récup des infos du participant
                            Joueur j = partie.AutreJoueurs.Where(x => x.IdParticipant == idParticipant).First();
                            j.Equipe = new Equipe {
                                Id = Int32.Parse(playersStats["teamId"].ToString())
                            };
                            j.Champion = champion;

                            //Récup info invocateur pricipal
                            if (idParticipant == partie.Joueur.Id)
                            {
                                //Victoire ?
                                partie.Victoire = bool.Parse(playersStats["stats"]["win"].ToString());
                                //Spells invocateur
                                partie.Joueur.Sort1 = "http://ddragon.leagueoflegends.com/cdn/10.6.1/img/spell/" +
                                                      await Helpers.IdToName.GetSpellName(Int32.Parse(playersStats["spell1Id"].ToString()))
                                                      + ".png";

                                partie.Joueur.Sort2 = "http://ddragon.leagueoflegends.com/cdn/10.6.1/img/spell/" +
                                                      await Helpers.IdToName.GetSpellName(Int32.Parse(playersStats["spell2Id"].ToString()))
                                                      + ".png";

                                //Img du champion
                                partie.Joueur.NbTue    = Int32.Parse(playersStats["stats"]["kills"].ToString());
                                partie.Joueur.NbMort   = Int32.Parse(playersStats["stats"]["deaths"].ToString());
                                partie.Joueur.NbAssist = Int32.Parse(playersStats["stats"]["assists"].ToString());
                                partie.Joueur.NbSbire  = Int32.Parse(playersStats["stats"]["totalMinionsKilled"].ToString());
                                partie.Joueur.Level    = Int32.Parse(playersStats["stats"]["champLevel"].ToString());
                                partie.Joueur.KDA      = (partie.Joueur.NbTue + partie.Joueur.NbAssist) / partie.Joueur.NbMort;
                                partie.Joueur.Poste    = playersStats["timeline"]["lane"].ToString();

                                for (int i = 0; i < 6; i++)
                                {
                                    string path = VirtualPathUtility.ToAbsolute("~/Content/img/assets/empty.png");

                                    Equipement item = await Helpers.IdToName.GetItem(Int32.Parse(playersStats["stats"]["item" + i].ToString()));

                                    partie.Joueur.Equipements.Add(item);
                                }
                            }
                        }
                        history.Add(partie);
                    }
                }
            }
            return(history);
        }