diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs index 2eafb85..f4c2a7a 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs @@ -13,13 +13,12 @@ namespace LaDOSE.Api.Controllers [Route("api/[controller]")] public class TournamentController : Controller { - public IGameService GameService { get; } - - private IWordPressService _service; + + private ITournamentService _service; // GET - public TournamentController(IWordPressService service, IGameService gameService) + public TournamentController(ITournamentService service) { - GameService = gameService; + _service = service; } @@ -29,6 +28,21 @@ namespace LaDOSE.Api.Controllers var tournaments = await _service.GetTournaments(DateTime.Now.AddMonths(-2), null); return AutoMapper.Mapper.Map>(tournaments); + return null; + } + + + [HttpPost("GetResults")] + public async Task GetResults([FromBody] List ids) + { + if (ids == null) + { + throw new Exception("Invalid arguments"); + } + + var tournamentsResult = await _service.GetTournamentsResult(ids); + return AutoMapper.Mapper.Map(tournamentsResult); + } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index 04926c8..e5de491 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -119,11 +119,15 @@ namespace LaDOSE.Api cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + + cfg.CreateMap(); cfg.CreateMap().ForMember(e=>e.Meta,opt=>opt.MapFrom(s=>s.Meta.CleanWpMeta())); cfg.CreateMapTwoWay(); - cfg.CreateMapTwoWay(); - cfg.CreateMapTwoWay(); cfg.CreateMapTwoWay(); }); @@ -139,6 +143,7 @@ namespace LaDOSE.Api services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs index be92ad8..c139a28 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs @@ -38,8 +38,7 @@ namespace LaDOSE.DesktopApp.ViewModels var restService = IoC.Get(); restService.UpdatedJwtEvent += TokenUpdate; restService.Connect(uri, user, password); - var wordPressViewModel = new WordPressViewModel(IoC.Get()); - ActivateItem(wordPressViewModel); + base.OnInitialize(); @@ -52,7 +51,7 @@ namespace LaDOSE.DesktopApp.ViewModels public BitmapFrame AppIcon { get; set; } - + public void LoadEvent() { ActivateItem(new WordPressViewModel(IoC.Get())); @@ -64,7 +63,7 @@ namespace LaDOSE.DesktopApp.ViewModels public void TournamentResult() { - ActivateItem(new TournamentResultViewModel()); + ActivateItem(new TournamentResultViewModel(IoC.Get())); } } } \ 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 45ff786..7671aff 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs @@ -1,4 +1,10 @@ -using Caliburn.Micro; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using Caliburn.Micro; +using LaDOSE.DTO; +using LaDOSE.REST; namespace LaDOSE.DesktopApp.ViewModels { @@ -7,5 +13,86 @@ namespace LaDOSE.DesktopApp.ViewModels public class TournamentResultViewModel : Screen { public override string DisplayName => "Tournament Result"; + + private RestService RestService { get; set; } + + public TournamentResultViewModel(RestService restService) + { + this.RestService = restService; + _selectedTournaments = new ObservableCollection(); + Tournaments = new List(); + + } + private ObservableCollection _selectedTournaments; + private TournamentsResultDTO _results; + public List Tournaments { get; set; } + + public TournamentsResultDTO Results + { + get => _results; + set + { + _results = value; + NotifyOfPropertyChange(() => Results); + } + } + + public ObservableCollection SelectedTournaments + { + get { return _selectedTournaments; } + set + { + _selectedTournaments = value; + NotifyOfPropertyChange(() => SelectedTournaments); + } + } + + private GameDTO _selectedGame; + + public GameDTO SelectedGame + { + get { return _selectedGame; } + set + { + _selectedGame = value; + //TODO: QUICK AND DIRTY + var resultForGame = this.Results.Results.Where(e => e.GameId == SelectedGame.Id).ToList(); + First = resultForGame.OrderByDescending(e=>e.Point).First().Player; + NotifyOfPropertyChange(() => SelectedGame); + } + } + private String _first; + public String First + { + get { return _first; } + set + { + _first = value; + NotifyOfPropertyChange(() => First); + } + } + protected override void OnInitialize() + { + LoadTournaments(); + base.OnInitialize(); + } + + public void LoadTournaments() + { + var tournamentDtos = this.RestService.GetTournaments().ToList(); + this.Tournaments = tournamentDtos; + NotifyOfPropertyChange("Tournaments"); + } + + public void Select() + { + var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList(); + var resultsDto = this.RestService.GetResults(tournamentsIds); + this.Results = resultsDto; + + } + + } + } \ 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 ec08a76..fac08d9 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml @@ -13,13 +13,86 @@ d:DesignHeight="450" d:DesignWidth="800"> + - + + + + + + + + - + + + + + - + + + + + + + + + + + + + + + Jeux : + + + + + + + + + + + + + + + + Participents : + + + + + + + + + + + + + + + + Resultat : + + First : + + + + Second : + + + + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Entity/Challonge/Tournament.cs b/LaDOSE.Src/LaDOSE.Entity/Challonge/Tournament.cs index 7c7ab61..a898808 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Challonge/Tournament.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Challonge/Tournament.cs @@ -6,7 +6,7 @@ namespace LaDOSE.Entity.Challonge { public int Id { get; set; } public string Name { get; set; } - public string Game { get; set; } + public Game Game { get; set; } public List Participents { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs b/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs new file mode 100644 index 0000000..726e5af --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Entity/Challonge/TournamentsResult.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; + +namespace LaDOSE.Entity.Challonge +{ + public class TournamentsResult + { + public List Participents { get; set; } + public List Games{ get; set; } + + public List Results { get; set; } + } + + public class Result + { + public Result(string player, int gameId, int point) + { + Player = player; + GameId = gameId; + Point = point; + } + + public Result() + { + } + + + public int GameId { get; set; } + public string Player { get; set; } + public int Point { get; set; } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.REST/RestService.cs b/LaDOSE.Src/LaDOSE.REST/RestService.cs index 12e4fc7..f048d63 100644 --- a/LaDOSE.Src/LaDOSE.REST/RestService.cs +++ b/LaDOSE.Src/LaDOSE.REST/RestService.cs @@ -250,7 +250,24 @@ namespace LaDOSE.REST #endregion + #region Tournaments + public List GetTournaments() + { + CheckToken(); + var restRequest = new RestRequest("/api/Tournament/GetTournaments", Method.GET); + var restResponse = Client.Get>(restRequest); + return restResponse.Data; + } + + public TournamentsResultDTO GetResults(List ids) + { + CheckToken(); + return Post,TournamentsResultDTO>("Api/Tournament/GetResults", ids); + + } + + #endregion } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs index bc9f25c..7ec461e 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs @@ -14,6 +14,7 @@ namespace LaDOSE.Business.Interface Task AddPlayer(int tournamentId, string userName); Task> GetTournaments(DateTime? start, DateTime? end); - Task> GetParticipents(int tournamentId); + Task> GetParticipents(int idTournament); + Task GetTournament(int idTournament); } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs new file mode 100644 index 0000000..469ca76 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using LaDOSE.Entity.Challonge; + +namespace LaDOSE.Business.Interface +{ + public interface ITournamentService + { + Task> GetTournaments(DateTime? start, DateTime? end); + + Task GetTournamentsResult(List ids); + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs index 82372ce..88745ba 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using LaDOSE.Entity; -using LaDOSE.Entity.Challonge; using LaDOSE.Entity.Wordpress; namespace LaDOSE.Business.Interface @@ -18,6 +16,7 @@ namespace LaDOSE.Business.Interface Task GetLastChallonge(); - Task> GetTournaments(DateTime? start, DateTime? end); + } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs b/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs index 9754619..6fc9d9a 100644 --- a/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs +++ b/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs @@ -68,21 +68,35 @@ namespace LaDOSE.Business.Provider return tournaments; } - public async Task> GetParticipents(int tournamentId) + public async Task> GetParticipents(int idTournament) { - var participentResults = new ParticipantsQuery().call(ApiCaller); + var participentResults = await new ParticipantsQuery(){tournamentID = idTournament }.call(ApiCaller); List participants = new List(); - participentResults.Result.ForEach(w => participants.Add(new Participent() + participentResults.ForEach(w => participants.Add(new Participent() { Id = w.id, Name = w.name, Rank = w.final_rank, - IsMember = false, + IsMember = true, })); return participants; } + public async Task GetTournament(int idTournament) + { + + var tournamentResult = await new TournamentQuery(idTournament).call(ApiCaller); + + return new Tournament() + { + Id = tournamentResult.id, + Name = tournamentResult.name, + Participents = new List() + }; + + } + public async Task GetLastTournament() { string dernierTournois = null; diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs new file mode 100644 index 0000000..0f1b2e2 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using LaDOSE.Business.Interface; +using LaDOSE.Entity.Challonge; +using LaDOSE.Entity.Context; +using LaDOSE.Entity.Wordpress; +using Microsoft.EntityFrameworkCore.Internal; + +namespace LaDOSE.Business.Service +{ + public class TournamentService : ITournamentService + { + private class Rules + { + public int PlayerMin { get; set; } + public int PlayerMax { get; set; } + public int FirstPoint { get; set; } + public int SecondPoint { get; set; } + public int ThirdFourthPoint { get; set; } + public int Top8Point { get; set; } + public int Top16Point { get; set; } + + public Rules(int playerMin, int playerMax, int firstPoint, int secondPoint, int thirdFourthPoint, int top8Point, int top16Point) + { + PlayerMin = playerMin; + PlayerMax = playerMax; + FirstPoint = firstPoint; + SecondPoint = secondPoint; + ThirdFourthPoint = thirdFourthPoint; + Top8Point = top8Point; + Top16Point = top16Point; + } + } + + private LaDOSEDbContext _context; + private IChallongeProvider _challongeProvider; + + 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), + }; + + public TournamentService(LaDOSEDbContext context, IChallongeProvider challongeProvider) + { + this._context = context; + this._challongeProvider = challongeProvider; + } + public async Task> GetTournaments(DateTime? start, DateTime? end) + { + List wpUsers = _context.WPUser.ToList(); + var tournaments = await _challongeProvider.GetTournaments(start, end); + + foreach (var tournament in tournaments) + { + List participents = await _challongeProvider.GetParticipents(tournament.Id); + tournament.Participents = participents; + } + return tournaments; + } + + public async Task GetTournamentsResult(List ids) + { + TournamentsResult result = new TournamentsResult(); + result.Results = new List(); + var tournaments = new List(); + foreach (var idTournament in ids) + { + var tournament = await _challongeProvider.GetTournament(idTournament); + tournament.Participents = await _challongeProvider.GetParticipents(tournament.Id); + tournaments.Add(tournament); + } + + 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(); + foreach (var participent in allParticipent) + { + var player = players.FirstOrDefault(e => e.Name.Contains(participent.Name)); + 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; + var currentRule = TournamentRules.FirstOrDefault(rules => + rules.PlayerMin < playerCount && rules.PlayerMax >= playerCount + ); + if (currentRule == null) + { + throw new Exception("Unable to find rules"); + } + + 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)); + result.Results.Add(new Result(second.Name,tournament.Game.Id,currentRule.SecondPoint)); + thirdFourth.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.ThirdFourthPoint))); + Top8.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Top8Point))); + Top16.ForEach(r=> result.Results.Add(new Result(r.Name, tournament.Game.Id, currentRule.Top16Point))); + } + + result.Games = tournaments.Select(e => e.Game).ToList(); + + return result; + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs b/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs index 49d91ca..b68f424 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs @@ -10,7 +10,6 @@ using ChallongeCSharpDriver.Core.Results; using LaDOSE.Business.Helper; using LaDOSE.Business.Interface; using LaDOSE.Entity; -using LaDOSE.Entity.Challonge; using LaDOSE.Entity.Context; using LaDOSE.Entity.Wordpress; using Microsoft.EntityFrameworkCore; @@ -165,19 +164,9 @@ namespace LaDOSE.Business.Service var lastTournament = await _challongeProvider.GetLastTournament(); return lastTournament; } - public async Task> GetTournaments(DateTime? start, DateTime? end) - { - var tournaments = await _challongeProvider.GetTournaments(start,end); - - foreach (var tournament in tournaments) - { - List participents = await _challongeProvider.GetParticipents(tournament.Id); - tournament.Participents = participents; - } - return tournaments; - } + private string FormatCurrentEventName(string currentEventName) {