| 1 |
namespace Blog.Models; |
| 2 |
|
| 3 |
|
| 4 |
public enum DropCategory |
| 5 |
{ |
| 6 |
|
| 7 |
Mission, |
| 8 |
|
| 9 |
|
| 10 |
Key, |
| 11 |
|
| 12 |
|
| 13 |
Dynamic, |
| 14 |
|
| 15 |
|
| 16 |
Sortie, |
| 17 |
|
| 18 |
|
| 19 |
Bounty, |
| 20 |
|
| 21 |
|
| 22 |
Enemy |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public sealed record DropSource( |
| 35 |
DropCategory Category, |
| 36 |
string Location, |
| 37 |
string? Context, |
| 38 |
string Rarity, |
| 39 |
double Chance); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public sealed record RelicLine(string Relic, string Item, string Rarity, double Intact, double Radiant); |
| 46 |
|
| 47 |
|
| 48 |
public sealed class ItemDrops |
| 49 |
{ |
| 50 |
public required string Name { get; init; } |
| 51 |
|
| 52 |
|
| 53 |
public required IReadOnlyList<DropSource> Sources { get; init; } |
| 54 |
|
| 55 |
|
| 56 |
public required IReadOnlyList<RelicLine> InRelics { get; init; } |
| 57 |
|
| 58 |
|
| 59 |
public required IReadOnlyList<RelicLine> Contents { get; init; } |
| 60 |
|
| 61 |
public bool FromRelics => InRelics.Count > 0; |
| 62 |
|
| 63 |
public bool IsRelic => Contents.Count > 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
public sealed class DropTables |
| 68 |
{ |
| 69 |
public required string LastUpdate { get; init; } |
| 70 |
|
| 71 |
public required DateTimeOffset FetchedAt { get; init; } |
| 72 |
|
| 73 |
|
| 74 |
public required IReadOnlyDictionary<string, ItemDrops> Items { get; init; } |
| 75 |
|
| 76 |
|
| 77 |
public required IReadOnlyList<string> Names { get; init; } |
| 78 |
|
| 79 |
public ItemDrops? Find(string? name) => |
| 80 |
name is not null && Items.TryGetValue(name, out var item) ? item : null; |
| 81 |
|
| 82 |
|
| 83 |
public IReadOnlyList<DropSource> SourcesFor(string itemName) => Find(itemName)?.Sources ?? []; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public IReadOnlyList<string> Search(string query, int limit) |
| 91 |
{ |
| 92 |
query = query.Trim(); |
| 93 |
if (query.Length == 0) return []; |
| 94 |
|
| 95 |
var words = query.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 96 |
|
| 97 |
return Names |
| 98 |
.Select(name => (name, rank: Rank(name, query, words))) |
| 99 |
.Where(match => match.rank < int.MaxValue) |
| 100 |
.OrderBy(match => match.rank) |
| 101 |
.ThenBy(match => match.name.Length) |
| 102 |
.ThenBy(match => match.name, StringComparer.OrdinalIgnoreCase) |
| 103 |
.Take(limit) |
| 104 |
.Select(match => match.name) |
| 105 |
.ToArray(); |
| 106 |
} |
| 107 |
|
| 108 |
private static int Rank(string name, string query, string[] words) |
| 109 |
{ |
| 110 |
if (name.Equals(query, StringComparison.OrdinalIgnoreCase)) return 0; |
| 111 |
if (name.StartsWith(query, StringComparison.OrdinalIgnoreCase)) return 1; |
| 112 |
if (name.Contains(query, StringComparison.OrdinalIgnoreCase)) return 2; |
| 113 |
|
| 114 |
foreach (var word in words) |
| 115 |
{ |
| 116 |
if (!name.Contains(word, StringComparison.OrdinalIgnoreCase)) return int.MaxValue; |
| 117 |
} |
| 118 |
|
| 119 |
return 3; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
public sealed record DropTableStatus(DropTables? Tables, string? Error) |
| 125 |
{ |
| 126 |
public static DropTableStatus Ready(DropTables tables) => new(tables, null); |
| 127 |
|
| 128 |
public static DropTableStatus Failed(string error, DropTables? stale = null) => new(stale, error); |
| 129 |
} |