Add a CSV Export of Selected Tournament (can stack multiple rankings but the UI get messy)
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Caliburn.Micro;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using LaDOSE.DesktopApp.Utils;
|
||||
using LaDOSE.DTO;
|
||||
using LaDOSE.REST;
|
||||
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
|
||||
using Screen = Caliburn.Micro.Screen;
|
||||
|
||||
namespace LaDOSE.DesktopApp.ViewModels
|
||||
{
|
||||
|
||||
|
||||
public class TournamentResultViewModel : Screen
|
||||
{
|
||||
public override string DisplayName => "Tournament Result";
|
||||
@@ -22,7 +25,6 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
this.RestService = restService;
|
||||
_selectedTournaments = new ObservableCollection<TournamentDTO>();
|
||||
Tournaments = new List<TournamentDTO>();
|
||||
|
||||
}
|
||||
|
||||
private TournamentsResultDTO _results;
|
||||
@@ -37,7 +39,9 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
NotifyOfPropertyChange(() => Results);
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<TournamentDTO> _selectedTournaments;
|
||||
|
||||
public ObservableCollection<TournamentDTO> SelectedTournaments
|
||||
{
|
||||
get { return _selectedTournaments; }
|
||||
@@ -59,12 +63,14 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
_selectedGame = value;
|
||||
//TODO: QUICK AND DIRTY
|
||||
List<ResultDTO> resultForGame = this.Results.Results.Where(e => e.GameId == SelectedGame.Id).ToList();
|
||||
First = resultForGame.OrderByDescending(e=>e.Point).First().Player;
|
||||
First = resultForGame.OrderByDescending(e => e.Point).First().Player;
|
||||
SelectedGameResult = new ObservableCollection<ResultDTO>(resultForGame);
|
||||
NotifyOfPropertyChange(() => SelectedGame);
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<ResultDTO> _selectedGameResult;
|
||||
|
||||
public ObservableCollection<ResultDTO> SelectedGameResult
|
||||
{
|
||||
get { return _selectedGameResult; }
|
||||
@@ -76,6 +82,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
|
||||
private String _first;
|
||||
|
||||
public String First
|
||||
{
|
||||
get { return _first; }
|
||||
@@ -85,6 +92,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
NotifyOfPropertyChange(() => First);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
LoadTournaments();
|
||||
@@ -103,10 +111,106 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList();
|
||||
var resultsDto = this.RestService.GetResults(tournamentsIds);
|
||||
this.Results = resultsDto;
|
||||
}
|
||||
|
||||
public void Export()
|
||||
{
|
||||
if (this.Results == null)
|
||||
return;
|
||||
|
||||
|
||||
ComputeTable();
|
||||
}
|
||||
|
||||
private void ComputeTable()
|
||||
{
|
||||
SaveFileDialog sfDialog = new SaveFileDialog()
|
||||
{
|
||||
AddExtension = true
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
||||
sb.AppendLine(Results.Games.Aggregate("Player;", (current, t) => current + (t.Name + ";")));
|
||||
|
||||
for (int i = 0; i < resultsParticipents.Count; i++)
|
||||
{
|
||||
var entry = "";
|
||||
|
||||
var resultsParticipent = resultsParticipents[i];
|
||||
|
||||
entry= 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];
|
||||
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";
|
||||
//for (int j = 0; j < Results.Games.Count; j++)
|
||||
//{
|
||||
// resultCsv[0][j + 1] = Results.Games[j].Name;
|
||||
//}
|
||||
|
||||
//for (int i = 0; i < resultsParticipents.Count; i++)
|
||||
//{
|
||||
// resultCsv[i + 1] = new string[Results.Games.Count + 1];
|
||||
|
||||
// var resultsParticipent = resultsParticipents[i];
|
||||
|
||||
// resultCsv[i + 1][0] = resultsParticipent.Name;
|
||||
// for (int j = 0; j < Results.Games.Count; j++)
|
||||
// {
|
||||
// var resultsGame = Results.Games[j];
|
||||
// var dictionary = computed[resultsParticipent.Name];
|
||||
// if (dictionary.ContainsKey(resultsGame.Id))
|
||||
// {
|
||||
// var i1 = dictionary[resultsGame.Id];
|
||||
// resultCsv[i + 1][j + 1] = i1.ToString();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//Save
|
||||
File.WriteAllText(sfDialog.FileName,sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,8 @@
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
@@ -49,6 +51,8 @@
|
||||
<ColumnDefinition Width="2*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Button Grid.Row="0" Grid.ColumnSpan="3" x:Name="Select">Select</Button>
|
||||
|
||||
|
||||
<StackPanel Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal">
|
||||
<TextBlock> Jeux :</TextBlock>
|
||||
<TextBlock Margin="5,0,0,0" Text="{Binding Results.Games.Count}" />
|
||||
@@ -90,7 +94,7 @@
|
||||
<TextBlock Text="{Binding SelectedGameResult.Count,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
|
||||
</StackPanel>
|
||||
<ListView ItemsSource="{Binding SelectedGameResult}" Margin="5,5,5,5"
|
||||
IsTextSearchEnabled="True" TextSearch.TextPath="Name">
|
||||
IsTextSearchEnabled="True" TextSearch.TextPath="Name" DockPanel.Dock="Top">
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
@@ -101,8 +105,10 @@
|
||||
</ListView.ItemTemplate>
|
||||
|
||||
</ListView>
|
||||
|
||||
</DockPanel>
|
||||
<Button Grid.Row="3" Grid.ColumnSpan="3" x:Name="Export">Export</Button>
|
||||
</Grid>
|
||||
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -134,7 +134,7 @@ namespace LaDOSE.Business.Service
|
||||
|
||||
}
|
||||
|
||||
result.Games = tournaments.Select(e => e.Game).ToList();
|
||||
result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name==game1.Name).ToList();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user