mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
code style
This commit is contained in:
@@ -128,5 +128,5 @@ internal sealed partial class SettingEntry
|
||||
/// </summary>
|
||||
public const string GeetestCustomCompositeUrl = "GeetestCustomCompositeUrl";
|
||||
|
||||
public const string Region = "Region";
|
||||
public const string AnnouncementRegion = "AnnouncementRegion";
|
||||
}
|
||||
|
||||
@@ -78,8 +78,8 @@ internal sealed partial class AppOptions : DbStoreOptions
|
||||
|
||||
public Region Region
|
||||
{
|
||||
get => GetOption(ref region, SettingEntry.Region, v => Region.FromRegion(v), Regions[0].Value).Value;
|
||||
set => SetOption(ref region, SettingEntry.Region, value, value => value.ToStringOrEmpty());
|
||||
get => GetOption(ref region, SettingEntry.AnnouncementRegion, v => Region.FromRegionString(v), Region.CNGF01).Value;
|
||||
set => SetOption(ref region, SettingEntry.AnnouncementRegion, value, value => value.ToStringOrEmpty());
|
||||
}
|
||||
|
||||
public string GeetestCustomCompositeUrl
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Model;
|
||||
using Snap.Hutao.Web.Hoyolab;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Snap.Hutao.Service;
|
||||
@@ -12,4 +13,9 @@ internal static class AppOptionsExtension
|
||||
{
|
||||
return appOptions.Cultures.SingleOrDefault(c => c.Value == appOptions.CurrentCulture);
|
||||
}
|
||||
|
||||
public static NameValue<Region>? GetCurrentRegionForSelectionOrDefault(this AppOptions appOptions)
|
||||
{
|
||||
return appOptions.Regions.SingleOrDefault(c => c.Value.Value == appOptions.Region.Value);
|
||||
}
|
||||
}
|
||||
@@ -8,28 +8,16 @@ namespace Snap.Hutao.Service;
|
||||
|
||||
internal static class KnownRegions
|
||||
{
|
||||
private static readonly Region RegionCNGF01 = new("cn_gf01");
|
||||
private static readonly Region RegionCNQD01 = new("cn_qd01");
|
||||
private static readonly Region RegionOSUSA = new("os_usa");
|
||||
private static readonly Region RegionOSEURO = new("os_euro");
|
||||
private static readonly Region RegionOSASIA = new("os_asia");
|
||||
private static readonly Region RegionOSCHT = new("os_cht");
|
||||
|
||||
public static List<NameValue<Region>> Get()
|
||||
{
|
||||
return
|
||||
[
|
||||
ToNameValue(RegionCNGF01),
|
||||
ToNameValue(RegionCNQD01),
|
||||
ToNameValue(RegionOSUSA),
|
||||
ToNameValue(RegionOSEURO),
|
||||
ToNameValue(RegionOSASIA),
|
||||
ToNameValue(RegionOSCHT),
|
||||
new(SH.WebHoyolabRegionCNGF01, Region.CNGF01),
|
||||
new(SH.WebHoyolabRegionCNQD01, Region.CNQD01),
|
||||
new(SH.WebHoyolabRegionOSUSA, Region.OSUSA),
|
||||
new(SH.WebHoyolabRegionOSEURO, Region.OSEURO),
|
||||
new(SH.WebHoyolabRegionOSASIA, Region.OSASIA),
|
||||
new(SH.WebHoyolabRegionOSCHT, Region.OSCHT),
|
||||
];
|
||||
}
|
||||
|
||||
private static NameValue<Region> ToNameValue(in Region region)
|
||||
{
|
||||
return new(region.DisplayName, region);
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,9 @@ internal static class SupportedCultures
|
||||
ToNameValue(CultureInfo.GetCultureInfo("zh-Hans")),
|
||||
ToNameValue(CultureInfo.GetCultureInfo("zh-Hant")),
|
||||
ToNameValue(CultureInfo.GetCultureInfo("en")),
|
||||
ToNameValue(CultureInfo.GetCultureInfo("ko")),
|
||||
ToNameValue(CultureInfo.GetCultureInfo("ja")),
|
||||
ToNameValue(CultureInfo.GetCultureInfo("id")),
|
||||
ToNameValue(CultureInfo.GetCultureInfo("ko")),
|
||||
];
|
||||
|
||||
public static List<NameValue<CultureInfo>> Get()
|
||||
|
||||
@@ -108,7 +108,7 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
|
||||
|
||||
public NameValue<Region>? SelectedRegion
|
||||
{
|
||||
get => selectedRegion ??= AppOptions.Regions.Single(t => t.Value.Value == AppOptions.Region.Value);
|
||||
get => selectedRegion ??= AppOptions.GetCurrentRegionForSelectionOrDefault();
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref selectedRegion, value) && value is not null)
|
||||
|
||||
@@ -29,11 +29,9 @@ internal sealed partial class AnnouncementClient
|
||||
/// <returns>公告列表</returns>
|
||||
public async ValueTask<Response<AnnouncementWrapper>> GetAnnouncementsAsync(string languageCode, Region region, CancellationToken token = default)
|
||||
{
|
||||
string annListUrl = region.IsOversea switch
|
||||
{
|
||||
true => ApiOsEndpoints.AnnList(languageCode, region.Value),
|
||||
false => ApiEndpoints.AnnList(languageCode, region.Value),
|
||||
};
|
||||
string annListUrl = region.IsOversea()
|
||||
? ApiOsEndpoints.AnnList(languageCode, region)
|
||||
: ApiEndpoints.AnnList(languageCode, region);
|
||||
|
||||
HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create()
|
||||
.SetRequestUri(annListUrl)
|
||||
@@ -55,11 +53,9 @@ internal sealed partial class AnnouncementClient
|
||||
/// <returns>公告内容列表</returns>
|
||||
public async ValueTask<Response<ListWrapper<AnnouncementContent>>> GetAnnouncementContentsAsync(string languageCode, Region region, CancellationToken token = default)
|
||||
{
|
||||
string annContentUrl = region.IsOversea switch
|
||||
{
|
||||
true => ApiOsEndpoints.AnnContent(languageCode, region.Value),
|
||||
false => ApiEndpoints.AnnContent(languageCode, region.Value),
|
||||
};
|
||||
string annContentUrl = region.IsOversea()
|
||||
? ApiOsEndpoints.AnnContent(languageCode, region)
|
||||
: ApiEndpoints.AnnContent(languageCode, region);
|
||||
|
||||
HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create()
|
||||
.SetRequestUri(annContentUrl)
|
||||
|
||||
@@ -28,7 +28,7 @@ internal readonly partial struct PlayerUid
|
||||
{
|
||||
Must.Argument(HoyolabRegex.UidRegex().IsMatch(value), SH.WebHoyolabInvalidUid);
|
||||
Value = value;
|
||||
Region = region ?? Region.FromUid(value);
|
||||
Region = region ?? Region.FromUidString(value);
|
||||
}
|
||||
|
||||
public static implicit operator PlayerUid(string source)
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
|
||||
namespace Snap.Hutao.Web.Hoyolab;
|
||||
|
||||
[JsonConverter(typeof(RegionConverter))]
|
||||
internal readonly partial struct Region
|
||||
{
|
||||
public readonly string DisplayName;
|
||||
public static readonly Region CNGF01 = new("cn_gf01");
|
||||
public static readonly Region CNQD01 = new("cn_qd01");
|
||||
public static readonly Region OSUSA = new("os_usa");
|
||||
public static readonly Region OSEURO = new("os_euro");
|
||||
public static readonly Region OSASIA = new("os_asia");
|
||||
public static readonly Region OSCHT = new("os_cht");
|
||||
|
||||
public readonly string Value;
|
||||
|
||||
public readonly bool IsOversea;
|
||||
|
||||
public Region(string value, bool? isOversea = default)
|
||||
public Region(string value)
|
||||
{
|
||||
Must.Argument(HoyolabRegex.RegionRegex().IsMatch(value), SH.WebHoyolabInvalidRegion);
|
||||
DisplayName = EvaluateDisplayName(value);
|
||||
Value = value;
|
||||
IsOversea = isOversea ?? EvaluateIsOversea(value.AsSpan()[0]);
|
||||
}
|
||||
|
||||
public static implicit operator Region(string value)
|
||||
{
|
||||
return FromRegion(value);
|
||||
return FromRegionString(value);
|
||||
}
|
||||
|
||||
public static Region FromRegion(string value)
|
||||
public static Region FromRegionString(string value)
|
||||
{
|
||||
return new(value);
|
||||
}
|
||||
|
||||
public static Region FromUid(string uid)
|
||||
public static Region FromUidString(string uid)
|
||||
{
|
||||
return uid.AsSpan()[0] switch
|
||||
{
|
||||
@@ -47,32 +48,23 @@ internal readonly partial struct Region
|
||||
};
|
||||
}
|
||||
|
||||
public static bool IsOversea(string value)
|
||||
{
|
||||
Must.Argument(HoyolabRegex.RegionRegex().IsMatch(value), SH.WebHoyolabInvalidRegion);
|
||||
return value.AsSpan()[..2] switch
|
||||
{
|
||||
"os" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
public readonly bool IsOversea()
|
||||
{
|
||||
return IsOversea(Value);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
|
||||
private static string EvaluateDisplayName(string value)
|
||||
{
|
||||
return value switch
|
||||
{
|
||||
"cn_gf01" => SH.WebHoyolabRegionCNGF01,
|
||||
"cn_qd01" => SH.WebHoyolabRegionCNQD01,
|
||||
"os_usa" => SH.WebHoyolabRegionOSUSA,
|
||||
"os_euro" => SH.WebHoyolabRegionOSEURO,
|
||||
"os_asia" => SH.WebHoyolabRegionOSASIA,
|
||||
"os_cht" => SH.WebHoyolabRegionOSCHT,
|
||||
_ => throw Must.NeverHappen(),
|
||||
};
|
||||
}
|
||||
|
||||
private static bool EvaluateIsOversea(in char first)
|
||||
{
|
||||
return first switch
|
||||
{
|
||||
'c' => false,
|
||||
'o' => true,
|
||||
_ => throw Must.NeverHappen(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,14 +9,14 @@ internal sealed class RegionConverter : JsonConverter<Region>
|
||||
{
|
||||
if (reader.GetString() is { } regionValue)
|
||||
{
|
||||
return Region.FromRegion(regionValue);
|
||||
return Region.FromRegionString(regionValue);
|
||||
}
|
||||
|
||||
return default;
|
||||
throw new JsonException();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Region value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString());
|
||||
writer.WriteStringValue(value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,6 @@ internal sealed class GenAuthKeyData
|
||||
/// 区域
|
||||
/// </summary>
|
||||
[JsonPropertyName("region")]
|
||||
[JsonConverter(typeof(RegionConverter))]
|
||||
public Region Region { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -19,7 +19,6 @@ internal sealed class UserGameRole
|
||||
/// 服务器
|
||||
/// </summary>
|
||||
[JsonPropertyName("region")]
|
||||
[JsonConverter(typeof(RegionConverter))]
|
||||
public Region Region { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -31,7 +31,6 @@ internal sealed class SignInData
|
||||
/// 地区代码
|
||||
/// </summary>
|
||||
[JsonPropertyName("region")]
|
||||
[JsonConverter(typeof(RegionConverter))]
|
||||
public Region Region { get; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -181,7 +181,6 @@ internal sealed partial class CalculateClient
|
||||
public string Uid { get; set; } = default!;
|
||||
|
||||
[JsonPropertyName("region")]
|
||||
[JsonConverter(typeof(RegionConverter))]
|
||||
public Region Region { get; set; } = default!;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,5 @@ internal sealed class CharacterData
|
||||
/// 服务器
|
||||
/// </summary>
|
||||
[JsonPropertyName("server")]
|
||||
[JsonConverter(typeof(RegionConverter))]
|
||||
public Region Server { get; }
|
||||
}
|
||||
@@ -25,7 +25,7 @@ internal sealed class Role
|
||||
/// 服务器名称
|
||||
/// </summary>
|
||||
[JsonPropertyName("region")]
|
||||
public string Region { get; set; } = default!;
|
||||
public string RegionName { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 等级
|
||||
|
||||
@@ -26,7 +26,7 @@ internal sealed class BasicRoleInfo
|
||||
/// 服务器名称
|
||||
/// </summary>
|
||||
[JsonPropertyName("region")]
|
||||
public string Region { get; set; } = default!;
|
||||
public string RegionName { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 等级
|
||||
|
||||
Reference in New Issue
Block a user