| 1 |
using System.Diagnostics; |
| 2 |
using Blog.Models; |
| 3 |
using Microsoft.Extensions.Options; |
| 4 |
|
| 5 |
namespace Blog.Services; |
| 6 |
|
| 7 |
|
| 8 |
public sealed class WarframeOptions |
| 9 |
{ |
| 10 |
public const string Section = "Warframe"; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public string FilePath { get; set; } = "Resources/droptables.html"; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public sealed class WarframeDropService( |
| 33 |
IOptions<WarframeOptions> options, |
| 34 |
ILogger<WarframeDropService> logger) |
| 35 |
{ |
| 36 |
private readonly WarframeOptions _options = options.Value; |
| 37 |
private readonly SemaphoreSlim _gate = new(1, 1); |
| 38 |
|
| 39 |
private DropTables? _tables; |
| 40 |
private DateTimeOffset _parsedStamp; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private string Path => System.IO.Path.Combine(AppContext.BaseDirectory, _options.FilePath); |
| 49 |
|
| 50 |
public async Task<DropTableStatus> GetTablesAsync(CancellationToken cancellationToken = default) |
| 51 |
{ |
| 52 |
var file = new FileInfo(Path); |
| 53 |
if (!file.Exists) |
| 54 |
{ |
| 55 |
logger.LogError("The drop tables are missing from {Path}", Path); |
| 56 |
return DropTableStatus.Failed($"The drop tables are missing from {_options.FilePath}.", _tables); |
| 57 |
} |
| 58 |
|
| 59 |
var stamp = new DateTimeOffset(file.LastWriteTimeUtc, TimeSpan.Zero); |
| 60 |
if (_tables is { } fresh && stamp == _parsedStamp) |
| 61 |
{ |
| 62 |
return DropTableStatus.Ready(fresh); |
| 63 |
} |
| 64 |
|
| 65 |
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 66 |
try |
| 67 |
{ |
| 68 |
|
| 69 |
if (_tables is { } current && stamp == _parsedStamp) |
| 70 |
{ |
| 71 |
return DropTableStatus.Ready(current); |
| 72 |
} |
| 73 |
|
| 74 |
var tables = await ParseAsync(stamp, cancellationToken).ConfigureAwait(false); |
| 75 |
_tables = tables; |
| 76 |
_parsedStamp = stamp; |
| 77 |
return DropTableStatus.Ready(tables); |
| 78 |
} |
| 79 |
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) |
| 80 |
{ |
| 81 |
logger.LogError(ex, "Could not read the drop tables from {Path}", Path); |
| 82 |
return DropTableStatus.Failed($"Could not read the drop tables ({ex.Message})", _tables); |
| 83 |
} |
| 84 |
finally |
| 85 |
{ |
| 86 |
_gate.Release(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private async Task<DropTables> ParseAsync(DateTimeOffset stamp, CancellationToken cancellationToken) |
| 91 |
{ |
| 92 |
var stopwatch = Stopwatch.StartNew(); |
| 93 |
var html = await File.ReadAllTextAsync(Path, cancellationToken).ConfigureAwait(false); |
| 94 |
var tables = DropTableParser.Parse(html, stamp); |
| 95 |
|
| 96 |
logger.LogInformation( |
| 97 |
"Read the Warframe drop tables ({Update}): {Bytes} bytes, {Items} items in {Elapsed} ms", |
| 98 |
tables.LastUpdate, html.Length, tables.Names.Count, stopwatch.ElapsedMilliseconds); |
| 99 |
|
| 100 |
return tables; |
| 101 |
} |
| 102 |
} |