Skip to content

Commit

Permalink
Add Store service
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Feb 23, 2024
1 parent dd5e609 commit aae8e1e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 14 deletions.
2 changes: 2 additions & 0 deletions NineChroniclesUtilBackend.Store/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ namespace NineChroniclesUtilBackend.Store;
public class Configuration
{
public string EmptyChronicleBaseUrl { get; init; }
public string MongoDbConnectionString { get; init; }
public string DatabaseName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using NineChroniclesUtilBackend.Store.Models;

namespace NineChroniclesUtilBackend.Store.Events;

public class ArenaDataCollectedEventArgs : EventArgs
{
public ArenaData ArenaData { get; set; }
public AvatarData AvatarData { get; set; }

public ArenaDataCollectedEventArgs(ArenaData arenaData, AvatarData avatarData)
{
ArenaData = arenaData;
AvatarData = avatarData;
}
}
14 changes: 14 additions & 0 deletions NineChroniclesUtilBackend.Store/Models/State/ArenaData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Nekoyume.Model.Arena;
using Newtonsoft.Json;
using NineChroniclesUtilBackend.Store.Util;

namespace NineChroniclesUtilBackend.Store.Models;

Expand All @@ -12,4 +14,16 @@ public ArenaData(ArenaScore score, ArenaInformation information)
Score = score;
Information = information;
}

public string ToJson()
{
var settings = new JsonSerializerSettings
{
Converters = new[] { new BigIntegerToStringConverter() },
Formatting = Formatting.Indented
};

string jsonString = JsonConvert.SerializeObject(this, settings);
return jsonString;
}
}
15 changes: 15 additions & 0 deletions NineChroniclesUtilBackend.Store/Models/State/AvataData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Nekoyume.Model.State;
using Newtonsoft.Json;
using NineChroniclesUtilBackend.Store.Util;


namespace NineChroniclesUtilBackend.Store.Models;

Expand All @@ -14,4 +17,16 @@ public AvatarData(AvatarState avatar, ItemSlotState itemSlot, List<RuneState> ru
ItemSlot = itemSlot;
RuneSlot = runeSlot;
}

public string ToJson()
{
var settings = new JsonSerializerSettings
{
Converters = new[] { new BigIntegerToStringConverter() },
Formatting = Formatting.Indented
};

string jsonString = JsonConvert.SerializeObject(this, settings);
return jsonString;
}
}
6 changes: 6 additions & 0 deletions NineChroniclesUtilBackend.Store/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
return new EmptyChroniclesClient(config.EmptyChronicleBaseUrl);
});

builder.Services.AddSingleton(serviceProvider =>
{
var config = serviceProvider.GetRequiredService<IOptions<Configuration>>().Value;
return new MongoDbStore(config.MongoDbConnectionString, config.DatabaseName);
});

builder.Services.AddSingleton<IStateService, EmptyChronicleStateService>();

builder.Services.AddHostedService<Worker>();
Expand Down
14 changes: 11 additions & 3 deletions NineChroniclesUtilBackend.Store/Scrapper/ArenaScrapper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using NineChroniclesUtilBackend.Store.Events;
using NineChroniclesUtilBackend.Store.Services;
using NineChroniclesUtilBackend.Store.Models;
using Nekoyume.TableData;
using Nekoyume.Model.Arena;
using Libplanet.Crypto;

namespace NineChroniclesUtilBackend.Store.Scrapper;

public class ArenaScrapper
{
private StateGetter _stateGetter;
public event EventHandler<ArenaDataCollectedEventArgs> OnDataCollected;

public ArenaScrapper(IStateService service)
{
_stateGetter = new StateGetter(service);
}

protected virtual void RaiseDataCollected(ArenaData arenaData, AvatarData avatarData)
{
OnDataCollected?.Invoke(this, new ArenaDataCollectedEventArgs(arenaData, avatarData));
}

public async Task ExecuteAsync()
{
var roundData = await GetArenaRoundData(900000);
Expand All @@ -24,8 +30,10 @@ public async Task ExecuteAsync()
foreach(var avatarAddress in arenaParticipants.AvatarAddresses)
{
var arenaData = await GetArenaData(roundData, avatarAddress);
var avataData = await GetAvatarData(avatarAddress);
Console.WriteLine();
var avatarData = await GetAvatarData(avatarAddress);

RaiseDataCollected(arenaData, avatarData);
break;
}
}

Expand Down
34 changes: 34 additions & 0 deletions NineChroniclesUtilBackend.Store/Services/MongoDbStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MongoDB.Bson;
using MongoDB.Driver;
using NineChroniclesUtilBackend.Store.Models;

namespace NineChroniclesUtilBackend.Store.Services;

public class MongoDbStore
{
private readonly IMongoCollection<BsonDocument> _arenaCollection;
private readonly IMongoCollection<BsonDocument> _avatarCollection;

public MongoDbStore(string connectionString, string databaseName)
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase(databaseName);

_arenaCollection = database.GetCollection<BsonDocument>("arena");
_avatarCollection = database.GetCollection<BsonDocument>("avatars");
}

public async Task SaveArenaDataAsync(ArenaData arenaData)
{
var jsonString = arenaData.ToJson();
var bsonDocument = BsonDocument.Parse(jsonString);
await _arenaCollection.InsertOneAsync(bsonDocument);
}

public async Task SaveAvatarDataAsync(AvatarData avatarData)
{
var jsonString = avatarData.ToJson();
var bsonDocument = BsonDocument.Parse(jsonString);
await _avatarCollection.InsertOneAsync(bsonDocument);
}
}
29 changes: 19 additions & 10 deletions NineChroniclesUtilBackend.Store/Worker.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
using NineChroniclesUtilBackend.Store.Events;
using NineChroniclesUtilBackend.Store.Scrapper;
using NineChroniclesUtilBackend.Store.Services;

namespace NineChroniclesUtilBackend.Store;

public class Worker : BackgroundService
{
private readonly MongoDbStore _store;
private readonly ArenaScrapper _scrapper;
private readonly ILogger<Worker> _logger;
private readonly IStateService _stateService;

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

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
if (_logger.IsEnabled(LogLevel.Information))
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
var store = new ArenaScrapper(_stateService);
await store.ExecuteAsync();

await Task.Delay(1000, stoppingToken);
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}

await _scrapper.ExecuteAsync();
}

private async void HandleDataCollected(object sender, ArenaDataCollectedEventArgs e)
{
_logger.LogInformation("{avatarAddress} Data Collected", e.AvatarData.Avatar.address);

await _store.SaveArenaDataAsync(e.ArenaData);
await _store.SaveAvatarDataAsync(e.AvatarData);
}
}
4 changes: 3 additions & 1 deletion NineChroniclesUtilBackend.Store/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
}
},
"Configuration": {
"EmptyChronicleBaseUrl": "http://localhost:5009"
"EmptyChronicleBaseUrl": "http://localhost:5009",
"MongoDbConnectionString": "mongodb://rootuser:rootpass@localhost:27017",
"DatabaseName": "9cutil_backend"
}
}

0 comments on commit aae8e1e

Please sign in to comment.