Certainly! The provided method can be optimized by reducing the redundancy, particularly in the repetitive loading and creation of SpawnPlayerConfig objects. We can make a single List<SpawnPlayerConfig> and dynamically choose the order of indices based on the condition. Here's an optimized version of your code:
csharp
private void LoadTestPartnerMode()
{
var heroInfos = Resources.LoadAll<HeroInfo>("Hero").OrderBy(h => h.heroType).ToList();
int[] indices = this.GetModel<IGameModel>().botDecks.Count == 0 ? new[] { 0, 2, 1, 3 } : new[] { 0, 1, 2, 3 };
List<SpawnPlayerConfig> configs = indices.Select(index => LoadPlayerConfig(heroInfos, index)).ToList();
SpawnPlayers(configs);
}
// Assuming LoadPlayerConfig and SpawnPlayers methods follow their definitions elsewhere in your code
Changes Made:
Index Array: Instead of hardcoding SpawnPlayerConfig creation based on specific indices separately in each condition, I introduced an indices array which stores the order of indices based on the condition (botDecks count).
Using LINQ's Select Method: I used the Select method to iterate over indices, applying LoadPlayerConfig to each index to create a List<SpawnPlayerConfig>.
Reduced Code Duplication: This approach minimizes duplication because it creates SpawnPlayerConfig objects in the same way, regardless of the condition.
This leads to cleaner, more maintainable, and compact code.
User
private void LoadTest()
{
var heroInfos = Resources.LoadAll("Hero").OrderBy(h => h.heroType).ToList();
Indices Array: I introduced an indices array containing the indices you want to load, [0, 1], for consistent behavior.
LINQ Select Method: We utilize the Select method to map each index to a SpawnPlayerConfig using the LoadPlayerConfig method. This iterates over the indices array and applies the same logic without repeating code.
Reduced Redundancy: This approach eliminates the need for explicitly naming each SpawnPlayerConfig variable and directly constructs the list.
Using this pattern makes the method more concise and scalable, simplifying future modifications or extensions.
使用命令行工具来解压文件,可能会有更好的效果。例如,在 Windows 上你可以使用 PowerShell 或者 WSL(Windows Subsystem for Linux)中安装的 unzip 命令。
重新获取文件:
如果以上方法都不成功,考虑让你的同事重新打包文件。可以建议他使用标准的压缩工具并明确设置编码方式。
总之,尝试使用支持多种编码格式的解压工具,会是解决乱码问题的关键。希望这些建议能帮到你!
User
public void StrategistTalent0(Card cardObject, ActorRef card)
{
Player player = GetActorObject(cardObject.GetOwnerPlayer());
BattleField battleField = GetBattleFieldObject(player.GetPlayerPosition());
code
AccurateCardPosition accurateCardPosition = GetCardPosition(card);
ActorRef<Card>? leftCard = battleField.GetDeployedMinionCard(new BattleFieldPosition(accurateCardPosition.BattleFieldPosition.Position - 1));
ActorRef<Card>? rightCard = battleField.GetDeployedMinionCard(new BattleFieldPosition(accurateCardPosition.BattleFieldPosition.Position + 1));
int leftBuffCount = 0;
int rightBuffCount = 0;
if (leftCard is { IsValid: true })
{
Card leftCardObject = GetActorObject(leftCard.Value);
if (leftCardObject.HasEffect(CardKeyWordType.SniperAttack) &&
cardObject.HasEffect(CardKeyWordType.SniperAttack))
{
leftBuffCount++;
}
if (leftCardObject.HasEffect(CardKeyWordType.Thorn) &&
cardObject.HasEffect(CardKeyWordType.Thorn))
{
leftBuffCount++;
}
if (leftCardObject.HasEffect(CardKeyWordType.HolyShield) &&
cardObject.HasEffect(CardKeyWordType.HolyShield))
{
leftBuffCount++;
}
}
if (rightCard is { IsValid: true })
{
Card rightCardObject = GetActorObject(rightCard.Value);
if (rightCardObject.HasEffect(CardKeyWordType.SniperAttack) &&
cardObject.HasEffect(CardKeyWordType.SniperAttack))
{
rightBuffCount++;
}
if (rightCardObject.HasEffect(CardKeyWordType.Thorn) &&
cardObject.HasEffect(CardKeyWordType.Thorn))
{
rightBuffCount++;
}
if (rightCardObject.HasEffect(CardKeyWordType.HolyShield) &&
cardObject.HasEffect(CardKeyWordType.HolyShield))
{
rightBuffCount++;
}
}
if (rightBuffCount >= leftBuffCount)
{
leftBuffCount = rightBuffCount;
}
for (int i = 0; i < leftBuffCount; i++)
{
BuffCardIn buffCardIn = new BuffCardIn
{
BuffValue = 1,
BuffCard = card
};
HeroTalent heroTalent = GetActorObject(player.GetHeroTalent());
BuffRecord buffRecord = new BuffRecord();
buffRecord.buffName = heroTalent.GetData().name;
buffRecord.attack = GameAttribute.One;
buffRecord.health = GameAttribute.One;
cardObject.AddBuffRecord(buffRecord);
BuffCardAttack(buffCardIn);
BuffCardHealth(buffCardIn);
}
} 优化下这段代码
Assistant
To optimize the StrategistTalent0 method, we can refactor the code to reduce redundancy and improve readability. The checks for the left and right cards contain repeated logic that can be extracted into a helper method. This will make the code more maintainable and concise. Here's how you can optimize it:
csharp
public void StrategistTalent0(Card cardObject, ActorRef<Card> card)
{
Player player = GetActorObject(cardObject.GetOwnerPlayer());
BattleField battleField = GetBattleFieldObject(player.GetPlayerPosition());
AccurateCardPosition accurateCardPosition = GetCardPosition(card);
// Define indices for left and right cards
int[] positions = {accurateCardPosition.BattleFieldPosition.Position - 1, accurateCardPosition.BattleFieldPosition.Position + 1 };
int maxBuffCount = positions.Select(position => GetBuffCountForAdjacentCard(battleField, cardObject, position)).Max();
ApplyBuffsToCard(card, cardObject, maxBuffCount, player);
}
private int GetBuffCountForAdjacentCard(BattleField battleField, Card cardObject, int position)
{
ActorRef<Card>? adjacentCard = battleField.GetDeployedMinionCard(new BattleFieldPosition(position));
if (adjacentCard is { IsValid: true })
{
Card adjacentCardObject = GetActorObject(adjacentCard.Value);
// List of effects to check
var effects = new[] { CardKeyWordType.SniperAttack, CardKeyWordType.Thorn, CardKeyWordType.HolyShield };
return effects.Count(effect => adjacentCardObject.HasEffect(effect) && cardObject.HasEffect(effect));
}
return 0;
}
private void ApplyBuffsToCard(ActorRef<Card> card, Card cardObject, int buffCount, Player player)
{
for (int i = 0; i < buffCount; i++)
{
BuffCardIn buffCardIn = new BuffCardIn
{
BuffValue = 1,
BuffCard = card
};
HeroTalent heroTalent = GetActorObject(player.GetHeroTalent());
BuffRecord buffRecord = new BuffRecord
{
buffName = heroTalent.GetData().name,
attack = GameAttribute.One,
health = GameAttribute.One
};
cardObject.AddBuffRecord(buffRecord);
BuffCardAttack(buffCardIn);
BuffCardHealth(buffCardIn);
}
}
User
/**
* 军师第一天赋
*/
public void StrategistTalent0(Card cardObject, ActorRef card)
{
Player player = GetActorObject(cardObject.GetOwnerPlayer());
BattleField battleField = GetBattleFieldObject(player.GetPlayerPosition());
code
AccurateCardPosition accurateCardPosition = GetCardPosition(card);
// Define indices for left and right cards
int[] positions = {accurateCardPosition.BattleFieldPosition.Position - 1, accurateCardPosition.BattleFieldPosition.Position + 1 };
int maxBuffCount = positions.Select(position => GetBuffCountForAdjacentCard(battleField, cardObject, position)).Max();
ApplyBuffsToCard(card, cardObject, maxBuffCount, player);
}
private int GetBuffCountForAdjacentCard(BattleField battleField, Card cardObject, int position)
{
ActorRef<Card>? adjacentCard = battleField.GetDeployedMinionCard(new BattleFieldPosition(position));
if (adjacentCard is { IsValid: true })
{
Card adjacentCardObject = GetActorObject(adjacentCard.Value);
// List of effects to check
var effects = new[] { CardKeyWordType.SniperAttack, CardKeyWordType.Thorn, CardKeyWordType.HolyShield };
return effects.Count(effect => adjacentCardObject.HasEffect(effect) && cardObject.HasEffect(effect));
}
return 0;
} 需要修改功能,如果左边CardKeyWordType.SniperAttack ,右边是CardKeyWordType.Thorn 则需要计数为2,现在这个代码还是计数1,怎么修改呢
Assistant
To address the new requirement where the code should count effects across both adjacent cards and needs to handle cases where different effects are on different sides, we need to slightly adjust the logic. We'll update the GetBuffCountForAdjacentCard method to consider the combination of effects across the left and right cards.
Here's how you can modify the approach:
Check All Combinations: Consider the potential combinations of effects between the left and right cards.
Count Unique Effects per Combination: Calculate and sum the total across the combinations.
Here's the modified code:
csharp
public void StrategistTalent0(Card cardObject, ActorRef<Card> card)
{
Player player = GetActorObject(cardObject.GetOwnerPlayer());
BattleField battleField = GetBattleFieldObject(player.GetPlayerPosition());
AccurateCardPosition accurateCardPosition = GetCardPosition(card);
// Define positions for left and right cards
int leftPosition = accurateCardPosition.BattleFieldPosition.Position - 1;
int rightPosition = accurateCardPosition.BattleFieldPosition.Position + 1;
int totalBuffCount = GetBuffCountAcrossCards(battleField, cardObject, leftPosition, rightPosition);
ApplyBuffsToCard(card, cardObject, totalBuffCount, player);
}
private int GetBuffCountAcrossCards(BattleField battleField, Card cardObject, int leftPosition, int rightPosition)
{
var effects = new[] { CardKeyWordType.SniperAttack, CardKeyWordType.Thorn, CardKeyWordType.HolyShield };
// Get any valid adjacent card
int count = 0;
foreach (var effect in effects)
{
bool leftHasEffect = HasEffectOnCard(battleField, cardObject, leftPosition, effect);
bool rightHasEffect = HasEffectOnCard(battleField, cardObject, rightPosition, effect);
if (leftHasEffect || rightHasEffect)
{
count++;
}
}
return count;
}
private bool HasEffectOnCard(BattleField battleField, Card cardObject, int position, CardKeyWordType effect)
{
ActorRef<Card>? adjacentCard = battleField.GetDeployedMinionCard(new BattleFieldPosition(position));
if (adjacentCard is { IsValid: true })
{
Card adjacentCardObject = GetActorObject(adjacentCard.Value);
return adjacentCardObject.HasEffect(effect) && cardObject.HasEffect(effect);
}
return false;
}
Not yet included in a published release.
Key Improvements:
Extracted Method for Buff Calculation: The logic to calculate the buff count for each adjacent card has been bottled into the GetBuffCountForAdjacentCard method. This reduces repetition and makes the main method cleaner.
Iterable Using LINQ: Instead of explicitly handling left and right cards separately, an array positions is used, and LINQ's Select is employed to apply GetBuffCountForAdjacentCard to each position.
Apply Buffs in a Separate Method: The logic to apply buffs is separated into ApplyBuffsToCard. This method handles the repeated buff application logic, improving clarity and separation of concerns.
By organizing the method in this way, we achieve a more modular and maintainable solution. Changes or additional effects can be managed more easily in the extracted helper functions.
Changes Made:
GetBuffCountAcrossCards Method: This method calculates the count of effects from both left and right cards. It iterates over each effect and checks if either the left or the right card has the effect, along with the cardObject.
HasEffectOnCard Method: Simplified logic to ascertain if a specific effect is present on a given card at a position.
Counting Logic for Both Cards: If either card (left or right) shares the effect with cardObject, it contributes to the buff count.
This approach can handle the requirement where different effects on each side should contribute to the total buff count correctly.