private PBEBattle(BinaryReader r) { ushort version = r.ReadUInt16(); Settings = new PBESettings(r); Settings.MakeReadOnly(); BattleFormat = (PBEBattleFormat)r.ReadByte(); Teams = new PBETeams(this); var packetProcessor = new PBEPacketProcessor(this); int numEvents = r.ReadInt32(); for (int i = 0; i < numEvents; i++) { INetPacket packet = packetProcessor.CreatePacket(r.ReadBytes(r.ReadInt16())); switch (packet) { case PBEWinnerPacket wp: { Winner = wp.WinningTeam; break; } } Events.Add(packet); } BattleState = PBEBattleState.Ended; OnStateChanged?.Invoke(this); }
public PBEBattle(PBEBattlePacket packet) { BattleFormat = packet.BattleFormat; BattleTerrain = packet.BattleTerrain; Weather = packet.Weather; Settings = packet.Settings; Teams = new PBETeams(this, packet, out ReadOnlyCollection <PBETrainer> trainers); Trainers = trainers; }
/// <summary>Creates a new <see cref="PBEBattle"/> object with the specified <see cref="PBEBattleFormat"/> and a copy of the specified <see cref="PBESettings"/>. <see cref="BattleState"/> will be <see cref="PBEBattleState.WaitingForPlayers"/>.</summary> /// <param name="battleFormat">The <see cref="PBEBattleFormat"/> of the battle.</param> /// <param name="settings">The <see cref="PBESettings"/> to copy for the battle to use.</param> public PBEBattle(PBEBattleFormat battleFormat, PBESettings settings) { if (battleFormat >= PBEBattleFormat.MAX) { throw new ArgumentOutOfRangeException(nameof(battleFormat)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } BattleFormat = battleFormat; Settings = new PBESettings(settings); Settings.MakeReadOnly(); Teams = new PBETeams(this); BattleState = PBEBattleState.WaitingForPlayers; OnStateChanged?.Invoke(this); }
/// <summary>Creates a new <see cref="PBEBattle"/> object with the specified <see cref="PBEBattleFormat"/> and teams. Each team must have equal settings. The battle's settings are set to a copy of the teams' settings. <see cref="BattleState"/> will be <see cref="PBEBattleState.ReadyToBegin"/>.</summary> /// <param name="battleFormat">The <see cref="PBEBattleFormat"/> of the battle.</param> /// <param name="team1Shell">The <see cref="PBETeamShell"/> object to use to create <see cref="Teams"/>[0].</param> /// <param name="team1TrainerName">The name of the trainer(s) on <see cref="Teams"/>[0].</param> /// <param name="team2Shell">The <see cref="PBETeamShell"/> object to use to create <see cref="Teams"/>[1].</param> /// <param name="team2TrainerName">The name of the trainer(s) on <see cref="Teams"/>[1].</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="team1Shell"/> or <paramref name="team2Shell"/> are null.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="team1Shell"/> and <paramref name="team2Shell"/> have unequal <see cref="PBETeamShell.Settings"/> or when <paramref name="team1TrainerName"/> or <paramref name="team2TrainerName"/> are invalid.</exception> public PBEBattle(PBEBattleFormat battleFormat, PBETeamShell team1Shell, string team1TrainerName, PBETeamShell team2Shell, string team2TrainerName) { if (battleFormat >= PBEBattleFormat.MAX) { throw new ArgumentOutOfRangeException(nameof(battleFormat)); } if (team1Shell == null) { throw new ArgumentNullException(nameof(team1Shell)); } if (team2Shell == null) { throw new ArgumentNullException(nameof(team2Shell)); } if (string.IsNullOrWhiteSpace(team1TrainerName)) { throw new ArgumentOutOfRangeException(nameof(team1TrainerName)); } if (string.IsNullOrWhiteSpace(team2TrainerName)) { throw new ArgumentOutOfRangeException(nameof(team2TrainerName)); } if (team1Shell.IsDisposed) { throw new ObjectDisposedException(nameof(team1Shell)); } if (team2Shell.IsDisposed) { throw new ObjectDisposedException(nameof(team2Shell)); } if (!team1Shell.Settings.Equals(team2Shell.Settings)) { throw new ArgumentOutOfRangeException(nameof(team1Shell.Settings), "Team settings must be equal to each other."); } BattleFormat = battleFormat; Settings = new PBESettings(team1Shell.Settings); Settings.MakeReadOnly(); Teams = new PBETeams(this, team1Shell, team1TrainerName, team2Shell, team2TrainerName); CheckForReadiness(); }
public PBEBattle(PBEBattleFormat battleFormat, PBESettings settings, IReadOnlyList <PBETrainerInfo> ti0, IReadOnlyList <PBETrainerInfo> ti1, PBEBattleTerrain battleTerrain = PBEBattleTerrain.Plain, PBEWeather weather = PBEWeather.None) { if (battleFormat >= PBEBattleFormat.MAX) { throw new ArgumentOutOfRangeException(nameof(battleFormat)); } if (battleTerrain >= PBEBattleTerrain.MAX) { throw new ArgumentOutOfRangeException(nameof(battleTerrain)); } if (weather >= PBEWeather.MAX) { throw new ArgumentOutOfRangeException(nameof(weather)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (!settings.IsReadOnly) { throw new ArgumentException("Settings must be read-only.", nameof(settings)); } if (ti0 == null || ti0.Any(t => t == null)) { throw new ArgumentNullException(nameof(ti0)); } if (ti1 == null || ti1.Any(t => t == null)) { throw new ArgumentNullException(nameof(ti1)); } BattleTerrain = battleTerrain; BattleFormat = battleFormat; Settings = settings; Weather = weather; Teams = new PBETeams(this, ti0, ti1, out ReadOnlyCollection <PBETrainer> trainers); Trainers = trainers; void QueueUp(PBETeam team, int i, PBEFieldPosition pos) { PBETrainer t; if (team.Trainers.Count == 1) { t = team.Trainers[0]; } else { t = team.GetTrainer(pos); i = 0; } PBEList <PBEBattlePokemon> party = t.Party; if (i < party.Count) { PBEBattlePokemon p = party[i]; p.Trainer.SwitchInQueue.Add((p, pos)); } } switch (BattleFormat) { case PBEBattleFormat.Single: { foreach (PBETeam team in Teams) { QueueUp(team, 0, PBEFieldPosition.Center); } break; } case PBEBattleFormat.Double: { foreach (PBETeam team in Teams) { QueueUp(team, 0, PBEFieldPosition.Left); QueueUp(team, 1, PBEFieldPosition.Right); } break; } case PBEBattleFormat.Triple: { foreach (PBETeam team in Teams) { QueueUp(team, 0, PBEFieldPosition.Left); QueueUp(team, 1, PBEFieldPosition.Center); QueueUp(team, 2, PBEFieldPosition.Right); } break; } case PBEBattleFormat.Rotation: { foreach (PBETeam team in Teams) { QueueUp(team, 0, PBEFieldPosition.Center); QueueUp(team, 1, PBEFieldPosition.Left); QueueUp(team, 2, PBEFieldPosition.Right); } break; } default: throw new ArgumentOutOfRangeException(nameof(BattleFormat)); } BattleState = PBEBattleState.ReadyToBegin; OnStateChanged?.Invoke(this); }