2024-06-16 19:28:34 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
2022-09-08 03:13:55 +00:00
|
|
|
using OrangeGuidanceTomestone.Helpers;
|
2022-09-03 12:59:06 +00:00
|
|
|
|
2022-09-03 02:59:45 +00:00
|
|
|
namespace OrangeGuidanceTomestone;
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
public class Pack {
|
2022-09-08 03:13:55 +00:00
|
|
|
internal static SemaphoreSlim AllMutex { get; } = new(1, 1);
|
2024-06-12 18:16:53 +00:00
|
|
|
internal static Pack[] All { get; set; } = [];
|
2024-06-16 19:28:34 +00:00
|
|
|
private static readonly JsonSerializerOptions Options = new() {
|
|
|
|
Converters = {
|
|
|
|
new TemplateConverter(),
|
|
|
|
},
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
|
|
|
};
|
2022-09-03 12:59:06 +00:00
|
|
|
|
2022-09-03 02:59:45 +00:00
|
|
|
public string Name { get; init; }
|
|
|
|
public Guid Id { get; init; }
|
2024-06-15 22:42:22 +00:00
|
|
|
|
2024-06-16 19:28:34 +00:00
|
|
|
public Template[] Templates { get; init; }
|
2024-06-15 22:42:22 +00:00
|
|
|
|
|
|
|
public string[]? Conjunctions { get; init; }
|
|
|
|
public List<WordList>? Words { get; init; }
|
2022-09-08 03:13:55 +00:00
|
|
|
|
|
|
|
internal static void UpdatePacks() {
|
|
|
|
Task.Run(async () => {
|
|
|
|
var resp = await ServerHelper.SendRequest(null, HttpMethod.Get, "/packs");
|
|
|
|
var json = await resp.Content.ReadAsStringAsync();
|
2024-06-16 19:28:34 +00:00
|
|
|
var packs = JsonSerializer.Deserialize<Pack[]>(json, Pack.Options)!;
|
2022-09-08 03:13:55 +00:00
|
|
|
await AllMutex.WaitAsync();
|
2023-01-22 03:42:03 +00:00
|
|
|
try {
|
|
|
|
All = packs;
|
|
|
|
} finally {
|
|
|
|
AllMutex.Release();
|
|
|
|
}
|
2022-09-08 03:13:55 +00:00
|
|
|
});
|
|
|
|
}
|
2022-09-03 02:59:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 19:28:34 +00:00
|
|
|
public class Template {
|
|
|
|
[JsonPropertyName("template")]
|
|
|
|
public string Text { get; init; }
|
|
|
|
public string[]? Words { get; init; }
|
2024-06-15 22:42:22 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 19:28:34 +00:00
|
|
|
public class TemplateConverter : JsonConverter<Template> {
|
|
|
|
private static JsonSerializerOptions RemoveSelf(JsonSerializerOptions old) {
|
|
|
|
var newOptions = new JsonSerializerOptions(old);
|
|
|
|
for (var i = 0; i < old.Converters.Count; i++) {
|
|
|
|
if (old.Converters[i] is TemplateConverter) {
|
|
|
|
newOptions.Converters.RemoveAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-06-15 22:42:22 +00:00
|
|
|
|
2024-06-16 19:28:34 +00:00
|
|
|
return newOptions;
|
|
|
|
}
|
2024-06-15 22:42:22 +00:00
|
|
|
|
2024-06-16 19:28:34 +00:00
|
|
|
public override Template? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
|
|
|
|
switch (reader.TokenType) {
|
|
|
|
case JsonTokenType.String: {
|
|
|
|
var template = reader.GetString() ?? throw new JsonException("template cannot be null");
|
|
|
|
return new Template {
|
|
|
|
Text = template,
|
|
|
|
Words = null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case JsonTokenType.StartObject: {
|
|
|
|
var newOptions = TemplateConverter.RemoveSelf(options);
|
|
|
|
return JsonSerializer.Deserialize<Template>(ref reader, newOptions);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new JsonException("unexpected template type");
|
2024-06-15 22:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 19:28:34 +00:00
|
|
|
public override void Write(Utf8JsonWriter writer, Template value, JsonSerializerOptions options) {
|
|
|
|
if (value.Words == null) {
|
|
|
|
JsonSerializer.Serialize(writer, value.Text, options);
|
2024-06-15 22:42:22 +00:00
|
|
|
} else {
|
2024-06-16 19:28:34 +00:00
|
|
|
var newOptions = TemplateConverter.RemoveSelf(options);
|
|
|
|
JsonSerializer.Serialize(writer, value, newOptions);
|
2024-06-15 22:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 02:59:45 +00:00
|
|
|
[Serializable]
|
|
|
|
public class WordList {
|
|
|
|
public string Name { get; init; }
|
|
|
|
public string[] Words { get; init; }
|
|
|
|
}
|