Add Tournament in Result DTO

Add a SpreadSheet for result
+ Fixes
This commit is contained in:
2019-05-30 22:06:02 +02:00
parent 0e08bec6f3
commit 3d73071925
5 changed files with 172 additions and 77 deletions

View File

@@ -31,5 +31,6 @@ namespace LaDOSE.DTO
public int GameId { get; set; } public int GameId { get; set; }
public string Player { get; set; } public string Player { get; set; }
public int Point { get; set; } public int Point { get; set; }
public int TournamendId { get; set; }
} }
} }

View File

@@ -2,9 +2,11 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Controls;
using System.Windows.Forms; using System.Windows.Forms;
using LaDOSE.DesktopApp.Utils; using LaDOSE.DesktopApp.Utils;
using LaDOSE.DTO; using LaDOSE.DTO;
@@ -82,6 +84,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
private String _first; private String _first;
private DataTable _gridDataTable;
public String First public String First
{ {
@@ -103,14 +106,69 @@ namespace LaDOSE.DesktopApp.ViewModels
{ {
var tournamentDtos = this.RestService.GetTournaments().ToList(); var tournamentDtos = this.RestService.GetTournaments().ToList();
this.Tournaments = tournamentDtos; this.Tournaments = tournamentDtos;
NotifyOfPropertyChange("Tournaments"); NotifyOfPropertyChange("Tournaments");
} }
public DataTable GridDataTable
{
get => _gridDataTable;
set
{
_gridDataTable = value;
NotifyOfPropertyChange(() => GridDataTable);
}
}
public void Select() public void Select()
{ {
var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList(); var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList();
var resultsDto = this.RestService.GetResults(tournamentsIds); var resultsDto = this.RestService.GetResults(tournamentsIds);
this.Results = resultsDto; 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() public void Export()
@@ -119,10 +177,10 @@ namespace LaDOSE.DesktopApp.ViewModels
return; return;
ComputeTable(); ExportToCSV();
} }
private void ComputeTable() private void ExportToCSV()
{ {
SaveFileDialog sfDialog = new SaveFileDialog() SaveFileDialog sfDialog = new SaveFileDialog()
{ {
@@ -130,28 +188,8 @@ namespace LaDOSE.DesktopApp.ViewModels
}; };
if (sfDialog.ShowDialog() == true) if (sfDialog.ShowDialog() == true)
{ {
var computed = new Dictionary<string, Dictionary<int, int>>();
;
var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList(); var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList();
foreach (var participent in resultsParticipents) var computed = ResultsToDataDictionary(resultsParticipents);
{
computed.Add(participent.Name, new Dictionary<int, int>());
}
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(); StringBuilder sb = new StringBuilder();
@@ -173,14 +211,12 @@ namespace LaDOSE.DesktopApp.ViewModels
entry += dictionary.ContainsKey(resultsGame.Id) entry += dictionary.ContainsKey(resultsGame.Id)
? dictionary[resultsGame.Id].ToString() + ";" ? dictionary[resultsGame.Id].ToString() + ";"
: ";"; : ";";
} }
sb.AppendLine(entry); sb.AppendLine(entry);
} }
//string[][] resultCsv = new string[resultsParticipents.Count + 1][]; //string[][] resultCsv = new string[resultsParticipents.Count + 1][];
//resultCsv[0] = new string[Results.Games.Count + 1]; //resultCsv[0] = new string[Results.Games.Count + 1];
//resultCsv[0][0] = "Player"; //resultCsv[0][0] = "Player";
@@ -212,5 +248,34 @@ namespace LaDOSE.DesktopApp.ViewModels
File.WriteAllText(sfDialog.FileName, sb.ToString()); File.WriteAllText(sfDialog.FileName, sb.ToString());
} }
} }
private Dictionary<string, Dictionary<int, int>> ResultsToDataDictionary(
List<ParticipentDTO> resultsParticipents)
{
var computed = new Dictionary<string, Dictionary<int, int>>();
foreach (var participent in resultsParticipents)
{
computed.Add(participent.Name, new Dictionary<int, int>());
}
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;
}
} }
} }

View File

@@ -87,7 +87,14 @@
</ListView> </ListView>
<DockPanel Grid.Row="2" Grid.Column="2" > <TabControl Grid.Row="2" Grid.Column="2" >
<TabItem Header="Result">
<DataGrid ItemsSource="{Binding GridDataTable}" AutoGenerateColumns="True" CanUserAddRows="False" CanUserDeleteRows="False">
</DataGrid>
</TabItem>
<TabItem Header="By Game">
<DockPanel >
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top"> <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<TextBlock> Total :</TextBlock> <TextBlock> Total :</TextBlock>
@@ -107,6 +114,9 @@
</ListView> </ListView>
</DockPanel> </DockPanel>
</TabItem>
</TabControl>
<Button Grid.Row="3" Grid.ColumnSpan="3" x:Name="Export">Export</Button> <Button Grid.Row="3" Grid.ColumnSpan="3" x:Name="Export">Export</Button>
</Grid> </Grid>

View File

@@ -13,18 +13,26 @@ namespace LaDOSE.Entity.Challonge
public class Result 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; Player = player;
GameId = gameId; GameId = gameId;
Point = point; Point = point;
TournamentdId = tournamentdId;
} }
public Result() public Result()
{ {
} }
public int TournamentdId { get; set; }
public int GameId { get; set; } public int GameId { get; set; }
public string Player { get; set; } public string Player { get; set; }
public int Point { get; set; } public int Point { get; set; }

View File

@@ -24,7 +24,8 @@ namespace LaDOSE.Business.Service
public int Participation => 1; 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; PlayerMin = playerMin;
PlayerMax = playerMax; PlayerMax = playerMax;
@@ -33,7 +34,6 @@ namespace LaDOSE.Business.Service
ThirdFourthPoint = thirdFourthPoint; ThirdFourthPoint = thirdFourthPoint;
Top8Point = top8Point; Top8Point = top8Point;
Top16Point = top16Point; Top16Point = top16Point;
} }
} }
@@ -53,6 +53,7 @@ namespace LaDOSE.Business.Service
this._context = context; this._context = context;
this._challongeProvider = challongeProvider; this._challongeProvider = challongeProvider;
} }
public async Task<List<Tournament>> GetTournaments(DateTime? start, DateTime? end) public async Task<List<Tournament>> GetTournaments(DateTime? start, DateTime? end)
{ {
List<WPUser> wpUsers = _context.WPUser.ToList(); List<WPUser> wpUsers = _context.WPUser.ToList();
@@ -63,6 +64,7 @@ namespace LaDOSE.Business.Service
List<Participent> participents = await _challongeProvider.GetParticipents(tournament.Id); List<Participent> participents = await _challongeProvider.GetParticipents(tournament.Id);
tournament.Participents = participents; tournament.Participents = participents;
} }
return tournaments; return tournaments;
} }
@@ -81,7 +83,8 @@ namespace LaDOSE.Business.Service
var games = _context.Game.ToList(); var games = _context.Game.ToList();
var players = _context.WPUser.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) foreach (var participent in allParticipent)
{ {
var player = players.FirstOrDefault(e => e.Name.Contains(participent.Name)); var player = players.FirstOrDefault(e => e.Name.Contains(participent.Name));
@@ -89,7 +92,6 @@ namespace LaDOSE.Business.Service
{ {
participent.IsMember = true; participent.IsMember = true;
} }
} }
result.Participents = allParticipent; result.Participents = allParticipent;
@@ -100,9 +102,6 @@ namespace LaDOSE.Business.Service
tournament.Game = game; tournament.Game = game;
var playerCount = tournament.Participents.Count; var playerCount = tournament.Participents.Count;
//Suppression des forfait
var lesSacs = tournament.Participents; var lesSacs = tournament.Participents;
var currentRule = TournamentRules.FirstOrDefault(rules => var currentRule = TournamentRules.FirstOrDefault(rules =>
rules.PlayerMin < playerCount && rules.PlayerMax >= playerCount rules.PlayerMin < playerCount && rules.PlayerMax >= playerCount
@@ -116,22 +115,34 @@ namespace LaDOSE.Business.Service
var second = tournament.Participents.First(p => p.Rank == 2); var second = tournament.Participents.First(p => p.Rank == 2);
var thirdFourth = tournament.Participents.Where(p => p.Rank == 3 || p.Rank == 4).ToList(); 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 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(); var Top16 = tournament.Participents.Where(p => p.Rank > 8 && p.Rank <= 16).ToList();
result.Results.Add(new Result(first.Name,tournament.Game.Id,currentRule.FirstPoint)); result.Results.Add(new Result(first.Name, tournament.Game.Id, tournament.Id, currentRule.FirstPoint));
lesSacs.Remove(first); 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); lesSacs.Remove(second);
thirdFourth.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.ThirdFourthPoint))); thirdFourth.ForEach(r =>
result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.Id,
currentRule.ThirdFourthPoint)));
thirdFourth.ForEach(p => lesSacs.Remove(p)); thirdFourth.ForEach(p => lesSacs.Remove(p));
Top8.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Top8Point))); 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)); Top8.ForEach(p => lesSacs.Remove(p));
Top16.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Top16Point))); }
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)); Top16.ForEach(p => lesSacs.Remove(p));
lesSacs.ForEach(r => result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Participation))); }
//
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();