From c78ba3cca9dfb88cd5017ca86e7a5668c28fbaae Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Fri, 9 Aug 2019 21:22:07 +0200 Subject: [PATCH] tournament result reworked (UI and code) Check for duplicate (case) in player's name. --- LaDOSE.Src/LaDOSE.Api/Startup.cs | 4 +- .../LaDOSE.DesktopApp.csproj | 1 + .../Utils/CustomEqualityCompare.cs | 28 +++++ .../ViewModels/TournamentResultViewModel.cs | 117 +++++++++++------- .../Views/TournamentResultView.xaml | 16 +-- .../Service/TournamentService.cs | 2 + 6 files changed, 114 insertions(+), 54 deletions(-) create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/Utils/CustomEqualityCompare.cs diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index e5de491..b51c744 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -121,8 +121,8 @@ namespace LaDOSE.Api cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); - cfg.CreateMap(); - cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); cfg.CreateMap(); diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj b/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj index ba8ae53..f464ecd 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj +++ b/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj @@ -120,6 +120,7 @@ + BookingUserControl.xaml diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Utils/CustomEqualityCompare.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/CustomEqualityCompare.cs new file mode 100644 index 0000000..4cd1caa --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/CustomEqualityCompare.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; + +namespace LaDOSE.DesktopApp.Utils +{ + public static class CustomListExtension + { + public sealed class EqualityComparer : IEqualityComparer where T : class + { + private readonly Func _compare; + + public EqualityComparer(Func c) + { + _compare = c; + } + + public bool Equals(T x, T y) + { + return _compare(x, y); + } + + public int GetHashCode(T obj) + { + return 0; + } + } + } +} diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs index 7424aed..d78cb1b 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs @@ -22,7 +22,7 @@ namespace LaDOSE.DesktopApp.ViewModels public override string DisplayName => "Tournament Result"; private RestService RestService { get; set; } - Dictionary> _computedResult; + //Dictionary> _computedResult; #region Properties @@ -234,19 +234,20 @@ namespace LaDOSE.DesktopApp.ViewModels if (selectedTournaments.Count > 0) selectedTournaments.ForEach(e => this.SelectedTournaments.AddUI(e)); } - + //This could be simplified the Dictionary was for a previous usage, but i m too lazy to rewrite it. private void ComputeDataGrid() { - var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList(); - - _computedResult = ResultsToDataDictionary(resultsParticipents); + var resultsParticipents = this.Results.Participents.Select(e=>e.Name).Distinct(new CustomListExtension.EqualityComparer((a, b) => a.ToUpperInvariant()== b.ToUpperInvariant())).OrderBy(e=>e).ToList(); + //At start the dictionnary was for some fancy dataviz things, but since the point are inside + //i m to lazy to rewrite this functions (this is so ugly...) + //_computedResult = ResultsToDataDictionary(resultsParticipents); StringBuilder sb = new StringBuilder(); DataTable grid = new DataTable(); var games = Results.Games.Distinct().OrderBy(e => e.Order).ToList(); grid.Columns.Add("Players"); - games.ForEach(e => grid.Columns.Add(e.Name.Replace('.', ' '))); + games.ForEach(e => grid.Columns.Add(e.Name.Replace('.', ' '),typeof(Int32))); grid.Columns.Add("Total").DataType = typeof(Int32); @@ -255,25 +256,18 @@ namespace LaDOSE.DesktopApp.ViewModels var dataRow = grid.Rows.Add(); var resultsParticipent = resultsParticipents[i]; int total = 0; - dataRow["Players"] = resultsParticipent.Name; + dataRow["Players"] = resultsParticipent; for (int j = 0; j < games.Count; j++) { var resultsGame = Results.Games[j]; - var dictionary = _computedResult[resultsParticipent.Name]; - if (dictionary.ContainsKey(resultsGame.Id)) - { - int points = dictionary[resultsGame.Id]; - dataRow[resultsGame.Name.Replace('.', ' ')] = points; - total += points; - } - - else - dataRow[resultsGame.Name.Replace('.', ' ')] = null; + var points = GetPlayerPoint(resultsParticipent, resultsGame.Id); + dataRow[resultsGame.Name.Replace('.', ' ')] = points!=0?(object) points:DBNull.Value; + total += points; } - dataRow["Total"] = total; + } grid.DefaultView.Sort = "Total DESC"; @@ -362,33 +356,68 @@ namespace LaDOSE.DesktopApp.ViewModels System.Windows.Clipboard.SetText(this.HtmlContent); } - private Dictionary> ResultsToDataDictionary( - List resultsParticipents) + private int GetPlayerPoint(string name, int gameid) { - var computed = new Dictionary>(); - - - 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); - } - } - } - - return computed; + return Results.Results.Where(e => e.GameId == gameid && e.Player.ToUpperInvariant() == name.ToUpperInvariant()).Sum(e=>e.Point); } + + // private Dictionary> ResultsToDataDictionary( + // List resultsParticipents) + // { + // var computed = new Dictionary>(); + + + // foreach (var participent in resultsParticipents) + // { + // computed.Add(participent.Name, new Dictionary()); + // } + + // foreach (var game in Results.Games) + // { + // var results = 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); + // } + // } + // } + + // MergeDuplicates(resultsParticipents, computed); + + // return computed; + // } + + // private static void MergeDuplicates(List resultsParticipents, Dictionary> computed) + // { + // var duplicates = computed.Keys.ToList().GroupBy(x => x.ToUpperInvariant()).Where(x => x.Count() > 1) + // .Select(x => x.Key) + // .ToList(); + // if (duplicates.Count > 0) + // { + // foreach (var duplicate in duplicates) + // { + // var lines = computed.Where(e => e.Key.ToUpperInvariant() == duplicate).ToList(); + // for (int i = lines.Count(); i > 1; --i) + // { + // var result = lines[--i]; + // foreach (var games in result.Value.Keys) + // { + // if (lines[0].Value.ContainsKey(games)) + // lines[0].Value[games] += result.Value[games]; + // else + // lines[0].Value.Add(games, result.Value[games]); + // } + + // computed.Remove(result.Key); + // resultsParticipents.Remove(resultsParticipents.First(e => e.Name == result.Key)); + // } + // } + // } + // } } } \ 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 45be217..a795d3d 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml @@ -62,9 +62,7 @@ - - - + - - - - - @@ -97,7 +91,13 @@ - + + + + + + + diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs index e9bfd3e..844957d 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs @@ -144,10 +144,12 @@ namespace LaDOSE.Business.Service return result; } + /// /// Check if the tournament exist in database otherwise call Challonge. /// /// tournaments ids + /// List of known games /// List of the challonge's tournament with participents private async Task> GetChallongeTournaments(List ids, List games) {