Support daily notes for global server player

This commit is contained in:
Xhichn
2023-03-13 12:16:38 +08:00
parent 388cdf1848
commit b0b3553d0c
2 changed files with 47 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ using Snap.Hutao.Model.Entity.Database;
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Service.Game;
using Snap.Hutao.Service.User;
using Snap.Hutao.Web.Hoyolab;
using Snap.Hutao.Web.Hoyolab.Takumi.GameRecord;
using System.Collections.ObjectModel;
using WebDailyNote = Snap.Hutao.Web.Hoyolab.Takumi.GameRecord.DailyNote.DailyNote;
@@ -61,14 +62,27 @@ internal sealed class DailyNoteService : IDailyNoteService, IRecipient<UserRemov
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
GameRecordClient gameRecordClient = scope.ServiceProvider.GetRequiredService<GameRecordClient>();
GameRecordClientOs gameRecordClientOs = scope.ServiceProvider.GetRequiredService<GameRecordClientOs>();
if (!appDbContext.DailyNotes.Any(n => n.Uid == roleUid))
{
DailyNoteEntry newEntry = DailyNoteEntry.Create(role);
Web.Response.Response<WebDailyNote> dailyNoteResponse = await gameRecordClient
// 根据 Uid 的地区选择不同的 API
Web.Response.Response<WebDailyNote> dailyNoteResponse;
PlayerUid playerUid = new(roleUid);
if (playerUid.Region == "cn_gf01" || playerUid.Region == "cn_qd01")
{
dailyNoteResponse = await gameRecordClient
.GetDailyNoteAsync(role)
.ConfigureAwait(false);
}
else
{
dailyNoteResponse = await gameRecordClientOs
.GetDailyNoteAsync(role)
.ConfigureAwait(false);
}
if (dailyNoteResponse.IsOk())
{
@@ -110,11 +124,13 @@ internal sealed class DailyNoteService : IDailyNoteService, IRecipient<UserRemov
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
GameRecordClient gameRecordClient = scope.ServiceProvider.GetRequiredService<GameRecordClient>();
GameRecordClientOs gameRecordClientOs = scope.ServiceProvider.GetRequiredService<GameRecordClientOs>();
bool isSilentMode = appDbContext.Settings
.SingleOrAdd(SettingEntry.DailyNoteSilentWhenPlayingGame, Core.StringLiterals.False)
.GetBoolean();
bool isGameRunning = scope.ServiceProvider.GetRequiredService<IGameService>().IsGameRunning();
if (isSilentMode && isGameRunning)
{
// Prevent notify when we are in game && silent mode.
@@ -123,9 +139,20 @@ internal sealed class DailyNoteService : IDailyNoteService, IRecipient<UserRemov
foreach (DailyNoteEntry entry in appDbContext.DailyNotes.Include(n => n.User))
{
Web.Response.Response<WebDailyNote> dailyNoteResponse = await gameRecordClient
Web.Response.Response<WebDailyNote> dailyNoteResponse;
PlayerUid playerUid = new(entry.Uid);
if (playerUid.Region == "cn_gf01" || playerUid.Region == "cn_qd01")
{
dailyNoteResponse = await gameRecordClient
.GetDailyNoteAsync(new(entry.User, entry.Uid))
.ConfigureAwait(false);
}
else
{
dailyNoteResponse = await gameRecordClientOs
.GetDailyNoteAsync(new(entry.User, entry.Uid))
.ConfigureAwait(false);
}
if (dailyNoteResponse.ReturnCode == 0)
{

View File

@@ -36,6 +36,24 @@ internal sealed class GameRecordClientOs
this.logger = logger;
}
/// <summary>
/// 异步获取实时便笺
/// </summary>
/// <param name="userAndUid">用户与角色</param>
/// <param name="token">取消令牌</param>
/// <returns>实时便笺</returns>
[ApiInformation(Cookie = CookieType.Cookie, Salt = SaltType.OS)]
public async Task<Response<DailyNote.DailyNote>> GetDailyNoteAsync(UserAndUid userAndUid, CancellationToken token = default)
{
Response<DailyNote.DailyNote>? resp = await httpClient
.SetUser(userAndUid.User, CookieType.Cookie)
.UseDynamicSecret(DynamicSecretVersion.Gen1, SaltType.OS, false)
.TryCatchGetFromJsonAsync<Response<DailyNote.DailyNote>>(ApiOsEndpoints.GameRecordDailyNote(userAndUid.Uid.Value), options, logger, token)
.ConfigureAwait(false);
return Response.Response.DefaultIfNull(resp);
}
/// <summary>
/// 获取玩家基础信息
/// </summary>