Esempio n. 1
0
        /// <summary>
        /// Creates a Transaction for an offer
        /// </summary>
        /// <param name="BO">BitOffer you want to buy</param>
        public BitTransaction(BitOffer BO)
        {
            Offer           = BO.Index;
            AddressSeller   = BO.Address;
            TransactionTime = DateTime.Now;

            _buyerComment = _sellerComment = _addrBuyer = null;
            _amount       = 0;
        }
Esempio n. 2
0
 private bool setOffer(BitOffer BO, GenericMessage M)
 {
     if (getValue(M.RawContent, "Title") != null)
     {
         try
         {
             BO.Title = getValue(M.RawContent, "Title");
         }
         catch (Exception ex)
         {
             sendErr(new GenericMessage()
             {
                 Receiver = M.Sender, RawContent = "Cannot edit the offer: " + ex.Message, Server = M.Server
             });
             return(false);
         }
     }
     if (getValue(M.RawContent, "Description") != null)
     {
         try
         {
             BO.Description = Encoding.UTF8.GetString(A85(getValue(M.RawContent, "Description")));
         }
         catch (Exception ex)
         {
             sendErr(new GenericMessage()
             {
                 Receiver = M.Sender, RawContent = "Cannot edit the offer: " + ex.Message, Server = M.Server
             });
             return(false);
         }
     }
     if (getValue(M.RawContent, "Category") != null)
     {
         int cat = 0;
         if (int.TryParse(getValue(M.RawContent, "Category"), out cat) && cat > -3 && cat != 0)
         {
             try
             {
                 BitCategory BC = new BitCategory(cat);
                 BO.Category = cat;
             }
             catch (Exception ex)
             {
                 sendErr(new GenericMessage()
                 {
                     Sender = M.Sender, RawContent = "Cannot edit the offer: " + ex.Message, Server = M.Server
                 });
                 return(false);
             }
         }
     }
     if (getValue(M.RawContent, "Files") != null)
     {
         string[] fIndexes = getValue(M.RawContent, "Files").Split(',');
         if (fIndexes.Length > 1 || !string.IsNullOrEmpty(fIndexes[0].ToString()))
         {
             try
             {
                 for (int i = 0; i < fIndexes.Length; i++)
                 {
                     int.Parse(fIndexes[i]);
                 }
                 BO.Files = getValue(M.RawContent, "Files");
             }
             catch (Exception ex)
             {
                 sendErr(new GenericMessage()
                 {
                     Receiver = M.Sender, RawContent = "Cannot edit the offer: " + ex.Message, Server = M.Server
                 });
                 return(false);
             }
         }
         else
         {
         }
     }
     if (getValue(M.RawContent, "Stock") != null)
     {
         try
         {
             int sCount = int.Parse(getValue(M.RawContent, "Stock"));
             if (sCount > -2)
             {
                 BO.Stock = sCount;
             }
             else
             {
                 throw new Exception("Invalid stock count");
             }
         }
         catch (Exception ex)
         {
             sendErr(new GenericMessage()
             {
                 Receiver = M.Sender, RawContent = "Cannot edit the offer: " + ex.Message, Server = M.Server
             });
             return(false);
         }
     }
     if (getValue(M.RawContent, "PriceMap") != null)
     {
         try
         {
             PriceMap PM = new PriceMap(getValue(M.RawContent, "PriceMap"));
             BO.Prices = PM.ToString();
         }
         catch (Exception ex)
         {
             sendErr(new GenericMessage()
             {
                 Receiver = M.Sender, RawContent = "Cannot edit the offer: " + ex.Message, Server = M.Server
             });
             return(false);
         }
     }
     return(true);
 }
Esempio n. 3
0
        private void parseOffer(GenericMessage M)
        {
            //OFFER <ID> <ACTION>
            //ACTION: GET,SET,DEL,BUY

            string[] Parts = M.Command.Split(' ');
            if (Parts.Length == 3)
            {
                int index = -1;
                if (int.TryParse(Parts[1], out index))
                {
                    BitOffer BO;
                    switch (Parts[2].ToUpper())
                    {
                    case "GET":
                        try
                        {
                            BO = new BitOffer(index);
                            sendMsg(new GenericMessage()
                            {
                                Receiver   = M.Sender,
                                Command    = M.Command,
                                RawContent = string.Format(@"Title={0}
Description={1}
Address={2}
Category={3}
Files={4}
Stock={5},{6}
PriceMap={7}
LastModify={8}
",
                                                           BO.Title, A85(Encoding.UTF8.GetBytes(BO.Description)),
                                                           BO.Address, BO.Category, BO.Files, BO.Stock,
                                                           BO.Stock > -1 ? BO.Stock - getUnconfirmedStock(BO.Index) : -1,
                                                           BO.Prices, BO.LastModify.ToString("dd.MM.yyyy hh:mm:ss")),
                                Server = M.Server
                            });
                        }
                        catch
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "Invalid Offer ID", Server = M.Server
                            });
                        }
                        break;

                    case "SET":
                        if (index >= 0)
                        {
                            try
                            {
                                BO = new BitOffer(index);
                            }
                            catch
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "Invalid Offer ID", Server = M.Server
                                });
                                break;
                            }
                            if (BO.Address == M.Sender)
                            {
                                if (setOffer(BO, M))
                                {
                                    if (BO.Category == -2)
                                    {
                                        sendMsg(new GenericMessage()
                                        {
                                            Receiver = M.Sender, Command = "OFFER " + BO.Index.ToString() + " SET", RawContent = BO.Index.ToString(), Server = M.Server
                                        });
                                    }
                                    else
                                    {
                                        sendBroadcast(new GenericMessage()
                                        {
                                            Command = "OFFER " + BO.Index.ToString() + " SET", RawContent = BO.Index.ToString(), Server = M.Server
                                        });
                                    }
                                }
                            }
                            else
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "This is not your offer. You cannot modify it", Server = M.Server
                                });
                            }
                        }
                        else
                        {
                            //new offer
                            BO = new BitOffer();
                            if (setOffer(BO, M))
                            {
                                if (BO.Category == -2)
                                {
                                    sendMsg(new GenericMessage()
                                    {
                                        Receiver = M.Sender, Command = "OFFER -1 SET", RawContent = BO.Index.ToString(), Server = M.Server
                                    });
                                }
                                else
                                {
                                    sendBroadcast(new GenericMessage()
                                    {
                                        Command = "OFFER -1 SET", RawContent = BO.Index.ToString(), Server = M.Server
                                    });
                                }
                            }
                        }
                        break;

                    case "DEL":
                        if (index >= 0)
                        {
                            try
                            {
                                BO = new BitOffer(index);
                            }
                            catch
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "Invalid Offer ID", Server = M.Server
                                });
                                break;
                            }
                            if (BO.Address == M.Sender)
                            {
                                bool hidden = BO.Category == -2;
                                BO.Kill();
                                if (hidden)
                                {
                                    sendMsg(new GenericMessage()
                                    {
                                        Receiver = M.Sender, Command = "OFFER " + index.ToString() + " DEL", RawContent = index.ToString(), Server = M.Server
                                    });
                                }
                                else
                                {
                                    sendBroadcast(new GenericMessage()
                                    {
                                        Command = "OFFER " + index.ToString() + " DEL", RawContent = index.ToString(), Server = M.Server
                                    });
                                }
                            }
                            else
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "This is not your offer. You cannot modify it", Server = M.Server
                                });
                            }
                        }
                        break;

                    case "BUY":
                        if (index >= 0)
                        {
                            try
                            {
                                BO = new BitOffer(index);
                            }
                            catch
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "Invalid Offer ID", Server = M.Server
                                });
                                break;
                            }

                            if (BO.Address != M.Sender)
                            {
                                var NVC = Base.ParseContent(M.RawContent);
                                int amount, understock;
                                //check if valid amount
                                if (!string.IsNullOrEmpty(NVC["Amount"]) && int.TryParse(NVC["Amount"], out amount) && amount > 0)
                                {
                                    //check or create underStock
                                    if (!string.IsNullOrEmpty(NVC["UnderStock"]))
                                    {
                                        if (!int.TryParse(NVC["UnderStock"], out understock) || understock <= 0 || understock > amount)
                                        {
                                            sendErr(new GenericMessage()
                                            {
                                                Receiver = M.Sender, RawContent = "Invalid UnderStock value", Server = M.Server
                                            });
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        understock = amount;
                                    }

                                    if (BO.CanBuy(amount, understock))
                                    {
                                        BitTransaction BT = BO.Buy(amount, understock);
                                        if (BT != null)
                                        {
                                            BT.AddressBuyer = M.Sender;
                                            sendMsg(new GenericMessage()
                                            {
                                                Receiver = M.Sender, Command = M.Command, RawContent = BT.Index.ToString(), Server = M.Server
                                            });
                                            sendMsg(new GenericMessage()
                                            {
                                                Receiver   = BT.AddressSeller,
                                                Command    = M.Command,
                                                RawContent = string.Format(@"ID={0}
AddressBuyer={1}
Amount={2}
Understock={3}
Message={4}",
                                                                           BT.Index.ToString(), BT.AddressBuyer,
                                                                           amount, understock, NVC["Message"]),
                                                Server = M.Server
                                            });
                                        }
                                        else
                                        {
                                            sendErr(new GenericMessage()
                                            {
                                                Receiver = M.Sender, RawContent = "Cannot buy the specific amount. Did you match the pricemap?", Server = M.Server
                                            });
                                        }
                                    }
                                }
                                else
                                {
                                    sendErr(new GenericMessage()
                                    {
                                        Receiver = M.Sender, RawContent = "Invalid stock amount", Server = M.Server
                                    });
                                }
                            }
                            else
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "You cannot buy your own stuff", Server = M.Server
                                });
                            }
                        }
                        break;
                    }
                }
            }
        }