public static int GetFieldPositionIndex(this PBETrainer trainer, PBEFieldPosition position) { if (!trainer.OwnsSpot(position)) { throw new ArgumentOutOfRangeException(nameof(position)); } PBEBattleFormat battleFormat = trainer.Battle.BattleFormat; int index = trainer.Team.Trainers.IndexOf(trainer); switch (battleFormat) { case PBEBattleFormat.Single: { switch (position) { case PBEFieldPosition.Center: return(0); } break; } case PBEBattleFormat.Double: { switch (position) { case PBEFieldPosition.Left: return(0); case PBEFieldPosition.Right: return(index == 1 ? 0 : 1); } break; } case PBEBattleFormat.Triple: { switch (position) { case PBEFieldPosition.Left: return(0); case PBEFieldPosition.Center: return(index == 1 ? 0 : 1); case PBEFieldPosition.Right: return(index == 2 ? 0 : 2); } break; } case PBEBattleFormat.Rotation: { switch (position) { case PBEFieldPosition.Center: return(0); case PBEFieldPosition.Left: return(1); case PBEFieldPosition.Right: return(2); } break; } } throw new Exception(); }
/// <summary>Determines whether chosen switches are valid.</summary> /// <param name="trainer">The trainer the inputted switches belong to.</param> /// <param name="switches">The switches the team wishes to execute.</param> /// <returns>False if the team already chose switches or the switches are illegal, True otherwise.</returns> /// <exception cref="InvalidOperationException">Thrown when <see cref="BattleState"/> is not <see cref="PBEBattleState.WaitingForSwitchIns"/>.</exception> public static bool AreSwitchesValid(PBETrainer trainer, IReadOnlyList <PBESwitchIn> switches) { if (trainer == null) { throw new ArgumentNullException(nameof(trainer)); } if (switches == null || switches.Any(s => s == null)) { throw new ArgumentNullException(nameof(switches)); } if (trainer.Battle.BattleState != PBEBattleState.WaitingForSwitchIns) { throw new InvalidOperationException($"{nameof(BattleState)} must be {PBEBattleState.WaitingForSwitchIns} to validate switches."); } if (trainer.SwitchInsRequired == 0 || switches.Count != trainer.SwitchInsRequired) { return(false); } var verified = new List <PBEBattlePokemon>(trainer.SwitchInsRequired); foreach (PBESwitchIn s in switches) { if (s.Position == PBEFieldPosition.None || s.Position >= PBEFieldPosition.MAX || !trainer.OwnsSpot(s.Position)) { return(false); } PBEBattlePokemon pkmn = trainer.TryGetPokemon(s.PokemonId); if (pkmn == null || pkmn.HP == 0 || pkmn.FieldPosition != PBEFieldPosition.None || verified.Contains(pkmn)) { return(false); } verified.Add(pkmn); } return(true); }
internal static bool AreSwitchesValid(PBETrainer trainer, IReadOnlyCollection <PBESwitchIn> switches, [NotNullWhen(false)] out string?invalidReason) { if (trainer.Battle._battleState != PBEBattleState.WaitingForSwitchIns) { throw new InvalidOperationException($"{nameof(BattleState)} must be {PBEBattleState.WaitingForSwitchIns} to validate switches."); } if (trainer.SwitchInsRequired == 0) { invalidReason = "Switches were already submitted"; return(false); } if (switches.Count != trainer.SwitchInsRequired) { invalidReason = $"Invalid amount of switches submitted; required amount is {trainer.SwitchInsRequired}"; return(false); } var verified = new List <PBEBattlePokemon>(trainer.SwitchInsRequired); foreach (PBESwitchIn s in switches) { if (s.Position == PBEFieldPosition.None || s.Position >= PBEFieldPosition.MAX || !trainer.OwnsSpot(s.Position)) { invalidReason = $"Invalid position ({s.PokemonId})"; return(false); } if (!trainer.TryGetPokemon(s.PokemonId, out PBEBattlePokemon? pkmn)) { invalidReason = $"Invalid Pokémon ID ({s.PokemonId})"; return(false); } if (pkmn.HP == 0) { invalidReason = $"Pokémon {s.PokemonId} is fainted"; return(false); } if (pkmn.PBEIgnore) { invalidReason = $"Pokémon {s.PokemonId} cannot battle"; return(false); } if (pkmn.FieldPosition != PBEFieldPosition.None) { invalidReason = $"Pokémon {s.PokemonId} is already on the field"; return(false); } if (verified.Contains(pkmn)) { invalidReason = $"Pokémon {s.PokemonId} was asked to be switched in multiple times"; return(false); } verified.Add(pkmn); } invalidReason = null; return(true); }
internal static string AreSwitchesValid(PBETrainer trainer, IReadOnlyCollection <PBESwitchIn> switches) { if (switches == null || switches.Any(s => s == null)) { throw new ArgumentNullException(nameof(switches)); } if (trainer.Battle._battleState != PBEBattleState.WaitingForSwitchIns) { throw new InvalidOperationException($"{nameof(BattleState)} must be {PBEBattleState.WaitingForSwitchIns} to validate switches."); } if (trainer.SwitchInsRequired == 0) { return("Switches were already submitted"); } if (switches.Count != trainer.SwitchInsRequired) { return($"Invalid amount of switches submitted; required amount is {trainer.SwitchInsRequired}"); } var verified = new List <PBEBattlePokemon>(trainer.SwitchInsRequired); foreach (PBESwitchIn s in switches) { if (s.Position == PBEFieldPosition.None || s.Position >= PBEFieldPosition.MAX || !trainer.OwnsSpot(s.Position)) { return($"Invalid position ({s.PokemonId})"); } PBEBattlePokemon pkmn = trainer.TryGetPokemon(s.PokemonId); if (pkmn is null) { return($"Invalid Pokémon ID ({s.PokemonId})"); } if (pkmn.HP == 0) { return($"Pokémon {s.PokemonId} is fainted"); } if (pkmn.FieldPosition != PBEFieldPosition.None) { return($"Pokémon {s.PokemonId} is already on the field"); } if (verified.Contains(pkmn)) { return($"Pokémon {s.PokemonId} was asked to be switched in multiple times"); } verified.Add(pkmn); } return(null); }