Commit fa036177 authored by Tomáš Strachoň's avatar Tomáš Strachoň
Browse files

feat: add results export

parent 46717a8d
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -26,7 +26,17 @@ namespace TournamentManager.Managers
            using var db = new TournamentManagerDbContext();
            return await db.TournamentTeams.Where(tt => tt.IdTournament == idTournament && tt.IdTeam != Team.IdDummy).Join(
                db.Team, tournamentTeam => tournamentTeam.IdTeam, team => team.Id, (tournamentTeam, team) => new Result {
                    Name = team.Name, Points = tournamentTeam.Points, Place = tournamentTeam.Place, Group = tournamentTeam.Group}).OrderBy(tt => tt.Place).ToListAsync();
                    Name = team.Name, Points = tournamentTeam.Points, Place = tournamentTeam.Place,
                    Group = tournamentTeam.Group}).OrderBy(tt => tt.Place).ToListAsync();
        }

        public static List<Result> GetResults(List<TournamentTeam> teams)
        {
            using var db = new TournamentManagerDbContext();
            return [.. teams.Join(
                db.Team, tournamentTeam => tournamentTeam.IdTeam, team => team.Id, (tournamentTeam, team) => new Result {
                    Name = team.Name, Points = tournamentTeam.Points,Place = tournamentTeam.Place,
                    Group = tournamentTeam.Group }).OrderBy(tt => tt.Place)];
        }

        public static async Task<List<Team>> GetTeams(Tournament? tournament, bool signed)
+82 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -24,6 +25,9 @@ namespace TournamentManager.ViewModels

        public AsyncCommand DeleteCommand { get; set; }

        public AsyncCommand ExportResultsCommand { get; set; }
        public AsyncCommand ExportMatchesCommand { get; set; }

        public int minRound = 0;

        public TournamentsViewModel()
@@ -31,6 +35,8 @@ namespace TournamentManager.ViewModels
            ShowWindowCommand = new AsyncCommand(ShowWindow, _ => true);
            DeleteCommand = new AsyncCommand(Delete, _ => true);
            ShowMatchEditWindowCommand = new RelayCommand(ShowMatchEditWindow, CanShowMatchEditWindow);
            ExportResultsCommand = new AsyncCommand(ExportResults, _ => true);
            ExportMatchesCommand = new AsyncCommand(ExportMatches, _ => true);

            MainWindow.ViewChangedEvent += ViewChangedEventHandler;
        }
@@ -98,5 +104,81 @@ namespace TournamentManager.ViewModels
                }
            }
        }

        private async Task ExportResults(object? obj)
        {
            var tournamentsList = obj as ListView;

            if (tournamentsList?.SelectedItem is Tournament selected)
            {
                using StreamWriter file = File.CreateText("results.txt");
                {
                    file.WriteLine(selected.Title + '\n');

                    await WriteGroupResults(file, selected, TournamentTeamGroup.A);
                    await WriteGroupResults(file, selected, TournamentTeamGroup.B);

                    file.WriteLine("\nOverall standings");
                    WriteResults(file, await TournamentTeamManager.GetResults(selected.Id));
                }

                MessageBox.Show($"Results have been exported to {Path.GetFullPath("results.txt")}",
                    "Export results", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private static async Task WriteGroupResults(StreamWriter file, Tournament tournament, TournamentTeamGroup group)
        {
            if (tournament.Strategy != TournamentStrategy.Groups)
            {
                return;
            }

            List<TournamentTeam> teams = await TournamentTeamManager.GetAll(tournament.Id);
            List<Match> matches = await MatchManager.GetAll(tournament.Id);

            teams = [.. teams.Where(tt => tt.Group == group)];
            Dictionary<int, TournamentTeam> dict = [];

            foreach (var team in teams)
            {
                dict.Add(team.IdTeam, team);
            }

            matches = [.. matches.Where(m => m.Round == 0 && dict.ContainsKey(m.IdTeamGuest))];

            foreach (var match in matches)
            {
                var points = MatchManager.GetPoints(match.GoalsHost, match.GoalsGuest, match.End);

                dict[match.IdTeamHost].Points += points.Item1;
                dict[match.IdTeamGuest].Points += points.Item2;
            }

            TournamentTeamManager.UpdateStandings(teams);

            file.WriteLine("\nGroup " + group);
            WriteResults(file, TournamentTeamManager.GetResults(teams));
        }

        private static void WriteResults(StreamWriter file, List<Result> results)
        {
            file.WriteLine("------------------------------------------------------------------");
            foreach (var result in results)
            {
                file.WriteLine(string.Format("| {0,2} | {1,4} | {2,-50} |", result.Place, result.Points, result.Name));
            }
            file.WriteLine("------------------------------------------------------------------");
        }

        private async Task ExportMatches(object? obj)
        {
            var tournamentsList = obj as ListView;

            if (tournamentsList?.SelectedItem is Tournament selected)
            {
                throw new NotImplementedException();
            }
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -74,8 +74,8 @@
                        </GridView>
                    </ListView.View>
                </ListView>
                <Button Name="ButtonExportResults" Content="📥 Export results" Grid.Row="2" IsEnabled="False" Background="#7ae7c7" Height="19" Width="160" VerticalAlignment="Bottom"/>
                <Button Name="ButtonExportMatches" Content="📥 Export matches" Grid.Row="2" Grid.Column="1" IsEnabled="False" Background="#7ae7c7" Height="19" Width="160" VerticalAlignment="Bottom"/>
                <Button Name="ButtonExportResults" Command="{Binding ExportResultsCommand, IsAsync=True}" CommandParameter="{x:Reference Name=TournamentsList}" Content="📥 Export results" Grid.Row="2" IsEnabled="False" Background="#7ae7c7" Height="19" Width="160" VerticalAlignment="Bottom"/>
                <Button Name="ButtonExportMatches" Command="{Binding ExportMatchesCommand, IsAsync=True}" CommandParameter="{x:Reference Name=TournamentsList}" Content="📥 Export matches" Grid.Row="2" Grid.Column="1" IsEnabled="False" Background="#7ae7c7" Height="19" Width="160" VerticalAlignment="Bottom"/>
            </Grid>
        </Grid>