Exemplo n.º 1
0
        private void OnJoinGame(BinaryReader packet)
        {
            /*int lflist = (int)*/ packet.ReadUInt32();
            /*int rule = */ packet.ReadByte();
            /*int mode = */ packet.ReadByte();
            int duel_rule = packet.ReadByte();

            _ai.Duel.IsNewRule = (duel_rule == 4);
            BinaryWriter deck = GamePacketFactory.Create(CtosMessage.UpdateDeck);

            deck.Write(Deck.Cards.Count + Deck.ExtraCards.Count);
            deck.Write(Deck.SideCards.Count);
            foreach (NamedCard card in Deck.Cards)
            {
                deck.Write(card.Id);
            }
            foreach (NamedCard card in Deck.ExtraCards)
            {
                deck.Write(card.Id);
            }
            foreach (NamedCard card in Deck.SideCards)
            {
                deck.Write(card.Id);
            }
            Connection.Send(deck);
            _ai.OnJoinGame();
        }
Exemplo n.º 2
0
        private void OnSelectCounter(BinaryReader packet)
        {
            packet.ReadByte(); // player
            int type     = packet.ReadInt16();
            int quantity = packet.ReadInt16();

            IList <ClientCard> cards    = new List <ClientCard>();
            IList <int>        counters = new List <int>();
            int count = packet.ReadByte();

            for (int i = 0; i < count; ++i)
            {
                packet.ReadInt32(); // card id
                int          player = GetLocalPlayer(packet.ReadByte());
                CardLocation loc    = (CardLocation)packet.ReadByte();
                int          seq    = packet.ReadByte();
                int          num    = packet.ReadInt16();
                cards.Add(_duel.GetCard(player, loc, seq));
                counters.Add(num);
            }

            IList <int> used = _ai.OnSelectCounter(type, quantity, cards, counters);

            byte[] result = new byte[used.Count * 2];
            for (int i = 0; i < used.Count; ++i)
            {
                result[i * 2]     = (byte)(used[i] & 0xff);
                result[i * 2 + 1] = (byte)(used[i] >> 8);
            }
            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }
Exemplo n.º 3
0
        public void Chat(string message)
        {
            byte[]       content = Encoding.Unicode.GetBytes(message + "\0");
            BinaryWriter chat    = GamePacketFactory.Create(CtosMessage.Chat);

            chat.Write(content);
            Connection.Send(chat);
        }
Exemplo n.º 4
0
        public void Chat(string message, bool forced)
        {
            if (!forced && !_chat)
            {
                return;
            }
            byte[]       content = Encoding.Unicode.GetBytes(message + "\0");
            BinaryWriter chat    = GamePacketFactory.Create(CtosMessage.Chat);

            chat.Write(content);
            Connection.Send(chat);
        }
Exemplo n.º 5
0
        private void OnConnected()
        {
            BinaryWriter packet = GamePacketFactory.Create(CtosMessage.PlayerInfo);

            packet.WriteUnicode(Username, 20);
            Connection.Send(packet);

            byte[] junk = { 0xCC, 0xCC, 0x00, 0x00, 0x00, 0x00 };
            packet = GamePacketFactory.Create(CtosMessage.JoinGame);
            packet.Write(_proVersion);
            packet.Write(junk);
            packet.WriteUnicode(_roomInfo, 30);
            Connection.Send(packet);
        }
Exemplo n.º 6
0
        private void OnJoinGame(BinaryReader packet)
        {
            BinaryWriter deck = GamePacketFactory.Create(CtosMessage.UpdateDeck);

            deck.Write(Deck.Cards.Count + Deck.ExtraCards.Count);
            deck.Write(Deck.SideCards.Count);
            foreach (NamedCard card in Deck.Cards)
            {
                deck.Write(card.Id);
            }
            foreach (NamedCard card in Deck.ExtraCards)
            {
                deck.Write(card.Id);
            }
            foreach (NamedCard card in Deck.SideCards)
            {
                deck.Write(card.Id);
            }
            Connection.Send(deck);
        }
Exemplo n.º 7
0
        private void OnSelectPlace(BinaryReader packet)
        {
            packet.ReadByte(); // player
            packet.ReadByte(); // min
            int field = ~packet.ReadInt32();

            byte[] resp = new byte[3];

            bool pendulumZone = false;

            int filter;

            if ((field & 0x7f) != 0)
            {
                resp[0] = (byte)GetLocalPlayer(0);
                resp[1] = 0x4;
                filter  = field & 0x7f;
            }
            else if ((field & 0x1f00) != 0)
            {
                resp[0] = (byte)GetLocalPlayer(0);
                resp[1] = 0x8;
                filter  = (field >> 8) & 0x1f;
            }
            else if ((field & 0xc000) != 0)
            {
                resp[0]      = (byte)GetLocalPlayer(0);
                resp[1]      = 0x8;
                filter       = (field >> 14) & 0x3;
                pendulumZone = true;
            }
            else if ((field & 0x7f0000) != 0)
            {
                resp[0] = (byte)GetLocalPlayer(1);
                resp[1] = 0x4;
                filter  = (field >> 16) & 0x7f;
            }
            else if ((field & 0x1f000000) != 0)
            {
                resp[0] = (byte)GetLocalPlayer(1);
                resp[1] = 0x8;
                filter  = (field >> 24) & 0x1f;
            }
            else
            {
                resp[0]      = (byte)GetLocalPlayer(1);
                resp[1]      = 0x8;
                filter       = (field >> 30) & 0x3;
                pendulumZone = true;
            }

            if (!pendulumZone)
            {
                if ((filter & 0x40) != 0)
                {
                    resp[2] = 6;
                }
                else if ((filter & 0x20) != 0)
                {
                    resp[2] = 5;
                }
                else if ((filter & 0x4) != 0)
                {
                    resp[2] = 2;
                }
                else if ((filter & 0x2) != 0)
                {
                    resp[2] = 1;
                }
                else if ((filter & 0x8) != 0)
                {
                    resp[2] = 3;
                }
                else if ((filter & 0x1) != 0)
                {
                    resp[2] = 0;
                }
                else if ((filter & 0x10) != 0)
                {
                    resp[2] = 4;
                }
            }
            else
            {
                if ((filter & 0x1) != 0)
                {
                    resp[2] = 6;
                }
                if ((filter & 0x2) != 0)
                {
                    resp[2] = 7;
                }
            }

            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(resp);
            Connection.Send(reply);
        }
Exemplo n.º 8
0
        private void InternalOnSelectCard(BinaryReader packet, Func <IList <ClientCard>, int, int, bool, IList <ClientCard> > func)
        {
            packet.ReadByte(); // player
            bool cancelable = packet.ReadByte() != 0;
            int  min        = packet.ReadByte();
            int  max        = packet.ReadByte();

            IList <ClientCard> cards = new List <ClientCard>();
            int count = packet.ReadByte();

            for (int i = 0; i < count; ++i)
            {
                int          id     = packet.ReadInt32();
                int          player = GetLocalPlayer(packet.ReadByte());
                CardLocation loc    = (CardLocation)packet.ReadByte();
                int          seq    = packet.ReadByte();
                packet.ReadByte(); // pos
                ClientCard card;
                if (((int)loc & (int)CardLocation.Overlay) != 0)
                {
                    card = new ClientCard(id, CardLocation.Overlay);
                }
                else
                {
                    card = _duel.GetCard(player, loc, seq);
                }
                if (card == null)
                {
                    continue;
                }
                if (card.Id == 0)
                {
                    card.SetId(id);
                }
                cards.Add(card);
            }

            IList <ClientCard> selected = func(cards, min, max, cancelable);

            if (selected.Count == 0 && cancelable)
            {
                Connection.Send(CtosMessage.Response, -1);
                return;
            }

            byte[] result = new byte[selected.Count + 1];
            result[0] = (byte)selected.Count;
            for (int i = 0; i < selected.Count; ++i)
            {
                int id = 0;
                for (int j = 0; j < count; ++j)
                {
                    if (cards[j] == null)
                    {
                        continue;
                    }
                    if (cards[j].Equals(selected[i]))
                    {
                        id = j;
                        break;
                    }
                }
                result[i + 1] = (byte)id;
            }

            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }
Exemplo n.º 9
0
 private void OnChainSorting(BinaryReader packet)
 {
     /*BinaryWriter writer =*/ GamePacketFactory.Create(CtosMessage.Response);
     Connection.Send(CtosMessage.Response, -1);
 }
Exemplo n.º 10
0
        private void OnSelectSum(BinaryReader packet)
        {
            bool mode = packet.ReadByte() == 0;

            packet.ReadByte(); // player
            int sumval = packet.ReadInt32();
            int min    = packet.ReadByte();
            int max    = packet.ReadByte();

            IList <ClientCard> mandatoryCards = new List <ClientCard>();
            IList <ClientCard> cards          = new List <ClientCard>();

            for (int j = 0; j < 2; ++j)
            {
                int count = packet.ReadByte();
                for (int i = 0; i < count; ++i)
                {
                    int          cardId = packet.ReadInt32();
                    int          player = GetLocalPlayer(packet.ReadByte());
                    CardLocation loc    = (CardLocation)packet.ReadByte();
                    int          seq    = packet.ReadByte();
                    ClientCard   card   = _duel.GetCard(player, loc, seq);
                    if (cardId != 0 && card.Id != cardId)
                    {
                        card.SetId(cardId);
                    }
                    card.SelectSeq = i;
                    int OpParam  = packet.ReadInt32();
                    int OpParam1 = OpParam & 0xffff;
                    int OpParam2 = OpParam >> 16;
                    if (OpParam2 > 0 && OpParam1 > OpParam2)
                    {
                        card.OpParam1 = OpParam2;
                        card.OpParam2 = OpParam1;
                    }
                    else
                    {
                        card.OpParam1 = OpParam1;
                        card.OpParam2 = OpParam2;
                    }
                    if (j == 0)
                    {
                        mandatoryCards.Add(card);
                    }
                    else
                    {
                        cards.Add(card);
                    }
                }
            }

            for (int k = 0; k < mandatoryCards.Count; ++k)
            {
                sumval -= mandatoryCards[k].OpParam1;
            }

            IList <ClientCard> selected = _ai.OnSelectSum(cards, sumval, min, max, mode);

            byte[] result = new byte[mandatoryCards.Count + selected.Count + 1];
            int    index  = 0;

            result[index++] = (byte)(mandatoryCards.Count + selected.Count);
            while (index <= mandatoryCards.Count)
            {
                result[index++] = 0;
            }
            for (int i = 0; i < selected.Count; ++i)
            {
                result[index++] = (byte)selected[i].SelectSeq;
            }

            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }
Exemplo n.º 11
0
        private void OnSelectPlace(BinaryReader packet)
        {
            packet.ReadByte(); // player
            packet.ReadByte(); // min
            int field = ~packet.ReadInt32();

            const int LOCATION_MZONE = 0x4;
            const int LOCATION_SZONE = 0x8;
            const int LOCATION_PZONE = 0x200;

            int player;
            int location;
            int filter;

            if ((field & 0x7f) != 0)
            {
                player   = 0;
                location = LOCATION_MZONE;
                filter   = field & Zones.MonsterZones;
            }
            else if ((field & 0x1f00) != 0)
            {
                player   = 0;
                location = LOCATION_SZONE;
                filter   = (field >> 8) & Zones.SpellZones;
            }
            else if ((field & 0xc000) != 0)
            {
                player   = 0;
                location = LOCATION_PZONE;
                filter   = (field >> 14) & Zones.PendulumZones;
            }
            else if ((field & 0x7f0000) != 0)
            {
                player   = 1;
                location = LOCATION_MZONE;
                filter   = (field >> 16) & Zones.MonsterZones;
            }
            else if ((field & 0x1f000000) != 0)
            {
                player   = 1;
                location = LOCATION_SZONE;
                filter   = (field >> 24) & Zones.SpellZones;
            }
            else
            {
                player   = 1;
                location = LOCATION_PZONE;
                filter   = (field >> 30) & Zones.PendulumZones;
            }

            int selected = _ai.OnSelectPlace(_select_hint, player, location, filter);

            _select_hint = 0;

            byte[] resp = new byte[3];
            resp[0] = (byte)GetLocalPlayer(player);

            if (location != LOCATION_PZONE)
            {
                resp[1] = (byte)location;
                if ((selected & filter) > 0)
                {
                    filter &= selected;
                }

                if ((filter & Zones.z6) != 0)
                {
                    resp[2] = 6;
                }
                else if ((filter & Zones.z5) != 0)
                {
                    resp[2] = 5;
                }
                else if ((filter & Zones.z2) != 0)
                {
                    resp[2] = 2;
                }
                else if ((filter & Zones.z1) != 0)
                {
                    resp[2] = 1;
                }
                else if ((filter & Zones.z3) != 0)
                {
                    resp[2] = 3;
                }
                else if ((filter & Zones.z0) != 0)
                {
                    resp[2] = 0;
                }
                else if ((filter & Zones.z4) != 0)
                {
                    resp[2] = 4;
                }
            }
            else
            {
                resp[1] = (byte)LOCATION_SZONE;
                if ((selected & filter) > 0)
                {
                    filter &= selected;
                }

                if ((filter & Zones.z0) != 0)
                {
                    resp[2] = 6;
                }
                if ((filter & Zones.z1) != 0)
                {
                    resp[2] = 7;
                }
            }

            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(resp);
            Connection.Send(reply);
        }
Exemplo n.º 12
0
        private void OnConnected()
        {
            BinaryWriter packet = GamePacketFactory.Create(CtosMessage.PlayerInfo);

            packet.WriteUnicode(Username, 20);
            Connection.Send(packet);
            byte[] padding2 = { 0xAA, 0xBB };
            if (CreateGame == null)
            {
                packet = GamePacketFactory.Create(CtosMessage.JoinGame);
                packet.Write((short)_proVersion);
                packet.Write(padding2);
                packet.Write(RoomId);
                packet.WriteUnicode(_roomInfo, 20);
                packet.Write(_proVersion);
                Connection.Send(packet);
                return;
            }
            byte[] unused   = { 0x00, 0x00 };
            byte[] padding3 = { 0xAA, 0xBB, 0xCC };
            packet = GamePacketFactory.Create(CtosMessage.CreateGame);
            // hostInfo
            packet.Write(CreateGame.banlistHash);
            packet.Write(CreateGame.allowed);
            packet.Write(unused); // mode & duelRule
            packet.Write((byte)(CreateGame.dontCheckDeck ? 1 : 0));
            packet.Write((byte)(CreateGame.dontShuffleDeck ? 1 : 0));
            packet.Write(padding3);
            packet.Write(CreateGame.startingLP);
            packet.Write(CreateGame.startingDrawCount);
            packet.Write(CreateGame.drawCountPerTurn);
            packet.Write(CreateGame.timeLimitInSeconds);
            packet.Write((uint)((CreateGame.duelFlags >> 32) & 0xFFFFFFFF));
            packet.Write((uint)4043399681); // handshake
            packet.Write(_proVersion);      // version
            packet.Write(CreateGame.t0Count);
            packet.Write(CreateGame.t1Count);
            packet.Write(CreateGame.bestOf);
            packet.Write((uint)(CreateGame.duelFlags & 0xFFFFFFFF));
            packet.Write(CreateGame.forb);
            packet.Write(CreateGame.extraRules);
            packet.Write(padding2);
            // name
            packet.WriteUnicode("", 20); // UNUSED
            // pass
            packet.WriteUnicode(_roomInfo, 20);
            // notes
            const int MAX_NOTES_LENGTH = 200;

            try
            {
                // Write notes in UTF8 format making sure to always write exactly
                // MAX_NOTES_LENGTH bytes.
                byte[] content = Encoding.UTF8.GetBytes(CreateGame.notes + "\0");
                if (content.Length > MAX_NOTES_LENGTH)
                {
                    throw new Exception();
                }
                packet.Write(content);
                for (int i = MAX_NOTES_LENGTH - content.Length; i > 0; i--)
                {
                    packet.Write((byte)0);
                }
            }
            catch (Exception)
            {
                Logger.WriteErrorLine("Warning: Unable to encode CreateGame.notes, sending empty string instead.");
                for (int i = 0; i < (MAX_NOTES_LENGTH / 8); i++)
                {
                    packet.Write((ulong)0);
                }
            }
            Connection.Send(packet);
        }
Exemplo n.º 13
0
        private void OnSelectSum(BinaryReader packet)
        {
            packet.ReadByte(); // mode
            packet.ReadByte(); // player
            int sumval = packet.ReadInt32();
            int min    = packet.ReadByte();
            int max    = packet.ReadByte();

            IList <ClientCard> mandatoryCards = new List <ClientCard>();
            IList <ClientCard> cards          = new List <ClientCard>();

            for (int j = 0; j < 2; ++j)
            {
                int count = packet.ReadByte();
                for (int i = 0; i < count; ++i)
                {
                    int          cardId = packet.ReadInt32();
                    int          player = GetLocalPlayer(packet.ReadByte());
                    CardLocation loc    = (CardLocation)packet.ReadByte();
                    int          seq    = packet.ReadByte();
                    ClientCard   card   = _duel.GetCard(player, loc, seq);
                    if (card != null)
                    {
                        if (cardId != 0 && card.Id != cardId)
                        {
                            card.SetId(cardId);
                        }
                    }
                    if (j == 0)
                    {
                        mandatoryCards.Add(card);
                    }
                    else
                    {
                        cards.Add(card);
                    }
                    packet.ReadInt32();
                }
            }

            IList <ClientCard> selected = _ai.OnSelectSum(cards, sumval, min, max);

            byte[] result = new byte[mandatoryCards.Count + selected.Count + 1];
            int    index  = 0;

            result[index++] = (byte)(mandatoryCards.Count + selected.Count);
            while (index < mandatoryCards.Count)
            {
                result[index++] = 0;
            }
            for (int i = 0; i < selected.Count; ++i)
            {
                int id = 0;
                for (int j = 0; j < cards.Count; ++j)
                {
                    if (cards[j] == null)
                    {
                        continue;
                    }
                    if (cards[j].Equals(selected[i]))
                    {
                        id = j;
                        break;
                    }
                }
                result[index++] = (byte)id;
            }

            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }