Skip to content

Commit

Permalink
fix: 修复了一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Aug 5, 2023
1 parent dafeb05 commit 4ffa020
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
78 changes: 38 additions & 40 deletions GeniusInvokationAutoToy/Core/GameControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,25 +877,7 @@ public void WaitForMyTurn(Duel duel, int waitTime = 0)
{
if (IsActiveCharacterTakenOut())
{
MyLogger.Info("当前出战角色被打败,需要选择新的出战角色");
bool[] defeatedArray = WhatCharacterDefeated(duel.CharacterCardRects);

for (int i = defeatedArray.Length - 1; i >= 0; i--)
{
duel.Characters[i + 1].IsDefeated = defeatedArray[i];
}

foreach (int j in duel.GetCharacterSwitchOrder())
{
if (!duel.Characters[j].IsDefeated)
{
duel.Characters[j].SwitchWhenTakenOut();
break;
}
}

ClickGameWindowCenter();
Sleep(2000); // 切人动画
DoWhenCharacterDefeated(duel);
}
else
{
Expand Down Expand Up @@ -942,25 +924,7 @@ public void WaitOpponentAction(Duel duel)
{
if (IsActiveCharacterTakenOut())
{
MyLogger.Info("当前出战角色被打败,需要选择新的出战角色");
bool[] defeatedArray = WhatCharacterDefeated(duel.CharacterCardRects);

for (int i = defeatedArray.Length - 1; i >= 0; i--)
{
duel.Characters[i + 1].IsDefeated = defeatedArray[i];
}

foreach (int j in duel.GetCharacterSwitchOrder())
{
if (!duel.Characters[j].IsDefeated)
{
duel.Characters[j].SwitchWhenTakenOut();
break;
}
}

ClickGameWindowCenter();
Sleep(2000); // 切人动画
DoWhenCharacterDefeated(duel);
}
}
else if (IsDuelEnd())
Expand Down Expand Up @@ -991,6 +955,40 @@ public void WaitOpponentAction(Duel duel)
}
}

/// <summary>
/// 角色被打败后要切换角色
/// </summary>
/// <param name="duel"></param>
/// <exception cref="DuelEndException"></exception>
public void DoWhenCharacterDefeated(Duel duel)
{
MyLogger.Info("当前出战角色被打败,需要选择新的出战角色");
bool[] defeatedArray = WhatCharacterDefeated(duel.CharacterCardRects);

for (int i = defeatedArray.Length - 1; i >= 0; i--)
{
duel.Characters[i + 1].IsDefeated = defeatedArray[i];
}

List<int> orderList = duel.GetCharacterSwitchOrder();
if (orderList.Count == 0)
{
throw new DuelEndException("后续行动策略中,已经没有可切换且存活的角色了,结束自动打牌(建议添加更多行动)");
}

foreach (int j in orderList)
{
if (!duel.Characters[j].IsDefeated)
{
duel.Characters[j].SwitchWhenTakenOut();
break;
}
}

ClickGameWindowCenter();
Sleep(2000); // 切人动画
}


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

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

int cnt = 0;
Expand Down Expand Up @@ -1114,7 +1112,7 @@ public Character WhichCharacterActive(Duel duel)

public Character WhichCharacterActiveWithRetry(Duel duel)
{
return Retry.Do(() => WhichCharacterActive(duel), TimeSpan.FromSeconds(0.5), 8);
return Retry.Do(() => WhichCharacterActive(duel), TimeSpan.FromSeconds(0.3), 12);
}
}
}
3 changes: 2 additions & 1 deletion GeniusInvokationAutoToy/Strategy/Model/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ public bool SwitchLater()
}

/// <summary>
/// 角色死亡的时候双击角色牌重新出战
/// 角色被打败的时候双击角色牌重新出战
/// </summary>
/// <returns></returns>
public bool SwitchWhenTakenOut()
{
MyLogger.Info($"有角色被打败,当前选择{Name}出战");
Point p = GameControl.GetInstance().MakeOffset(Area.GetCenterPoint());
// 选择角色
MouseUtils.Click(p);
Expand Down
22 changes: 16 additions & 6 deletions GeniusInvokationAutoToy/Strategy/Model/Duel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
while (true)
{
// 没骰子了就结束行动
MyLogger.Info($"当前骰子数[{CurrentDiceCount}],当前手牌数[{CurrentCardCount}]");
MyLogger.Info($"行动开始,当前骰子数[{CurrentDiceCount}],当前手牌数[{CurrentCardCount}]");
if (CurrentDiceCount <= 0)
{
MyLogger.Info("骰子已经用完");
Expand All @@ -127,7 +127,8 @@ public void CustomStrategyRun(CancellationTokenSource cts1)

List<int> alreadyExecutedActionIndex = new List<int>();
List<ActionCommand> alreadyExecutedActionCommand = new List<ActionCommand>();
for (var i = 0; i < ActionCommandQueue.Count; i++)
var i = 0;
for (i = 0; i < ActionCommandQueue.Count; i++)
{
var actionCommand = ActionCommandQueue[i];
// 指令中的角色未被打败、角色有异常状态 跳过指令
Expand All @@ -149,7 +150,7 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
{
if (CurrentDiceCount >= 1)
{
actionCommand.Character.SwitchLater();
actionCommand.SwitchLater();
CurrentDiceCount--;
alreadyExecutedActionIndex.Add(-actionCommand.Character.Index); // 标记为已执行
var switchAction = new ActionCommand
Expand All @@ -170,7 +171,7 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
}

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

break;
}
}



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

Expand All @@ -230,8 +241,6 @@ public void CustomStrategyRun(CancellationTokenSource cts1)
GameControl.GetInstance().WaitOpponentAction(this);
RoundNum++;
}

MyLogger.Info("没活了,结束自动打牌");
}
catch (TaskCanceledException ex)
{
Expand Down Expand Up @@ -367,6 +376,7 @@ public int GetCharacterAliveNum()
num++;
}
}

return num;
}
}
Expand Down

0 comments on commit 4ffa020

Please sign in to comment.