Cache the Challonge results
This commit is contained in:
@@ -6,6 +6,7 @@ namespace LaDOSE.DTO
|
||||
public class TournamentDTO
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int ChallongeId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Game { get; set; }
|
||||
public List<ParticipentDTO> Participents {get;set;}
|
||||
@@ -14,6 +15,7 @@ namespace LaDOSE.DTO
|
||||
public class ParticipentDTO
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int ChallongeId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int Rank { get; set; }
|
||||
public bool? IsMember{ get; set; }
|
||||
|
||||
@@ -206,7 +206,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
{
|
||||
WpfUtil.Await(() =>
|
||||
{
|
||||
var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList();
|
||||
var tournamentsIds = SelectedTournaments.Select(e => e.ChallongeId).ToList();
|
||||
var resultsDto = this.RestService.GetResults(tournamentsIds);
|
||||
this.Results = resultsDto;
|
||||
ComputeDataGrid();
|
||||
|
||||
16
LaDOSE.Src/LaDOSE.Entity/Challonge/ChallongeParticipent.cs
Normal file
16
LaDOSE.Src/LaDOSE.Entity/Challonge/ChallongeParticipent.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LaDOSE.Entity.Challonge
|
||||
{
|
||||
public class ChallongeParticipent : Context.Entity
|
||||
|
||||
{
|
||||
public ChallongeTournament ChallongeTournament { get; set; }
|
||||
public int ChallongeTournamentId { get; set; }
|
||||
public int ChallongeId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int? Rank { get; set; }
|
||||
public bool? IsMember { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
16
LaDOSE.Src/LaDOSE.Entity/Challonge/ChallongeTournament.cs
Normal file
16
LaDOSE.Src/LaDOSE.Entity/Challonge/ChallongeTournament.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LaDOSE.Entity.Challonge
|
||||
{
|
||||
public class ChallongeTournament : Context.Entity
|
||||
{
|
||||
public int ChallongeId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public Game Game { get; set; }
|
||||
public List<ChallongeParticipent> Participents { get; set; }
|
||||
public string Url { get; set; }
|
||||
|
||||
public DateTime Sync { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace LaDOSE.Entity.Challonge
|
||||
{
|
||||
public class Participent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int? Rank { get; set; }
|
||||
public bool? IsMember { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LaDOSE.Entity.Challonge
|
||||
{
|
||||
public class Tournament
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public Game Game { get; set; }
|
||||
public List<Participent> Participents { get; set; }
|
||||
public string Url { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace LaDOSE.Entity.Challonge
|
||||
{
|
||||
public class TournamentsResult
|
||||
{
|
||||
public List<Participent> Participents { get; set; }
|
||||
public List<ChallongeParticipent> Participents { get; set; }
|
||||
public List<Game> Games{ get; set; }
|
||||
|
||||
public List<Result> Results { get; set; }
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using LaDOSE.Entity.Wordpress;
|
||||
using LaDOSE.Entity.Challonge;
|
||||
using LaDOSE.Entity.Wordpress;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LaDOSE.Entity.Context
|
||||
@@ -20,6 +21,8 @@ namespace LaDOSE.Entity.Context
|
||||
#endregion
|
||||
public DbSet<SeasonGame> SeasonGame { get; set; }
|
||||
public DbSet<EventGame> EventGame { get; set; }
|
||||
public DbSet<ChallongeParticipent> ChallongeParticipent { get; set; }
|
||||
public DbSet<ChallongeTournament> ChallongeTournament { get; set; }
|
||||
|
||||
public LaDOSEDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
@@ -87,6 +90,12 @@ namespace LaDOSE.Entity.Context
|
||||
.HasForeignKey(pt => pt.WPUserId);
|
||||
#endregion
|
||||
|
||||
#region Challonge
|
||||
modelBuilder.Entity<ChallongeParticipent>()
|
||||
.HasOne(pt => pt.ChallongeTournament)
|
||||
.WithMany(p => p.Participents)
|
||||
.HasForeignKey(pt => pt.ChallongeTournamentId);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ namespace LaDOSE.Business.Interface
|
||||
Task<TournamentResult> CreateTournament(string name, string url, DateTime? startAt);
|
||||
Task<ParticipantResult> AddPlayer(int tournamentId, string userName);
|
||||
|
||||
Task<List<Tournament>> GetTournaments(DateTime? start, DateTime? end);
|
||||
Task<List<Participent>> GetParticipents(int idTournament);
|
||||
Task<Tournament> GetTournament(int idTournament);
|
||||
Task<Tournament> GetTournament(string urlTournament);
|
||||
Task<List<ChallongeTournament>> GetTournaments(DateTime? start, DateTime? end);
|
||||
Task<List<ChallongeParticipent>> GetParticipents(int idTournament);
|
||||
Task<ChallongeTournament> GetTournament(int idTournament);
|
||||
Task<ChallongeTournament> GetTournament(string urlTournament);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace LaDOSE.Business.Interface
|
||||
{
|
||||
public interface ITournamentService
|
||||
{
|
||||
Task<List<Tournament>> GetTournaments(DateTime? start, DateTime? end);
|
||||
Task<List<ChallongeTournament>> GetTournaments(DateTime? start, DateTime? end);
|
||||
|
||||
Task<TournamentsResult> GetTournamentsResult(List<int> ids);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace LaDOSE.Business.Provider
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<Tournament>> GetTournaments(DateTime? start, DateTime? end)
|
||||
public async Task<List<ChallongeTournament>> GetTournaments(DateTime? start, DateTime? end)
|
||||
{
|
||||
|
||||
List<TournamentResult> tournamentResultList = await new TournamentsQuery()
|
||||
@@ -58,28 +58,29 @@ namespace LaDOSE.Business.Provider
|
||||
|
||||
}
|
||||
.call(this.ApiCaller);
|
||||
List<Tournament> tournaments = new List<Tournament>();
|
||||
tournamentResultList.ForEach(w => tournaments.Add(new Tournament()
|
||||
List<ChallongeTournament> tournaments = new List<ChallongeTournament>();
|
||||
tournamentResultList.ForEach(w => tournaments.Add(new ChallongeTournament()
|
||||
{
|
||||
Id = w.id,
|
||||
ChallongeId = w.id,
|
||||
Name = w.name,
|
||||
Participents = new List<Participent>()
|
||||
Participents = new List<ChallongeParticipent>()
|
||||
}));
|
||||
return tournaments;
|
||||
}
|
||||
|
||||
public async Task<List<Participent>> GetParticipents(int idTournament)
|
||||
public async Task<List<ChallongeParticipent>> GetParticipents(int idTournament)
|
||||
{
|
||||
var participentResults = await new ParticipantsQuery(){tournamentID = idTournament }.call(ApiCaller);
|
||||
|
||||
List<Participent> participants = new List<Participent>();
|
||||
List<ChallongeParticipent> participants = new List<ChallongeParticipent>();
|
||||
participentResults.ForEach(w =>
|
||||
{
|
||||
if (w.active)
|
||||
{
|
||||
participants.Add(new Participent()
|
||||
participants.Add(new ChallongeParticipent()
|
||||
{
|
||||
Id = w.id,
|
||||
ChallongeTournamentId = idTournament,
|
||||
ChallongeId = w.id,
|
||||
Name = w.name,
|
||||
Rank = w.final_rank,
|
||||
IsMember = false,
|
||||
@@ -90,32 +91,32 @@ namespace LaDOSE.Business.Provider
|
||||
return participants;
|
||||
}
|
||||
|
||||
public async Task<Tournament> GetTournament(int idTournament)
|
||||
public async Task<ChallongeTournament> GetTournament(int idTournament)
|
||||
{
|
||||
|
||||
var tournamentResult = await new TournamentQuery(idTournament).call(ApiCaller);
|
||||
|
||||
return new Tournament()
|
||||
return new ChallongeTournament()
|
||||
{
|
||||
Id = tournamentResult.id,
|
||||
ChallongeId = tournamentResult.id,
|
||||
Name = tournamentResult.name,
|
||||
Url = tournamentResult.url,
|
||||
Participents = new List<Participent>()
|
||||
Participents = new List<ChallongeParticipent>()
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
public async Task<Tournament> GetTournament(string urlTournament)
|
||||
public async Task<ChallongeTournament> GetTournament(string urlTournament)
|
||||
{
|
||||
|
||||
var tournamentResult = await new TournamentQuery(urlTournament).call(ApiCaller);
|
||||
|
||||
return new Tournament()
|
||||
return new ChallongeTournament()
|
||||
{
|
||||
Id = tournamentResult.id,
|
||||
ChallongeId = tournamentResult.id,
|
||||
Name = tournamentResult.name,
|
||||
Url = tournamentResult.url,
|
||||
Participents = new List<Participent>()
|
||||
Participents = new List<ChallongeParticipent>()
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -3,15 +3,20 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LaDOSE.Business.Interface;
|
||||
using LaDOSE.Entity;
|
||||
using LaDOSE.Entity.Challonge;
|
||||
using LaDOSE.Entity.Context;
|
||||
using LaDOSE.Entity.Wordpress;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
||||
namespace LaDOSE.Business.Service
|
||||
{
|
||||
public class TournamentService : ITournamentService
|
||||
public class TournamentService : BaseService<ChallongeTournament>,ITournamentService
|
||||
{
|
||||
private IChallongeProvider _challongeProvider;
|
||||
|
||||
#region Rules
|
||||
private class Rules
|
||||
{
|
||||
public int PlayerMin { get; set; }
|
||||
@@ -37,9 +42,7 @@ namespace LaDOSE.Business.Service
|
||||
}
|
||||
}
|
||||
|
||||
private LaDOSEDbContext _context;
|
||||
private IChallongeProvider _challongeProvider;
|
||||
|
||||
//Rules Definitions (Min Players,Max Players,First Reward,Second Reward,Third / Fourth Reward, Top 8 reward, Top 16 Reward
|
||||
private List<Rules> TournamentRules = new List<Rules>()
|
||||
{
|
||||
new Rules(0, 8, 5, 3, 2, 0, 0),
|
||||
@@ -47,59 +50,51 @@ namespace LaDOSE.Business.Service
|
||||
new Rules(16, 32, 12, 8, 5, 3, 2),
|
||||
new Rules(32, Int32.MaxValue, 18, 12, 8, 5, 3),
|
||||
};
|
||||
#endregion
|
||||
|
||||
public TournamentService(LaDOSEDbContext context, IChallongeProvider challongeProvider)
|
||||
public TournamentService(LaDOSEDbContext context, IChallongeProvider challongeProvider) : base(context)
|
||||
{
|
||||
this._context = context;
|
||||
this._challongeProvider = challongeProvider;
|
||||
}
|
||||
|
||||
public async Task<List<Tournament>> GetTournaments(DateTime? start, DateTime? end)
|
||||
{
|
||||
List<WPUser> wpUsers = _context.WPUser.ToList();
|
||||
var tournaments = await _challongeProvider.GetTournaments(start, end);
|
||||
|
||||
foreach (var tournament in tournaments)
|
||||
{
|
||||
List<Participent> participents = await _challongeProvider.GetParticipents(tournament.Id);
|
||||
tournament.Participents = participents;
|
||||
}
|
||||
|
||||
return tournaments;
|
||||
public async Task<List<ChallongeTournament>> GetTournaments(DateTime? start, DateTime? end)
|
||||
{
|
||||
return await _challongeProvider.GetTournaments(start, end);
|
||||
//Useless
|
||||
//foreach (var tournament in tournaments)
|
||||
//{
|
||||
// List<ChallongeParticipent> participents = await _challongeProvider.GetParticipents(tournament.ChallongeId);
|
||||
// tournament.Participents = participents;
|
||||
//}
|
||||
}
|
||||
|
||||
public async Task<TournamentsResult> GetTournamentsResult(List<int> ids)
|
||||
{
|
||||
TournamentsResult result = new TournamentsResult();
|
||||
result.Results = new List<Result>();
|
||||
var tournaments = new List<Tournament>();
|
||||
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 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();
|
||||
foreach (var participent in allParticipent)
|
||||
{
|
||||
var player = players.FirstOrDefault(e => e.Name.Contains(participent.Name));
|
||||
if (player != null)
|
||||
{
|
||||
participent.IsMember = true;
|
||||
}
|
||||
}
|
||||
|
||||
//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 game = games.First(g => tournament.Name.Contains(g.Name));
|
||||
tournament.Game = game;
|
||||
|
||||
|
||||
var playerCount = tournament.Participents.Count;
|
||||
var lesSacs = tournament.Participents;
|
||||
@@ -117,18 +112,18 @@ namespace LaDOSE.Business.Service
|
||||
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, tournament.Id, tournament.Url, currentRule.FirstPoint));
|
||||
result.Results.Add(new Result(first.Name, tournament.Game.Id, tournament.ChallongeId, tournament.Url, currentRule.FirstPoint));
|
||||
lesSacs.Remove(first);
|
||||
result.Results.Add(new Result(second.Name, tournament.Game.Id, tournament.Id, tournament.Url, currentRule.SecondPoint));
|
||||
result.Results.Add(new Result(second.Name, tournament.Game.Id, tournament.ChallongeId, tournament.Url, currentRule.SecondPoint));
|
||||
lesSacs.Remove(second);
|
||||
thirdFourth.ForEach(r =>
|
||||
result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.Id, tournament.Url,
|
||||
result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.ChallongeId, tournament.Url,
|
||||
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, tournament.Url, currentRule.Top8Point)));
|
||||
result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.ChallongeId, tournament.Url, currentRule.Top8Point)));
|
||||
Top8.ForEach(p => lesSacs.Remove(p));
|
||||
}
|
||||
|
||||
@@ -136,12 +131,12 @@ namespace LaDOSE.Business.Service
|
||||
{
|
||||
Top16.ForEach(r =>
|
||||
result.Results.Add(
|
||||
new Result(r.Name, tournament.Game.Id, tournament.Id, tournament.Url, currentRule.Top16Point)));
|
||||
new Result(r.Name, tournament.Game.Id, tournament.ChallongeId, tournament.Url, currentRule.Top16Point)));
|
||||
Top16.ForEach(p => lesSacs.Remove(p));
|
||||
}
|
||||
|
||||
lesSacs.ForEach(r =>
|
||||
result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.Id, tournament.Url,
|
||||
result.Results.Add(new Result(r.Name, tournament.Game.Id, tournament.ChallongeId, tournament.Url,
|
||||
currentRule.Participation)));
|
||||
}
|
||||
|
||||
@@ -149,5 +144,43 @@ namespace LaDOSE.Business.Service
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Check if the tournament exist in database otherwise call Challonge.
|
||||
/// </summary>
|
||||
/// <param name="ids">tournaments ids</param>
|
||||
/// <returns>List of the challonge's tournament with participents</returns>
|
||||
private async Task<List<ChallongeTournament>> GetChallongeTournaments(List<int> ids, List<Game> games)
|
||||
{
|
||||
var tournaments = new List<ChallongeTournament>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user