From 87c9883245eb986c997ce66ec7888b40fae10de5 Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Sun, 20 Mar 2022 02:59:48 +0100 Subject: [PATCH] Start of the Structure of event not tied to Challonge or smash. Lads und Canzer \o> --- .../LaDOSE.Api/Controllers/SmashController.cs | 18 +- .../LaDOSE.Api/Controllers/TestController.cs | 50 +++ .../Controllers/TournamentController.cs | 5 +- LaDOSE.Src/LaDOSE.Api/Startup.cs | 8 +- .../ViewModels/TournamentResultViewModel.cs | 2 + .../LaDOSE.Entity/Context/LaDOSEDbContext.cs | 24 +- LaDOSE.Src/LaDOSE.Entity/Game.cs | 4 +- .../TournamentEntities/Player.cs | 11 +- .../TournamentEntities/Result.cs | 2 +- .../TournamentEntities/Tournament.cs | 11 +- LaDOSE.Src/LaDOSE.REST/RestService.cs | 11 + .../LaDOSE.Service/Interface/IEventService.cs | 13 +- .../LaDOSE.Service/Interface/IGameService.cs | 1 + .../Interface/IPlayerService.cs | 10 + .../Interface/ISeasonService.cs | 9 - .../Interface/ISmashProvider.cs | 9 +- .../Interface/ITournamentService.cs | 17 - .../Provider/SmashProvider/SmashProvider.cs | 201 ++++++++++-- .../Provider/SmashProvider/Tournament.cs | 118 ++++--- .../LaDOSE.Service/Service/EventService.cs | 295 ++++++++++++++++-- .../LaDOSE.Service/Service/PlayerService.cs | 45 +++ .../Service/TournamentService.cs | 290 ----------------- 22 files changed, 709 insertions(+), 445 deletions(-) create mode 100644 LaDOSE.Src/LaDOSE.Api/Controllers/TestController.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Interface/IPlayerService.cs delete mode 100644 LaDOSE.Src/LaDOSE.Service/Interface/ISeasonService.cs delete mode 100644 LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Service/PlayerService.cs delete mode 100644 LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/SmashController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/SmashController.cs index 5c6262f..d946dad 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/SmashController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/SmashController.cs @@ -18,12 +18,12 @@ namespace LaDOSE.Api.Controllers public class SmashController : Controller { - private ITournamentService _service; + private IEventService _service; private IMapper _mapper; // GET - public SmashController(IMapper mapper, ITournamentService service) + public SmashController(IMapper mapper, IEventService service) { _mapper = mapper; _service = service; @@ -36,6 +36,7 @@ namespace LaDOSE.Api.Controllers { if (!String.IsNullOrEmpty(tournamentSlug)) { + var tournaments = await _service.GetSmashResult(tournamentSlug); return Ok(tournaments); @@ -43,8 +44,19 @@ namespace LaDOSE.Api.Controllers return null; } + [HttpGet("AddTournament/{tournamentSlug}")] + public async Task AddSmashTournament(string tournamentSlug) + { + if (!String.IsNullOrEmpty(tournamentSlug)) + { + var tournaments = await _service.GetSmashResult2(tournamentSlug); + + return Ok(tournaments); + } + + return null; + } - } } diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/TestController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/TestController.cs new file mode 100644 index 0000000..c0cf412 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/TestController.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using LaDOSE.Business.Interface; +using LaDOSE.DTO; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace LaDOSE.Api.Controllers +{ +#if DEBUG + [AllowAnonymous] +#endif + [Produces("application/json")] + [Route("api/[controller]")] + public class TestController : Controller + { + + private IEventService _service; + + private IMapper _mapper; + + // GET + public TestController(IMapper mapper, IEventService service) + { + _mapper = mapper; + _service = service; + } + //This may be a get , but i dont know what the RFC State for Get request with Body + //As i don't like to populate GET request with body this will be a post (and i think + //it will be easier to proxy. + [HttpGet("Test/{tournamentSlug}")] + public async Task GetSmashTournament(string tournamentSlug) + { + if (!String.IsNullOrEmpty(tournamentSlug)) + { + var tournaments = await _service.GetSmashResult(tournamentSlug); + + return Ok(tournaments); + } + + return null; + } + + + + } +} diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs index 2d98c1b..34a12f6 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs @@ -15,12 +15,12 @@ namespace LaDOSE.Api.Controllers public class TournamentController : Controller { - private ITournamentService _service; + private IEventService _service; private IMapper _mapper; // GET - public TournamentController(IMapper mapper,ITournamentService service) + public TournamentController(IMapper mapper, IEventService service) { _mapper = mapper; _service = service; @@ -49,6 +49,7 @@ namespace LaDOSE.Api.Controllers } var tournamentsResult = await _service.GetTournamentsResult(ids); + return _mapper.Map(tournamentsResult); } diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index bd6981c..d06000b 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -143,14 +143,16 @@ namespace LaDOSE.Api { services.AddTransient(p => new ChallongeProvider(this.Configuration["ApiKey:ChallongeApiKey"])); - services.AddTransient(p => new SmashProvider(this.Configuration["ApiKey:SmashApiKey"])); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); - + services.AddScoped(); + services.AddScoped(); + services.AddScoped(p => new SmashProvider(p.GetRequiredService(), p.GetRequiredService(), this.Configuration["ApiKey:SmashApiKey"])); + } diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs index 32fe7c9..3c57181 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs @@ -179,6 +179,7 @@ namespace LaDOSE.DesktopApp.ViewModels this.RestService = restService; _selectedTournaments = new ObservableCollection(); Tournaments = new List(); + } @@ -187,6 +188,7 @@ namespace LaDOSE.DesktopApp.ViewModels this.To = DateTime.Now; this.From = DateTime.Now.AddMonths(-1); this.SelectRegex = "Ranking"; + this.Slug = "ranking-1001"; LoadTournaments(); base.OnInitialize(); } diff --git a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs index ea866fd..31b5e02 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs @@ -40,15 +40,23 @@ namespace LaDOSE.Entity.Context base.OnModelCreating(modelBuilder); - - - //modelBuilder.Entity() - // .HasOne(s => s.Season) - // .WithMany(p => p.Event) - // .HasForeignKey(fk => fk.SeasonId); - + + modelBuilder.Entity() + .HasMany(s => s.Tournaments); + + + modelBuilder.Entity() + .HasOne(e => e.Game) + .WithMany(e=>e.Tournaments) + .HasForeignKey(pt=>pt.GameId) + ; + modelBuilder.Entity() + .HasOne(e => e.Event) + .WithMany(e => e.Tournaments) + .HasForeignKey(pt => pt.EventId) + ; //#region SeasonGame //modelBuilder.Entity() @@ -67,7 +75,7 @@ namespace LaDOSE.Entity.Context //#region EventGame - //modelBuilder.Entity() + //modelBuilder.Entity() // .HasKey(t => new { t.EventId, t.GameId }); //modelBuilder.Entity() diff --git a/LaDOSE.Src/LaDOSE.Entity/Game.cs b/LaDOSE.Src/LaDOSE.Entity/Game.cs index 206f1a0..419c1e2 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Game.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Game.cs @@ -15,6 +15,8 @@ namespace LaDOSE.Entity public int? SmashId { get; set; } + public List Tournaments { get; set; } + } - + } diff --git a/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Player.cs b/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Player.cs index 3d6854b..15cb48f 100644 --- a/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Player.cs +++ b/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Player.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel.DataAnnotations.Schema; using System.Security.Cryptography.X509Certificates; namespace LaDOSE.Entity @@ -11,19 +12,13 @@ namespace LaDOSE.Entity { } - public Player(string name, int? challongeId, int? smashId) - { - this.Name = name; - ChallongeId = challongeId; - SmashId = smashId; - } - - public String SmashName { get; set; } + public String Gamertag { get; set; } public String Name { get; set; } public int? ChallongeId {get;set;} public int? SmashId {get;set;} + [NotMapped] public Boolean IsChallonge => ChallongeId.HasValue; diff --git a/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Result.cs b/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Result.cs index 3429138..84f1019 100644 --- a/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Result.cs +++ b/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Result.cs @@ -19,7 +19,7 @@ public int PlayerId { get; set; } public Player Player { get; set; } - public int IdTournament { get; set; } + public int TournamentId { get; set; } public Tournament Tournament{ get; set; } public int Point { get; set; } public int Rank { get; set; } diff --git a/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Tournament.cs b/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Tournament.cs index 51cb361..6a8bb2e 100644 --- a/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Tournament.cs +++ b/LaDOSE.Src/LaDOSE.Entity/TournamentEntities/Tournament.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using Microsoft.Extensions.Logging; namespace LaDOSE.Entity { @@ -15,11 +17,18 @@ namespace LaDOSE.Entity SmashId = smashId; } + public int EventId { get; set; } + public Event Event { get; set; } public String Name { get; set; } public int ChallongeId {get;set;} public int SmashId {get;set;} public int? GameId {get;set;} public Game Game { get; set; } - + + public List Results { 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 5565c7a..f6c3078 100644 --- a/LaDOSE.Src/LaDOSE.REST/RestService.cs +++ b/LaDOSE.Src/LaDOSE.REST/RestService.cs @@ -250,7 +250,18 @@ namespace LaDOSE.REST #endregion + #region Tournaments + public TournamentsResultDTO Test(string test) + { + CheckToken(); + var restRequest = new RestRequest($"Api/Test/Test/{test}", Method.GET); + var restResponse = Client.Get(restRequest); + return restResponse.Data; + + } + + #endregion #region Tournaments public List GetTournaments(TimeRangeDTO timeRange) diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs index 5950328..baaab3d 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs @@ -1,12 +1,21 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; using LaDOSE.Entity; +using LaDOSE.Entity.Challonge; using LaDOSE.Entity.Wordpress; namespace LaDOSE.Business.Interface { public interface IEventService : IBaseService { - + Task> GetTournaments(DateTime? start, DateTime? end); + + Task GetTournamentsResult(List ids); + Task GetSmashResult(string tournamentSlug); + + Task GetSmashResult2(string tournamentSlug); + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs index 9e44e44..1f95a92 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs @@ -6,5 +6,6 @@ namespace LaDOSE.Business.Interface public interface IGameService : IBaseService { + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IPlayerService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IPlayerService.cs new file mode 100644 index 0000000..a3ebfd8 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IPlayerService.cs @@ -0,0 +1,10 @@ +using LaDOSE.Business.Provider.SmashProvider; +using LaDOSE.Entity; + +namespace LaDOSE.Business.Interface +{ + public interface IPlayerService : IBaseService + { + int GetBySmash(PlayerType playerUser); + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/ISeasonService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/ISeasonService.cs deleted file mode 100644 index 93f2ee8..0000000 --- a/LaDOSE.Src/LaDOSE.Service/Interface/ISeasonService.cs +++ /dev/null @@ -1,9 +0,0 @@ -using LaDOSE.Entity; - -namespace LaDOSE.Business.Interface -{ - //public interface ISeasonService : IBaseService - //{ - - //} -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/ISmashProvider.cs b/LaDOSE.Src/LaDOSE.Service/Interface/ISmashProvider.cs index 97512ca..18b6924 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/ISmashProvider.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/ISmashProvider.cs @@ -2,14 +2,19 @@ using System.Collections.Generic; using System.Threading.Tasks; using LaDOSE.Business.Provider.SmashProvider; +using LaDOSE.Entity; using LaDOSE.Entity.Challonge; namespace LaDOSE.Business.Interface { public interface ISmashProvider { - Task> GetTournaments(DateTime? start, DateTime? end); - Task GetTournament(string sludge); + Task GetEvent(string slug); + Task> GetResults(ref List tournaments); + Task ParseEvent(string slug); + Task GetTournament(string sludge); + + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs deleted file mode 100644 index 3b6635f..0000000 --- a/LaDOSE.Src/LaDOSE.Service/Interface/ITournamentService.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using LaDOSE.Business.Provider.SmashProvider; -using LaDOSE.Entity.Challonge; - -namespace LaDOSE.Business.Interface -{ - public interface ITournamentService - { - Task> GetTournaments(DateTime? start, DateTime? end); - - Task GetTournamentsResult(List ids); - Task GetSmashResult(string tournamentSlug); - - } -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/SmashProvider.cs b/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/SmashProvider.cs index 8188e6e..60773fc 100644 --- a/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/SmashProvider.cs +++ b/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/SmashProvider.cs @@ -1,59 +1,198 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using GraphQL; using GraphQL.Client.Http; using GraphQL.Client.Serializer.Newtonsoft; using LaDOSE.Business.Interface; +using LaDOSE.Business.Service; +using LaDOSE.Entity; using LaDOSE.Entity.Challonge; +using Result = LaDOSE.Entity.Result; namespace LaDOSE.Business.Provider.SmashProvider { public class SmashProvider : ISmashProvider { public string ApiKey { get; set; } - public SmashProvider(string apiKey) + //public SmashProvider(string apiKey) + //{ + // this.ApiKey = apiKey; + //} + public SmashProvider(IGameService gameService, IPlayerService playerService, string apiKey) { this.ApiKey = apiKey; + this.GameService = gameService; + this.PlayerService = playerService; } - public Task> GetTournaments(DateTime? start, DateTime? end) - { - var list = new List(); - var personAndFilmsRequest = new GraphQLRequest - { - Query = @" - query TournamentsByOwner($perPage: Int!, $ownerId: ID!) { - tournaments(query: { - perPage: $perPage - filter: { - ownerId: $ownerId - } - }) { - nodes { - id - name - slug - } - } - }", + public IPlayerService PlayerService { get; set; } - OperationName = "PersonAndFilms", + public IGameService GameService { get; set; } + + private async Task QuerySmash(GraphQLRequest req) + { + var graphQLClient = new GraphQLHttpClient("https://api.smash.gg/gql/alpha", new NewtonsoftJsonSerializer()); + graphQLClient.HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {ApiKey}"); + + var graphQLResponse = await graphQLClient.SendQueryAsync(req); + if (graphQLResponse.Errors != null) + { + //EventType not done ? + //throw new Exception("Error"); + } + + return graphQLResponse.Data; + } + + public async Task GetEvent(string slug) + { + + var query = new GraphQLRequest + { + Query = @"query TournamentQuery($slug: String) { + tournament(slug: $slug){ + id + name, + startAt, + events { + id + name, + state, + videogame { + id, + name, + displayName + } + } + } + }", + OperationName = "TournamentQuery", Variables = new { - - ownerId = "161429", - perPage = "4" - + slug = slug, } }; + var games = GameService.GetAll(); + var querySmash = QuerySmash(query); + List tournaments = querySmash.Result.Tournament.Events.Select(e => new Tournament() + { + Name = e.name, + SmashId = e.id, + Game = games.FirstOrDefault(g => g.SmashId == e.videogame.id) + }).ToList(); + return new Event + { + Name = querySmash.Result.Tournament.Name, + SmashId = querySmash.Result.Tournament.id, + Date = querySmash.Result.Tournament.startAt, + Tournaments = tournaments, + + }; - return Task.FromResult(list); } - public async Task GetTournament(string slug) + public Task> GetResults(ref List tournaments) { + + foreach (var tournament in tournaments) + { + var query = new GraphQLRequest + { + Query = @"query EventQuery($event: ID,$page:Int) { + event(id: $event){ + standings(query: {page:$page,perPage:20}){ + pageInfo { + total + totalPages + page + perPage + sortBy + filter + }, + nodes{ + id, + player{ + id, + gamerTag, + user { + id, + name, + player { + id + } + } + } + placement + } + } + } + }", + OperationName = "EventQuery", + Variables = new + { + page = 1, + @event = tournament.SmashId, + } + }; + var querySmash = QuerySmash(query); + + if (querySmash.Result.Event.standings.nodes != null) + { + var standings = querySmash.Result.Event.standings.nodes; + if (querySmash.Result.Event.standings.pageInfo.totalPages > 1) + { + while (querySmash.Result.Event.standings.pageInfo.page < + querySmash.Result.Event.standings.pageInfo.totalPages) + { + query.Variables = new + { + page = querySmash.Result.Event.standings.pageInfo.page, + eventsId = tournament.SmashId, + }; + querySmash = QuerySmash(query); + standings.AddRange(querySmash.Result.Event.standings.nodes); + } + } + + var res= standings.Select(x => new Result() + { + Tournament = tournament, + TournamentId = tournament.Id, + + PlayerId = PlayerService.GetBySmash(x.player), + Rank = x.placement + }).ToList(); + tournament.Results = res; + } + + //tournament.Results.AddRange(); + //List tournaments = querySmash.Result.Tournament.Events.Select(e => new Tournament() + //{ + // Name = e.name, + // SmashId = e.id, + // Game = games.FirstOrDefault(g => g.SmashId == e.videogame.id) + //}).ToList(); + + + + } + + return Task.FromResult(tournaments); + } + + public async Task ParseEvent(string slug) + { + Event e = await this.GetEvent(slug); + return e; + } + + + public async Task GetTournament(string slug) + { + var graphQLClient = new GraphQLHttpClient("https://api.smash.gg/gql/alpha", new NewtonsoftJsonSerializer()); graphQLClient.HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {ApiKey}"); var Event = new GraphQLRequest @@ -100,16 +239,16 @@ namespace LaDOSE.Business.Provider.SmashProvider } }; - //GraphQLHttpRequest preprocessedRequest = await graphQLClient.Options.PreprocessRequest(Event, graphQLClient); + //GraphQLHttpRequest preprocessedRequest = await graphQLClient.Options.PreprocessRequest(EventType, graphQLClient); //var x = preprocessedRequest.ToHttpRequestMessage(graphQLClient.Options, new NewtonsoftJsonSerializer()); //System.Diagnostics.Trace.WriteLine(x.Content.ReadAsStringAsync().Result); //var sendAsync = await graphQLClient.HttpClient.SendAsync(x); //System.Diagnostics.Trace.WriteLine(sendAsync.Content.ReadAsStringAsync().Result); - var graphQLResponse = await graphQLClient.SendQueryAsync(Event); + var graphQLResponse = await graphQLClient.SendQueryAsync(Event); if (graphQLResponse.Errors != null) { - //Event not done ? + //EventType not done ? //throw new Exception("Error"); } System.Diagnostics.Trace.Write(graphQLResponse.Data.Tournament.Name); diff --git a/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/Tournament.cs b/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/Tournament.cs index c1a30da..74d2b39 100644 --- a/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/Tournament.cs +++ b/LaDOSE.Src/LaDOSE.Service/Provider/SmashProvider/Tournament.cs @@ -1,66 +1,90 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace LaDOSE.Business.Provider.SmashProvider { - public class ResponseType + public class PageInfoType { - public TournamentType Tournament { get; set; } - public class TournamentType - { - public int id { get; set; } + public int total { get; set; } + public int totalPages { get; set; } + public int page { get; set; } + public int perPage { get; set; } + public string sortBy { get; set; } + public string filter { get; set; } + } - public string Name { get; set; } + public class TournamentType + { + public int id { get; set; } - public List Events { get; set; } + public string Name { get; set; } - } + [JsonConverter(typeof(UnixDateTimeConverter))] + public DateTime startAt { get; set; } + public List Events { get; set; } - public class Event - { - public int id { get; set; } + } + public class VideoGameType + { + public int id { get; set; } + public string Name { get; set; } + } - public string name { get; set; } - public string state { get; set; } - - public VideoGame videogame { get; set; } - public Node standings { get; set; } - } + public class StandingType + { + public int id { get; set; } - public class VideoGame - { - public int id { get; set; } - public string Name { get; set; } - } + public int placement { get; set; } - public class Node - { - public List nodes { get; set; } - - } - public class Standing - { - public int id { get; set; } - - public int placement { get; set; } + public PlayerType player { get; set; } + } + public class PlayerType + { + public int id { get; set; } + public string gamerTag { get; set; } + public UserType user { get; set; } + } + public class UserType + { + public int id { get; set; } + public string name { get; set; } - public Player player { get; set; } - } - public class Player - { - public int id { get; set; } - public string gamerTag { get; set; } - public User user { get; set; } - } - public class User - { - public int id { get; set; } - public string name { get; set; } - - } + } + public class EventType + { + public int id { get; set; } + + public string name { get; set; } + public string state { get; set; } + + public VideoGameType videogame { get; set; } + public Node standings { get; set; } + } + + public class Node + { + public PageInfoType pageInfo { get; set; } + public List nodes { get; set; } } + public class TournamentResponse + { + public TournamentType Tournament { get; set; } + + } + + public class EventResponse + { + public EventType Event { get; set; } + + } + + + } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs index 2155577..4d3dde9 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs @@ -1,43 +1,298 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using LaDOSE.Business.Helper; using LaDOSE.Business.Interface; +using LaDOSE.Business.Provider.SmashProvider; using LaDOSE.Entity; +using LaDOSE.Entity.Challonge; using LaDOSE.Entity.Context; using LaDOSE.Entity.Wordpress; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Internal; +using Result = LaDOSE.Entity.Challonge.Result; namespace LaDOSE.Business.Service { - public class EventService : BaseService, IEventService + public class EventService : BaseService, IEventService { private IChallongeProvider _challongeProvider; - public EventService(LaDOSEDbContext context, IChallongeProvider challongeProvider) : base(context) + #region Rules + private class Rules { - this._challongeProvider = challongeProvider; + 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 int Participation => 1; + + 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; + } } - //public override Event GetById(int id) - //{ - // re - // //return _context.Event.Include(e => e.Season).Include(e => e.Games).ThenInclude(e => e.Game) - // // .FirstOrDefault(e => e.Id == id); - //} + //Rules Definitions (Min Players,Max Players,First Reward,Second Reward,Third / Fourth Reward, Top 8 reward, Top 16 Reward + 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 override Event Create(Event e) - //{ - // if (e.Id != 0) - // { - // throw new Exception("Id is invalid"); - // } + private ISmashProvider _smashProvider; + + #endregion + + public EventService(LaDOSEDbContext context, IChallongeProvider challongeProvider, ISmashProvider _smashProvider) : base(context) + { + this._context = context; + this._challongeProvider = challongeProvider; + this._smashProvider = _smashProvider; + } + + public async Task> GetTournaments(DateTime? start, DateTime? end) + { + return await _challongeProvider.GetTournaments(start, end); + //Useless + //foreach (var tournament in tournaments) + //{ + // List participents = await _challongeProvider.GetParticipents(tournament.ChallongeId); + // tournament.Participents = participents; + //} + } + + public async Task GetTournamentsResult(List ids) + { + TournamentsResult result = new TournamentsResult(); + result.Results = new List(); + var players = _context.WPUser.ToList(); + var games = _context.Game.ToList(); + var tournaments = await GetChallongeTournaments(ids,games); + + var allParticipent = tournaments.SelectMany(e => e.Participents).Distinct((a, b) => a.Name == b.Name) + .ToList(); + + + allParticipent.RemoveAll(e => e.Name.StartsWith("[FORFAIT]")); + //USELESS + //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 playerCount = tournament.Participents.Count; + var lesSacs = tournament.Participents; + 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 > 8 && p.Rank <= 16).ToList(); + + result.Results.Add(new Result(first.Name, tournament.Game?.Id??0, tournament.ChallongeId, tournament.Url, currentRule.FirstPoint,first.Rank??0)); + lesSacs.Remove(first); + result.Results.Add(new Result(second.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, currentRule.SecondPoint, second.Rank ?? 0)); + lesSacs.Remove(second); + thirdFourth.ForEach(r => + result.Results.Add(new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, + currentRule.ThirdFourthPoint, r.Rank ?? 0))); + thirdFourth.ForEach(p => lesSacs.Remove(p)); + if (currentRule.Top8Point != 0) + { + Top8.ForEach(r => + result.Results.Add(new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, currentRule.Top8Point, r.Rank ?? 0))); + Top8.ForEach(p => lesSacs.Remove(p)); + } + + if (currentRule.Top16Point != 0) + { + Top16.ForEach(r => + result.Results.Add( + new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, currentRule.Top16Point, r.Rank ?? 0))); + Top16.ForEach(p => lesSacs.Remove(p)); + } + + lesSacs.ForEach(r => + result.Results.Add(new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, + currentRule.Participation, r.Rank ?? 0))); + } + + result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name == game1.Name).Where(e=>e!=null).ToList(); + if (result.Games == null) + { + result.Games = new List(); + } + result.Games.Add(new Game() {Id = 0, Order = 9999,Name = "UNKNOW"}); + return result; + } + + public async Task GetSmashResult(string tournamentSlug) + { + var test = this._smashProvider.GetEvent(tournamentSlug).Result; + var testTournaments = test.Tournaments; + var getResultEvents = this._smashProvider.GetResults(ref testTournaments).Result; + this._context.Event.Add(test); + this._context.SaveChanges(); + + var tournaments = await _smashProvider.GetTournament(tournamentSlug); + var players = tournaments.Tournament.Events.Where(e=>e.standings != null ).SelectMany(e => e.standings.nodes.Select(e => e.player)).ToList(); + var distinctp = players.DistinctBy(e=>new {e.user.id}).ToList(); + var games = _context.Game.ToList(); + + TournamentsResult result = new TournamentsResult(); + result.Results = new List(); + result.Games = new List(); + result.Participents = new List(); + distinctp.ForEach(e => + { + var x = new ChallongeParticipent() + { + Name = e.gamerTag, + ChallongeId = e.id, + Id = e.id + }; + result.Participents.Add(x); + }); + games.ForEach(e => + { + e.Id = e.SmashId ?? e.Id; + result.Games.Add(e); + }); + + foreach (var tournament in tournaments.Tournament.Events.Where(e=>e.standings!=null).ToList()) + { + + + var playerCount = tournament.standings.nodes.Count; + var lesSacs = tournament.standings.nodes; + var currentRule = TournamentRules.FirstOrDefault(rules => + rules.PlayerMin < playerCount && rules.PlayerMax >= playerCount + ); + if (currentRule == null) + { + throw new Exception("Unable to find rules"); + } + + var first = tournament.standings.nodes.First(p => p.placement == 1); + var second = tournament.standings.nodes.First(p => p.placement == 2); + var thirdFourth = tournament.standings.nodes.Where(p => p.placement == 3 || p.placement == 4).ToList(); + var Top8 = tournament.standings.nodes.Where(p => p.placement > 4 && p.placement < 9).ToList(); + var Top16 = tournament.standings.nodes.Where(p => p.placement > 8 && p.placement <= 16).ToList(); + + result.Results.Add(new Result(first.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, currentRule.FirstPoint, first.placement)); + lesSacs.Remove(first); + result.Results.Add(new Result(second.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, currentRule.SecondPoint, second.placement)); + lesSacs.Remove(second); + thirdFourth.ForEach(r => + result.Results.Add(new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, + currentRule.ThirdFourthPoint, r.placement))); + thirdFourth.ForEach(p => lesSacs.Remove(p)); + if (currentRule.Top8Point != 0) + { + Top8.ForEach(r => + result.Results.Add(new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, + currentRule.ThirdFourthPoint, r.placement))); + Top8.ForEach(p => lesSacs.Remove(p)); + } + + if (currentRule.Top16Point != 0) + { + Top16.ForEach(r => + result.Results.Add( + new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, + currentRule.ThirdFourthPoint, r.placement))); + Top16.ForEach(p => lesSacs.Remove(p)); + } + + lesSacs.ForEach(r => + result.Results.Add(new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, + currentRule.ThirdFourthPoint, r.placement))); + + } + + + + return await Task.FromResult(result); + } + + public Task GetSmashResult2(string tournamentSlug) + { + throw new NotImplementedException(); + } + + + /// + /// Check if the tournament exist in database otherwise call Challonge. + /// + /// tournaments ids + /// List of known games + /// List of the challonge's tournament with participents + private async Task> GetChallongeTournaments(List ids, List games) + { + var tournaments = new List(); + foreach (var idTournament in ids) + { + if (!TournamentExist(idTournament)) + { + ChallongeTournament challongeTournament = await _challongeProvider.GetTournament(idTournament); + challongeTournament.Participents = + await _challongeProvider.GetParticipents(challongeTournament.ChallongeId); + + var game = games.FirstOrDefault(g => challongeTournament.Name.Contains(g.Name)); + if (game != null) challongeTournament.Game = game; + challongeTournament.Sync = DateTime.Now; + + tournaments.Add(challongeTournament); + _context.ChallongeTournament.Add(challongeTournament); + _context.SaveChanges(); + } + else + { + tournaments.Add(_context.ChallongeTournament.Where(e => e.ChallongeId == idTournament) + .Include(e => e.Participents).First()); + } + } + + return tournaments; + } + + private bool TournamentExist(int idTournament) + { + return this._context.ChallongeTournament.Any(e => e.ChallongeId == idTournament); + } - // var eventAdded = _context.Event.Add(e); - // _context.SaveChanges(); - // return eventAdded.Entity; - //} } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/PlayerService.cs b/LaDOSE.Src/LaDOSE.Service/Service/PlayerService.cs new file mode 100644 index 0000000..d04ec23 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/PlayerService.cs @@ -0,0 +1,45 @@ +using System.Linq; +using LaDOSE.Business.Interface; +using LaDOSE.Business.Provider.SmashProvider; +using LaDOSE.Entity; +using LaDOSE.Entity.Context; +using Microsoft.EntityFrameworkCore.Internal; + +namespace LaDOSE.Business.Service +{ + public class PlayerService : BaseService, IPlayerService + { + + public PlayerService(LaDOSEDbContext context) : base(context) + { + } + + public int GetBySmash(PlayerType playerUser) + { + //var p2 = _context.Player.ToList(); + + var p = _context.Player.FirstOrDefault(e => e.SmashId == playerUser.user.id); + if (p == null) + { + var entity = new Player() + { + Gamertag = playerUser.gamerTag, + Name = string.IsNullOrEmpty(playerUser.user.name)? playerUser.gamerTag : playerUser.user.name, + SmashId = playerUser.user.id, + }; + _context.Player.Add(entity); + _context.SaveChanges(); + return entity.Id; + } + + if (p.Gamertag != playerUser.gamerTag) + { + p.Name = playerUser.gamerTag; + _context.SaveChanges(); + } + + return p.Id; + + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs deleted file mode 100644 index f502b32..0000000 --- a/LaDOSE.Src/LaDOSE.Service/Service/TournamentService.cs +++ /dev/null @@ -1,290 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using LaDOSE.Business.Helper; -using LaDOSE.Business.Interface; -using LaDOSE.Business.Provider.SmashProvider; -using LaDOSE.Entity; -using LaDOSE.Entity.Challonge; -using LaDOSE.Entity.Context; -using LaDOSE.Entity.Wordpress; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Internal; -using Result = LaDOSE.Entity.Challonge.Result; - -namespace LaDOSE.Business.Service -{ - public class TournamentService : BaseService,ITournamentService - { - private IChallongeProvider _challongeProvider; - - #region Rules - 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 int Participation => 1; - - 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; - } - } - - //Rules Definitions (Min Players,Max Players,First Reward,Second Reward,Third / Fourth Reward, Top 8 reward, Top 16 Reward - 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), - }; - - private ISmashProvider _smashProvider; - - #endregion - - public TournamentService(LaDOSEDbContext context, IChallongeProvider challongeProvider, ISmashProvider _smashProvider) : base(context) - { - this._context = context; - this._challongeProvider = challongeProvider; - this._smashProvider = _smashProvider; - } - - public async Task> GetTournaments(DateTime? start, DateTime? end) - { - return await _challongeProvider.GetTournaments(start, end); - //Useless - //foreach (var tournament in tournaments) - //{ - // List participents = await _challongeProvider.GetParticipents(tournament.ChallongeId); - // tournament.Participents = participents; - //} - } - - public async Task GetTournamentsResult(List ids) - { - TournamentsResult result = new TournamentsResult(); - result.Results = new List(); - var players = _context.WPUser.ToList(); - var games = _context.Game.ToList(); - var tournaments = await GetChallongeTournaments(ids,games); - - var allParticipent = tournaments.SelectMany(e => e.Participents).Distinct((a, b) => a.Name == b.Name) - .ToList(); - - - allParticipent.RemoveAll(e => e.Name.StartsWith("[FORFAIT]")); - //USELESS - //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 playerCount = tournament.Participents.Count; - var lesSacs = tournament.Participents; - 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 > 8 && p.Rank <= 16).ToList(); - - result.Results.Add(new Result(first.Name, tournament.Game?.Id??0, tournament.ChallongeId, tournament.Url, currentRule.FirstPoint,first.Rank??0)); - lesSacs.Remove(first); - result.Results.Add(new Result(second.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, currentRule.SecondPoint, second.Rank ?? 0)); - lesSacs.Remove(second); - thirdFourth.ForEach(r => - result.Results.Add(new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, - currentRule.ThirdFourthPoint, r.Rank ?? 0))); - thirdFourth.ForEach(p => lesSacs.Remove(p)); - if (currentRule.Top8Point != 0) - { - Top8.ForEach(r => - result.Results.Add(new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, currentRule.Top8Point, r.Rank ?? 0))); - Top8.ForEach(p => lesSacs.Remove(p)); - } - - if (currentRule.Top16Point != 0) - { - Top16.ForEach(r => - result.Results.Add( - new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, currentRule.Top16Point, r.Rank ?? 0))); - Top16.ForEach(p => lesSacs.Remove(p)); - } - - lesSacs.ForEach(r => - result.Results.Add(new Result(r.Name, tournament.Game?.Id ?? 0, tournament.ChallongeId, tournament.Url, - currentRule.Participation, r.Rank ?? 0))); - } - - result.Games = tournaments.Select(e => e.Game).Distinct((game, game1) => game.Name == game1.Name).Where(e=>e!=null).ToList(); - if (result.Games == null) - { - result.Games = new List(); - } - result.Games.Add(new Game() {Id = 0, Order = 9999,Name = "UNKNOW"}); - return result; - } - - public async Task GetSmashResult(string tournamentSlug) - { - var tournaments = await _smashProvider.GetTournament(tournamentSlug); - var players = tournaments.Tournament.Events.Where(e=>e.standings != null ).SelectMany(e => e.standings.nodes.Select(e => e.player)).ToList(); - var distinctp = players.DistinctBy(e=>new {e.user.id}).ToList(); - var games = _context.Game.ToList(); - - TournamentsResult result = new TournamentsResult(); - result.Results = new List(); - result.Games = new List(); - result.Participents = new List(); - distinctp.ForEach(e => - { - var x = new ChallongeParticipent() - { - Name = e.gamerTag, - ChallongeId = e.id, - Id = e.id - }; - result.Participents.Add(x); - }); - games.ForEach(e => - { - e.Id = e.SmashId ?? e.Id; - result.Games.Add(e); - }); - - foreach (var tournament in tournaments.Tournament.Events.Where(e=>e.standings!=null).ToList()) - { - - - var playerCount = tournament.standings.nodes.Count; - var lesSacs = tournament.standings.nodes; - var currentRule = TournamentRules.FirstOrDefault(rules => - rules.PlayerMin < playerCount && rules.PlayerMax >= playerCount - ); - if (currentRule == null) - { - throw new Exception("Unable to find rules"); - } - - var first = tournament.standings.nodes.First(p => p.placement == 1); - var second = tournament.standings.nodes.First(p => p.placement == 2); - var thirdFourth = tournament.standings.nodes.Where(p => p.placement == 3 || p.placement == 4).ToList(); - var Top8 = tournament.standings.nodes.Where(p => p.placement > 4 && p.placement < 9).ToList(); - var Top16 = tournament.standings.nodes.Where(p => p.placement > 8 && p.placement <= 16).ToList(); - - result.Results.Add(new Result(first.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, currentRule.FirstPoint, first.placement)); - lesSacs.Remove(first); - result.Results.Add(new Result(second.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, currentRule.SecondPoint, second.placement)); - lesSacs.Remove(second); - thirdFourth.ForEach(r => - result.Results.Add(new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, - currentRule.ThirdFourthPoint, r.placement))); - thirdFourth.ForEach(p => lesSacs.Remove(p)); - if (currentRule.Top8Point != 0) - { - Top8.ForEach(r => - result.Results.Add(new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, - currentRule.ThirdFourthPoint, r.placement))); - Top8.ForEach(p => lesSacs.Remove(p)); - } - - if (currentRule.Top16Point != 0) - { - Top16.ForEach(r => - result.Results.Add( - new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, - currentRule.ThirdFourthPoint, r.placement))); - Top16.ForEach(p => lesSacs.Remove(p)); - } - - lesSacs.ForEach(r => - result.Results.Add(new Result(r.player.gamerTag, tournament.videogame.id, tournament.id, tournament.name, - currentRule.ThirdFourthPoint, r.placement))); - - } - - - - return await Task.FromResult(result); - } - - - - /// - /// Check if the tournament exist in database otherwise call Challonge. - /// - /// tournaments ids - /// List of known games - /// List of the challonge's tournament with participents - private async Task> GetChallongeTournaments(List ids, List games) - { - var tournaments = new List(); - foreach (var idTournament in ids) - { - if (!TournamentExist(idTournament)) - { - ChallongeTournament challongeTournament = await _challongeProvider.GetTournament(idTournament); - challongeTournament.Participents = - await _challongeProvider.GetParticipents(challongeTournament.ChallongeId); - - var game = games.FirstOrDefault(g => challongeTournament.Name.Contains(g.Name)); - if (game != null) challongeTournament.Game = game; - challongeTournament.Sync = DateTime.Now; - - tournaments.Add(challongeTournament); - _context.ChallongeTournament.Add(challongeTournament); - _context.SaveChanges(); - } - else - { - tournaments.Add(_context.ChallongeTournament.Where(e => e.ChallongeId == idTournament) - .Include(e => e.Participents).First()); - } - } - - return tournaments; - } - - public Task GetSmashTop(string tournamentSlug, string eventId,int playerCount) - { - throw new NotImplementedException(); - } - private bool TournamentExist(int idTournament) - { - return this._context.ChallongeTournament.Any(e => e.ChallongeId == idTournament); - } - } -} \ No newline at end of file