Ejemplo n.º 1
0
        private IEnumerable <MatchPlayerStats> GetMatchPlayerStas(JObject jsonToJObject, string matchId)
        {
            var match = jsonToJObject.SelectToken("data").ToObject <MatchSummary>();

            JArray playerResultsIncluded = (JArray)jsonToJObject["included"];

            var rosterMatchPlayerStats = playerResultsIncluded.Where(x => x.Value <string>("type") == "roster");

            var matchRoster = rosterMatchPlayerStats.Select(s => new MatchRoster()
            {
                Id                  = (string)s["id"],
                Type                = (string)s["type"],
                RosterAttributes    = s.SelectToken("attributes").ToObject <RosterAttributes>(),
                RosterRelationShips = new RosterRelationShips()
                {
                    Participant = s.SelectToken("relationships.participants.data").Select(p => new Participant()
                    {
                        Id   = (string)p["id"],
                        Type = (string)p["type"]
                    })
                }
            });

            var participantMatchPlayerStats = playerResultsIncluded.Where(x => x.Value <string>("type") == "participant");

            var matchParticipant = participantMatchPlayerStats.Select(s => new MatchParticipant()
            {
                Id   = (string)s["id"],
                Type = (string)s["type"],
                ParticipantAttributes = s.SelectToken("attributes").ToObject <ParticipantAttributes>()
            });

            var teamPlayers = _teamPlayerRepository.GetTeamPlayers();

            var teamParticipants = new List <MatchPlayerStats>();

            foreach (var item in matchRoster)
            {
                foreach (var item1 in item.RosterRelationShips.Participant)
                {
                    foreach (var item2 in matchParticipant)
                    {
                        var teamParticipant = new MatchPlayerStats();

                        if (item1.Id == item2.Id)
                        {
                            teamParticipant.MatchId       = match.Id;
                            teamParticipant.RosterId      = item.Id;
                            teamParticipant.ParticipantId = item2.Id;
                            teamParticipant.stats         = item2.ParticipantAttributes.stats;
                            teamParticipant.TeamId        = teamPlayers.Result.Where(cn => cn.PlayerName.ToLower().Trim() == item2.ParticipantAttributes.stats.Name.ToLower().Trim()).FirstOrDefault().TeamId;
                            teamParticipant.Rank          = item.RosterAttributes.Stats.Rank;
                            teamParticipant.ShortTeamId   = teamPlayers.Result.Where(cn => cn.PlayerName.ToLower().Trim() == item2.ParticipantAttributes.stats.Name.ToLower().Trim()).FirstOrDefault().TeamIdShort;
                            teamParticipants.Add(teamParticipant);
                        }
                    }
                }
            }
            return(teamParticipants);
        }
        /// <summary>
        /// Once all the match data is parsed this method begins inserting into the database.
        /// </summary>
        private void PersistMatchData()
        {
            logger.Info("Saving all data to database");
            using (var db = new StatsDbContext())
            {
                /*
                 * We saved the initial match data in the SaveBeforeParsing method.
                 * let's finish saving the match details now that we've parsed the data.
                 */

                // find the match by the matchId
                var match = db.Match
                            .Where(b => b.MatchId == MatchResults.MatchId)
                            .FirstOrDefault();
                match.MatchType      = MatchResults.MatchType;
                match.MapName        = MatchResults.MapName;
                match.MatchTimeLimit = MatchResults.MatchTimeLimit.ConvertToInt();
                db.SaveChanges();

                // Save each individual team stats.
                foreach (KeyValuePair <string, MatchTeamStatDTO> teamEntry in MatchResults.ListOfTeams)
                {
                    var newMatchTeamStat = new MatchTeamStats()
                    {
                        MatchId               = match.Id,
                        TeamColor             = teamEntry.Value.TeamColor,
                        TeamVerdict           = teamEntry.Value.TeamVerdict,
                        TeamTotalFrags        = teamEntry.Value.TeamTotalFrags.ConvertToInt(),
                        TeamTotalQuads        = teamEntry.Value.TeamTotalQuads.ConvertToInt(),
                        TeamTotalPents        = teamEntry.Value.TeamTotalPents.ConvertToInt(),
                        TeamTotalEyes         = teamEntry.Value.TeamTotalEyes.ConvertToInt(),
                        TeamTotalRL           = teamEntry.Value.TeamTotalRL.ConvertToInt(),
                        TeamTotalLG           = teamEntry.Value.TeamTotalLG.ConvertToInt(),
                        TeamTotalGL           = teamEntry.Value.TeamTotalGL.ConvertToInt(),
                        TeamTotalSNG          = teamEntry.Value.TeamTotalSNG.ConvertToInt(),
                        TeamTotalNG           = teamEntry.Value.TeamTotalNG.ConvertToInt(),
                        TeamTotalMH           = teamEntry.Value.TeamTotalMH.ConvertToInt(),
                        TeamTotalRA           = teamEntry.Value.TeamTotalRA.ConvertToInt(),
                        TeamTotalYA           = teamEntry.Value.TeamTotalYA.ConvertToInt(),
                        TeamTotalGA           = teamEntry.Value.TeamTotalGA.ConvertToInt(),
                        TeamPlusRLPack        = teamEntry.Value.TeamPlusRLPack.Trim(),
                        TeamMinusRLPack       = teamEntry.Value.TeamMinusRLPack.Trim(),
                        TeamControlPercentage = teamEntry.Value.TeamControlPercentage.ConvertEfficiency(),
                    };
                    db.MatchTeamStats.Add(newMatchTeamStat);
                    db.SaveChanges();
                }

                // add all player detailed stats
                foreach (KeyValuePair <string, MatchPlayerDTO> playerEntry in MatchResults.ListOfPlayers)
                {
                    var playerId = playerEntry.Key.ConvertToInt();
                    var mpStats  = new MatchPlayerStats();

                    // Because I'm setting every unique non logged in player to an int < 0 in the dictionary, I basically check if this user is logged in.
                    // Of course this could be handled more elagantly with a proper object but time is limited at the moment.
                    // if the player in this entry is logged in
                    if (playerId > 0)
                    {
                        // let's test that the player id is actually a user in the database since we could have an ID in config but not in the database.
                        var playerInDatabase = db.Player
                                               .Where(x => x.Id == playerId)
                                               .FirstOrDefault();

                        // if the player id isn't found in the database throw exception.
                        if (playerInDatabase == null)
                        {
                            throw new Exception("PlayerId: " + playerId + " was deemed logged in but player was not found in the database. Please check id exists.");
                        }

                        // let's make sure the player it found wasn't the unknown player for whatever reason.
                        if (playerId != 999 && playerId > 0)
                        {
                            // populate team stats
                            mpStats.KillEfficiency   = playerEntry.Value.KillEfficiency.ConvertEfficiency();
                            mpStats.WeaponEfficiency = playerEntry.Value.WeaponEfficiency.ConvertEfficiency();
                            // populate quad stats
                            mpStats.NumberOfQuads     = playerEntry.Value.NumberOfQuads.ConvertToInt();
                            mpStats.QuadEfficiency    = playerEntry.Value.QuadEfficiency.ConvertEfficiency();
                            mpStats.NumQuadEnemyKills = playerEntry.Value.NumQuadEnemyKills.ConvertToInt();
                            mpStats.NumQuadSelfKills  = playerEntry.Value.NumQuadSelfKills.ConvertToInt();
                            mpStats.NumQuadTeamKills  = playerEntry.Value.NumQuadTeamKills.ConvertToInt();
                            // populate kill stats
                            mpStats.NumOfFrags      = playerEntry.Value.NumOfFrags.ConvertToInt();
                            mpStats.NumOfEnemyKills = playerEntry.Value.NumOfEnemyKills.ConvertToInt();
                            mpStats.NumOfSelfKills  = playerEntry.Value.NumOfSelfKills.ConvertToInt();
                            mpStats.NumOfTeamKills  = playerEntry.Value.NumOfTeamKills.ConvertToInt();
                            mpStats.NumOfDeaths     = playerEntry.Value.NumOfDeaths.ConvertToInt();
                            // populate efficiency stats
                            mpStats.BulletEfficiency    = playerEntry.Value.BulletEfficiency.ConvertEfficiency();
                            mpStats.NailsEfficiency     = playerEntry.Value.NailsEfficiency.ConvertEfficiency();
                            mpStats.RocketEfficiency    = playerEntry.Value.RocketEfficiency.ConvertEfficiency();
                            mpStats.LightningEfficiency = playerEntry.Value.LightningEfficiency.ConvertEfficiency();
                            mpStats.TotalEfficiency     = playerEntry.Value.TotalEfficiency.ConvertEfficiency();
                            // populate bad stats
                            mpStats.DroppedPaks = playerEntry.Value.DroppedPaks.ConvertToInt();
                            mpStats.SelfDamage  = playerEntry.Value.SelfDamage.ConvertEfficiency();
                            mpStats.TeamDamage  = playerEntry.Value.TeamDamage.ConvertEfficiency();
                        }
                        else
                        {
                            throw new Exception("Some how the query for playerId: " + playerId + " came back as 999");
                        }
                    }
                    else
                    {
                        // set id to unknown player
                        playerId = 999;
                    }

                    // Tracking Info
                    mpStats.MatchId   = match.Id;
                    mpStats.PlayerId  = playerId;
                    mpStats.TeamColor = playerEntry.Value.TeamColor;
                    db.MatchPlayerStats.Add(mpStats);
                    db.SaveChanges();
                }
            }
        }