Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="NewCards"></param>
        private void AddToCollection(MTGCollection NewCards)
        {
            try
            {
                ArrayList Display = new ArrayList();


                // update the collection data grid
                Collection.Add(NewCards);

                foreach (Int32 cardnumber in Collection.Cards)
                {
                    MTGCard card = new MTGCard();

                    foreach (MTGCard cardInfo in ((MTGCardSet)CardSets[0]).CardSet)
                    {
                        if (cardInfo.ID == cardnumber)
                        {
                            card = cardInfo;
                            if (Display.Contains(card))
                            {
                                // increment the quantity
                                Int32 index = Display.IndexOf(card);
                                ((MTGCard)Display[index]).Quantity += 1;
                            }
                            else
                            {
                                Display.Add(card);
                            }
                            break;
                        }
                    }
                }

                UpdateCollection(Display);

                Display = new ArrayList();

                UpdateCombo("Create New");
                // now update the combobox
                foreach (String DeckName in Collection.Decks.Keys)
                {
                    UpdateCombo(DeckName);
                    UpdateCombo2(DeckName);
                }
            }
            catch (Exception ex)
            {
                LogError("AddToCollection:  Unable to add cards to collection. [" + ex.Message + "]");
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        public MTGClientForm()
        {
            InitializeComponent();

            // init the collection data
            Collection = new MTGCollection();

            // setup the collection data grid view
            SetupDataGridView();

            // Welcome the player
            UpdateStatusStrip("Welcome to Magic the Gathering Online!");

            // Disable all the extra tabs until someone is logged in
            EnableTabPages(false);

            // Load the picture data set
            CardSets = new ArrayList();
            try
            {
                // Attempt to load the last cardset saved
                Stream          stream     = File.Open("10E.dat", FileMode.Open);
                BinaryFormatter bformatter = new BinaryFormatter();

                MTGCardSet cards = new MTGCardSet();
                cards = (MTGCardSet)bformatter.Deserialize(stream);
                stream.Close();

                CardSets.Add(cards);
            }
            catch (Exception ex)
            {
                if (!ex.Message.StartsWith("Could not find file"))
                {
                    LogError(String.Format("Init: ", ex.Message));
                }
                else
                {
                    LogError("Cannot find the cards data pack.");
                }
            }

            WaitingData = new byte[PacketSize];
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Purchase"></param>
        /// <returns></returns>
        private MTGCollection DeterminePurchases(String Purchase)
        {
            MTGCollection Collection = new MTGCollection();

            // mmb - todo:

            // Info about Purchase String
            // Edition:TypeOfPurchase:Quanity
            // Edition Example: 10th is the only edition we have right now.
            // TypeOfPurchase Example: Foil, Preconstructed Theme Deck, Starter Deck, and/or Fat Pack
            // Quantity Example:  1, 2, 3, etc.  How many do they want to buy?


//Set                       Set symbol              Set code        Release date        Size    Common  Uncommon    Rare    Basic Land  Other
//Core Set - Tenth Edition  A Roman-numeral ten     10E             July 14, 2007[13]   383     121     121         121     20          —



            //Tenth Edition  features 383 cards, including randomly inserted premium versions of all cards in the set.
            //It is available in booster packs, preconstructed theme decks, fat packs, and two-player starter packs.

            // Magic the Gathering Tenth Edition Core Set includes:
            // a Player's Guide with complete visual encyclopedia,
            // two card boxes with panoramic art and six plastic dividers,
            // six Tenth Edition booster packs,
            // 40 card Tenth Edition basic land pack,
            // Tenth Edition Spindown life counter, (available only in the fat pack),
            // plus one random Pro Tour Player card.

            // Magic The Gathering - 10th Edition Theme Deck Set Of 5 Includes
            // Each starter theme deck includes a pre-constructed, ready to play 40-card deck
            // A strategy insert, and a random Pro Player card.
            // Theme deck includes:
            // Kamahl's Temper (Red), Arcanis's Guile (Blue), Molimo's Might (Green) , Evincar's Tyranny (Black), Cho-Manno's Resolve (White) Theme Decks

            // Foil = 15 Cards.  1 Land, 1 Rare or Mythic Rare, 3 Uncommon, and 10 Common.

            /*
             *
             * Kamahl's Temper
             * Core Set - Tenth Edition Theme Deck
             *
             * Patience is no virtue. With the pit fighter Kamahl and a horde of temper-challenged "haste" creatures at your command,
             * you'll scorch your way to victory before your enemy even finds the snooze button.
             * Attack and burn anything that stands in your way. Why wait?
             *
             #	Name	            Rarity	Cost
             # 1	Raging Goblin	        C   Red Mana
             # 1	Viashino Sandscout	    C	1 ManaRed Mana
             # 2	Bloodrock Cyclops	    C	2 ManaRed Mana
             # 2	Bogardan Firefiend	    C	2 ManaRed Mana
             # 1	Prodigal Pyromancer	    C	2 ManaRed Mana
             # 2	Lightning Elemental	    C	3 ManaRed Mana
             # 1	Furnace Whelp	        U	2 ManaRed ManaRed Mana
             # 2	Thundering Giant	    U	3 ManaRed ManaRed Mana
             # 1	Kamahl, Pit Fighter	    R	4 ManaRed ManaRed Mana
             # 1	Shock	                C	Red Mana
             # 2	Incinerate	            C	1 ManaRed Mana
             # 2	Spitting Earth	        C	1 ManaRed Mana
             # 1	Threaten	            U   2 ManaRed Mana
             # 1	Beacon of Destruction	R	3 ManaRed ManaRed Mana
             # 1	Blaze	                U	X ManaRed Mana
             # 1	Dragon's Claw	        U	2 Mana
             # 1	Phyrexian Vault	        U	3 Mana
             # 17	Mountain
             # Kamahl's Temper
             #
             * = from a previous set
             * */


            try
            {
                // Edition:TypeOfPurchase:Quantity
                Int32  FirstColon     = Purchase.IndexOf(":");
                Int32  SecondColon    = Purchase.IndexOf(":", FirstColon + 1);
                String Edition        = Purchase.Substring(0, FirstColon);
                String TypeOfPurchase = Purchase.Substring(FirstColon + 1, SecondColon - FirstColon - 1);
                Int32  Quantity       = Convert.ToInt32(Purchase.Substring(SecondColon + 1));

                // mmb - don't do anything about the Edition right now... Will always be 10E

                // loop for the quantity
                for (Int32 i = 0; i < Quantity; i++)
                {
                    //
                    switch (TypeOfPurchase)
                    {
                    case "FOIL":

                        // Foil = 15 Cards.  1 Land, 1 Rare or Mythic Rare, 3 Uncommon, and 10 Common.

                        // mmb - make cards picks random!

                        // mmb - but for current testing use these...

                        // land x 1
                        Collection.Cards.Add(129559);

                        // commons x 10
                        Collection.Cards.Add(130522);
                        Collection.Cards.Add(135185);
                        Collection.Cards.Add(130985);
                        Collection.Cards.Add(132106);
                        Collection.Cards.Add(135194);
                        Collection.Cards.Add(129671);
                        Collection.Cards.Add(134758);
                        Collection.Cards.Add(135216);
                        Collection.Cards.Add(129579);
                        Collection.Cards.Add(129533);

                        // uncommon x 3
                        Collection.Cards.Add(135267);
                        Collection.Cards.Add(129459);
                        Collection.Cards.Add(129495);

                        // rare x 1
                        Collection.Cards.Add(106426);

                        break;

                    case "KAMAHLSTEMPER":

                        // Kamahl's Temper Red Theme Deck for 10th Edition

                        // lands x 17
                        for (i = 0; i < 17; i++)
                        {
                            Collection.Cards.Add(129650);
                        }

                        // commons x 14
                        Collection.Cards.Add(129688);
                        Collection.Cards.Add(130387);
                        Collection.Cards.Add(130384);
                        Collection.Cards.Add(130384);
                        Collection.Cards.Add(130534);
                        Collection.Cards.Add(130534);
                        Collection.Cards.Add(134752);
                        Collection.Cards.Add(129624);
                        Collection.Cards.Add(129624);
                        Collection.Cards.Add(129624);
                        Collection.Cards.Add(134751);
                        Collection.Cards.Add(134751);
                        Collection.Cards.Add(136509);
                        Collection.Cards.Add(136509);

                        // uncommon x 7
                        Collection.Cards.Add(130386);
                        Collection.Cards.Add(130381);
                        Collection.Cards.Add(130381);
                        Collection.Cards.Add(129767);
                        Collection.Cards.Add(129484);
                        Collection.Cards.Add(129527);
                        Collection.Cards.Add(135281);

                        // rare x 2
                        Collection.Cards.Add(106398);
                        Collection.Cards.Add(135262);

                        break;
                    }
                }

                // Add these purchases to the player's current collection
                // mmb - todo

                // if this is a preconstructed theme deck, then add it to the decks too
                if (TypeOfPurchase == "KAMAHLSTEMPER")
                {
                    Collection.Decks.Add("Kamahl's Temper", Collection.Cards);
                }
            }
            catch (Exception ex)
            {
                AddError(String.Format("DeterminePurchases: {0}", ex.Message));
            }

            // send back the players newly updated collection
            return(Collection);
        }
Beispiel #4
0
        /// <summary>
        /// When a packet is received, this function is called
        /// </summary>
        /// <param name="ar"></param>
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                Socket clientSocket = (Socket)ar.AsyncState;
                clientSocket.EndReceive(ar);

                ClientInfo clientInfo;
                byte[]     message = null;
                String     User    = "******";


                //Transform the array of bytes received from the user into an
                //intelligent form of object Data
                MTGNetworkPacket msgReceived = new MTGNetworkPacket(byteData);
                MTGNetworkPacket msgToSend   = new MTGNetworkPacket();


                //If the message is to login, logout, or simple text message
                //then when send to others the type of the message remains the same
                switch (msgReceived.OpCode)
                {
                // *** Login ***
                case MTGNetworkPacket.MTGOpCode.Login:

                    Int32 result = 0;

                    // verify that this user can log on
                    String data = msgReceived.Data.ToString();
                    String user = data.Substring(0, data.IndexOf(":"));
                    String pass = data.Substring(data.IndexOf(":") + 1);

                    // now check the user against the database...
                    if (MTGDB.ValidateUser(user, pass))
                    {
                        result = 1;

                        if (MTGDB.IsAdmin(user, pass))
                        {
                            result += 1;
                        }
                    }

                    //When a user logs in to the server then we add them to our list of clients
                    clientInfo        = new ClientInfo();
                    clientInfo.Socket = clientSocket;
                    clientInfo.Player = user;
                    ClientList.Add(clientInfo);

                    // Send back a login packet to the user to tell them if user was validate against db
                    msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.Login;
                    msgToSend.Data   = result;

                    message = msgToSend.ToByte();

                    clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);

                    // this player isn't a valid player, so disconnect the client
                    if (result == 0)
                    {
                        // error, just disconnect this connection
                        foreach (ClientInfo client in ClientList)
                        {
                            if (client.Socket == clientSocket)
                            {
                                // remove this user from the client list
                                ClientList.Remove(client);
                                break;
                            }
                        }
                    }
                    else
                    {
                        // mmb - todo
                        // save data like online field, login time, etc

                        //Send the name of the users in the chat room... User has logged in.
                        // Send back a login packet to the user to tell them if user was validate against db
                        msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.Chat;
                        msgToSend.Data   = "Player [" + user + "] is now online";
                        message          = msgToSend.ToByte();
                        foreach (ClientInfo client in ClientList)
                        {
                            client.Socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), client.Socket);
                        }

                        // Query DB to get Player's collection and send it back to that client
                        MTGCollection collection = MTGDB.GetPlayerCollection(user);
                        if (collection.Cards.Count > 0)
                        {
                            // send the collection data for this user to his client
                            msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.ReceiveCollection;
                            msgToSend.Data   = collection;
                            message          = msgToSend.ToByte();
                            clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
                        }
                    }

                    break;

                // *** Logout ***
                case MTGNetworkPacket.MTGOpCode.Logout:

                    // mmb - todo
                    // save data like online field, logout time, etc

                    // send an acknowledgement logout message back to client
                    msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.Logout;
                    msgToSend.Data   = "1";

                    message = msgToSend.ToByte();

                    clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSendAndClose), clientSocket);


                    //Send the name of the users in the chat room... User has logged in.
                    // Send back a login packet to the user to tell them if user was validate against db
                    msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.Chat;

                    foreach (ClientInfo client in ClientList)
                    {
                        if (client.Socket == clientSocket)
                        {
                            User = "******" + client.Player + "] ";
                        }
                    }
                    msgToSend.Data = "Player [" + User + "] is offline";
                    message        = msgToSend.ToByte();


                    //When a user wants to log out of the server then we search for her
                    //in the list of clients and close the corresponding connection
                    foreach (ClientInfo client in ClientList)
                    {
                        if (client.Socket == clientSocket)
                        {
                            // remove user from client list
                            ClientList.Remove(client);
                            break;
                        }
                        else
                        {
                            //Send the name of this logged out player to all the players in the chat room...
                            client.Socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), client.Socket);
                        }
                    }

                    // break the network connection with the client
                    clientSocket.Close();

                    break;

                // *** Buy ***
                case MTGNetworkPacket.MTGOpCode.Purchase:

                    // Determine what is purchased
                    String        Purchase   = msgReceived.Data.ToString();
                    MTGCollection Collection = DeterminePurchases(Purchase);

                    // send back the udpated collection with the purchased items in it
                    msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.PurchaseReceive;
                    msgToSend.Data   = Collection;
                    message          = msgToSend.ToByte();
                    clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);

                    break;

                // *** Chat ***
                case MTGNetworkPacket.MTGOpCode.Chat:

                    // First, get the string
                    String ChatData = msgReceived.Data.ToString();

                    // now find out the player's name
                    foreach (ClientInfo client in ClientList)
                    {
                        if (client.Socket == clientSocket)
                        {
                            // this is the player, collect the name
                            User = "******" + client.Player + "] ";
                        }
                    }

                    // Check to see if this is a command
                    // examples:
                    // /who
                    // /help
                    // /commands
                    // /friends
                    if (ChatData.StartsWith("/"))
                    {
                        String Outbound = "UNKOWN";


                        switch (ChatData.Substring(1).ToUpper())
                        {
                        case "WHO":
                            // list all the online players
                            // mmb - TODO
                            Outbound = "An unknown number of people are online";
                            break;

                        default:
                            Outbound = "Unknown Command";
                            break;
                        }

                        // Create the outgoing chat packet
                        msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.Chat;
                        msgToSend.Data   = Outbound;
                        message          = msgToSend.ToByte();
                        clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
                    }
                    else
                    {
                        // This is just a normal chat message, so send it to all the other online players

                        // Create the outgoing chat packet
                        msgToSend.OpCode = MTGNetworkPacket.MTGOpCode.Chat;
                        msgToSend.Data   = User + ChatData;

                        message = msgToSend.ToByte();

                        foreach (ClientInfo client in ClientList)
                        {
                            // send chat message back to other players
                            // including the player who sent it
                            client.Socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), client.Socket);
                        }
                    }

                    break;

                // *** Catchall...
                default:
                    // record this because it shouldn't be happening
                    AddError(String.Format("OnReceive: Unknown Packet Recieved. {0}", msgReceived.OpCode));
                    break;
                }

                //If the user is logging out then we need not listen from her
                if (msgReceived.OpCode != MTGNetworkPacket.MTGOpCode.Logout)
                {
                    //Start listening to the message send by the user
                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), clientSocket);
                }
            }
            catch (Exception ex)
            {
                AddError(String.Format("OnReceive: {0}", ex.Message));
            }
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ar"></param>
        private void OnReceive(IAsyncResult ar)
        {
            Int32 size = 0;


            try
            {
                LogDebug("Client has received a message from the server.");

                if (ar.IsCompleted)
                {
                    size = clientSocket.EndReceive(ar);


                    if (size < PacketSize)
                    {
                        if (WaitingForData > 1)
                        {
                            byte[] temp = new byte[PacketSize * WaitingForData];
                            WaitingData.CopyTo(temp, 0);
                            byteData.CopyTo(temp, WaitingData.Length);
                            WaitingData = new byte[PacketSize * WaitingForData];
                            WaitingData = temp;
                        }
                        else
                        {
                            WaitingForData = 1;
                            byteData.CopyTo(WaitingData, 0);
                        }

                        MTGNetworkPacket packet = new MTGNetworkPacket(WaitingData);

                        //Accordingly process the message received
                        switch (packet.OpCode)
                        {
                        case MTGNetworkPacket.MTGOpCode.Login:
                            String result = packet.Data.ToString();
                            //AddStatus("Login: [" + result + "]");

                            // was login successful?
                            if (result != "0")
                            {
                                // if so, then change login button, make edit boxes uneditable, etc
                                SetLoginButtonText("Logout");
                                EnableUserTextBox(false);
                                EnablePasswordTextBox(false);

                                LogInfo("Login successful!  Welcome to the MTG Server!");
                                UpdateStatusStrip("Successfully Logged on to the MTG Server");

                                // check to see if this user is an admin
                                if (result == "2")
                                {
                                    Admin = true;
                                }

                                EnableTabPages(true);
                            }
                            else
                            {
                                // if not, then disconnect from server
                                UpdateStatusStrip("Failed to Log on to the MTG Server");

                                // message was successfully sent
                                clientSocket.Close();

                                SetLoginButtonText("Login");
                                EnableUserTextBox(true);
                                EnablePasswordTextBox(true);

                                Connected = false;
                                LogInfo("Log on has failed!  Disconnected");
                            }
                            break;

                        case MTGNetworkPacket.MTGOpCode.Logout:

                            // mmb - this won't happen!

                            LogDebug("Logout: [" + packet.Data.ToString() + "]");
                            UpdateStatusStrip("Successfully Logged off the MTG Server");

                            EnableTabPages(false);
                            Receiving = true;

                            break;

                        case MTGNetworkPacket.MTGOpCode.PurchaseReceive:
                            LogDebug("PurchaseReceive: [" + packet.Data.ToString() + "]");
                            MTGCollection NewCards = (MTGCollection)packet.Data;
                            UpdateStatusStrip("Purchase made.");
                            AddToCollection(NewCards);
                            UpdateStatusStrip("New purchase of cards have been added to your collection.");
                            break;

                        case MTGNetworkPacket.MTGOpCode.ReceiveCollection:
                            LogDebug("ReceiveCollection: [" + packet.Data.ToString() + "]");
                            Collection.Clear();
                            Collection = (MTGCollection)packet.Data;
                            UpdateStatusStrip("Player Collection has been received.");
                            break;

                        case MTGNetworkPacket.MTGOpCode.Chat:
                            Log(packet.Data.ToString());
                            break;
                        }

                        WaitingData = new byte[PacketSize];
                    }
                    else
                    {
                        // wait for more data?
                        LogDebug("OnReceive:  Waiting for more data from server...");

                        byte[] temp = new byte[PacketSize * WaitingForData];
                        WaitingData.CopyTo(temp, 0);
                        byteData.CopyTo(temp, (PacketSize * (WaitingForData - 1)));
                        WaitingData = new byte[PacketSize * WaitingForData];
                        WaitingData = temp;
                        WaitingForData++;
                    }
                }
            }
            catch (ObjectDisposedException exo)
            {
                String Error = "OnReceive:  Unable to receive. ObjectDisposed. [" + exo.Message + "]";
                //AddError(Error);
                LogError("Connection to the server was terminated.");
            }
            catch (Exception ex)
            {
                LogError("OnReceive:  Unable to receive. [" + ex.Message + "]");
            }

            if (size != 0 && Connected)
            {
                // now wait for a reply
                try
                {
                    byteData = new byte[PacketSize];
                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
                catch (Exception ex)
                {
                    LogError("OnReceive:  Unable to begin to receive. [" + ex.Message + "]");
                }
            }
        }