Skip to content

Commit

Permalink
game banner and post and notices
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Dec 16, 2024
1 parent e29354a commit aa7776b
Show file tree
Hide file tree
Showing 18 changed files with 953 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Starward.Core/GameBiz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Starward.Core;
public record struct GameBiz
{


public string Value { get; init; }
private string _value;
public string Value => _value ?? "";


public string Game => Value.Contains("_") ? Value.Substring(0, Value.IndexOf('_')) : Value;
Expand All @@ -18,7 +18,7 @@ public record struct GameBiz

public GameBiz(string? value)
{
Value = value ?? "";
_value = value ?? "";
}


Expand Down
18 changes: 18 additions & 0 deletions src/Starward.Core/GameNotice/AlertAnn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;

namespace Starward.Core.GameNotice;

internal class AlertAnn
{
[JsonPropertyName("alert")]
public bool Alert { get; set; }

[JsonPropertyName("alert_id")]
public int AlertId { get; set; }

[JsonPropertyName("remind")]
public bool Remind { get; set; }

[JsonPropertyName("extra_remind")]
public bool ExtraRemind { get; set; }
}
89 changes: 89 additions & 0 deletions src/Starward.Core/GameNotice/GameNoticeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Net;
using System.Net.Http.Json;

namespace Starward.Core.GameNotice;

public class GameNoticeClient
{

private readonly HttpClient _httpClient;



public GameNoticeClient(HttpClient? httpClient = null)
{
_httpClient = httpClient ?? new(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All }) { DefaultRequestVersion = HttpVersion.Version20 };
}




private async Task<T> CommonSendAsync<T>(HttpRequestMessage request, CancellationToken cancellationToken = default) where T : class
{
request.Version = HttpVersion.Version20;
var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync(typeof(miHoYoApiWrapper<T>), GameNoticeJsonContext.Default, cancellationToken) as miHoYoApiWrapper<T>;
if (responseData is null)
{
throw new miHoYoApiException(-1, "Can not parse the response body.");
}
if (responseData.Retcode != 0)
{
throw new miHoYoApiException(responseData.Retcode, responseData.Message);
}
return responseData.Data;
}




public static string GetGameNoticeUrl(GameBiz biz, long uid, string? lang = null)
{
lang = LanguageUtil.FilterLanguage(lang);
uid = uid == 0 ? 100000000 : uid;
return biz.Value switch
{
GameBiz.hk4e_cn or GameBiz.clgm_cn or GameBiz.hk4e_bilibili => $"https://webstatic.mihoyo.com/hk4e/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=hk4e_cn&channel_id=1&game=hk4e&game_biz=hk4e_cn&lang={lang}&level=60&platform=pc&region=cn_gf01&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}",
GameBiz.hk4e_global => $"https://sdk.hoyoverse.com/hk4e/announcement/index.html?announcement_version=1.37&auth_appid=announcement&bundle_id=hk4e_global&channel_id=1&game=hk4e&game_biz=hk4e_global&lang={lang}&level=60&platform=pc&region=os_asia&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&uid={uid}",
GameBiz.hkrpg_cn or GameBiz.hkrpg_bilibili => $"https://webstatic.mihoyo.com/hkrpg/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=hkrpg_cn&channel_id=1&game=hkrpg&game_biz=hkrpg_cn&lang={lang}&level=70&platform=pc&region=prod_gf_cn&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}",
GameBiz.hkrpg_global => $"https://sdk.hoyoverse.com/hkrpg/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=hkrpg_global&channel_id=1&game=hkrpg&game_biz=hkrpg_global&lang={lang}&level=1&platform=pc&region=prod_official_asia&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}",
GameBiz.bh3_cn => $"https://webstatic.mihoyo.com/bh3/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=bh3_cn&channel_id=1&game=bh3&game_biz=bh3_cn&lang=zh-cn&level=88&platform=pc&region=android01&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}",
GameBiz.bh3_global or GameBiz.bh3_jp or GameBiz.bh3_kr or GameBiz.bh3_os or GameBiz.bh3_asia => $"https://sdk.hoyoverse.com/bh3/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=bh3_os&channel_id=1&game=bh3&game_biz=bh3_os&lang={lang}&level=88&platform=pc&region=overseas01&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}",
GameBiz.nap_cn or GameBiz.nap_bilibili => $"https://sdk.mihoyo.com/nap/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=nap_cn&channel_id=1&game=nap&game_biz=nap_cn&lang=zh-cn&level=60&platform=pc&region=prod_gf_cn&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}&version=2.27",
GameBiz.nap_global => $"https://sdk.hoyoverse.com/nap/announcement/index.html?auth_appid=announcement&authkey_ver=1&bundle_id=nap_global&channel_id=1&game=nap&game_biz=nap_global&lang={lang}&level=60&platform=pc&region=prod_gf_jp&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid={uid}&version=2.27",
_ => throw new ArgumentOutOfRangeException($"Unknown region {biz}"),
};
}



public async Task<bool> IsNoticeAlertAsync(GameBiz biz, long uid, string? lang = null, CancellationToken cancellationToken = default)
{
lang = LanguageUtil.FilterLanguage(lang);
uid = uid == 0 ? 100000000 : uid;
string url = biz.Value switch
{
GameBiz.hk4e_cn or GameBiz.clgm_cn or GameBiz.hk4e_bilibili => $"https://hk4e-ann-api.mihoyo.com/common/hk4e_cn/announcement/api/getAlertAnn?bundle_id=hk4e_cn&channel_id=1&game=hk4e&game_biz=hk4e_cn&lang={lang}&level=60&platform=pc&region=cn_gf01&uid={uid}",
GameBiz.hk4e_global => $"https://sg-hk4e-api.hoyoverse.com/common/hk4e_global/announcement/api/getAlertAnn?game=hk4e&game_biz=hk4e_global&lang={lang}&bundle_id=hk4e_global&channel_id=1&level=60&platform=pc&region=os_asia&uid={uid}",
GameBiz.hkrpg_cn or GameBiz.hkrpg_bilibili => $"https://hkrpg-ann-api.mihoyo.com/common/hkrpg_cn/announcement/api/getAlertAnn?bundle_id=hkrpg_cn&channel_id=1&game=hkrpg&game_biz=hkrpg_cn&lang={lang}&level=70&platform=pc&region=prod_gf_cn&uid={uid}",
GameBiz.hkrpg_global => $"https://sg-hkrpg-api.hoyoverse.com/common/hkrpg_global/announcement/api/getAlertAnn?bundle_id=hkrpg_global&channel_id=1&game=hkrpg&game_biz=hkrpg_global&lang={lang}&level=1&platform=pc&region=prod_official_asia&uid={uid}",
GameBiz.bh3_cn => $"https://ann-api.mihoyo.com/common/bh3_cn/announcement/api/getAlertAnn?game=bh3&game_biz=bh3_cn&lang={lang}&bundle_id=bh3_cn&platform=pc&region=android01&level=88&channel_id=1&uid={uid}",
GameBiz.bh3_global or GameBiz.bh3_jp or GameBiz.bh3_kr or GameBiz.bh3_os or GameBiz.bh3_asia => $"https://sg-public-api.hoyoverse.com/common/bh3_global/announcement/api/getAlertAnn?game=bh3&game_biz=bh3_global&lang={lang}&bundle_id=bh3_os&platform=pc&region=overseas01&level=88&channel_id=1&uid={uid}",
GameBiz.nap_cn or GameBiz.hk4e_bilibili => $"https://announcement-api.mihoyo.com/common/nap_cn/announcement/api/getAlertAnn?bundle_id=nap_cn&channel_id=1&game=nap&game_biz=nap_cn&lang=zh-cn&level=60&platform=pc&region=prod_gf_cn&uid={uid}",
GameBiz.nap_global => $"https://sg-announcement-api.hoyoverse.com/common/nap_global/announcement/api/getAlertAnn?bundle_id=nap_global&channel_id=1&game=nap&game_biz=nap_global&lang={lang}&level=60&platform=pc&region=prod_gf_jp&uid={uid}",
_ => throw new ArgumentOutOfRangeException($"Unknown region {biz}"),
};
var request = new HttpRequestMessage(HttpMethod.Get, url);
var alertAnn = await CommonSendAsync<AlertAnn>(request, cancellationToken);
return alertAnn.Remind || alertAnn.ExtraRemind;
}








}
10 changes: 10 additions & 0 deletions src/Starward.Core/GameNotice/GameNoticeJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace Starward.Core.GameNotice;


[JsonSerializable(typeof(miHoYoApiWrapper<AlertAnn>))]
internal partial class GameNoticeJsonContext : JsonSerializerContext
{

}
11 changes: 11 additions & 0 deletions src/Starward/Features/Background/AppBackground.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public GameId CurrentGameId
{
if (field is null)
{
field = value;
InitializeBackgroundImage();
}
field = value;
Expand All @@ -75,6 +76,9 @@ public ImageSource? BackgroundImageSource
}


private string? lastBackgroundFile;



private MediaPlayer? mediaPlayer;

Expand Down Expand Up @@ -105,6 +109,7 @@ private void InitializeBackgroundImage()
if (!BackgroundService.FileIsSupportedVideo(file))
{
BackgroundImageSource = new BitmapImage(new Uri(file));
lastBackgroundFile = file;
try
{
string? hex = AppConfig.AccentColor;
Expand All @@ -120,6 +125,7 @@ private void InitializeBackgroundImage()
else
{
BackgroundImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Image/UI_CutScene_1130320101A.png"));
lastBackgroundFile = file;
}
}
catch (Exception ex)
Expand Down Expand Up @@ -150,6 +156,10 @@ public async Task UpdateBackgroundAsync()
}

var file = await _backgroundService.GetBackgroundFileAsync(CurrentGameId);
if (file == lastBackgroundFile)
{
return;
}
if (file != null)
{
if (BackgroundService.FileIsSupportedVideo(file))
Expand All @@ -160,6 +170,7 @@ public async Task UpdateBackgroundAsync()
{
await ChangeBackgroundImageAsync(file, cancelSource.Token);
}
lastBackgroundFile = file;
}
}
catch (COMException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
Storyboard.TargetName="PlaceholderImage"
Storyboard.TargetProperty="Opacity"
To="0"
Duration="0:0:0.6" />
Duration="0:0:0.3" />
<DoubleAnimation AutoReverse="False"
BeginTime="0"
Storyboard.TargetName="Image"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0:0:0.6" />
Duration="0:0:0.3" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unloaded">
Expand All @@ -64,13 +64,13 @@
Storyboard.TargetName="PlaceholderImage"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0:0:0.6" />
Duration="0:0:0.3" />
<DoubleAnimation AutoReverse="False"
BeginTime="0"
Storyboard.TargetName="Image"
Storyboard.TargetProperty="Opacity"
To="0"
Duration="0:0:0.6" />
Duration="0:0:0.3" />
</Storyboard>
</VisualState>
</VisualStateGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Starward/Features/Background/BackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public BackgroundService(ILogger<BackgroundService> logger, HoYoPlayService hoYo
private static bool TryGetCustomBgFilePath(GameId gameId, out string? path)
{
path = null;
if (gameId is null)
{
return false;
}
if (AppSetting.GetEnableCustomBg(gameId.GameBiz))
{
path = GetCustomBgFilePath(AppSetting.GetCustomBg(gameId.GameBiz));
Expand Down
40 changes: 36 additions & 4 deletions src/Starward/Features/GameFeatureConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,31 @@ private GameFeatureConfig()
}


/// <summary>
/// 支持的页面
/// </summary>
public List<string> SupportedPages { get; init; } = [];


/// <summary>
/// 游戏内通知内容
/// </summary>
public bool InGameNoticesWindow { get; init; }



public static GameFeatureConfig FromGameId(GameId? gameId)
{
if (gameId is null)
{
return None;
}
return gameId.GameBiz.Value switch
return gameId.GameBiz.Game switch
{
GameBiz.bh3_cn => bh3_cn,
GameBiz.bh3 => bh3,
GameBiz.hk4e => hk4e,
GameBiz.hkrpg => hkrpg,
GameBiz.nap => nap,
_ => Default,
};
}
Expand All @@ -45,12 +57,32 @@ public static GameFeatureConfig FromGameId(GameId? gameId)
};


private static readonly GameFeatureConfig bh3_cn = new()
private static readonly GameFeatureConfig bh3 = new()
{
SupportedPages = [nameof(GameLauncherPage)],
InGameNoticesWindow = true,
};


private static readonly GameFeatureConfig hk4e = new()
{
SupportedPages = [nameof(GameLauncherPage), "GameSettingPage"],
SupportedPages = [nameof(GameLauncherPage)],
InGameNoticesWindow = true,
};


private static readonly GameFeatureConfig hkrpg = new()
{
SupportedPages = [nameof(GameLauncherPage)],
InGameNoticesWindow = true,
};

private static readonly GameFeatureConfig nap = new()
{
SupportedPages = [nameof(GameLauncherPage)],
InGameNoticesWindow = true,
};



}
Loading

0 comments on commit aa7776b

Please sign in to comment.