Skip to content

Commit 4ffa020

Browse files
committed
fix: 修复了一些问题
1 parent dafeb05 commit 4ffa020

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

GeniusInvokationAutoToy/Core/GameControl.cs

+38-40
Original file line numberDiff line numberDiff line change
@@ -877,25 +877,7 @@ public void WaitForMyTurn(Duel duel, int waitTime = 0)
877877
{
878878
if (IsActiveCharacterTakenOut())
879879
{
880-
MyLogger.Info("当前出战角色被打败,需要选择新的出战角色");
881-
bool[] defeatedArray = WhatCharacterDefeated(duel.CharacterCardRects);
882-
883-
for (int i = defeatedArray.Length - 1; i >= 0; i--)
884-
{
885-
duel.Characters[i + 1].IsDefeated = defeatedArray[i];
886-
}
887-
888-
foreach (int j in duel.GetCharacterSwitchOrder())
889-
{
890-
if (!duel.Characters[j].IsDefeated)
891-
{
892-
duel.Characters[j].SwitchWhenTakenOut();
893-
break;
894-
}
895-
}
896-
897-
ClickGameWindowCenter();
898-
Sleep(2000); // 切人动画
880+
DoWhenCharacterDefeated(duel);
899881
}
900882
else
901883
{
@@ -942,25 +924,7 @@ public void WaitOpponentAction(Duel duel)
942924
{
943925
if (IsActiveCharacterTakenOut())
944926
{
945-
MyLogger.Info("当前出战角色被打败,需要选择新的出战角色");
946-
bool[] defeatedArray = WhatCharacterDefeated(duel.CharacterCardRects);
947-
948-
for (int i = defeatedArray.Length - 1; i >= 0; i--)
949-
{
950-
duel.Characters[i + 1].IsDefeated = defeatedArray[i];
951-
}
952-
953-
foreach (int j in duel.GetCharacterSwitchOrder())
954-
{
955-
if (!duel.Characters[j].IsDefeated)
956-
{
957-
duel.Characters[j].SwitchWhenTakenOut();
958-
break;
959-
}
960-
}
961-
962-
ClickGameWindowCenter();
963-
Sleep(2000); // 切人动画
927+
DoWhenCharacterDefeated(duel);
964928
}
965929
}
966930
else if (IsDuelEnd())
@@ -991,6 +955,40 @@ public void WaitOpponentAction(Duel duel)
991955
}
992956
}
993957

958+
/// <summary>
959+
/// 角色被打败后要切换角色
960+
/// </summary>
961+
/// <param name="duel"></param>
962+
/// <exception cref="DuelEndException"></exception>
963+
public void DoWhenCharacterDefeated(Duel duel)
964+
{
965+
MyLogger.Info("当前出战角色被打败,需要选择新的出战角色");
966+
bool[] defeatedArray = WhatCharacterDefeated(duel.CharacterCardRects);
967+
968+
for (int i = defeatedArray.Length - 1; i >= 0; i--)
969+
{
970+
duel.Characters[i + 1].IsDefeated = defeatedArray[i];
971+
}
972+
973+
List<int> orderList = duel.GetCharacterSwitchOrder();
974+
if (orderList.Count == 0)
975+
{
976+
throw new DuelEndException("后续行动策略中,已经没有可切换且存活的角色了,结束自动打牌(建议添加更多行动)");
977+
}
978+
979+
foreach (int j in orderList)
980+
{
981+
if (!duel.Characters[j].IsDefeated)
982+
{
983+
duel.Characters[j].SwitchWhenTakenOut();
984+
break;
985+
}
986+
}
987+
988+
ClickGameWindowCenter();
989+
Sleep(2000); // 切人动画
990+
}
991+
994992

995993
/// <summary>
996994
/// 哪个角色处于出战状态
@@ -1027,7 +1025,7 @@ public Character WhichCharacterActive(Duel duel)
10271025
Cv2.ImWrite("logs\\active_character_error.jpg", outMat);
10281026
}
10291027

1030-
throw new RetryException("角色Hp区块未识别到");
1028+
throw new RetryException($"角色Hp区块识别有误,识别到区块数量{pList.Count} != 当前存活角色数{duel.GetCharacterAliveNum()}");
10311029
}
10321030

10331031
int cnt = 0;
@@ -1114,7 +1112,7 @@ public Character WhichCharacterActive(Duel duel)
11141112

11151113
public Character WhichCharacterActiveWithRetry(Duel duel)
11161114
{
1117-
return Retry.Do(() => WhichCharacterActive(duel), TimeSpan.FromSeconds(0.5), 8);
1115+
return Retry.Do(() => WhichCharacterActive(duel), TimeSpan.FromSeconds(0.3), 12);
11181116
}
11191117
}
11201118
}

GeniusInvokationAutoToy/Strategy/Model/Character.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ public bool SwitchLater()
8181
}
8282

8383
/// <summary>
84-
/// 角色死亡的时候双击角色牌重新出战
84+
/// 角色被打败的时候双击角色牌重新出战
8585
/// </summary>
8686
/// <returns></returns>
8787
public bool SwitchWhenTakenOut()
8888
{
89+
MyLogger.Info($"有角色被打败,当前选择{Name}出战");
8990
Point p = GameControl.GetInstance().MakeOffset(Area.GetCenterPoint());
9091
// 选择角色
9192
MouseUtils.Click(p);

GeniusInvokationAutoToy/Strategy/Model/Duel.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
115115
while (true)
116116
{
117117
// 没骰子了就结束行动
118-
MyLogger.Info($"当前骰子数[{CurrentDiceCount}],当前手牌数[{CurrentCardCount}]");
118+
MyLogger.Info($"行动开始,当前骰子数[{CurrentDiceCount}],当前手牌数[{CurrentCardCount}]");
119119
if (CurrentDiceCount <= 0)
120120
{
121121
MyLogger.Info("骰子已经用完");
@@ -127,7 +127,8 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
127127

128128
List<int> alreadyExecutedActionIndex = new List<int>();
129129
List<ActionCommand> alreadyExecutedActionCommand = new List<ActionCommand>();
130-
for (var i = 0; i < ActionCommandQueue.Count; i++)
130+
var i = 0;
131+
for (i = 0; i < ActionCommandQueue.Count; i++)
131132
{
132133
var actionCommand = ActionCommandQueue[i];
133134
// 指令中的角色未被打败、角色有异常状态 跳过指令
@@ -149,7 +150,7 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
149150
{
150151
if (CurrentDiceCount >= 1)
151152
{
152-
actionCommand.Character.SwitchLater();
153+
actionCommand.SwitchLater();
153154
CurrentDiceCount--;
154155
alreadyExecutedActionIndex.Add(-actionCommand.Character.Index); // 标记为已执行
155156
var switchAction = new ActionCommand
@@ -170,7 +171,7 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
170171
}
171172

172173
// 2. 判断使用技能
173-
if (actionCommand.GetAllDiceUseCount() >= CurrentDiceCount)
174+
if (actionCommand.GetAllDiceUseCount() > CurrentDiceCount)
174175
{
175176
MyLogger.Info("骰子不足以进行下一步:" + actionCommand);
176177
break;
@@ -188,12 +189,16 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
188189
else
189190
{
190191
MyLogger.Warn("→指令执行失败(可能是手牌不够):" + actionCommand);
192+
GameControl.GetInstance().Sleep(1000);
193+
GameControl.GetInstance().ClickGameWindowCenter();
191194
}
192195

193196
break;
194197
}
195198
}
196199

200+
201+
197202
if (alreadyExecutedActionIndex.Count != 0)
198203
{
199204
foreach (var index in alreadyExecutedActionIndex)
@@ -213,6 +218,12 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
213218
else
214219
{
215220
// 如果没有任何指令可以执行 则跳出循环
221+
// TODO 也有可能是角色死亡/所有角色被冻结导致没有指令可以执行
222+
//if (i >= ActionCommandQueue.Count)
223+
//{
224+
// throw new DuelEndException("策略中所有指令已经执行完毕,结束自动打牌");
225+
//}
226+
GameControl.GetInstance().Sleep(1500);
216227
break;
217228
}
218229

@@ -230,8 +241,6 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
230241
GameControl.GetInstance().WaitOpponentAction(this);
231242
RoundNum++;
232243
}
233-
234-
MyLogger.Info("没活了,结束自动打牌");
235244
}
236245
catch (TaskCanceledException ex)
237246
{
@@ -367,6 +376,7 @@ public int GetCharacterAliveNum()
367376
num++;
368377
}
369378
}
379+
370380
return num;
371381
}
372382
}

0 commit comments

Comments
 (0)