Skip to content

Commit

Permalink
Get latest block information
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Feb 26, 2024
1 parent 09b1b47 commit e4b745f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
28 changes: 26 additions & 2 deletions NineChroniclesUtilBackend.Store/Client/EmptyChronicleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace NineChroniclesUtilBackend.Store.Client;

public class EmptyChroniclesClient
public class EmptyChronicleClient
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;

public EmptyChroniclesClient(string baseUrl)
public EmptyChronicleClient(string baseUrl)
{
_baseUrl = baseUrl;
_httpClient = new HttpClient();
Expand Down Expand Up @@ -36,4 +36,28 @@ public async Task<StateResponse> GetStateByAddressAsync(string address, string?

return stateResponse;
}

public async Task<BlockResponse> GetLatestBlock()
{
var url = $"{_baseUrl}/api/blocks/latest";

var response = await _httpClient.GetAsync(url);

response.EnsureSuccessStatusCode();

var content = await response.Content.ReadAsStringAsync();

var stateResponse = JsonConvert.DeserializeObject<BlockResponse>(content);
if (stateResponse == null)
{
throw new InvalidOperationException("StateResponse is null.");
}

return stateResponse;
}

// public async Task<StateResponse> GetBlock(int index)
// {
// return stateResponse;
// }
}
13 changes: 13 additions & 0 deletions NineChroniclesUtilBackend.Store/Models/BlockResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NineChroniclesUtilBackend.Store.Models;

public class BlockResponse
{
public string Hash { get; }
public long Index { get; }

public BlockResponse(string hash, long index)
{
Hash = hash;
Index = index;
}
}
2 changes: 1 addition & 1 deletion NineChroniclesUtilBackend.Store/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
builder.Services.AddSingleton(serviceProvider =>
{
var config = serviceProvider.GetRequiredService<IOptions<Configuration>>().Value;
return new EmptyChroniclesClient(config.EmptyChronicleBaseUrl);
return new EmptyChronicleClient(config.EmptyChronicleBaseUrl);
});

builder.Services.AddSingleton(serviceProvider =>
Expand Down
10 changes: 7 additions & 3 deletions NineChroniclesUtilBackend.Store/Scrapper/ArenaScrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NineChroniclesUtilBackend.Store.Events;
using NineChroniclesUtilBackend.Store.Services;
using NineChroniclesUtilBackend.Store.Models;
using NineChroniclesUtilBackend.Store.Client;
using Nekoyume.TableData;
using Libplanet.Crypto;

Expand All @@ -12,12 +13,14 @@ public class ArenaScrapper
public readonly ScrapperResult Result = new ScrapperResult();

private StateGetter _stateGetter;
private EmptyChronicleClient _client;
public event EventHandler<ArenaDataCollectedEventArgs> OnDataCollected;

public ArenaScrapper(ILogger<ArenaScrapper> logger, IStateService service)
public ArenaScrapper(ILogger<ArenaScrapper> logger, IStateService service, EmptyChronicleClient client)
{
_stateGetter = new StateGetter(service);
_logger = logger;
_client = client;
}

protected virtual void RaiseDataCollected(ArenaData arenaData, AvatarData avatarData)
Expand All @@ -28,8 +31,9 @@ protected virtual void RaiseDataCollected(ArenaData arenaData, AvatarData avatar
public async Task ExecuteAsync()
{
Result.StartTime = DateTime.UtcNow;
var latestBlock = await _client.GetLatestBlock();

var roundData = await GetArenaRoundData(900000);
var roundData = await GetArenaRoundData(latestBlock.Index);

var arenaParticipants = await _stateGetter.GetArenaParticipantsState(roundData.ChampionshipId, roundData.Round);

Expand All @@ -47,7 +51,7 @@ public async Task ExecuteAsync()
Result.TotalElapsedMinutes = DateTime.UtcNow.Subtract(Result.StartTime).Minutes;
}

public async Task<ArenaSheet.RoundData> GetArenaRoundData(int index)
public async Task<ArenaSheet.RoundData> GetArenaRoundData(long index)
{
var arenaSheet = await _stateGetter.GetSheet<ArenaSheet>();
var roundData = arenaSheet.GetRoundByBlockIndex(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace NineChroniclesUtilBackend.Store.Services;

public class EmptyChronicleStateService : IStateService
{
private readonly EmptyChroniclesClient client;
private readonly EmptyChronicleClient client;
private static readonly Codec Codec = new();

public EmptyChronicleStateService(EmptyChroniclesClient client)
public EmptyChronicleStateService(EmptyChronicleClient client)
{
this.client = client;
}
Expand Down
10 changes: 8 additions & 2 deletions NineChroniclesUtilBackend.Store/Worker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NineChroniclesUtilBackend.Store.Events;
using NineChroniclesUtilBackend.Store.Scrapper;
using NineChroniclesUtilBackend.Store.Client;
using NineChroniclesUtilBackend.Store.Services;

namespace NineChroniclesUtilBackend.Store;
Expand All @@ -11,12 +12,17 @@ public class Worker : BackgroundService
private readonly ILogger<Worker> _logger;
private readonly IStateService _stateService;

public Worker(ILogger<Worker> logger, ILogger<ArenaScrapper> scrapperLogger, IStateService stateService, MongoDbStore store)
public Worker(
ILogger<Worker> logger,
ILogger<ArenaScrapper> scrapperLogger,
IStateService stateService,
MongoDbStore store,
EmptyChronicleClient client)
{
_logger = logger;
_stateService = stateService;
_store = store;
_scrapper = new ArenaScrapper(scrapperLogger, _stateService);
_scrapper = new ArenaScrapper(scrapperLogger, _stateService, client);
_scrapper.OnDataCollected += HandleDataCollected;
}

Expand Down

0 comments on commit e4b745f

Please sign in to comment.