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 string Player { get; set; }
public int Point { get; set; }
public int TournamendId { get; set; }
}
}

View File

@@ -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,28 +188,8 @@ namespace LaDOSE.DesktopApp.ViewModels
};
if (sfDialog.ShowDialog() == true)
{
var computed = new Dictionary<string, Dictionary<int, int>>();
;
var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList();
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);
}
}
}
var computed = ResultsToDataDictionary(resultsParticipents);
StringBuilder sb = new StringBuilder();
@@ -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<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>
<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">
<TextBlock> Total :</TextBlock>
@@ -107,6 +114,9 @@
</ListView>
</DockPanel>
</TabItem>
</TabControl>
<Button Grid.Row="3" Grid.ColumnSpan="3" x:Name="Export">Export</Button>
</Grid>

View File

@@ -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; }

View File

@@ -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<Rules> TournamentRules = new List<Rules>()
{
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<List<Tournament>> GetTournaments(DateTime? start, DateTime? end)
{
List<WPUser> wpUsers = _context.WPUser.ToList();
@@ -63,6 +64,7 @@ namespace LaDOSE.Business.Service
List<Participent> participents = await _challongeProvider.GetParticipents(tournament.Id);
tournament.Participents = participents;
}
return tournaments;
}
@@ -81,15 +83,15 @@ 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;
@@ -100,9 +102,6 @@ namespace LaDOSE.Business.Service
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();
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,currentRule.FirstPoint));
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)));
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));
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)));
//
}
result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name==game1.Name).ToList();
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();
return result;
}