Start of the Structure of event not tied to Challonge or smash.
Lads und Canzer \o>
This commit is contained in:
@@ -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<List<ChallongeTournament>> GetTournaments(DateTime? start, DateTime? end)
|
||||
{
|
||||
var list = new List<ChallongeTournament>();
|
||||
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<T> QuerySmash<T>(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<T>(req);
|
||||
if (graphQLResponse.Errors != null)
|
||||
{
|
||||
//EventType not done ?
|
||||
//throw new Exception("Error");
|
||||
}
|
||||
|
||||
return graphQLResponse.Data;
|
||||
}
|
||||
|
||||
public async Task<Event> 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<TournamentResponse>(query);
|
||||
List<Tournament> 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<ResponseType> GetTournament(string slug)
|
||||
public Task<List<Tournament>> GetResults(ref List<Tournament> 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<EventResponse>(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<EventResponse>(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<Tournament> 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<Event> ParseEvent(string slug)
|
||||
{
|
||||
Event e = await this.GetEvent(slug);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
public async Task<TournamentResponse> 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<ResponseType>(Event);
|
||||
var graphQLResponse = await graphQLClient.SendQueryAsync<TournamentResponse>(Event);
|
||||
if (graphQLResponse.Errors != null)
|
||||
{
|
||||
//Event not done ?
|
||||
//EventType not done ?
|
||||
//throw new Exception("Error");
|
||||
}
|
||||
System.Diagnostics.Trace.Write(graphQLResponse.Data.Tournament.Name);
|
||||
|
||||
Reference in New Issue
Block a user