Exemplo n.º 1
0
        public IPBEPokemonCollection AskForParty(bool requireLegal)
        {
            Send(new PBEPartyRequestPacket(BattleId, requireLegal));
            WaitForResponse(requireLegal ? typeof(PBELegalPartyResponsePacket) : typeof(PBEPartyResponsePacket));
            IPBEPokemonCollection ret = _party;

            _party = null;
            return(ret);
        }
Exemplo n.º 2
0
 public PBEPartyResponsePacket(IPBEPokemonCollection party)
 {
     using (var ms = new MemoryStream())
         using (var w = new EndianBinaryWriter(ms, encoding: EncodingType.UTF16))
         {
             w.Write(Code);
             (Party = party).ToBytes(w);
             Data = new ReadOnlyCollection <byte>(ms.ToArray());
         }
 }
Exemplo n.º 3
0
        internal static void ToBytes(this IPBEPokemonCollection party, EndianBinaryWriter w)
        {
            byte count = (byte)party.Count;

            w.Write(count);
            for (int i = 0; i < count; i++)
            {
                party[i].ToBytes(w);
            }
        }
Exemplo n.º 4
0
        private void OnPacketReceived(object sender, IPBEPacket packet)
        {
            // TODO: Kick players who are sending broken packets or sending too many
            Type type = packet.GetType();

            Debug.WriteLine($"Packet received ({BattleId} {TrainerName} \"{type.Name}\")");
            if (_packetType != null && type.Equals(_packetType))
            {
                _packetType = null;
                switch (packet)
                {
                case PBELegalPartyResponsePacket lprp:
                {
                    Console.WriteLine($"Received party ({BattleId} {TrainerName})");
                    if (!Server.Settings.Equals(lprp.Party.Settings))
                    {
                        Console.WriteLine("Party does not have matching settings!");
                        Console.WriteLine("\tServer: \"{0}\"", Server.Settings);
                        Console.WriteLine("\tParty: \"{0}\"", lprp.Party.Settings);
                    }
                    else
                    {
                        _party = lprp.Party;
                        resetEvent.Set();
                    }
                    break;
                }

                case PBEPartyResponsePacket prp:
                {
                    Console.WriteLine($"Received party ({BattleId} {TrainerName})");
                    _party = prp.Party;
                    resetEvent.Set();
                    break;
                }

                default: resetEvent.Set(); break;
                }
            }
            else if (_actionType != null && (type.Equals(_actionType) || type.Equals(typeof(PBEFleeResponsePacket))))
            {
                _actionType = null;
                switch (packet)
                {
                case PBEActionsResponsePacket arp: Server.ActionsSubmitted(this, arp.Actions); break;

                case PBEFleeResponsePacket _: Server.FleeSubmitted(this); break;

                case PBESwitchInResponsePacket sirp: Server.SwitchesSubmitted(this, sirp.Switches); break;

                default: throw new ArgumentOutOfRangeException(nameof(packet));
                }
            }
        }
Exemplo n.º 5
0
 public PBEPartyResponsePacket(IPBEPokemonCollection party)
 {
     if (party == null)
     {
         throw new ArgumentNullException(nameof(party));
     }
     using (var ms = new MemoryStream())
         using (var w = new EndianBinaryWriter(ms, encoding: EncodingType.UTF16))
         {
             w.Write(Code);
             (Party = party).ToBytes(w);
             Data = new ReadOnlyCollection <byte>(ms.GetBuffer());
         }
 }
Exemplo n.º 6
0
 protected PBETrainerInfoBase(IPBEPokemonCollection party)
 {
     if (party == null)
     {
         throw new ArgumentNullException(nameof(party));
     }
     if (party.Count < 1)
     {
         throw new ArgumentException("Party count must be at least 1", nameof(party));
     }
     if (party is PBELegalPokemonCollection lp)
     {
         _requiredSettings = lp.Settings;
     }
     Party = new ReadOnlyCollection <IPBEPokemon>(party.ToArray());
 }
 public PBETrainerInfo(IPBEPokemonCollection party, string name)
 {
     if (party == null)
     {
         throw new ArgumentNullException(nameof(party));
     }
     if (party.Count < 1)
     {
         throw new ArgumentException("Party count must be at least 1", nameof(party));
     }
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentOutOfRangeException(nameof(name));
     }
     if (party is PBELegalPokemonCollection lp)
     {
         _requiredSettings = lp.Settings;
     }
     Party = new ReadOnlyCollection <IPBEPokemon>(party.ToArray());
     Name  = name;
 }
Exemplo n.º 8
0
 protected PBETrainerInfoBase(IPBEPokemonCollection party)
 {
     if (party is IPBEPartyPokemonCollection ppc)
     {
         if (!ppc.Any(p => p.HP > 0 && !p.PBEIgnore))
         {
             throw new ArgumentException("Party must have at least 1 conscious battler", nameof(party));
         }
     }
     else
     {
         if (!party.Any(p => !p.PBEIgnore))
         {
             throw new ArgumentException("Party must have at least 1 conscious battler", nameof(party));
         }
     }
     if (party is PBELegalPokemonCollection lp)
     {
         _requiredSettings = lp.Settings;
     }
     Party = new ReadOnlyCollection <IPBEPokemon>(party.ToArray());
 }
Exemplo n.º 9
0
 internal PBEPartyResponsePacket(byte[] data, EndianBinaryReader r)
 {
     Data  = new ReadOnlyCollection <byte>(data);
     Party = new PBEReadOnlyPokemonCollection(r);
 }
Exemplo n.º 10
0
        private void OnClientConnected(object sender, PBEServerClient client)
        {
            // Need to spawn a new thread so "WaitOne()" doesn't block the thread that receives client packets
            new Thread(() =>
            {
                lock (this)
                {
                    // Wait for the server to be in a state where no events will be sent
                    _resetEvent.WaitOne();

                    string name = PBEDataProvider.GlobalRandom.RandomElement(new string[] { "Sasha", "Nikki", "Lara", "Violet", "Naomi", "Rose", "Sabrina", "Nicole" });
                    if (_battlerCounter < NumBattlers)
                    {
                        byte i = _battlerCounter;
                        Console.WriteLine($"Client connected ({client.IP} {i} {name})");
                        var newPlayer = new Player(this, client, i, name);
                        IPBEPokemonCollection party = newPlayer.AskForParty(RequireLegalParties);
                        if (party == null)
                        {
                            newPlayer.Dispose();
                            return;
                        }
                        _incomingTrainers[i / NumTrainersPerTeam][i % NumTrainersPerTeam] = new PBETrainerInfo(party, name);
                        _battlers[i] = newPlayer;
                        _readyPlayers.Add(client, newPlayer);

                        // Start battle
                        if (++_battlerCounter == NumBattlers)
                        {
                            Console.WriteLine("All players connected!");
                            _battle                 = new PBEBattle(BattleFormat, Settings, _incomingTrainers[0], _incomingTrainers[1]);
                            _incomingTrainers       = null;
                            _battle.OnNewEvent     += PBEBattle.ConsoleBattleEventHandler;
                            _battle.OnNewEvent     += BattleEventHandler;
                            _battle.OnStateChanged += BattleStateHandler;
                            _server.Battle          = _battle;
                            BattleStateHandler(_battle); // Call RunTurn, which sends battle packet
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Client connected ({client.IP} {byte.MaxValue} {name})");
                        var newPlayer   = new Player(this, client, byte.MaxValue, name);
                        var chakoPackay = new PBEPlayerJoinedPacket(name);
                        foreach (Player p in _readyPlayers.Values.ToArray())
                        {
                            p.Send(chakoPackay);
                            if (!p.WaitForResponse(typeof(PBEResponsePacket)) && p.BattleId != byte.MaxValue)
                            {
                                newPlayer.Dispose();
                                return;
                            }
                        }
                        _spectatorPackets.Add(chakoPackay);
                        foreach (IPBEPacket packet in _spectatorPackets)
                        {
                            newPlayer.Send(packet);
                            if (!newPlayer.WaitForResponse(typeof(PBEResponsePacket)))
                            {
                                newPlayer.Dispose();
                                return;
                            }
                        }
                        _readyPlayers.Add(client, newPlayer);
                    }
                }
            })
            {
                Name = "Client Connected Thread"
            }.Start();
        }