129 lines
4.5 KiB
C#
129 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Vanguard.Api;
|
|
using Vanguard.Api.Contracts;
|
|
using Vanguard.State;
|
|
|
|
namespace Vanguard.Meta
|
|
{
|
|
/// <summary>Обёртка над мета-эндпоинтами: магазин, коллекция, колоды.</summary>
|
|
public sealed class MetaService
|
|
{
|
|
private readonly ApiClient _api;
|
|
private readonly PlayerState _player;
|
|
|
|
public MetaService(ApiClient api, PlayerState player)
|
|
{
|
|
_api = api;
|
|
_player = player;
|
|
}
|
|
|
|
public string BaseUrl => _api.BaseUrl;
|
|
|
|
public async Task<StoreResponse> GetStoreAsync()
|
|
{
|
|
var store = await _api.GetAsync<StoreResponse>("/api/store");
|
|
_player.ApplyBalances(store.Balances);
|
|
return store;
|
|
}
|
|
|
|
public async Task<OnboardingResponse> OnboardAsync(string deckId)
|
|
{
|
|
var response = await _api.PostAsync<OnboardingResponse>(
|
|
"/api/meta/onboarding",
|
|
new OnboardingRequest { DeckId = deckId });
|
|
|
|
_player.ApplyBalances(response.Balances);
|
|
_player.MarkOnboarded(response.DeckId);
|
|
return response;
|
|
}
|
|
|
|
public async Task<CollectionResponse> GetCollectionAsync()
|
|
{
|
|
var collection = await _api.GetAsync<CollectionResponse>("/api/collection");
|
|
_player.ApplyBalances(collection.Balances);
|
|
return collection;
|
|
}
|
|
|
|
public async Task<Balances> DismantleAsync(string printingId, int count)
|
|
{
|
|
var balances = await _api.PostAsync<Balances>(
|
|
"/api/collection/dismantle",
|
|
new DismantleRequest
|
|
{
|
|
PrintingId = printingId,
|
|
Count = count,
|
|
IdempotencyKey = Guid.NewGuid().ToString("N"),
|
|
});
|
|
|
|
_player.ApplyBalances(balances);
|
|
return balances;
|
|
}
|
|
|
|
public async Task<List<DeckDto>> GetDecksAsync()
|
|
{
|
|
var decks = await _api.GetAsync<List<DeckDto>>("/api/decks");
|
|
return decks ?? new List<DeckDto>();
|
|
}
|
|
|
|
public async Task<DeckDto> CreateDeckAsync(string name, List<string> cards) =>
|
|
await _api.PostAsync<DeckDto>(
|
|
"/api/decks",
|
|
new DeckRequest { Name = name, Cards = cards });
|
|
|
|
public Task DeleteDeckAsync(Guid deckId) =>
|
|
_api.DeleteAsync("/api/decks/" + deckId.ToString("D"));
|
|
|
|
public async Task<DeckValidationResponse> ValidateDeckAsync(Guid deckId, List<string> cards) =>
|
|
await _api.PostAsync<DeckValidationResponse>(
|
|
"/api/decks/" + deckId.ToString("D") + "/validate",
|
|
new DeckRequest { Cards = cards });
|
|
|
|
public async Task<QueueAccepted> EnqueueAsync(Guid deckId, string mode) =>
|
|
await _api.PostAsync<QueueAccepted>(
|
|
"/api/game/queue",
|
|
new QueueRequest { DeckId = deckId, Mode = mode });
|
|
|
|
public async Task<QueueStatusResponse> GetQueueStatusAsync(Guid queueId) =>
|
|
await _api.GetAsync<QueueStatusResponse>(
|
|
"/api/game/queue/" + queueId.ToString("D"));
|
|
|
|
public Task CancelQueueAsync(Guid queueId) =>
|
|
_api.DeleteAsync("/api/game/queue/" + queueId.ToString("D"));
|
|
|
|
public async Task<TicketResponse> CreateTicketAsync(string matchId) =>
|
|
await _api.PostAsync<TicketResponse>(
|
|
"/api/game/tickets",
|
|
new TicketRequest { MatchId = matchId });
|
|
|
|
public async Task<BoosterResultDto> BuyBoosterAsync(string payment)
|
|
{
|
|
var result = await _api.PostAsync<BoosterResultDto>(
|
|
"/api/store/buy/booster",
|
|
new BoosterBuyRequest
|
|
{
|
|
Payment = payment,
|
|
IdempotencyKey = Guid.NewGuid().ToString("N"),
|
|
});
|
|
|
|
_player.ApplyBalances(result.Balances);
|
|
return result;
|
|
}
|
|
|
|
public async Task<Balances> BuyTrialDeckAsync(string deckId)
|
|
{
|
|
var balances = await _api.PostAsync<Balances>(
|
|
"/api/store/buy/trialdeck",
|
|
new TrialDeckBuyRequest
|
|
{
|
|
DeckId = deckId,
|
|
IdempotencyKey = Guid.NewGuid().ToString("N"),
|
|
});
|
|
|
|
_player.ApplyBalances(balances);
|
|
return balances;
|
|
}
|
|
}
|
|
}
|