示例#1
0
文件: Client.cs 项目: siferati/TDIN01
        /// <summary>
        /// Adds a new order.
        /// </summary>
        /// <param name="type">Type of order to add (buying or selling).</param>
        /// <param name="amount">Amount of diginotes to buy / sell.</param>
        /// <returns>Status.</returns>
        public Info AddOrder(OrderType type, long amount)
        {
            Log("Emitting new order...");

            UpdateUser();

            List <Order> pendingOrders  = GetPendingOrders();
            long         sellingAmount  = 0;
            long         purchaseAmount = 0;

            foreach (Order order in pendingOrders)
            {
                if (order.Type == OrderType.Selling)
                {
                    sellingAmount += (order.Amount - order.CurrentAmount);
                }
                else if (order.Type == OrderType.Purchase)
                {
                    purchaseAmount += (order.Amount - order.CurrentAmount);
                }
            }

            if (type == OrderType.Selling && User.Wallet.Count < (amount + sellingAmount))
            {
                Log("Emission failed: not enough diginotes to proceed with sale.");
                return(Info.Failed);
            }
            else if (type == OrderType.Purchase && User.Money < ((amount + purchaseAmount) * GetQuote()))
            {
                Log("Emission failed: not enough money to proceed with purchase.");
                return(Info.Failed);
            }

            Info status = JsonConvert.DeserializeObject <Info>(
                server.AddOrder(type, User.Id, amount)
                );

            if (status == Info.Failed)
            {
                Log("Emission failed: order already exists.");
            }
            else
            {
                Log("Emition successful: " + status);
            }

            return(status);
        }