Beispiel #1
0
        public BitTransaction Buy(int amount, int understock)
        {
            BitTransaction BT = null;
            PriceMap       PM = new PriceMap(Prices);

            if (CanBuy(amount, understock))
            {
                if (!double.IsNaN(PM.getValue(amount, 0)))
                {
                    BT        = new BitTransaction(this);
                    BT.Amount = amount;
                }
                if (!double.IsNaN(PM.getValue(understock, 0)))
                {
                    BT        = new BitTransaction(this);
                    BT.Amount = understock;
                }
            }
            return(BT);
        }
Beispiel #2
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;
                    }
                }
            }
        }
Beispiel #3
0
        private void parseTransaction(GenericMessage M)
        {
            //TRANSACTION <ID> <ACTION>
            //ACTION: GET,CONFIRM,REJECT,COMMENT
            string[] Parts = M.Command.Split(' ');
            if (Parts.Length == 3)
            {
                int index = -1;
                if (int.TryParse(Parts[1], out index))
                {
                    BitTransaction BT = null;
                    try
                    {
                        //check if transaction exists and if user is member of it.
                        BT = new BitTransaction(index);
                        if (BT.AddressBuyer != M.Sender && BT.AddressSeller != M.Sender)
                        {
                            throw new Exception("CRAP!");
                        }
                    }
                    catch
                    {
                        sendErr(new GenericMessage()
                        {
                            Receiver = M.Sender, RawContent = "invalid transaction ID", Server = M.Server
                        });
                        return;
                    }
                    switch (Parts[2].ToUpper())
                    {
                    case "GET":
                        sendMsg(new GenericMessage()
                        {
                            Receiver   = M.Sender,
                            Command    = M.Command,
                            RawContent = string.Format(@"AddressBuyer={0}
AddressSeller={1}
Amount={2}
Offer={3}
State={4}
BuyerComment={5}
BuyerRating={6}
SellerComment={7}
SellerRating={8}
TransactionTime={9}",
                                                       BT.AddressBuyer, BT.AddressSeller,
                                                       BT.Offer, (int)BT.State, BT.Amount,
                                                       Base.B64enc(BT.BuyerComment),
                                                       (int)BT.BuyerRating,
                                                       Base.B64enc(BT.SellerComment),
                                                       (int)BT.SellerRating,
                                                       BT.TransactionTime.ToString(Base.DT_FORMAT)),
                            Server = M.Server
                        });
                        break;

                    case "CONFIRM":
                        try
                        {
                            BT.Confirm(M.Sender);
                        }
                        catch (Exception ex)
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "cannot confirm transaction: " + ex.Message, Server = M.Server
                            });
                            break;
                        }
                        GenericMessage Clone1 = M.Clone();
                        Clone1.Command = "TRANSACTION " + BT.Index.ToString() + " GET";
                        Clone1.Sender  = BT.AddressSeller;
                        parseTransaction(Clone1);
                        Clone1.Sender = BT.AddressBuyer;
                        parseTransaction(Clone1);
                        break;

                    case "REJECT":
                        try
                        {
                            BT.Reject(M.Sender);
                        }
                        catch (Exception ex)
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "cannot reject transaction: " + ex.Message, Server = M.Server
                            });
                            break;
                        }
                        GenericMessage Clone2 = M.Clone();
                        Clone2.Command = "TRANSACTION " + BT.Index.ToString() + " GET";
                        Clone2.Sender  = BT.AddressSeller;
                        parseTransaction(Clone2);
                        Clone2.Sender = BT.AddressBuyer;
                        parseTransaction(Clone2);
                        break;

                    case "COMMENT":
                        var NVC = Base.ParseContent(M.RawContent);
                        if (BT.canRate(M.Sender))
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(NVC["Comment"]))
                                {
                                    BT.Comment(M.Sender, Base.B64dec(NVC["Comment"]));
                                }
                                if (!string.IsNullOrEmpty(NVC["Rating"]))
                                {
                                    BT.Rate(M.Sender, (Rating)int.Parse(NVC["Rating"]));
                                }
                            }
                            catch
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "The value for 'Comment' or 'Rating' is invalid", Server = M.Server
                                });
                            }
                        }
                        else
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "You cannot rate the given transaction", Server = M.Server
                            });
                        }
                        break;

                    default:
                        sendErr(new GenericMessage()
                        {
                            Receiver = M.Sender, RawContent = "invalid TRANSACTION command", Server = M.Server
                        });
                        break;
                    }
                }
            }
        }