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.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
namespace BotWrapper
{
class BotWrapper
{
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, int uType);
const int MB_ICONERROR = 0x00000010;
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetFullPath("WindBot");
startInfo.FileName = startInfo.WorkingDirectory + "\\WindBot.exe";
if (args.Length == 3)
{
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string arg = args[0];
Match match = Regex.Match(arg, "Random=(.*)");
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
namespace BotWrapper
{
class BotWrapper
{
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, int uType);
const int MB_ICONERROR = 0x00000010;
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetFullPath("WindBot");
startInfo.FileName = startInfo.WorkingDirectory + "\\WindBot.exe";
if (args.Length == 3)
{
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
string arg = args[0];
Match match = Regex.Match(arg, "Random=(.*)");
if (match.Success)
{
string randomFlag = match.Groups[1].Value;
......@@ -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);
}
}
arg = arg.Replace("'", "\"");
if (int.Parse(args[1]) == 1)
{
arg += " Hand=1";
}
arg += " Port=" + args[2];
startInfo.Arguments = arg;
}
try
{
Process.Start(startInfo);
}
catch
{
MessageBox((IntPtr)0, "WindBot can't be started!", "WindBot", MB_ICONERROR);
}
}
}
arg = arg.Replace("'", "\"");
if (int.Parse(args[1]) == 1)
{
arg += " Hand=1";
}
arg += " Port=" + args[2];
startInfo.Arguments = arg;
}
try
{
Process.Start(startInfo);
}
catch
{
MessageBox((IntPtr)0, "WindBot can't be started!", "WindBot", MB_ICONERROR);
}
}
public class BotInfo
{
public string name;
public string command;
public string desc;
public string[] flags;
}
static public IList<BotInfo> Bots = new List<BotInfo>();
}
static public IList<BotInfo> Bots = new List<BotInfo>();
static void ReadBots()
{
using (StreamReader reader = new StreamReader("bot.conf"))
{
while (!reader.EndOfStream)
{
using (StreamReader reader = new StreamReader("bot.conf"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();
if (line.Length > 0 && line[0] == '!')
{
......@@ -88,8 +88,8 @@ namespace BotWrapper
}
}
}
}
}
static string GetRandomBot(string flag)
{
IList<BotInfo> foundBots = Bots.Where(bot => bot.flags.Contains(flag)).ToList();
......@@ -100,6 +100,6 @@ namespace BotWrapper
return bot.command;
}
return "";
}
}
}
}
}
}
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()
......
......@@ -353,13 +353,13 @@ namespace WindBot.Game.AI.Decks
}
return false;
}
private bool GamecieltheSeaTurtleKaijusp()
{
if (!Bot.HasInMonstersZone(CardId.UltimateConductorTytanno))
return DefaultKaijuSpsummon();
return false;
}
private bool GamecieltheSeaTurtleKaijusp()
{
if (!Bot.HasInMonstersZone(CardId.UltimateConductorTytanno))
return DefaultKaijuSpsummon();
return false;
}
private bool RadiantheMultidimensionalKaijusp()
......@@ -409,9 +409,9 @@ namespace WindBot.Game.AI.Decks
}
private bool MonsterRepos()
{
{
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.GlowUpBulb && Card.IsDefense()) return false;
if (Card.Id == CardId.ShaddollDragon && Card.IsFacedown() && Enemy.GetMonsterCount() >= 0) return true;
......@@ -526,8 +526,8 @@ namespace WindBot.Game.AI.Decks
private bool FairyTailSnowsummon()
{
ClientCard target = AI.Utils.GetBestEnemyMonster(true, true);
{
ClientCard target = AI.Utils.GetBestEnemyMonster(true, true);
if(target != null)
{
return true;
......@@ -549,13 +549,13 @@ namespace WindBot.Game.AI.Decks
int spell_count = 0;
IList<ClientCard> grave = Bot.Graveyard;
IList<ClientCard> all = new List<ClientCard>();
foreach (ClientCard check in grave)
{
if (check.Id == CardId.GiantRex)
{
all.Add(check);
}
IList<ClientCard> all = new List<ClientCard>();
foreach (ClientCard check in grave)
{
if (check.Id == CardId.GiantRex)
{
all.Add(check);
}
}
foreach (ClientCard check in grave)
{
......@@ -572,8 +572,8 @@ namespace WindBot.Game.AI.Decks
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 ||
Duel.Player == 0 && Duel.Phase==DuelPhase.BattleStart && Enemy.BattlingMonster == null && Enemy.LifePoints<=1850
)
......@@ -590,9 +590,9 @@ namespace WindBot.Game.AI.Decks
private bool SouleatingOviraptoreff()
{
if (!OvertexCoatlseff_used && Bot.GetRemainingCount(CardId.OvertexCoatls, 3) > 0)
{
AI.SelectCard(CardId.OvertexCoatls);
AI.SelectOption(0);
{
AI.SelectCard(CardId.OvertexCoatls);
AI.SelectOption(0);
}
else
{
......@@ -992,7 +992,7 @@ namespace WindBot.Game.AI.Decks
private bool LostWindeff()
{
if (Card.Location == CardLocation.Grave)
if (Card.Location == CardLocation.Grave)
return true;
List<ClientCard> check = Enemy.GetMonsters();
foreach (ClientCard m in check)
......
......@@ -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
......@@ -94,20 +96,18 @@ namespace WindBot.Game
}
return count;
}
public int GetFieldCount()
{
return GetSpellCount() + GetMonsterCount();
}
public int GetFieldHandCount()
{
return GetSpellCount() + GetMonsterCount() + GetHandCount();
}
public bool IsFieldEmpty()
{
return GetMonsters().Count == 0 && GetSpells().Count == 0;
......@@ -117,7 +117,6 @@ namespace WindBot.Game
{
return GetCards(MonsterZone);
}
public List<ClientCard> GetGraveyardMonsters()
{
......@@ -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)
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
if (hint == HINTMSG_RELEASE)
selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
switch (hint)
{
case HINTMSG_SMATERIAL:
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
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 &&
exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) &&
(exec.Func == null || exec.Func()))
return true;
return false;
return card != null &&
exec.Type == type &&
(exec.CardId == -1 || exec.CardId == card.Id) &&
(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