Files
better-genshin-impact/Test/BetterGenshinImpact.UnitTest/GameTaskTests/AutoFishingTests/BehavioursTests.GetFishpond.cs
FishmanTheMurloc 57d33c4312 又一波钓鱼优化 (#1301)
* GetFishBarRect方法添加更复杂的算法,并为其配备独立的单元测试,和分离难度较大的测试用例(未熟练时两侧出现黄色动态折线的情况);GetFishBoxArea行为去掉拉条框初始位置必须位于屏幕中轴线的条件,并添加其后续Fishing行为的单元测试来验证可行性;EnterFishingMode行为使用结束时间来代替Sleep,并添加整体超时时间;添加一个鱼咬钩的假阳性测试用例仅供娱乐

* 补充GetFishBarRect算法,使通过遗漏的测试"20250314002439020_Fishing_Succeeded.png"

* 拉条增加1秒未检测持续时间以应对瞬间丢失拉条框的情况;新增一个检查提竿结果的行为;新增一个检查开始钓一条鱼的初始状态的方法,以应对行为状态错配的情况;一些行为将Sleep优化为DateTime;修改上述改动对应的单元测试

* 解决合并冲突剩余问题,删掉ImageRegion的Bitmap构造函数重载

* 提供给测试用例初始化的 SystemInfo、TaskContext 方法,使用 InitForTest 即可

* InitForTest

* 和鸭蛋昨夜的提交撞车了。。。抽象了ISystemInto供单元测试实例化Fake类;给BaseAssets类定义了成员字段systemInfo(我想,既然都是图片模板数据集,如此定义是合理的),供继承类AutoFishingAssets使用,并定义了其在单元测试的派生类;添加了一个900p的选取鱼饵测试用例;blackboard改为负责携带AutoFishingAssets,并将其实例化时机挪到独立任务的Start方法中,避免由于TaskContext尚未初始化导致获取到的SystemInfo为空

* 一个特殊的测试用例:抛竿的瞬间、开始检测咬杆时遇到了假阳性

* Revert "InitForTest"

This reverts commit 225e9783a7.

* Revert "提供给测试用例初始化的 SystemInfo、TaskContext 方法,使用 InitForTest 即可"

This reverts commit 610c57263a.

* 为始终没有找到落点的情况添加计数,在第3次时直接退出,并添加此情况的单元测试

---------

Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
2025-03-18 19:51:42 +08:00

110 lines
5.4 KiB
C#

using BetterGenshinImpact.GameTask.AutoFishing;
using BehaviourTree;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BetterGenshinImpact.GameTask.Model.Area;
using BetterGenshinImpact.Core.Config;
using Compunet.YoloV8;
using BetterGenshinImpact.GameTask.AutoFishing.Model;
using Microsoft.Extensions.Time.Testing;
using OpenCvSharp;
namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
public partial class BehavioursTests
{
[Theory]
[InlineData("20250225101257889_GetFishpond_Succeeded.png", new string[] { "medaka", "butterflyfish", "pufferfish", "stickleback" })]
[InlineData("202502252347412417.png", new string[] { "medaka", "koi", "koi head" })]
[InlineData("202502252350206390.png", new string[] { "phony unihornfish", "magma rapidfish" })]
/// <summary>
/// 测试各种鱼的获取,结果为成功
/// </summary>
public void GetFishpondTest_VariousFishExist_ShouldSuccess(string screenshot1080p, IEnumerable<string> fishNames)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");
var imageRegion = new GameCaptureRegion(mat, 0, 0, drawContent: new FakeDrawContent());
var predictor = YoloV8Builder.CreateDefaultBuilder().UseOnnxModel(Global.Absolute(@"Assets\Model\Fish\bgi_fish.onnx")).Build();
var blackboard = new Blackboard(predictor, sleep: i => { });
//
GetFishpond sut = new GetFishpond("-", blackboard, new FakeLogger(), false, new FakeTimeProvider(), drawContent: new FakeDrawContent());
BehaviourStatus actualStatus = sut.Tick(imageRegion);
//
Assert.Equal(BehaviourStatus.Succeeded, actualStatus);
foreach (var g in fishNames.GroupBy(n => n))
{
string fishName = g.Key;
var fish = blackboard.fishpond.Fishes.Where(f => f.FishType.Name == fishName);
Assert.NotEmpty(fish);
}
}
[Theory]
[InlineData("20250225101257889_GetFishpond_Succeeded.png", new string[] { "fruit paste bait", "fruit paste bait", "redrot bait", "redrot bait" }, new string[] { "false worm bait", "false worm bait", "fake fly bait", "fake fly bait" })]
/// 测试鱼的鱼饵均在失败列表中且被忽略,结果为运行中
/// </summary>
public void GetFishpondTest_AllIgnored_ShouldBeRunning(string screenshot1080p, IEnumerable<string> chooseBaitfailures, IEnumerable<string> throwRodNoTargetFishfailures)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");
var imageRegion = new GameCaptureRegion(mat, 0, 0, drawContent: new FakeDrawContent());
var predictor = YoloV8Builder.CreateDefaultBuilder().UseOnnxModel(Global.Absolute(@"Assets\Model\Fish\bgi_fish.onnx")).Build();
var blackboard = new Blackboard(predictor, sleep: i => { });
blackboard.chooseBaitFailures = chooseBaitfailures.ToList();
blackboard.throwRodNoBaitFishFailures = throwRodNoTargetFishfailures.ToList();
//
GetFishpond sut = new GetFishpond("-", blackboard, new FakeLogger(), false, new FakeTimeProvider(), drawContent: new FakeDrawContent());
BehaviourStatus actualStatus = sut.Tick(imageRegion);
//
Assert.Equal(BehaviourStatus.Running, actualStatus);
Assert.NotEmpty(blackboard.fishpond.Fishes);
}
[Theory]
[InlineData("20250225101257889_GetFishpond_Succeeded.png", "medaka", 1)]
[InlineData("20250301192848793_GetFishpond_Succeeded.png", "medaka", 2)]
[InlineData("20250226161354285_ChooseBait_Succeeded.png", "medaka", 0)]
[InlineData("202503012143011486@900p.png", "medaka", 0)]
[InlineData("20250301231059172_GetFishpond_Succeeded.png", "medaka", 0)]
[InlineData("20250301234659009_GetFishpond_Succeeded.png", "axe marlin", 2)]
[InlineData("20250301235638915_GetFishpond_Succeeded.png", "butterflyfish", 1)]
[InlineData("20250302001049589_GetFishpond_Succeeded.png", "axe marlin", 0)]
[InlineData("20250306165029475_GetFishpond_Succeeded.png", "butterflyfish", 0)]
[InlineData("20250306171545590_GetFishpond_Succeeded.png", "heartfeather bass", 0)]
/// <summary>
/// 测试各种鱼的获取数量,数量应相符
/// </summary>
public void GetFishpondTest_FishCount_ShouldSuccess(string screenshot1080p, string fishName, int count)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");
var imageRegion = new GameCaptureRegion(mat, 0, 0, drawContent: new FakeDrawContent());
var predictor = YoloV8Builder.CreateDefaultBuilder().UseOnnxModel(Global.Absolute(@"Assets\Model\Fish\bgi_fish.onnx")).Build();
var blackboard = new Blackboard(predictor, sleep: i => { });
//
GetFishpond sut = new GetFishpond("-", blackboard, new FakeLogger(), false, new FakeTimeProvider(), drawContent: new FakeDrawContent());
sut.Tick(imageRegion);
int actual = blackboard.fishpond?.Fishes?.Count(f => f.FishType.Name == fishName) ?? 0;
//
Assert.Equal(count, actual);
}
}
}