-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
119 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
NineChroniclesUtilBackend.Store/Events/ArenaDataCollectedEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters