From 0e08bec6f33bbcdbd53c7947faf5a59372cba971 Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Thu, 30 May 2019 03:41:24 +0200 Subject: [PATCH] Add a CSV Export of Selected Tournament (can stack multiple rankings but the UI get messy) --- .../ViewModels/TournamentResultViewModel.cs | 122 ++++++++++++++++-- .../Views/TournamentResultView.xaml | 10 +- .../Service/TournamentService.cs | 2 +- 3 files changed, 122 insertions(+), 12 deletions(-) diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs index ed44a9d..58a66c4 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs @@ -1,16 +1,19 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.IO; using System.Linq; -using Caliburn.Micro; +using System.Text; +using System.Windows.Forms; using LaDOSE.DesktopApp.Utils; using LaDOSE.DTO; using LaDOSE.REST; +using SaveFileDialog = Microsoft.Win32.SaveFileDialog; +using Screen = Caliburn.Micro.Screen; namespace LaDOSE.DesktopApp.ViewModels { - - public class TournamentResultViewModel : Screen { public override string DisplayName => "Tournament Result"; @@ -22,9 +25,8 @@ namespace LaDOSE.DesktopApp.ViewModels this.RestService = restService; _selectedTournaments = new ObservableCollection(); Tournaments = new List(); - } - + private TournamentsResultDTO _results; public List Tournaments { get; set; } @@ -37,7 +39,9 @@ namespace LaDOSE.DesktopApp.ViewModels NotifyOfPropertyChange(() => Results); } } + private ObservableCollection _selectedTournaments; + public ObservableCollection SelectedTournaments { get { return _selectedTournaments; } @@ -59,12 +63,14 @@ namespace LaDOSE.DesktopApp.ViewModels _selectedGame = value; //TODO: QUICK AND DIRTY List resultForGame = this.Results.Results.Where(e => e.GameId == SelectedGame.Id).ToList(); - First = resultForGame.OrderByDescending(e=>e.Point).First().Player; + First = resultForGame.OrderByDescending(e => e.Point).First().Player; SelectedGameResult = new ObservableCollection(resultForGame); NotifyOfPropertyChange(() => SelectedGame); } } + private ObservableCollection _selectedGameResult; + public ObservableCollection SelectedGameResult { get { return _selectedGameResult; } @@ -76,6 +82,7 @@ namespace LaDOSE.DesktopApp.ViewModels } private String _first; + public String First { get { return _first; } @@ -85,6 +92,7 @@ namespace LaDOSE.DesktopApp.ViewModels NotifyOfPropertyChange(() => First); } } + protected override void OnInitialize() { LoadTournaments(); @@ -103,10 +111,106 @@ namespace LaDOSE.DesktopApp.ViewModels var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList(); var resultsDto = this.RestService.GetResults(tournamentsIds); this.Results = resultsDto; - } - - } + public void Export() + { + if (this.Results == null) + return; + + ComputeTable(); + } + + private void ComputeTable() + { + SaveFileDialog sfDialog = new SaveFileDialog() + { + AddExtension = true + }; + if (sfDialog.ShowDialog() == true) + { + var computed = new Dictionary>(); + ; + var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList(); + foreach (var participent in resultsParticipents) + { + computed.Add(participent.Name, new Dictionary()); + } + + foreach (var game in Results.Games) + { + var results = this.Results.Results.Where(e => e.GameId == game.Id).ToList(); + foreach (var result in results) + { + var dictionary = computed[result.Player]; + if (dictionary.ContainsKey(result.GameId)) + dictionary[game.Id] += result.Point; + else + { + dictionary.Add(game.Id, result.Point); + } + } + } + + StringBuilder sb = new StringBuilder(); + + + sb.AppendLine(Results.Games.Aggregate("Player;", (current, t) => current + (t.Name + ";"))); + + for (int i = 0; i < resultsParticipents.Count; i++) + { + var entry = ""; + + var resultsParticipent = resultsParticipents[i]; + + entry= resultsParticipent.Name+";"; + var gameDtos = Results.Games.Distinct().ToList(); + for (int j = 0; j < gameDtos.Count; j++) + { + var resultsGame = Results.Games[j]; + var dictionary = computed[resultsParticipent.Name]; + entry += dictionary.ContainsKey(resultsGame.Id) + ? dictionary[resultsGame.Id].ToString() + ";" + : ";"; + + } + + sb.AppendLine(entry); + } + + + + //string[][] resultCsv = new string[resultsParticipents.Count + 1][]; + //resultCsv[0] = new string[Results.Games.Count + 1]; + //resultCsv[0][0] = "Player"; + //for (int j = 0; j < Results.Games.Count; j++) + //{ + // resultCsv[0][j + 1] = Results.Games[j].Name; + //} + + //for (int i = 0; i < resultsParticipents.Count; i++) + //{ + // resultCsv[i + 1] = new string[Results.Games.Count + 1]; + + // var resultsParticipent = resultsParticipents[i]; + + // resultCsv[i + 1][0] = resultsParticipent.Name; + // for (int j = 0; j < Results.Games.Count; j++) + // { + // var resultsGame = Results.Games[j]; + // var dictionary = computed[resultsParticipent.Name]; + // if (dictionary.ContainsKey(resultsGame.Id)) + // { + // var i1 = dictionary[resultsGame.Id]; + // resultCsv[i + 1][j + 1] = i1.ToString(); + // } + // } + //} + + //Save + File.WriteAllText(sfDialog.FileName,sb.ToString()); + } + } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml index 8175c4e..86f8b24 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml @@ -42,6 +42,8 @@ + + @@ -49,6 +51,8 @@ + + Jeux : @@ -90,7 +94,7 @@ + IsTextSearchEnabled="True" TextSearch.TextPath="Name" DockPanel.Dock="Top"> @@ -101,8 +105,10 @@ + - + + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs index 8f7d052..3565247 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs @@ -134,7 +134,7 @@ namespace LaDOSE.Business.Service } - result.Games = tournaments.Select(e => e.Game).ToList(); + result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name==game1.Name).ToList(); return result; }