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

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

parent 3f572de6
using System; using System;
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Linq; using System.Linq;
namespace BotWrapper namespace BotWrapper
{ {
class BotWrapper class BotWrapper
{ {
[DllImport("User32.dll", CharSet = CharSet.Unicode)] [DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, int uType); public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, int uType);
const int MB_ICONERROR = 0x00000010; const int MB_ICONERROR = 0x00000010;
static void Main(string[] args) static void Main(string[] args)
{ {
ProcessStartInfo startInfo = new ProcessStartInfo(); ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false; startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetFullPath("WindBot"); startInfo.WorkingDirectory = Path.GetFullPath("WindBot");
startInfo.FileName = startInfo.WorkingDirectory + "\\WindBot.exe"; startInfo.FileName = startInfo.WorkingDirectory + "\\WindBot.exe";
if (args.Length == 3) if (args.Length == 3)
{ {
startInfo.CreateNoWindow = true; startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string arg = args[0]; string arg = args[0];
Match match = Regex.Match(arg, "Random=(.*)"); Match match = Regex.Match(arg, "Random=(.*)");
if (match.Success) if (match.Success)
{ {
string randomFlag = match.Groups[1].Value; string randomFlag = match.Groups[1].Value;
...@@ -39,42 +39,42 @@ namespace BotWrapper ...@@ -39,42 +39,42 @@ namespace BotWrapper
{ {
MessageBox((IntPtr)0, "Can't find random bot with this flag!\n\nA totally random bot will appear instead.", "WindBot", MB_ICONERROR); MessageBox((IntPtr)0, "Can't find random bot with this flag!\n\nA totally random bot will appear instead.", "WindBot", MB_ICONERROR);
} }
} }
arg = arg.Replace("'", "\""); arg = arg.Replace("'", "\"");
if (int.Parse(args[1]) == 1) if (int.Parse(args[1]) == 1)
{ {
arg += " Hand=1"; arg += " Hand=1";
} }
arg += " Port=" + args[2]; arg += " Port=" + args[2];
startInfo.Arguments = arg; startInfo.Arguments = arg;
} }
try try
{ {
Process.Start(startInfo); Process.Start(startInfo);
} }
catch catch
{ {
MessageBox((IntPtr)0, "WindBot can't be started!", "WindBot", MB_ICONERROR); MessageBox((IntPtr)0, "WindBot can't be started!", "WindBot", MB_ICONERROR);
} }
} }
public class BotInfo public class BotInfo
{ {
public string name; public string name;
public string command; public string command;
public string desc; public string desc;
public string[] flags; public string[] flags;
} }
static public IList<BotInfo> Bots = new List<BotInfo>(); static public IList<BotInfo> Bots = new List<BotInfo>();
static void ReadBots() static void ReadBots()
{ {
using (StreamReader reader = new StreamReader("bot.conf")) using (StreamReader reader = new StreamReader("bot.conf"))
{ {
while (!reader.EndOfStream) while (!reader.EndOfStream)
{ {
string line = reader.ReadLine().Trim(); string line = reader.ReadLine().Trim();
if (line.Length > 0 && line[0] == '!') if (line.Length > 0 && line[0] == '!')
{ {
...@@ -88,8 +88,8 @@ namespace BotWrapper ...@@ -88,8 +88,8 @@ namespace BotWrapper
} }
} }
} }
} }
static string GetRandomBot(string flag) static string GetRandomBot(string flag)
{ {
IList<BotInfo> foundBots = Bots.Where(bot => bot.flags.Contains(flag)).ToList(); IList<BotInfo> foundBots = Bots.Where(bot => bot.flags.Contains(flag)).ToList();
...@@ -100,6 +100,6 @@ namespace BotWrapper ...@@ -100,6 +100,6 @@ namespace BotWrapper
return bot.command; return bot.command;
} }
return ""; return "";
} }
} }
} }
This diff is collapsed.
This diff is collapsed.
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using WindBot; using WindBot;
using WindBot.Game; using WindBot.Game;
using WindBot.Game.AI; using WindBot.Game.AI;
...@@ -140,25 +141,9 @@ namespace WindBot.Game.AI.Decks ...@@ -140,25 +141,9 @@ namespace WindBot.Game.AI.Decks
Logger.DebugWriteLine("OnSelectCard MelodyOfAwakeningDragon"); Logger.DebugWriteLine("OnSelectCard MelodyOfAwakeningDragon");
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = new List<ClientCard>();
if (!Bot.HasInHand(CardId.WhiteDragon)) if (!Bot.HasInHand(CardId.WhiteDragon))
{ result.Add(cards.FirstOrDefault(card => card.Id == CardId.WhiteDragon));
foreach (ClientCard card in cards) result = result.Concat(cards.Where(card => card.Id == CardId.AlternativeWhiteDragon)).ToList();
{ return AI.Utils.CheckSelectCount(result, cards, min, max);
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;
} }
Logger.DebugWriteLine("Use default."); Logger.DebugWriteLine("Use default.");
return null; return null;
...@@ -167,10 +152,8 @@ namespace WindBot.Game.AI.Decks ...@@ -167,10 +152,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
Logger.DebugWriteLine("OnSelectXyzMaterial " + cards.Count + " " + min + " " + max); Logger.DebugWriteLine("OnSelectXyzMaterial " + cards.Count + " " + min + " " + max);
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(UsedAlternativeWhiteDragon, cards, min, max);
AI.Utils.SelectPreferredCards(result, UsedAlternativeWhiteDragon, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
public override IList<ClientCard> OnSelectSynchroMaterial(IList<ClientCard> cards, int sum, int min, int max) public override IList<ClientCard> OnSelectSynchroMaterial(IList<ClientCard> cards, int sum, int min, int max)
......
...@@ -114,8 +114,8 @@ namespace WindBot.Game.AI.Decks ...@@ -114,8 +114,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count >= max) if (result.Count >= max)
break; break;
} }
AI.Utils.CheckSelectCount(result, cards, min, max);
return result; return AI.Utils.CheckSelectCount(result, cards, min, max);
} }
private bool ReinforcementOfTheArmyEffect() private bool ReinforcementOfTheArmyEffect()
......
...@@ -353,13 +353,13 @@ namespace WindBot.Game.AI.Decks ...@@ -353,13 +353,13 @@ namespace WindBot.Game.AI.Decks
} }
return false; return false;
} }
private bool GamecieltheSeaTurtleKaijusp() private bool GamecieltheSeaTurtleKaijusp()
{ {
if (!Bot.HasInMonstersZone(CardId.UltimateConductorTytanno)) if (!Bot.HasInMonstersZone(CardId.UltimateConductorTytanno))
return DefaultKaijuSpsummon(); return DefaultKaijuSpsummon();
return false; return false;
} }
private bool RadiantheMultidimensionalKaijusp() private bool RadiantheMultidimensionalKaijusp()
...@@ -409,9 +409,9 @@ namespace WindBot.Game.AI.Decks ...@@ -409,9 +409,9 @@ namespace WindBot.Game.AI.Decks
} }
private bool MonsterRepos() private bool MonsterRepos()
{ {
if (Card.Id == CardId.UltimateConductorTytanno && Card.IsFacedown()) return true; if (Card.Id == CardId.UltimateConductorTytanno && Card.IsFacedown()) return true;
if (Card.Id == CardId.ElShaddollConstruct && Card.IsFacedown()) return true; if (Card.Id == CardId.ElShaddollConstruct && Card.IsFacedown()) return true;
if (Card.Id == CardId.ElShaddollConstruct && Card.IsAttack()) return false; if (Card.Id == CardId.ElShaddollConstruct && Card.IsAttack()) return false;
if (Card.Id == CardId.GlowUpBulb && Card.IsDefense()) return false; if (Card.Id == CardId.GlowUpBulb && Card.IsDefense()) return false;
if (Card.Id == CardId.ShaddollDragon && Card.IsFacedown() && Enemy.GetMonsterCount() >= 0) return true; if (Card.Id == CardId.ShaddollDragon && Card.IsFacedown() && Enemy.GetMonsterCount() >= 0) return true;
...@@ -526,8 +526,8 @@ namespace WindBot.Game.AI.Decks ...@@ -526,8 +526,8 @@ namespace WindBot.Game.AI.Decks
private bool FairyTailSnowsummon() private bool FairyTailSnowsummon()
{ {
ClientCard target = AI.Utils.GetBestEnemyMonster(true, true); ClientCard target = AI.Utils.GetBestEnemyMonster(true, true);
if(target != null) if(target != null)
{ {
return true; return true;
...@@ -549,13 +549,13 @@ namespace WindBot.Game.AI.Decks ...@@ -549,13 +549,13 @@ namespace WindBot.Game.AI.Decks
int spell_count = 0; int spell_count = 0;
IList<ClientCard> grave = Bot.Graveyard; IList<ClientCard> grave = Bot.Graveyard;
IList<ClientCard> all = new List<ClientCard>(); IList<ClientCard> all = new List<ClientCard>();
foreach (ClientCard check in grave) foreach (ClientCard check in grave)
{ {
if (check.Id == CardId.GiantRex) if (check.Id == CardId.GiantRex)
{ {
all.Add(check); all.Add(check);
} }
} }
foreach (ClientCard check in grave) foreach (ClientCard check in grave)
{ {
...@@ -572,8 +572,8 @@ namespace WindBot.Game.AI.Decks ...@@ -572,8 +572,8 @@ namespace WindBot.Game.AI.Decks
all.Add(check); all.Add(check);
} }
} }
if (AI.Utils.ChainContainsCard(CardId.FairyTailSnow)) return false; if (AI.Utils.ChainContainsCard(CardId.FairyTailSnow)) return false;
if ( Duel.Player == 1 && Duel.Phase == DuelPhase.BattleStart && Bot.BattlingMonster == null && Enemy_atk >=Bot.LifePoints || if ( Duel.Player == 1 && Duel.Phase == DuelPhase.BattleStart && Bot.BattlingMonster == null && Enemy_atk >=Bot.LifePoints ||
Duel.Player == 0 && Duel.Phase==DuelPhase.BattleStart && Enemy.BattlingMonster == null && Enemy.LifePoints<=1850 Duel.Player == 0 && Duel.Phase==DuelPhase.BattleStart && Enemy.BattlingMonster == null && Enemy.LifePoints<=1850
) )
...@@ -590,9 +590,9 @@ namespace WindBot.Game.AI.Decks ...@@ -590,9 +590,9 @@ namespace WindBot.Game.AI.Decks
private bool SouleatingOviraptoreff() private bool SouleatingOviraptoreff()
{ {
if (!OvertexCoatlseff_used && Bot.GetRemainingCount(CardId.OvertexCoatls, 3) > 0) if (!OvertexCoatlseff_used && Bot.GetRemainingCount(CardId.OvertexCoatls, 3) > 0)
{ {
AI.SelectCard(CardId.OvertexCoatls); AI.SelectCard(CardId.OvertexCoatls);
AI.SelectOption(0); AI.SelectOption(0);
} }
else else
{ {
...@@ -992,7 +992,7 @@ namespace WindBot.Game.AI.Decks ...@@ -992,7 +992,7 @@ namespace WindBot.Game.AI.Decks
private bool LostWindeff() private bool LostWindeff()
{ {
if (Card.Location == CardLocation.Grave) if (Card.Location == CardLocation.Grave)
return true; return true;
List<ClientCard> check = Enemy.GetMonsters(); List<ClientCard> check = Enemy.GetMonsters();
foreach (ClientCard m in check) foreach (ClientCard m in check)
......
...@@ -172,8 +172,8 @@ namespace WindBot.Game.AI.Decks ...@@ -172,8 +172,8 @@ namespace WindBot.Game.AI.Decks
if (result.Count > 0) if (result.Count > 0)
break; break;
} }
AI.Utils.CheckSelectCount(result, cards, min, max);
return result; return AI.Utils.CheckSelectCount(result, cards, min, max);
} }
private bool UnexpectedDaiEffect() private bool UnexpectedDaiEffect()
......
...@@ -120,15 +120,13 @@ namespace WindBot.Game.AI.Decks ...@@ -120,15 +120,13 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
AI.Utils.SelectPreferredCards(result, new[] {
CardId.MistArchfiend, CardId.MistArchfiend,
CardId.PanzerDragon, CardId.PanzerDragon,
CardId.SolarWindJammer, CardId.SolarWindJammer,
CardId.StarDrawing CardId.StarDrawing
}, cards, min, max); }, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
private bool NormalSummon() private bool NormalSummon()
......
...@@ -181,10 +181,8 @@ namespace WindBot.Game.AI.Decks ...@@ -181,10 +181,8 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(CardId.YosenjuTsujik, cards, min, max);
AI.Utils.SelectPreferredCards(result, CardId.YosenjuTsujik, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
private bool PotOfDualityEffect() private bool PotOfDualityEffect()
......
...@@ -138,14 +138,12 @@ namespace WindBot.Game.AI.Decks ...@@ -138,14 +138,12 @@ namespace WindBot.Game.AI.Decks
public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max) public override IList<ClientCard> OnSelectXyzMaterial(IList<ClientCard> cards, int min, int max)
{ {
IList<ClientCard> result = new List<ClientCard>(); IList<ClientCard> result = AI.Utils.SelectPreferredCards(new[] {
AI.Utils.SelectPreferredCards(result, new[] {
CardId.StarDrawing, CardId.StarDrawing,
CardId.SolarWindJammer, CardId.SolarWindJammer,
CardId.Goblindbergh CardId.Goblindbergh
}, cards, min, max); }, cards, min, max);
AI.Utils.CheckSelectCount(result, cards, min, max); return AI.Utils.CheckSelectCount(result, cards, min, max);
return result;
} }
private bool Number39Utopia() private bool Number39Utopia()
......
...@@ -127,7 +127,7 @@ namespace WindBot.Game.AI ...@@ -127,7 +127,7 @@ namespace WindBot.Game.AI
public void SendOnDirectAttack(string attacker) public void SendOnDirectAttack(string attacker)
{ {
if (attacker == "" || attacker == null) if (string.IsNullOrEmpty(attacker))
{ {
attacker = _facedownmonstername; attacker = _facedownmonstername;
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
using WindBot; using WindBot;
using WindBot.Game; using WindBot.Game;
...@@ -82,12 +83,12 @@ namespace WindBot.Game.AI ...@@ -82,12 +83,12 @@ namespace WindBot.Game.AI
public virtual void OnChaining(int player, ClientCard card) public virtual void OnChaining(int player, ClientCard card)
{ {
// For overriding
} }
public virtual void OnChainEnd() public virtual void OnChainEnd()
{ {
// For overriding
} }
public virtual void OnNewPhase() public virtual void OnNewPhase()
{ {
...@@ -239,12 +240,7 @@ namespace WindBot.Game.AI ...@@ -239,12 +240,7 @@ namespace WindBot.Game.AI
private bool DefaultNoExecutor() private bool DefaultNoExecutor()
{ {
foreach (CardExecutor exec in Executors) return Executors.All(exec => exec.Type != Type || exec.CardId != Card.Id);
{
if (exec.Type == Type && exec.CardId == Card.Id)
return false;
}
return true;
} }
} }
} }
\ No newline at end of file
...@@ -151,27 +151,27 @@ namespace WindBot.Game ...@@ -151,27 +151,27 @@ namespace WindBot.Game
public bool HasLinkMarker(int dir) public bool HasLinkMarker(int dir)
{ {
return ((LinkMarker & dir) != 0); return (LinkMarker & dir) != 0;
} }
public bool HasLinkMarker(LinkMarker dir) public bool HasLinkMarker(LinkMarker dir)
{ {
return ((LinkMarker & (int)dir) != 0); return (LinkMarker & (int)dir) != 0;
} }
public bool HasType(CardType type) public bool HasType(CardType type)
{ {
return ((Type & (int)type) != 0); return (Type & (int)type) != 0;
} }
public bool HasPosition(CardPosition position) public bool HasPosition(CardPosition position)
{ {
return ((Position & (int)position) != 0); return (Position & (int)position) != 0;
} }
public bool HasAttribute(CardAttribute attribute) public bool HasAttribute(CardAttribute attribute)
{ {
return ((Attribute & (int)attribute) != 0); return (Attribute & (int)attribute) != 0;
} }
public bool IsMonster() public bool IsMonster()
...@@ -221,7 +221,7 @@ namespace WindBot.Game ...@@ -221,7 +221,7 @@ namespace WindBot.Game
public bool IsDisabled() public bool IsDisabled()
{ {
return (Disabled != 0); return Disabled != 0;
} }
public bool HasXyzMaterial() public bool HasXyzMaterial()
......
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game namespace WindBot.Game
...@@ -94,20 +96,18 @@ namespace WindBot.Game ...@@ -94,20 +96,18 @@ namespace WindBot.Game
} }
return count; return count;
} }
public int GetFieldCount() public int GetFieldCount()
{ {
return GetSpellCount() + GetMonsterCount(); return GetSpellCount() + GetMonsterCount();
} }
public int GetFieldHandCount() public int GetFieldHandCount()
{ {
return GetSpellCount() + GetMonsterCount() + GetHandCount(); return GetSpellCount() + GetMonsterCount() + GetHandCount();
} }
public bool IsFieldEmpty() public bool IsFieldEmpty()
{ {
return GetMonsters().Count == 0 && GetSpells().Count == 0; return GetMonsters().Count == 0 && GetSpells().Count == 0;
...@@ -117,7 +117,6 @@ namespace WindBot.Game ...@@ -117,7 +117,6 @@ namespace WindBot.Game
{ {
return GetCards(MonsterZone); return GetCards(MonsterZone);
} }
public List<ClientCard> GetGraveyardMonsters() public List<ClientCard> GetGraveyardMonsters()
{ {
...@@ -141,23 +140,12 @@ namespace WindBot.Game ...@@ -141,23 +140,12 @@ namespace WindBot.Game
public List<ClientCard> GetMonstersInExtraZone() public List<ClientCard> GetMonstersInExtraZone()
{ {
List<ClientCard> cards = new List<ClientCard>(); return GetMonsters().Where((card, i) => i >= 5).ToList();
if (MonsterZone[5] != null)
cards.Add(MonsterZone[5]);
if (MonsterZone[6] != null)
cards.Add(MonsterZone[6]);
return cards;
} }
public List<ClientCard> GetMonstersInMainZone() public List<ClientCard> GetMonstersInMainZone()
{ {
List<ClientCard> cards = new List<ClientCard>(); return GetMonsters().Where((card, i) => i < 5).ToList();
for (int i = 0; i < 5; i++)
{
if (MonsterZone[i] != null)
cards.Add(MonsterZone[i]);
}
return cards;
} }
public bool HasInHand(int cardId) public bool HasInHand(int cardId)
...@@ -202,24 +190,12 @@ namespace WindBot.Game ...@@ -202,24 +190,12 @@ namespace WindBot.Game
public bool HasAttackingMonster() public bool HasAttackingMonster()
{ {
IList<ClientCard> monsters = GetMonsters(); return GetMonsters().Any(card => card.IsAttack());
foreach (ClientCard card in monsters)
{
if (card.IsAttack())
return true;
}
return false;
} }
public bool HasDefendingMonster() public bool HasDefendingMonster()
{ {
IList<ClientCard> monsters = GetMonsters(); return GetMonsters().Any(card => card.IsDefense());
foreach (ClientCard card in monsters)
{
if (card.IsDefense())
return true;
}
return false;
} }
public bool HasInMonstersZone(int cardId, bool notDisabled = false, bool hasXyzMaterial = false) public bool HasInMonstersZone(int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
...@@ -315,94 +291,46 @@ namespace WindBot.Game ...@@ -315,94 +291,46 @@ namespace WindBot.Game
public int GetRemainingCount(int cardId, int initialCount) public int GetRemainingCount(int cardId, int initialCount)
{ {
int remaining = initialCount; int remaining = initialCount;
foreach (ClientCard card in Hand) remaining = remaining - Hand.Count(card => card != null && card.Id == cardId);
if (card != null && card.Id == cardId) remaining = remaining - SpellZone.Count(card => card != null && card.Id == cardId);
remaining--; remaining = remaining - Graveyard.Count(card => card != null && card.Id == cardId);
foreach (ClientCard card in SpellZone) remaining = remaining - Banished.Count(card => card != null && card.Id == cardId);
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--;
return (remaining < 0) ? 0 : remaining; return (remaining < 0) ? 0 : remaining;
} }
private static int GetCount(IEnumerable<ClientCard> cards) private static int GetCount(IEnumerable<ClientCard> cards)
{ {
int count = 0; return cards.Count(card => card != null);
foreach (ClientCard card in cards)
{
if (card != null)
count++;
}
return count;
} }
public int GetCountCardInZone(IEnumerable<ClientCard> cards, int cardId) public int GetCountCardInZone(IEnumerable<ClientCard> cards, int cardId)
{ {
int count = 0; return cards.Count(card => card != null && card.Id == cardId);
foreach (ClientCard card in cards)
{
if (card != null && card.Id == cardId)
count++;
}
return count;
} }
public int GetCountCardInZone(IEnumerable<ClientCard> cards, List<int> cardId) public int GetCountCardInZone(IEnumerable<ClientCard> cards, List<int> cardId)
{ {
int count = 0; return cards.Count(card => card != null && cardId.Contains(card.Id));
foreach (ClientCard card in cards)
{
if (card != null && cardId.Contains(card.Id))
count++;
}
return count;
} }
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards, CardType type) private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards, CardType type)
{ {
List<ClientCard> nCards = new List<ClientCard>(); return cards.Where(card => card != null && card.HasType(type)).ToList();
foreach (ClientCard card in cards)
{
if (card != null && card.HasType(type))
nCards.Add(card);
}
return nCards;
} }
private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards) private static List<ClientCard> GetCards(IEnumerable<ClientCard> cards)
{ {
List<ClientCard> nCards = new List<ClientCard>(); return cards.Where(card => card != null).ToList();
foreach (ClientCard card in cards)
{
if (card != null)
nCards.Add(card);
}
return nCards;
} }
private static bool HasInCards(IEnumerable<ClientCard> cards, int cardId, bool notDisabled = false, bool hasXyzMaterial = false) private static bool HasInCards(IEnumerable<ClientCard> cards, int cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{ {
foreach (ClientCard card in cards) return cards.Any(card => card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
{
if (card != null && card.Id == cardId && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
} }
private static bool HasInCards(IEnumerable<ClientCard> cards, IList<int> cardId, bool notDisabled = false, bool hasXyzMaterial = false) private static bool HasInCards(IEnumerable<ClientCard> cards, IList<int> cardId, bool notDisabled = false, bool hasXyzMaterial = false)
{ {
foreach (ClientCard card in cards) return cards.Any(card => card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()));
{
if (card != null && cardId.Contains(card.Id) && !(notDisabled && card.IsDisabled()) && !(hasXyzMaterial && !card.HasXyzMaterial()))
return true;
}
return false;
} }
} }
} }
\ No newline at end of file
...@@ -83,8 +83,7 @@ namespace WindBot.Game ...@@ -83,8 +83,7 @@ namespace WindBot.Game
} }
catch (Exception) catch (Exception)
{ {
if (reader != null) reader?.Close();
reader.Close();
return null; return null;
} }
} }
......
...@@ -360,10 +360,7 @@ namespace WindBot.Game ...@@ -360,10 +360,7 @@ namespace WindBot.Game
return result; return result;
result = new List<ClientCard>(); result = new List<ClientCard>();
// TODO: use selector // TODO: use selector
for (int i = 0; i < cards.Count; i++) result = cards.ToList();
{
result.Add(cards[i]);
}
return result; return result;
} }
...@@ -550,10 +547,15 @@ namespace WindBot.Game ...@@ -550,10 +547,15 @@ namespace WindBot.Game
} }
else else
{ {
if (hint == HINTMSG_SMATERIAL) switch (hint)
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max); {
if (hint == HINTMSG_RELEASE) case HINTMSG_SMATERIAL:
selected = Executor.OnSelectRitualTribute(cards, sum, min, max); selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
break;
case HINTMSG_RELEASE:
selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
break;
}
} }
if (selected != null) if (selected != null)
{ {
...@@ -1017,12 +1019,7 @@ namespace WindBot.Game ...@@ -1017,12 +1019,7 @@ namespace WindBot.Game
/// <returns>A list of the selected attributes.</returns> /// <returns>A list of the selected attributes.</returns>
public virtual IList<CardAttribute> OnAnnounceAttrib(int count, IList<CardAttribute> attributes) public virtual IList<CardAttribute> OnAnnounceAttrib(int count, IList<CardAttribute> attributes)
{ {
IList<CardAttribute> foundAttributes = new List<CardAttribute>(); IList<CardAttribute> foundAttributes = m_attributes.Where(attributes.Contains).ToList();
foreach (CardAttribute attribute in m_attributes)
{
if(attributes.Contains(attribute))
foundAttributes.Add(attribute);
}
if (foundAttributes.Count > 0) if (foundAttributes.Count > 0)
return foundAttributes; return foundAttributes;
...@@ -1037,12 +1034,7 @@ namespace WindBot.Game ...@@ -1037,12 +1034,7 @@ namespace WindBot.Game
/// <returns>A list of the selected races.</returns> /// <returns>A list of the selected races.</returns>
public virtual IList<CardRace> OnAnnounceRace(int count, IList<CardRace> races) public virtual IList<CardRace> OnAnnounceRace(int count, IList<CardRace> races)
{ {
IList<CardRace> foundRaces = new List<CardRace>(); IList<CardRace> foundRaces = m_races.Where(races.Contains).ToList();
foreach (CardRace race in m_races)
{
if (races.Contains(race))
foundRaces.Add(race);
}
if (foundRaces.Count > 0) if (foundRaces.Count > 0)
return foundRaces; return foundRaces;
...@@ -1080,12 +1072,10 @@ namespace WindBot.Game ...@@ -1080,12 +1072,10 @@ namespace WindBot.Game
private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1) private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1)
{ {
Executor.SetCard(type, card, desc); Executor.SetCard(type, card, desc);
if (card != null && return card != null &&
exec.Type == type && exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) && (exec.CardId == -1 || exec.CardId == card.Id) &&
(exec.Func == null || exec.Func())) (exec.Func == null || exec.Func());
return true;
return false;
} }
} }
} }
...@@ -619,7 +619,7 @@ namespace WindBot.Game ...@@ -619,7 +619,7 @@ namespace WindBot.Game
_duel.Fields[attackcard.Controller].BattlingMonster = attackcard; _duel.Fields[attackcard.Controller].BattlingMonster = attackcard;
_duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard; _duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard;
if (ld == 0 && (attackcard != null) && (ca != 0)) if (ld == 0 && ca != 0)
{ {
_ai.OnDirectAttack(attackcard); _ai.OnDirectAttack(attackcard);
} }
...@@ -732,9 +732,8 @@ namespace WindBot.Game ...@@ -732,9 +732,8 @@ namespace WindBot.Game
packet.ReadInt32(); // ??? packet.ReadInt32(); // ???
ClientCard card = _duel.GetCard(player, (CardLocation)loc, seq); 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) private void OnUpdateData(BinaryReader packet)
...@@ -1448,11 +1447,7 @@ namespace WindBot.Game ...@@ -1448,11 +1447,7 @@ namespace WindBot.Game
ClientCard equipCard = _duel.GetCard(equipCardControler, (CardLocation)equipCardLocation, equipCardSequence); ClientCard equipCard = _duel.GetCard(equipCardControler, (CardLocation)equipCardLocation, equipCardSequence);
ClientCard targetCard = _duel.GetCard(targetCardControler, (CardLocation)targetCardLocation, targetCardSequence); ClientCard targetCard = _duel.GetCard(targetCardControler, (CardLocation)targetCardLocation, targetCardSequence);
if (equipCard == null || targetCard == null) return; if (equipCard == null || targetCard == null) return;
if (equipCard.EquipTarget != null) equipCard.EquipTarget?.EquipCards.Remove(equipCard);
{
equipCard.EquipTarget.EquipCards.Remove(equipCard);
}
equipCard.EquipTarget = targetCard; equipCard.EquipTarget = targetCard;
targetCard.EquipCards.Add(equipCard); 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