Commit b14a08df authored by mercury233's avatar mercury233 Committed by GitHub

refactor: use Linq and other code style improvements (#75)

parent 3f572de6
This diff is collapsed.
This diff is collapsed.
using YGOSharp.OCGWrapper.Enums;
using System.Collections.Generic;
using System.Linq;
using WindBot;
using WindBot.Game;
using WindBot.Game.AI;
......@@ -140,25 +141,9 @@ namespace WindBot.Game.AI.Decks
Logger.DebugWriteLine("OnSelectCard MelodyOfAwakeningDragon");
IList<ClientCard> result = new List<ClientCard>();
if (!Bot.HasInHand(CardId.WhiteDragon))
{
foreach (ClientCard card in cards)
{
if (card.Id == CardId.WhiteDragon)
{
result.Add(card);
break;
}
}
}
foreach (ClientCard card in cards)
{
if (card.Id == CardId.AlternativeWhiteDragon && result.Count < max)
{
result.Add(card);
}
}
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
result.Add(cards.FirstOrDefault(card => card.Id == CardId.WhiteDragon));
result = result.Concat(cards.Where(card => card.Id == CardId.AlternativeWhiteDragon)).ToList();
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
Logger.DebugWriteLine("Use default.");
return null;
......@@ -167,10 +152,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
Logger.DebugWriteLine("OnSelectXyzMaterial " + cards.Count + " " + min + " " + max);
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, UsedAlternativeWhiteDragon, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
IList<ClientCard> result = AI.Utils.SelectPreferredCards(UsedAlternativeWhiteDragon, cards, min, max);
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
public override IList<ClientCard> OnSelectSynchroMaterial(IList<ClientCard> cards, int sum, int min, int max)
......
......@@ -114,8 +114,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count >= max)
break;
}
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool ReinforcementOfTheArmyEffect()
......
......@@ -172,8 +172,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count > 0)
break;
}
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool UnexpectedDaiEffect()
......
......@@ -120,15 +120,13 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, new[] {
IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
CardId.MistArchfiend,
CardId.PanzerDragon,
CardId.SolarWindJammer,
CardId.StarDrawing
}, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool NormalSummon()
......
......@@ -181,10 +181,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, CardId.YosenjuTsujik, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
IList<ClientCard> result = AI.Utils.SelectPreferredCards(CardId.YosenjuTsujik, cards, min, max);
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool PotOfDualityEffect()
......
......@@ -138,14 +138,12 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{
IList<ClientCard> result = new List<ClientCard>();
AI.Utils.SelectPreferredCards(result, new[] {
IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
CardId.StarDrawing,
CardId.SolarWindJammer,
CardId.Goblindbergh
}, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
return AI.Utils.CheckSelectCount(result, cards, min, max);
}
private bool Number39Utopia()
......
......@@ -127,7 +127,7 @@ namespace WindBot.Game.AI
public void SendOnDirectAttack(string attacker)
{
if (attacker == "" || attacker == null)
if (string.IsNullOrEmpty(attacker))
{
attacker = _facedownmonstername;
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using YGOSharp.OCGWrapper.Enums;
using WindBot;
using WindBot.Game;
......@@ -82,12 +83,12 @@ namespace WindBot.Game.AI
public virtual void OnChaining(int player, ClientCard card)
{
// For overriding
}
public virtual void OnChainEnd()
{
// For overriding
}
public virtual void OnNewPhase()
{
......@@ -239,12 +240,7 @@ namespace WindBot.Game.AI
private bool DefaultNoExecutor()
{
foreach (CardExecutor exec in Executors)
{
if (exec.Type == Type && exec.CardId == Card.Id)
return false;
}
return true;
return Executors.All(exec => exec.Type != Type || exec.CardId != Card.Id);
}
}
}
\ No newline at end of file
......@@ -151,27 +151,27 @@ namespace WindBot.Game
public bool HasLinkMarker(int dir)
{
return ((LinkMarker & dir) != 0);
return (LinkMarker & dir) != 0;
}
public bool HasLinkMarker(LinkMarker dir)
{
return ((LinkMarker & (int)dir) != 0);
return (LinkMarker & (int)dir) != 0;
}
public bool HasType(CardType type)
{
return ((Type & (int)type) != 0);
return (Type & (int)type) != 0;
}
public bool HasPosition(CardPosition position)
{
return ((Position & (int)position) != 0);
return (Position & (int)position) != 0;
}
public bool HasAttribute(CardAttribute attribute)
{
return ((Attribute & (int)attribute) != 0);
return (Attribute & (int)attribute) != 0;
}
public bool IsMonster()
......@@ -221,7 +221,7 @@ namespace WindBot.Game
public bool IsDisabled()
{
return (Disabled != 0);
return Disabled != 0;
}
public bool HasXyzMaterial()
......
using System.Collections.Generic;
using System.Linq;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game
......@@ -101,13 +103,11 @@ namespace WindBot.Game
return GetSpellCount() + GetMonsterCount();
}
public int GetFieldHandCount()
{
return GetSpellCount() + GetMonsterCount() + GetHandCount();
}
public bool IsFieldEmpty()
{
return GetMonsters().Count == 0 && GetSpells().Count == 0;
......@@ -118,7 +118,6 @@ namespace WindBot.Game
return GetCards(MonsterZone);
}
public List<ClientCard> GetGraveyardMonsters()
{
return GetCards(Graveyard, CardType.Monster);
......@@ -141,23 +140,12 @@ namespace WindBot.Game
public List<ClientCard> GetMonstersInExtraZone()
{
List<ClientCard> cards = new List<ClientCard>();
if (MonsterZone[5] != null)
cards.Add(MonsterZone[5]);
if (MonsterZone[6] != null)
cards.Add(MonsterZone[6]);
return cards;
return GetMonsters().Where((card, i) => i >= 5).ToList();
}
public List<ClientCard> GetMonstersInMainZone()
{
List<ClientCard> cards = new List<ClientCard>();
for (int i = 0; i < 5; i++)
{
if (MonsterZone[i] != null)
cards.Add(MonsterZone[i]);
}
return cards;
return GetMonsters().Where((card, i) => i < 5).ToList();
}
public bool HasInHand(int cardId)
......@@ -202,24 +190,12 @@ namespace WindBot.Game
public bool HasAttackingMonster()
{
IList<ClientCard> monsters = GetMonsters();
foreach (ClientCard card in monsters)
{
if (card.IsAttack())
return true;
}
return false;
return GetMonsters().Any(card => card.IsAttack());
}
public bool HasDefendingMonster()
{
IList<ClientCard> monsters = GetMonsters();
foreach (ClientCard card in monsters)
{
if (card.IsDefense())
return true;
}
return false;
return GetMonsters().Any(card => card.IsDefense());
}
public bool HasInMonstersZone(int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
......@@ -315,94 +291,46 @@ namespace WindBot.Game
public int GetRemainingCount(int cardId, int initialCount)
{
int remaining = initialCount;
foreach (ClientCard card in Hand)
if (card != null && card.Id == cardId)
remaining--;
foreach (ClientCard card in SpellZone)
if (card != null && card.Id == cardId)
remaining--;
foreach (ClientCard card in Graveyard)
if (card != null && card.Id == cardId)
remaining--;
foreach (ClientCard card in Banished)
if (card != null && card.Id == cardId)
remaining--;
remaining = remaining - Hand.Count(card => card != null && card.Id == cardId);
remaining = remaining - SpellZone.Count(card => card != null && card.Id == cardId);
remaining = remaining - Graveyard.Count(card => card != null && card.Id == cardId);
remaining = remaining - Banished.Count(card => card != null && card.Id == cardId);
return (remaining < 0) ? 0 : remaining;
}
private static int GetCount(IEnumerable<ClientCard> cards)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card != null)
count++;
}
return count;
return cards.Count(card => card != null);
}
public int GetCountCardInZone(IEnumerable<ClientCard> cards, int cardId)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card != null && card.Id == cardId)
count++;
}
return count;
return cards.Count(card => card != null && card.Id == cardId);
}
public int GetCountCardInZone(IEnumerable<ClientCard> cards, List<int> cardId)
{
int count = 0;
foreach (ClientCard card in cards)
{
if (card != null && cardId.Contains(card.Id))
count++;
}
return count;
return cards.Count(card => card != null && cardId.Contains(card.Id));
}
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards, CardType type)
{
List<ClientCard> nCards = new List<ClientCard>();
foreach (ClientCard card in cards)
{
if (card != null && card.HasType(type))
nCards.Add(card);
}
return nCards;
return cards.Where(card => card != null && card.HasType(type)).ToList();
}
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards)
{
List<ClientCard> nCards = new List<ClientCard>();
foreach (ClientCard card in cards)
{
if (card != null)
nCards.Add(card);
}
return nCards;
return cards.Where(card => card != null).ToList();
}
private static bool HasInCards(IEnumerable<ClientCard> cards, int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{
foreach (ClientCard card in cards)
{
if (card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
return cards.Any(card => card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
}
private static bool HasInCards(IEnumerable<ClientCard> cards, IList<int> cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{
foreach (ClientCard card in cards)
{
if (card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
return cards.Any(card => card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
}
}
}
\ No newline at end of file
......@@ -83,8 +83,7 @@ namespace WindBot.Game
}
catch (Exception)
{
if (reader != null)
reader.Close();
reader?.Close();
return null;
}
}
......
......@@ -360,10 +360,7 @@ namespace WindBot.Game
return result;
result = new List<ClientCard>();
// TODO: use selector
for (int i = 0; i < cards.Count; i++)
{
result.Add(cards[i]);
}
result = cards.ToList();
return result;
}
......@@ -550,10 +547,15 @@ namespace WindBot.Game
}
else
{
if (hint == HINTMSG_SMATERIAL)
switch (hint)
{
case HINTMSG_SMATERIAL:
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
if (hint == HINTMSG_RELEASE)
break;
case HINTMSG_RELEASE:
selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
break;
}
}
if (selected != null)
{
......@@ -1017,12 +1019,7 @@ namespace WindBot.Game
/// <returns>A list of the selected attributes.</returns>
public virtual IList<CardAttribute> OnAnnounceAttrib(int count, IList<CardAttribute> attributes)
{
IList<CardAttribute> foundAttributes = new List<CardAttribute>();
foreach (CardAttribute attribute in m_attributes)
{
if(attributes.Contains(attribute))
foundAttributes.Add(attribute);
}
IList<CardAttribute> foundAttributes = m_attributes.Where(attributes.Contains).ToList();
if (foundAttributes.Count > 0)
return foundAttributes;
......@@ -1037,12 +1034,7 @@ namespace WindBot.Game
/// <returns>A list of the selected races.</returns>
public virtual IList<CardRace> OnAnnounceRace(int count, IList<CardRace> races)
{
IList<CardRace> foundRaces = new List<CardRace>();
foreach (CardRace race in m_races)
{
if (races.Contains(race))
foundRaces.Add(race);
}
IList<CardRace> foundRaces = m_races.Where(races.Contains).ToList();
if (foundRaces.Count > 0)
return foundRaces;
......@@ -1080,12 +1072,10 @@ namespace WindBot.Game
private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1)
{
Executor.SetCard(type, card, desc);
if (card != null &&
return card != null &&
exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) &&
(exec.Func == null || exec.Func()))
return true;
return false;
(exec.Func == null || exec.Func());
}
}
}
......@@ -619,7 +619,7 @@ namespace WindBot.Game
_duel.Fields[attackcard.Controller].BattlingMonster = attackcard;
_duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard;
if (ld == 0 && (attackcard != null) && (ca != 0))
if (ld == 0 && ca != 0)
{
_ai.OnDirectAttack(attackcard);
}
......@@ -732,9 +732,8 @@ namespace WindBot.Game
packet.ReadInt32(); // ???
ClientCard card = _duel.GetCard(player, (CardLocation)loc, seq);
if (card == null) return;
card.Update(packet, _duel);
card?.Update(packet, _duel);
}
private void OnUpdateData(BinaryReader packet)
......@@ -1448,11 +1447,7 @@ namespace WindBot.Game
ClientCard equipCard = _duel.GetCard(equipCardControler, (CardLocation)equipCardLocation, equipCardSequence);
ClientCard targetCard = _duel.GetCard(targetCardControler, (CardLocation)targetCardLocation, targetCardSequence);
if (equipCard == null || targetCard == null) return;
if (equipCard.EquipTarget != null)
{
equipCard.EquipTarget.EquipCards.Remove(equipCard);
}
equipCard.EquipTarget?.EquipCards.Remove(equipCard);
equipCard.EquipTarget = targetCard;
targetCard.EquipCards.Add(equipCard);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment