public ActionResult Index(List <WOT_CW_Assistant.Models.Tank> tanks)
        {
            List <TankStatistics> ordered = new List <TankStatistics>();

            try
            {
                List <TankStatistics> tsl = new List <TankStatistics>();
                string plNick             = User.Identity.GetPlayerNickName();
                Player loggedPlayer       = context.Players.Where(p => p.playerNickName == plNick).FirstOrDefault();
                string clanId             = loggedPlayer.clanId;
                var    dbPlayers          = context.Players.Where(p => p.clanId == loggedPlayer.clanId).ToList();

                foreach (Player player in dbPlayers)
                {
                    foreach (Tank tank in tanks)
                    {
                        if (tank.selected)
                        {
                            TankStatistics ts = context.TankStatistics.Where(t => t.playerNo == player.playerNo && t.tankId == tank.tankNo).FirstOrDefault();
                            if (ts != null)
                            {
                                tsl.Add(ts);
                            }
                        }
                    }
                }
                ordered = tsl.OrderByDescending(x => x.avgExperiencePerBattle).ToList();
                HttpContext.Session.Add("tanksStatistics", tsl);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(View(ordered));
        }
 public TankStatisticsViewModel(TankStatistics stats, ExpectedValues expectedValues, ExpectedValues comparisonExpectedValues)
     : base(stats.Statistics)
 {
     TankStatistics           = stats;
     ExpectedValues           = expectedValues;
     ComparisonExpectedValues = comparisonExpectedValues;
 }
        protected virtual void AddTanksStats(List <Player> clanPlayers) //add update tanks stats
        {
            try
            {
                context = new ApplicationDbContext();
                List <Tank>   tList     = context.Tanks.ToList();
                List <string> tanksList = context.Tanks.ToList().Select(t => t.tankNo).ToList();
                foreach (Player newPlayer in clanPlayers)
                {
                    int queriedTanks = 0;
                    List <TankStatistics> newTanksStatisticList    = new List <TankStatistics>();
                    List <TankStatistics> updateTanksStatisticList = new List <TankStatistics>();
                    int listLength = 100;
                    while (tanksList.Count() > queriedTanks)
                    {
                        try
                        {
                            List <string> tanksSublist = tanksList.GetRange(queriedTanks, listLength); //selecting max of 100 tanks per query
                            string        tanks        = string.Join(",", tanksSublist);

                            using (var webClient = new System.Net.WebClient())
                            {
                                string  statsUrl          = "https://api.worldoftanks.eu/wot/tanks/stats/?application_id=9d3d88ea7bec100a6a1c71edc0e12416&account_id=" + newPlayer.playerNo + "&tank_id=" + tanks;
                                string  playerStats       = webClient.DownloadString(statsUrl);
                                JObject playerStatsJObj   = JObject.Parse(playerStats);
                                JArray  playerStatsJArray = playerStatsJObj["data"][newPlayer.playerNo] as JArray;
                                if (playerStatsJArray != null)
                                {
                                    foreach (JObject tankStatsObj in playerStatsJArray)
                                    {
                                        TankStatistics tankStatisctic = new TankStatistics();
                                        tankStatisctic.tankId                 = tankStatsObj["tank_id"].ToString();
                                        tankStatisctic.tank                   = tList.Where(t => t.tankNo == tankStatisctic.tankId).Select(t => t.tankName).FirstOrDefault();
                                        tankStatisctic.playerNo               = newPlayer.playerNo;
                                        tankStatisctic.lastUpdate             = DateTime.Now;
                                        tankStatisctic.damageDealt            = (int)tankStatsObj["all"]["damage_dealt"];
                                        tankStatisctic.battlesCount           = (int)tankStatsObj["all"]["battles"];
                                        tankStatisctic.avgExperiencePerBattle = (int)tankStatsObj["all"]["battle_avg_xp"];
                                        tankStatisctic.spotted                = (int)tankStatsObj["all"]["spotted"];
                                        tankStatisctic.avgDamageBlocked       = (int)tankStatsObj["all"]["avg_damage_blocked"];
                                        int    battlesWon     = (int)tankStatsObj["all"]["wins"];
                                        double winningPercent = 0;
                                        tankStatisctic.playerNickName = newPlayer.playerNickName;
                                        if (tankStatisctic.battlesCount != 0)
                                        {
                                            tankStatisctic.avgDamagePerBattle = (int)((double)(tankStatisctic.damageDealt / tankStatisctic.battlesCount));
                                            tankStatisctic.spotPerBattle      = Math.Round((double)tankStatisctic.spotted / tankStatisctic.battlesCount, 2);
                                            winningPercent = (double)battlesWon / tankStatisctic.battlesCount * 100;
                                        }
                                        else
                                        {
                                            tankStatisctic.avgDamagePerBattle = 0;
                                            tankStatisctic.spotPerBattle      = 0;
                                        }
                                        tankStatisctic.winningPercent = Convert.ToInt32(winningPercent);
                                        TankStatistics dbTankPlayerStats = context.TankStatistics.Where(p => p.playerNo == newPlayer.playerNo && p.tankId == tankStatisctic.tankId).FirstOrDefault(); //check if player stats are in table - for exaple player was already in different clan
                                        if (dbTankPlayerStats == null)                                                                                                                                //new player or tank without stats in db - if player has stats update below
                                        {
                                            context.TankStatistics.Add(tankStatisctic);
                                            context.SaveChanges();
                                        }
                                        else  //update stats for players who have stats in db already
                                        {
                                            tankStatisctic.id = dbTankPlayerStats.id;
                                            context.Entry(dbTankPlayerStats).CurrentValues.SetValues(tankStatisctic);
                                            context.SaveChanges();
                                        }
                                    }
                                }
                                queriedTanks += 100;
                                if (queriedTanks > tanksList.Count() - 100)   //only 100 responses per page
                                {
                                    listLength = tanksList.Count() - queriedTanks;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                    }
                }
                context.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }