tournament result reworked (UI and code)

Check for duplicate (case) in player's name.
This commit is contained in:
2019-08-09 21:22:07 +02:00
parent a9f3da1b8e
commit c78ba3cca9
6 changed files with 114 additions and 54 deletions

View File

@@ -121,8 +121,8 @@ namespace LaDOSE.Api
cfg.CreateMap<WPEvent, LaDOSE.DTO.WPEventDTO>(); cfg.CreateMap<WPEvent, LaDOSE.DTO.WPEventDTO>();
cfg.CreateMap<Result, LaDOSE.DTO.ResultDTO>(); cfg.CreateMap<Result, LaDOSE.DTO.ResultDTO>();
cfg.CreateMap<TournamentsResult, LaDOSE.DTO.TournamentsResultDTO>(); cfg.CreateMap<TournamentsResult, LaDOSE.DTO.TournamentsResultDTO>();
cfg.CreateMap<Participent, LaDOSE.DTO.ParticipentDTO>(); cfg.CreateMap<ChallongeParticipent, LaDOSE.DTO.ParticipentDTO>();
cfg.CreateMap<Tournament, LaDOSE.DTO.TournamentDTO>(); cfg.CreateMap<ChallongeTournament, LaDOSE.DTO.TournamentDTO>();
cfg.CreateMap<ApplicationUser, LaDOSE.DTO.ApplicationUserDTO>(); cfg.CreateMap<ApplicationUser, LaDOSE.DTO.ApplicationUserDTO>();

View File

@@ -120,6 +120,7 @@
<Compile Include="Bootstrapper.cs" /> <Compile Include="Bootstrapper.cs" />
<Compile Include="Themes\LeftMarginMultiplierConverter.cs" /> <Compile Include="Themes\LeftMarginMultiplierConverter.cs" />
<Compile Include="Themes\TreeViewItemExtensions.cs" /> <Compile Include="Themes\TreeViewItemExtensions.cs" />
<Compile Include="Utils\CustomEqualityCompare.cs" />
<Compile Include="Utils\PhpSerialize.cs" /> <Compile Include="Utils\PhpSerialize.cs" />
<Compile Include="UserControls\BookingUserControl.xaml.cs"> <Compile Include="UserControls\BookingUserControl.xaml.cs">
<DependentUpon>BookingUserControl.xaml</DependentUpon> <DependentUpon>BookingUserControl.xaml</DependentUpon>

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
namespace LaDOSE.DesktopApp.Utils
{
public static class CustomListExtension
{
public sealed class EqualityComparer<T> : IEqualityComparer<T> where T : class
{
private readonly Func<T, T, bool> _compare;
public EqualityComparer(Func<T, T, bool> c)
{
_compare = c;
}
public bool Equals(T x, T y)
{
return _compare(x, y);
}
public int GetHashCode(T obj)
{
return 0;
}
}
}
}

View File

@@ -22,7 +22,7 @@ namespace LaDOSE.DesktopApp.ViewModels
public override string DisplayName => "Tournament Result"; public override string DisplayName => "Tournament Result";
private RestService RestService { get; set; } private RestService RestService { get; set; }
Dictionary<string, Dictionary<int, int>> _computedResult; //Dictionary<string, Dictionary<int, int>> _computedResult;
#region Properties #region Properties
@@ -234,19 +234,20 @@ namespace LaDOSE.DesktopApp.ViewModels
if (selectedTournaments.Count > 0) if (selectedTournaments.Count > 0)
selectedTournaments.ForEach(e => this.SelectedTournaments.AddUI(e)); 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() private void ComputeDataGrid()
{ {
var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList(); var resultsParticipents = this.Results.Participents.Select(e=>e.Name).Distinct(new CustomListExtension.EqualityComparer<String>((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
_computedResult = ResultsToDataDictionary(resultsParticipents); //i m to lazy to rewrite this functions (this is so ugly...)
//_computedResult = ResultsToDataDictionary(resultsParticipents);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
DataTable grid = new DataTable(); DataTable grid = new DataTable();
var games = Results.Games.Distinct().OrderBy(e => e.Order).ToList(); var games = Results.Games.Distinct().OrderBy(e => e.Order).ToList();
grid.Columns.Add("Players"); 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); grid.Columns.Add("Total").DataType = typeof(Int32);
@@ -255,25 +256,18 @@ namespace LaDOSE.DesktopApp.ViewModels
var dataRow = grid.Rows.Add(); var dataRow = grid.Rows.Add();
var resultsParticipent = resultsParticipents[i]; var resultsParticipent = resultsParticipents[i];
int total = 0; int total = 0;
dataRow["Players"] = resultsParticipent.Name; dataRow["Players"] = resultsParticipent;
for (int j = 0; j < games.Count; j++) for (int j = 0; j < games.Count; j++)
{ {
var resultsGame = Results.Games[j]; var resultsGame = Results.Games[j];
var dictionary = _computedResult[resultsParticipent.Name]; var points = GetPlayerPoint(resultsParticipent, resultsGame.Id);
if (dictionary.ContainsKey(resultsGame.Id)) dataRow[resultsGame.Name.Replace('.', ' ')] = points!=0?(object) points:DBNull.Value;
{ total += points;
int points = dictionary[resultsGame.Id];
dataRow[resultsGame.Name.Replace('.', ' ')] = points;
total += points;
}
else
dataRow[resultsGame.Name.Replace('.', ' ')] = null;
} }
dataRow["Total"] = total; dataRow["Total"] = total;
} }
grid.DefaultView.Sort = "Total DESC"; grid.DefaultView.Sort = "Total DESC";
@@ -362,33 +356,68 @@ namespace LaDOSE.DesktopApp.ViewModels
System.Windows.Clipboard.SetText(this.HtmlContent); System.Windows.Clipboard.SetText(this.HtmlContent);
} }
private Dictionary<string, Dictionary<int, int>> ResultsToDataDictionary( private int GetPlayerPoint(string name, int gameid)
List<ParticipentDTO> resultsParticipents)
{ {
var computed = new Dictionary<string, Dictionary<int, int>>(); return Results.Results.Where(e => e.GameId == gameid && e.Player.ToUpperInvariant() == name.ToUpperInvariant()).Sum(e=>e.Point);
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;
} }
// 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 = 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<ParticipentDTO> resultsParticipents, Dictionary<string, Dictionary<int, int>> 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));
// }
// }
// }
// }
} }
} }

View File

@@ -62,9 +62,7 @@
<Label>Usefull :</Label> <Label>Usefull :</Label>
<Button Padding="5,0,5,0" x:Name="SelectMonth">Month</Button> <Button Padding="5,0,5,0" x:Name="SelectMonth">Month</Button>
<Button Padding="5,0,5,0" Margin="5,0,5,0" x:Name="SelectYear">Year</Button> <Button Padding="5,0,5,0" Margin="5,0,5,0" x:Name="SelectYear">Year</Button>
<Label>Select :</Label>
<TextBox Width="200" Text="{Binding SelectRegex}"></TextBox>
<Button Padding="5,0,5,0" Margin="5,0,5,0" x:Name="SelectRegexp">Select</Button>
</StackPanel> </StackPanel>
<Button Grid.Row="1" x:Name="LoadTournaments">Update</Button> <Button Grid.Row="1" x:Name="LoadTournaments">Update</Button>
<ListView Grid.Row="2" ItemsSource="{Binding Tournaments}" x:Name="TournamentList" Margin="0,0,0,5" <ListView Grid.Row="2" ItemsSource="{Binding Tournaments}" x:Name="TournamentList" Margin="0,0,0,5"
@@ -74,11 +72,7 @@
<ListView.ItemTemplate> <ListView.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Id}" />
<TextBlock Margin="5,0,0,0" Text="{Binding Name}" /> <TextBlock Margin="5,0,0,0" Text="{Binding Name}" />
<TextBlock> - </TextBlock>
<TextBlock Margin="5,0,0,0" Text="{Binding Game.Name}" />
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
</ListView.ItemTemplate> </ListView.ItemTemplate>
@@ -97,7 +91,13 @@
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" /> <ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.ColumnSpan="3" x:Name="Select">Select</Button> <DockPanel Grid.Row="0" Grid.ColumnSpan="3" Dock="Left">
<Label>Select :</Label>
<TextBox Width="200" Text="{Binding SelectRegex}"></TextBox>
<Button Padding="5,0,5,0" Margin="5,0,5,0" x:Name="SelectRegexp">Select</Button>
<Button x:Name="Select" >Get Tournaments Result</Button>
</DockPanel>
<StackPanel Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal"> <StackPanel Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal">

View File

@@ -144,10 +144,12 @@ namespace LaDOSE.Business.Service
return result; return result;
} }
/// <summary> /// <summary>
/// Check if the tournament exist in database otherwise call Challonge. /// Check if the tournament exist in database otherwise call Challonge.
/// </summary> /// </summary>
/// <param name="ids">tournaments ids</param> /// <param name="ids">tournaments ids</param>
/// <param name="games">List of known games</param>
/// <returns>List of the challonge's tournament with participents</returns> /// <returns>List of the challonge's tournament with participents</returns>
private async Task<List<ChallongeTournament>> GetChallongeTournaments(List<int> ids, List<Game> games) private async Task<List<ChallongeTournament>> GetChallongeTournaments(List<int> ids, List<Game> games)
{ {