From ef6ea123740f435259c3f42b2457dd01fe2bd7b5 Mon Sep 17 00:00:00 2001 From: Polaris_cn <2984098720@qq.com> Date: Sat, 12 Aug 2023 21:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E6=97=B6=E9=97=B4=E8=AE=A1=E7=AE=97=E7=9A=84=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=20(#590)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 基本完成升级时间计算的升级,并增加一个单元测试。 * 调整了升级时间的日志描述 --- .../AccountDomainService.cs | 47 ++++++++++++++++-- .../Interfaces/IAccountDomainService.cs | 7 +++ .../CalculateUpgradeTimeTest.cs | 48 +++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 test/DomainServiceTest/CalculateUpgradeTimeTest.cs diff --git a/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs b/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs index efa7f6e68..e6d12dae5 100644 --- a/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs @@ -25,6 +25,7 @@ public class AccountDomainService : IAccountDomainService private readonly IRelationApi _relationApi; private readonly UnfollowBatchedTaskOptions _unfollowBatchedTaskOptions; private readonly BiliCookie _cookie; + private readonly DailyTaskOptions _dailyTaskOptions; public AccountDomainService( ILogger logger, @@ -32,14 +33,15 @@ public AccountDomainService( BiliCookie cookie, IUserInfoApi userInfoApi, IRelationApi relationApi, - IOptionsMonitor unfollowBatchedTaskOptions - ) + IOptionsMonitor unfollowBatchedTaskOptions, + IOptionsMonitor dailyTaskOptions) { _logger = logger; _dailyTaskApi = dailyTaskApi; _cookie = cookie; _userInfoApi = userInfoApi; _relationApi = relationApi; + _dailyTaskOptions = dailyTaskOptions.CurrentValue; _unfollowBatchedTaskOptions = unfollowBatchedTaskOptions.CurrentValue; } @@ -66,9 +68,9 @@ public async Task LoginByCookie() if (useInfo.Level_info.Current_level < 6) { - _logger.LogInformation("【距升级Lv{0}】{1}天(如每日做满65点经验)", + _logger.LogInformation("【距升级Lv{0}】预计{1}天", useInfo.Level_info.Current_level + 1, - (useInfo.Level_info.GetNext_expLong() - useInfo.Level_info.Current_exp) / Constants.EveryDayExp); + CalculateUpgradeTime(useInfo)); } else { @@ -216,5 +218,42 @@ private async Task GetTag(string groupName) TagDto tag = tagList.FirstOrDefault(x => x.Name == groupName); return tag; } + + /// + /// 计算升级时间 + /// + /// + /// 升级时间 + public int CalculateUpgradeTime(UserInfo useInfo) + { + double availableCoins = decimal.ToDouble(useInfo.Money ?? 0) - _dailyTaskOptions.NumberOfProtectedCoins; + long needExp = useInfo.Level_info.GetNext_expLong() - useInfo.Level_info.Current_exp; + int needDay; + + if (availableCoins < 0) + needDay = (int)((double)needExp / 25 + _dailyTaskOptions.NumberOfProtectedCoins - Math.Abs(availableCoins)); + + switch (_dailyTaskOptions.NumberOfCoins) + { + case 0: + needDay = (int)(needExp / 15); + break; + case 1: + needDay = (int)(needExp / 25); + break; + default: + int dailyExpAvailable = 15 + _dailyTaskOptions.NumberOfCoins * 10; + double needFrontDay = availableCoins / (_dailyTaskOptions.NumberOfCoins - 1); + + if ((double)needExp / dailyExpAvailable > needFrontDay) + needDay = (int)(needFrontDay + (needExp - dailyExpAvailable * needFrontDay) / 25); + else + needDay= (int)(needExp / dailyExpAvailable ); + break; + } + + return needDay; + } + } } diff --git a/src/Ray.BiliBiliTool.DomainService/Interfaces/IAccountDomainService.cs b/src/Ray.BiliBiliTool.DomainService/Interfaces/IAccountDomainService.cs index b599bd78c..83d03da69 100644 --- a/src/Ray.BiliBiliTool.DomainService/Interfaces/IAccountDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/Interfaces/IAccountDomainService.cs @@ -24,5 +24,12 @@ public interface IAccountDomainService : IDomainService /// 批量取关 /// Task UnfollowBatched(); + + /// + /// 计算升级时间 + /// + /// + /// 升级时间 + int CalculateUpgradeTime(UserInfo useInfo); } } diff --git a/test/DomainServiceTest/CalculateUpgradeTimeTest.cs b/test/DomainServiceTest/CalculateUpgradeTimeTest.cs new file mode 100644 index 000000000..b9b70df03 --- /dev/null +++ b/test/DomainServiceTest/CalculateUpgradeTimeTest.cs @@ -0,0 +1,48 @@ +using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos; + +namespace DomainServiceTest; + +public class CalculateUpgradeTimeTest +{ + public CalculateUpgradeTimeTest() + { + Program.CreateHost(new[] { "--ENVIRONMENT=Development" }); + } + + [Fact] + public void TestCalculateUpgradeTime() + { + using var scope = Global.ServiceProviderRoot.CreateScope(); + var accountDomainService = scope.ServiceProvider.GetRequiredService(); + int needDay = accountDomainService.CalculateUpgradeTime(new UserInfo() + { + Money = 7, + Level_info = new LevelInfo() + { + Current_level = 5, + Current_exp = 100, + Next_exp = 200 + } + }); + int needDay2 = accountDomainService.CalculateUpgradeTime(new UserInfo() + { + Money = 7, + Level_info = new LevelInfo() + { + Current_level = 5, + Current_exp = 1000, + Next_exp = 2000 + } + }); + + Assert.Equal(1,needDay); + Assert.Equal(37,needDay2); + + } +} + + + + + +