Skip to content

Commit

Permalink
实现重复单关卡任意次,当理智耗尽时等待恢复功能;改进控制台输出
Browse files Browse the repository at this point in the history
  • Loading branch information
Rcmcpe committed May 10, 2020
1 parent 54c8f4f commit 1f7f4fe
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 87 deletions.
9 changes: 8 additions & 1 deletion Arknights Automation Library/Adb.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using OpenCvSharp;
using Console = Colorful.Console;
using Point = System.Drawing.Point;

namespace REVUnit.AutoArknights.Core
Expand All @@ -24,6 +26,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

private static void Log(string message)
{
Console.WriteLine($"[ADB]: {message}", Color.Gray);
}

public bool Connect(string target)
{
Target = target;
Expand All @@ -49,7 +56,7 @@ public string Exec(string parameter, bool muteOut = true)

private MemoryStream ExecBin(string parameter, bool muteOut = true)
{
if (!muteOut) Console.WriteLine(parameter);
if (!muteOut) Log(parameter);

using var process = new Process
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.10" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Refit" Version="5.1.67" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
Expand Down
14 changes: 6 additions & 8 deletions Arknights Automation Library/Automation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@ public class Automation : IDisposable
{
public Automation(string adbPath)
{
UI = new UI(adbPath);
Schedule = new Schedule(UI);
Ui = new UI(adbPath);
}

public Automation(string adbPath, string adbRemote)
{
UI = new UI(adbPath, adbRemote);
Schedule = new Schedule(UI);
Ui = new UI(adbPath, adbRemote);
}

public UI UI { get; }
public UI Ui { get; }

public Schedule Schedule { get; set; }
public Schedule Schedule { get; set; } = new Schedule();

public void Dispose()
{
UI.Dispose();
Ui.Dispose();
}

public void Connect(string adbRemote)
{
UI.NewRemote(adbRemote);
Ui.NewRemote(adbRemote);
}
}
}
17 changes: 7 additions & 10 deletions Arknights Automation Library/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
{
public abstract class Job
{
//public int RetryThreshold { get; set; }
public abstract ExecuteResult Execute(UI ui);
protected readonly UI Ui;

// public IEnumerable<Job> ReadJobs(string jobsDir)
// {
// return Directory.EnumerateFiles(jobsDir,
// "*.json",
// SearchOption.AllDirectories)
// .Select(jsonFile =>
// JsonConvert.DeserializeObject<Job>(File.ReadAllText(jsonFile)));
// }
protected Job(UI ui)
{
Ui = ui;
}

public abstract ExecuteResult Execute();
}
}
42 changes: 42 additions & 0 deletions Arknights Automation Library/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Drawing;
using Console = Colorful.Console;

namespace REVUnit.AutoArknights.Core
{
public static class Log
{
private static void WriteTime()
{
Console.Write($"[{DateTime.Now:HH:mm:ss}] ", Color.Gray);
}

public static void Info(string message, bool withTime = false)
{
if (withTime) WriteTime();
Console.Write("[Info]: ", Color.Gray);
Console.WriteLine(message);
}

public static void InfoAlt(string message, bool withTime = false)
{
if (withTime) WriteTime();
Console.Write("[Info]: ", Color.Yellow);
Console.WriteLine(message);
}

public static void Warning(string message, bool withTime = false)
{
if (withTime) WriteTime();
Console.Write("[Warn]: ", Color.Yellow);
Console.WriteLine(message);
}

public static void Error(string message, bool withTime = false)
{
if (withTime) WriteTime();
Console.Write("[ERROR]: ", Color.Red);
Console.WriteLine(message);
}
}
}
118 changes: 72 additions & 46 deletions Arknights Automation Library/RepeatLevelJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ public class RepeatLevelJob : Job
public enum Mode
{
SpecifiedTimes,
SpecTimesWithWait,
UntilNoSanity,
WaitWhileNoSanity
}

public RepeatLevelJob(Mode mode, int repeatTime)
private int _requiredSanity;

public RepeatLevelJob(UI ui, Mode mode, int repeatTime) : base(ui)
{
RepeatMode = mode;
RepeatTime = repeatTime;
Expand All @@ -21,90 +24,113 @@ public RepeatLevelJob(Mode mode, int repeatTime)
public Mode RepeatMode { get; set; }
public int RepeatTime { get; set; }

public override ExecuteResult Execute(UI ui)
public override ExecuteResult Execute()
{
Console.WriteLine(">>>任务开始");
Log.InfoAlt("任务开始", true);
_requiredSanity = Ui.GetRequiredSanity();
Log.Info($"检测到此关卡需要[{_requiredSanity}]理智");

switch (RepeatMode)
{
case Mode.SpecifiedTimes:
SpecifiedTimes(ui, RepeatTime);
SpecifiedTimes();
break;
case Mode.SpecTimesWithWait:
SpecTimesWithWait();
break;
case Mode.UntilNoSanity:
UntilNoSanity(ui);
UntilNoSanity();
break;
case Mode.WaitWhileNoSanity:
WaitWhileNoSanity(ui);
WaitWhileNoSanity();
break;
default:
throw new ArgumentOutOfRangeException();
}

Console.WriteLine(">>>任务完成");
Log.InfoAlt("任务完成", true);
return ExecuteResult.Success();
}

private static void WaitWhileNoSanity(UI ui)
private bool HaveEnoughSanity()
{
Sanity sanity = Ui.GetCurrentSanity();
Log.Info($"当前理智[{sanity}],需要理智[{_requiredSanity}]");
return sanity.Value >= _requiredSanity;
}

private void EnsureSanityEnough()
{
if (!HaveEnoughSanity()) WaitForSanityRecovery();
}

private void WaitForSanityRecovery()
{
Log.Info("正在等待理智恢复...", true);
while (Ui.GetCurrentSanity().Value < Ui.GetRequiredSanity())
Thread.Sleep(TimeSpan.FromSeconds(10));
Log.Info("...理智恢复完成", true);
}

private void SpecifiedTimes()
{
for (var currentTime = 1; currentTime <= RepeatTime; currentTime++)
{
Log.Info($"正在执行第{currentTime}次刷关", true);
RunOnce();
}
}

private void SpecTimesWithWait()
{
for (var currentTime = 1; currentTime <= RepeatTime; currentTime++)
{
Log.Info($"正在执行第{currentTime}次刷关", true);
EnsureSanityEnough();
RunOnce();
}
}

private void UntilNoSanity()
{
var currentTime = 0;
int requiredSanity = ui.GetRequiredSanity();
while (true)
{
Sanity sanity = ui.GetCurrentSanity();
bool flag = sanity.Value >= requiredSanity;
Console.WriteLine($">>当前理智[{sanity}],需要理智[{requiredSanity}],{(flag ? "继续" : "暂停")}");
if (flag)
if (HaveEnoughSanity())
{
RunOnce(ui);
RunOnce();
currentTime++;
Console.WriteLine($">>关卡完成,目前已刷关{currentTime}次");
Log.InfoAlt($"关卡完成,目前已刷关{currentTime}次");
}
else
{
Console.WriteLine(">>正在等待理智恢复...");
while (ui.GetCurrentSanity().Value - ui.GetRequiredSanity() < 0)
Thread.Sleep(TimeSpan.FromSeconds(15));
break;
}
}
}

private static void UntilNoSanity(UI ui)
private void WaitWhileNoSanity()
{
var currentTime = 0;
int requiredSanity = ui.GetRequiredSanity();
while (true)
{
Sanity sanity = ui.GetCurrentSanity();
bool flag = sanity.Value >= requiredSanity;
Console.WriteLine($">>当前理智[{sanity}],需要理智[{requiredSanity}],{(flag ? "继续" : "停止")}");
if (flag)
if (HaveEnoughSanity())
{
RunOnce(ui);
RunOnce();
currentTime++;
Console.WriteLine($">>关卡完成,目前已刷关{currentTime}次");
Log.Info($"关卡完成,目前已刷关{currentTime}次");
}
else
{
break;
WaitForSanityRecovery();
}
}
}

private static void SpecifiedTimes(UI ui, int time)
{
for (var currentTime = 1; currentTime <= time; currentTime++)
{
Console.WriteLine($">>正在执行第{currentTime}次刷关");
RunOnce(ui);
}
}

private static void RunOnce(UI i)
private void RunOnce()
{
i.Clk("作战 开始");
i.Clk("作战 确认");
i.WaitAp("作战 完成");
i.Slp(2);
i.Clk(5, 5);
Ui.Clk("作战 开始");
Ui.Clk("作战 确认");
Ui.WaitAp("作战 完成");
Log.Info("检测到关卡完成", true);
Ui.Slp(2);
Ui.Clk(5, 5);
}
}
}
8 changes: 1 addition & 7 deletions Arknights Automation Library/Schedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ namespace REVUnit.AutoArknights.Core
public class Schedule
{
private readonly Queue<Job> _jobs = new Queue<Job>();
private readonly UI _ui;

public Schedule(UI ui)
{
_ui = ui;
}

public IEnumerable<Job> Jobs => _jobs;

Expand All @@ -24,7 +18,7 @@ public void ExecuteAll()
for (var i = 0; i < _jobs.Count; i++)
{
Job job = _jobs.Dequeue();
job.Execute(_ui);
job.Execute();
}
}
}
Expand Down
18 changes: 4 additions & 14 deletions Auto Arknights CLI/AutoArknights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public AutoArknights()
{
if (!Library.CheckIfSupported())
{
Console.WriteLine("你的CPU不支持当前OpenCV构建,无法正常运行本程序,抱歉。");
Log.Error("你的CPU不支持当前OpenCV构建,无法正常运行本程序,抱歉。");
Console.ReadKey(true);
return;
}
Expand All @@ -28,25 +28,15 @@ public void Dispose()
_automation.Dispose();
}

#if DEBUG
public void Test()
{
while (true)
{
Console.Write("Current sanity: ");
Console.WriteLine(_automation.UI.GetCurrentSanity().ToString());
}
}
#endif

public void Run()
{
var cin = new Cin();
RepeatLevelJob.Mode mode = cin.Get<RepeatLevelJob.Mode>("输入模式");
int repeatTime = -1;
if (mode == RepeatLevelJob.Mode.SpecifiedTimes) repeatTime = cin.Get<int>("输入刷关次数");
if (mode == RepeatLevelJob.Mode.SpecifiedTimes || mode == RepeatLevelJob.Mode.SpecTimesWithWait)
repeatTime = cin.Get<int>("输入刷关次数");

_automation.Schedule.Add(new RepeatLevelJob(mode, repeatTime));
_automation.Schedule.Add(new RepeatLevelJob(_automation.Ui, mode, repeatTime));
_automation.Schedule.ExecuteAll();

Console.Beep();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
功能|状态|注释
-|-|-
任意次|已实现|Mode.SpecifiedTimes
任意次,当理智耗尽时等待恢复|计划中
任意次,当理智耗尽时等待恢复|已实现|Mode.SpecTimesWithWait
直到理智耗尽|已实现|Mode.UntilNoSanity
当理智耗尽时等待恢复|已实现|Mode.WaitWhileNoSanity
当理智耗尽时自动嗑药|计划中
Expand Down

0 comments on commit 1f7f4fe

Please sign in to comment.