Ejemplo n.º 1
0
        public void SaveGraph(NCHLTeam team)
        {
            try
            {
                foreach (PlayerPosition playPos in Enum.GetValues(typeof(PlayerPosition)))
                {
                    using (StreamWriter sw = new StreamWriter($"Graphs\\Graph{team}-{playPos}.txt"))
                    {
                        StringBuilder            sb            = new StringBuilder();
                        Dictionary <int, Player> playersToScan = new Dictionary <int, Player>();

                        // 1- Get players to scan
                        foreach (KeyValuePair <int, List <Player> > p in PlayersForWeek)
                        {
                            foreach (Player player in p.Value)
                            {
                                if (player.NCHLTeam == team && player.Pos == playPos && !playersToScan.ContainsKey(player.Id))
                                {
                                    playersToScan.Add(player.Id, player);
                                }
                            }
                        }

                        List <Tuple <double, string> > playSorting = new List <Tuple <double, string> >();

                        // 2- Get their stats
                        foreach (KeyValuePair <int, Player> p in playersToScan)
                        {
                            Player playerPos = Players.FirstOrDefault(pp => pp.Id == p.Key);
                            if (playerPos != null && (playerPos.Pos != playPos || playerPos.NCHLTeam != team))
                            {
                                continue;
                            }

                            string currentPlayerName = p.Value.Name;
                            int    currentPlayerId   = p.Key;
                            Dictionary <int, double> playerStatForWeek = new Dictionary <int, double>();

                            foreach (KeyValuePair <int, List <Player> > pfw in PlayersForWeek)
                            {
                                Player player    = pfw.Value.FirstOrDefault(play => play.Id == currentPlayerId);
                                double pctSystem = -0.01;
                                if (player != null)
                                {
                                    if (playPos == PlayerPosition.G)
                                    {
                                        pctSystem = player.TOI;
                                    }
                                    else
                                    {
                                        pctSystem = player.PctSystem;
                                    }
                                }

                                playerStatForWeek.Add(pfw.Key, pctSystem);
                            }


                            // 3- Get last 3 weeks stats average

                            playerStatForWeek.TryGetValue(playerStatForWeek.Count, out double thisWeekPlayerStat);
                            playerStatForWeek.TryGetValue(playerStatForWeek.Count - 1, out double oneWeekAgoPlayerStat);
                            playerStatForWeek.TryGetValue(playerStatForWeek.Count - 2, out double twoWeeksAgoPlayerStat);
                            playerStatForWeek.TryGetValue(playerStatForWeek.Count - 3, out double threeWeeksAgoPlayerStat);
                            playerStatForWeek.TryGetValue(playerStatForWeek.Count - 4, out double fourWeeksAgoPlayerStat);
                            playerStatForWeek.TryGetValue(playerStatForWeek.Count - 5, out double fiveWeeksAgoPlayerStat);



                            double ThreeWeekAvg = (thisWeekPlayerStat + oneWeekAgoPlayerStat + twoWeeksAgoPlayerStat) / (playerStatForWeek.Count < 3 ? playerStatForWeek.Count : 3);


                            double ThreeWeekStdVar = 0;
                            if (playerStatForWeek.Count > 1)
                            {
                                ThreeWeekStdVar = Math.Sqrt((Math.Pow(thisWeekPlayerStat - ThreeWeekAvg, 2) +
                                                             Math.Pow(oneWeekAgoPlayerStat - ThreeWeekAvg, 2) +
                                                             Math.Pow(twoWeeksAgoPlayerStat - ThreeWeekAvg, 2)) / ((playerStatForWeek.Count < 3 ? playerStatForWeek.Count : 3) - 1));
                            }

                            double SixWeekAvg = (thisWeekPlayerStat +
                                                 oneWeekAgoPlayerStat +
                                                 twoWeeksAgoPlayerStat +
                                                 threeWeeksAgoPlayerStat +
                                                 fourWeeksAgoPlayerStat +
                                                 fiveWeeksAgoPlayerStat) / (playerStatForWeek.Count < 6 ? playerStatForWeek.Count : 6);

                            double SixWeekStdVar = 0;

                            if (playerStatForWeek.Count > 1)
                            {
                                SixWeekStdVar = Math.Sqrt((Math.Pow(thisWeekPlayerStat - SixWeekAvg, 2) +
                                                           Math.Pow(oneWeekAgoPlayerStat - SixWeekAvg, 2) +
                                                           Math.Pow(twoWeeksAgoPlayerStat - SixWeekAvg, 2) +
                                                           Math.Pow(threeWeeksAgoPlayerStat - SixWeekAvg, 2) +
                                                           Math.Pow(fourWeeksAgoPlayerStat - SixWeekAvg, 2) +
                                                           Math.Pow(fiveWeeksAgoPlayerStat - SixWeekAvg, 2)) / ((playerStatForWeek.Count < 6 ? playerStatForWeek.Count : 6) - 1));
                            }


                            string displayedTitle = $"{currentPlayerName}\nLast three weeks average: {(int)ThreeWeekAvg} std.var: {(int)ThreeWeekStdVar}\nLast six weeks average: {(int)SixWeekAvg} std.var: {(int)SixWeekStdVar}\n";


                            // 4- Generate player graph
                            string graph;
                            if (playPos == PlayerPosition.G)
                            {
                                graph = BarGraph.Generate(playerStatForWeek, displayedTitle, 1, playerStatForWeek.Count, 1, 0, 300, 30);
                            }
                            else
                            {
                                graph = BarGraph.Generate(playerStatForWeek, displayedTitle, 1, playerStatForWeek.Count, 1, 0, 100, 10);
                            }

                            playSorting.Add(new Tuple <double, string>(ThreeWeekAvg, graph));
                        }

                        // 5- Sort stat from best to worst
                        List <Tuple <double, string> > orderedList = playSorting.OrderByDescending(playSort => playSort.Item1).ToList();

                        foreach (Tuple <double, string> tp in orderedList)
                        {
                            sb.Append(tp.Item2);
                        }

                        sw.WriteLine(sb.ToString());
                    }
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 2
0
        public void SaveReportData(NCHLTeam team, bool isWeekStats)
        {
            try
            {
                string fileName;
                if (isWeekStats)
                {
                    fileName = $"WeekReports\\Week{CurrentWeek}Report{team.ToString()}.xml";
                }
                else
                {
                    fileName = $"SeasonReports\\SeasonReport{team.ToString()}.xml";
                }

                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    foreach (PlayerPosition pos in Enum.GetValues(typeof(PlayerPosition)))
                    {
                        StringBuilder sb = new StringBuilder();

                        List <Player> players;
                        if (pos != PlayerPosition.G)
                        {
                            players = Players.Where(p => p.NCHLTeam == team && p.Pos == pos).OrderBy(pl => pl.PctSystem).Reverse().ToList();
                        }
                        else
                        {
                            players = Players.Where(p => p.NCHLTeam == team && p.Pos == pos).OrderBy(pl => pl.TOI).Reverse().ToList();
                        }

                        // Write Position
                        sb.AppendLine($"{pos}");
                        sb.Append("--------------------------------");
                        if (pos != PlayerPosition.G)
                        {
                            sb.Append("[GP|P|PIM|Hits|BkS|TkA|SH|TOIGP]");
                        }
                        sb.AppendLine();

                        if (players.Count > 0)
                        {
                            // Get max string lenght of the player
                            int maxPlayerNameLenght = players.Max(p => p.Name.Length);


                            foreach (Player playerToWriteCSV in players)
                            {
                                // Write Name
                                sb.Append(playerToWriteCSV.Name);

                                // Fill the rest with dots
                                int dotsToWrite = maxPlayerNameLenght - playerToWriteCSV.Name.Length;
                                for (int i = 0; i < dotsToWrite; i++)
                                {
                                    sb.Append(".");
                                }

                                // Create bar
                                string bar = string.Empty;
                                int    maxScore;
                                int    valueToUse;

                                if (pos != PlayerPosition.G)
                                {
                                    maxScore   = 100;
                                    valueToUse = (int)playerToWriteCSV.PctSystem;
                                }
                                else
                                {
                                    maxScore   = players.Max(p => p.TOI);
                                    valueToUse = playerToWriteCSV.TOI;
                                }

                                for (int i = maxScore / 10; i <= maxScore; i += maxScore / 10)
                                {
                                    if (valueToUse >= i)
                                    {
                                        bar += "█";
                                    }
                                    else
                                    {
                                        bar += "░";
                                    }
                                }

                                // Fill gap properly between number and bar
                                int    valueStringLenght = valueToUse.ToString().Length;
                                string gap = string.Empty;
                                for (int i = (4 - valueStringLenght); i > 0; i--)
                                {
                                    gap += " ";
                                }

                                // Show stats at right of player (not goalies)
                                string stats = string.Empty;
                                if (pos != PlayerPosition.G)
                                {
                                    stats = $"{playerToWriteCSV.GP}|{playerToWriteCSV.P}|{playerToWriteCSV.PIM}|{playerToWriteCSV.Hits}|{playerToWriteCSV.BkS}|{playerToWriteCSV.TkA}|{playerToWriteCSV.SH}|{playerToWriteCSV.TOIPG}]";
                                }

                                sb.Append($"{gap}{valueToUse} {bar} {stats}");

                                sb.AppendLine();
                            }

                            sw.WriteLine(sb.ToString());
                        }
                    }
                }
            }
            catch
            {
            }
        }