コード例 #1
0
        /// <summary>
        /// Destroy an existing item instance permanently.
        /// </summary>
        public void DiscardItem(ContainerType source, ushort srcSlot)
        {
            Item srcItem = GetContainer(source).GetItem(srcSlot);

            if (srcItem == null)
            {
                throw new ArgumentException($"Invalid source item in container: {source}, slot: {srcSlot}");
            }

            var transaction = new InventoryTransaction(owner);

            transaction.Add(InventoryAction.Discard, source, srcSlot);
            transaction.Commit();
        }
コード例 #2
0
        public string Buy(string pCDinfo, long pCC, long pPhone)
        {
            // minimize the tx time, get as much setup down before starting Tx
            // 2nd thread to simulate a call to Visa/MC to verify and charge amount to CC

            var words    = pCDinfo.Split(' ');
            var pCdID    = Convert.ToInt32(words[0]);
            var pCdPrice = Convert.ToDecimal(words[1]);

            var t2      = new Thread(ValidateCC); // create second Thread, point it to the code (method) to execute
            var theData = new MyData();

            theData.CC       = pCC;
            theData.phoneNum = pPhone;
            theData.CardOK   = false;
            theData.Price    = pCdPrice;
            var othersNotDone = true; // whill use this to decide when we can proceed

            t2.Start(theData);        // start the thread

            var connection    = new SqlConnection(@"Server = localhost;Database=NodeOrders;Integrated Security=SSPI");
            var updateCommand = new SqlCommand("UPDATE Inventory  " +
                                               " SET ItemQuantity = (ItemQuantity - 1) " +
                                               " WHERE CdID = " + pCdID +
                                               " AND ItemQuantity > 0 ", connection);
            // this constuction avoids concurrency issues (I think!)

            SqlTransaction InventoryTransaction;                       // declare a variable of type SqlTransaction

            connection.Open();                                         // after we create the connection, we can start the Tx
            InventoryTransaction      = connection.BeginTransaction(); //declare the begining of the transaction (set)
            updateCommand.Transaction = InventoryTransaction;          // assign a Transaction “master” to the SQL cmd

            // 2 parts of Tx are, deal with inventory table, and deal with VISA/MC

            var recChanged = updateCommand.ExecuteNonQuery(); // do the SQL


            while (othersNotDone) // waiting until thread 2 completes communicaiton with VISA/MC
            {
                othersNotDone = false;
                Thread.Sleep(1000); // be a bit patient
                if (t2.IsAlive)
                {
                    othersNotDone = true;
                    Console.WriteLine("tick!");
                }
            }

            var returnMsg = "Thank you for your purchase.";

            //checked that invt returned 1 (we had a disc, and CC went thru ok)
            if (recChanged != 1 || theData.CardOK == false)
            {
                InventoryTransaction.Rollback(); // we had one or more problems, so roll back the changes
                if (theData.CardOK)              // we charged the card, but had no inventory
                {
                    returnMsg = "Sorry, we are out of stock for that CD.";
                }
                else
                {
                    returnMsg = "Sorry, your credit card was refused.";
                }
            }
            else // all is well,
            {
                InventoryTransaction.Commit();
            }
            connection.Close();


            return(returnMsg);
        }