Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基本完成升级时间计算的升级,并增加一个单元测试 #590

Merged
merged 2 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ public class AccountDomainService : IAccountDomainService
private readonly IRelationApi _relationApi;
private readonly UnfollowBatchedTaskOptions _unfollowBatchedTaskOptions;
private readonly BiliCookie _cookie;
private readonly DailyTaskOptions _dailyTaskOptions;

public AccountDomainService(
ILogger<AccountDomainService> logger,
IDailyTaskApi dailyTaskApi,
BiliCookie cookie,
IUserInfoApi userInfoApi,
IRelationApi relationApi,
IOptionsMonitor<UnfollowBatchedTaskOptions> unfollowBatchedTaskOptions
)
IOptionsMonitor<UnfollowBatchedTaskOptions> unfollowBatchedTaskOptions,
IOptionsMonitor<DailyTaskOptions> dailyTaskOptions)
{
_logger = logger;
_dailyTaskApi = dailyTaskApi;
_cookie = cookie;
_userInfoApi = userInfoApi;
_relationApi = relationApi;
_dailyTaskOptions = dailyTaskOptions.CurrentValue;
_unfollowBatchedTaskOptions = unfollowBatchedTaskOptions.CurrentValue;
}

Expand All @@ -66,9 +68,9 @@ public async Task<UserInfo> 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
{
Expand Down Expand Up @@ -216,5 +218,42 @@ private async Task<TagDto> GetTag(string groupName)
TagDto tag = tagList.FirstOrDefault(x => x.Name == groupName);
return tag;
}

/// <summary>
/// 计算升级时间
/// </summary>
/// <param name="useInfo"></param>
/// <returns>升级时间</returns>
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;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ public interface IAccountDomainService : IDomainService
/// 批量取关
/// </summary>
Task UnfollowBatched();

/// <summary>
/// 计算升级时间
/// </summary>
/// <param name="useInfo"></param>
/// <returns>升级时间</returns>
int CalculateUpgradeTime(UserInfo useInfo);
}
}
48 changes: 48 additions & 0 deletions test/DomainServiceTest/CalculateUpgradeTimeTest.cs
Original file line number Diff line number Diff line change
@@ -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<IAccountDomainService>();
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);

}
}