From 3d7307192514d781db10b390db18c31849c7ac9c Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Thu, 30 May 2019 22:06:02 +0200 Subject: [PATCH] Add Tournament in Result DTO Add a SpreadSheet for result + Fixes --- LaDOSE.Src/LaDOSE.DTO/TournamentDTO.cs | 1 + .../ViewModels/TournamentResultViewModel.cs | 123 +++++++++++++----- .../Views/TournamentResultView.xaml | 48 ++++--- .../Challonge/TournamentsResult.cs | 10 +- .../Service/TournamentService.cs | 67 ++++++---- 5 files changed, 172 insertions(+), 77 deletions(-) diff --git a/LaDOSE.Src/LaDOSE.DTO/TournamentDTO.cs b/LaDOSE.Src/LaDOSE.DTO/TournamentDTO.cs index ac5686e..f5a9d20 100644 --- a/LaDOSE.Src/LaDOSE.DTO/TournamentDTO.cs +++ b/LaDOSE.Src/LaDOSE.DTO/TournamentDTO.cs @@ -31,5 +31,6 @@ namespace LaDOSE.DTO public int GameId { get; set; } public string Player { get; set; } public int Point { get; set; } + public int TournamendId { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs index 58a66c4..b735f5d 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs @@ -2,9 +2,11 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Data; using System.IO; using System.Linq; using System.Text; +using System.Windows.Controls; using System.Windows.Forms; using LaDOSE.DesktopApp.Utils; using LaDOSE.DTO; @@ -82,6 +84,7 @@ namespace LaDOSE.DesktopApp.ViewModels } private String _first; + private DataTable _gridDataTable; public String First { @@ -103,14 +106,69 @@ namespace LaDOSE.DesktopApp.ViewModels { var tournamentDtos = this.RestService.GetTournaments().ToList(); this.Tournaments = tournamentDtos; + NotifyOfPropertyChange("Tournaments"); } + public DataTable GridDataTable + { + get => _gridDataTable; + set + { + _gridDataTable = value; + NotifyOfPropertyChange(() => GridDataTable); + } + } + public void Select() { var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList(); var resultsDto = this.RestService.GetResults(tournamentsIds); this.Results = resultsDto; + ComputeDataGrid(); + } + + private void ComputeDataGrid() + { + var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList(); + var computed = ResultsToDataDictionary(resultsParticipents); + + StringBuilder sb = new StringBuilder(); + + DataTable grid = new DataTable(); + grid.Columns.Add("Players"); + Results.Games.ForEach(e => grid.Columns.Add(e.Name.Replace('.',' '))); + grid.Columns.Add("Total"); + + + for (int i = 0; i < resultsParticipents.Count; i++) + { + var dataRow = grid.Rows.Add(); + var resultsParticipent = resultsParticipents[i]; + int total = 0; + dataRow["Players"] = 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]; + if (dictionary.ContainsKey(resultsGame.Id)) + { + int points = dictionary[resultsGame.Id]; + dataRow[resultsGame.Name.Replace('.', ' ')] = points.ToString(); + total += points; + } + + else + dataRow[resultsGame.Name.Replace('.', ' ')] = null; + } + + dataRow["Total"] = total.ToString(); + } + + this.GridDataTable = grid; } public void Export() @@ -119,10 +177,10 @@ namespace LaDOSE.DesktopApp.ViewModels return; - ComputeTable(); + ExportToCSV(); } - private void ComputeTable() + private void ExportToCSV() { SaveFileDialog sfDialog = new SaveFileDialog() { @@ -130,32 +188,12 @@ namespace LaDOSE.DesktopApp.ViewModels }; 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); - } - } - } + var computed = ResultsToDataDictionary(resultsParticipents); StringBuilder sb = new StringBuilder(); - - + + sb.AppendLine(Results.Games.Aggregate("Player;", (current, t) => current + (t.Name + ";"))); for (int i = 0; i < resultsParticipents.Count; i++) @@ -164,7 +202,7 @@ namespace LaDOSE.DesktopApp.ViewModels var resultsParticipent = resultsParticipents[i]; - entry= resultsParticipent.Name+";"; + entry = resultsParticipent.Name + ";"; var gameDtos = Results.Games.Distinct().ToList(); for (int j = 0; j < gameDtos.Count; j++) { @@ -173,14 +211,12 @@ namespace LaDOSE.DesktopApp.ViewModels 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"; @@ -209,8 +245,37 @@ namespace LaDOSE.DesktopApp.ViewModels //} //Save - File.WriteAllText(sfDialog.FileName,sb.ToString()); + File.WriteAllText(sfDialog.FileName, sb.ToString()); } } + + 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 = 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; + } } } \ 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 86f8b24..5134c18 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml @@ -87,26 +87,36 @@ - - - - Total : - - - - - - - - - - - + + + + + + + + - - - + + Total : + + + + + + + + + + + + + + + + + + diff --git a/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs b/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs index 726e5af..9b4336c 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs @@ -13,18 +13,26 @@ namespace LaDOSE.Entity.Challonge public class Result { - public Result(string player, int gameId, int point) + public Result(string player, int gameId, int point) : this(player, gameId, 0, point) + { + + } + + public Result(string player, int gameId, int tournamentdId, int point) { Player = player; GameId = gameId; Point = point; + TournamentdId = tournamentdId; } + public Result() { } + public int TournamentdId { get; set; } public int GameId { get; set; } public string Player { get; set; } public int Point { get; set; } diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs index 3565247..5331cd6 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs @@ -24,7 +24,8 @@ namespace LaDOSE.Business.Service public int Participation => 1; - public Rules(int playerMin, int playerMax, int firstPoint, int secondPoint, int thirdFourthPoint, int top8Point, int top16Point) + public Rules(int playerMin, int playerMax, int firstPoint, int secondPoint, int thirdFourthPoint, + int top8Point, int top16Point) { PlayerMin = playerMin; PlayerMax = playerMax; @@ -33,7 +34,6 @@ namespace LaDOSE.Business.Service ThirdFourthPoint = thirdFourthPoint; Top8Point = top8Point; Top16Point = top16Point; - } } @@ -42,10 +42,10 @@ namespace LaDOSE.Business.Service private List TournamentRules = new List() { - new Rules(0,8,5,3,2,0,0), - new Rules(8,16,8,5,3,2,0), - new Rules(16,32,12,8,5,3,2), - new Rules(32,Int32.MaxValue,18,12,8,5,3), + new Rules(0, 8, 5, 3, 2, 0, 0), + new Rules(8, 16, 8, 5, 3, 2, 0), + new Rules(16, 32, 12, 8, 5, 3, 2), + new Rules(32, Int32.MaxValue, 18, 12, 8, 5, 3), }; public TournamentService(LaDOSEDbContext context, IChallongeProvider challongeProvider) @@ -53,6 +53,7 @@ namespace LaDOSE.Business.Service this._context = context; this._challongeProvider = challongeProvider; } + public async Task> GetTournaments(DateTime? start, DateTime? end) { List wpUsers = _context.WPUser.ToList(); @@ -63,6 +64,7 @@ namespace LaDOSE.Business.Service List participents = await _challongeProvider.GetParticipents(tournament.Id); tournament.Participents = participents; } + return tournaments; } @@ -80,29 +82,26 @@ namespace LaDOSE.Business.Service var games = _context.Game.ToList(); var players = _context.WPUser.ToList(); - - var allParticipent = tournaments.SelectMany(e => e.Participents).Distinct((a, b) => a.Name == b.Name).ToList(); + + var allParticipent = tournaments.SelectMany(e => e.Participents).Distinct((a, b) => a.Name == b.Name) + .ToList(); foreach (var participent in allParticipent) { var player = players.FirstOrDefault(e => e.Name.Contains(participent.Name)); - if (player!=null) + if (player != null) { participent.IsMember = true; } - } result.Participents = allParticipent; - + foreach (var tournament in tournaments) { var game = games.First(g => tournament.Name.Contains(g.Name)); tournament.Game = game; var playerCount = tournament.Participents.Count; - //Suppression des forfait - - var lesSacs = tournament.Participents; var currentRule = TournamentRules.FirstOrDefault(rules => rules.PlayerMin < playerCount && rules.PlayerMax >= playerCount @@ -115,26 +114,38 @@ namespace LaDOSE.Business.Service var first = tournament.Participents.First(p => p.Rank == 1); var second = tournament.Participents.First(p => p.Rank == 2); var thirdFourth = tournament.Participents.Where(p => p.Rank == 3 || p.Rank == 4).ToList(); - var Top8 = tournament.Participents.Where(p => p.Rank >4 && p.Rank<9).ToList(); - var Top16 = tournament.Participents.Where(p => p.Rank >9 && p.Rank<=16).ToList(); - - result.Results.Add(new Result(first.Name,tournament.Game.Id,currentRule.FirstPoint)); + var Top8 = tournament.Participents.Where(p => p.Rank > 4 && p.Rank < 9).ToList(); + var Top16 = tournament.Participents.Where(p => p.Rank > 8 && p.Rank <= 16).ToList(); + + result.Results.Add(new Result(first.Name, tournament.Game.Id, tournament.Id, currentRule.FirstPoint)); lesSacs.Remove(first); - result.Results.Add(new Result(second.Name,tournament.Game.Id,currentRule.SecondPoint)); + result.Results.Add(new Result(second.Name, tournament.Game.Id, tournament.Id, currentRule.SecondPoint)); lesSacs.Remove(second); - thirdFourth.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.ThirdFourthPoint))); - thirdFourth.ForEach(p=>lesSacs.Remove(p)); - Top8.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Top8Point))); - Top8.ForEach(p => lesSacs.Remove(p)); - Top16.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Top16Point))); - Top16.ForEach(p => lesSacs.Remove(p)); - lesSacs.ForEach(r => result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Participation))); - // + thirdFourth.ForEach(r => + result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.Id, + currentRule.ThirdFourthPoint))); + thirdFourth.ForEach(p => lesSacs.Remove(p)); + if (currentRule.Top8Point != 0) + { + Top8.ForEach(r => + result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.Id, currentRule.Top8Point))); + Top8.ForEach(p => lesSacs.Remove(p)); + } + if (currentRule.Top16Point != 0) + { + Top16.ForEach(r => + result.Results.Add( + new Result(r.Name, tournament.Game.Id, tournament.Id, currentRule.Top16Point))); + Top16.ForEach(p => lesSacs.Remove(p)); + } + lesSacs.ForEach(r => + result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.Id, + currentRule.Participation))); } - result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name==game1.Name).ToList(); + result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name == game1.Name).ToList(); return result; }