Example #1
0
        public void Ask_Adds1Order_OnExistingLimit_With1Order()
        {
            // Arrange.
            var existingOrder = new Order {
                Shares = 100, Subscription = new Subscription {
                    Owned = 100, UserId = _userId2
                }
            };

            var existingLimit = Limits.First();

            existingLimit.AskHead     = existingOrder;
            existingOrder.ParentLimit = existingLimit;

            Limits.Add(existingLimit);
            Orders.Add(existingOrder.Id, existingOrder);
            Subscriptions.Add(existingOrder.Subscription.UserId, existingOrder.Subscription);
            Subscriptions.Add(_userId1, new Subscription {
                Owned = 5, UserId = _userId1
            });

            // Act.
            Ask(_userId1, 1, 5);

            // Assert.
            var existingOrder2 = new Order {
                Shares = 100, Subscription = new Subscription {
                    Owned = 100, UserId = _userId2
                }
            };

            var existingLimit2 = new Limit {
                Price = 1 * ScaleFactor
            };

            existingOrder2.ParentLimit = existingLimit2;

            var order = new Order
            {
                Id           = 1,
                Next         = existingOrder2,
                Shares       = 5,
                Subscription = new Subscription
                {
                    Owned  = 5,
                    UserId = _userId1
                }
            };

            existingLimit2.AskHead = order;
            existingOrder2.Prev    = order;
            order.ParentLimit      = existingLimit2;

            Assert.AreEqual(1, Limits.Count);
            Assert.AreEqual(2, Orders.Count);
            Assert.AreEqual(2, Subscriptions.Count);
            CollectionAssert.AreEqual(new Limit[] { existingLimit2 }, Limits, _limitTestComparer);
            CollectionAssert.AreEqual(new Order[] { existingOrder2, order }, (ICollection)Orders.Values, _orderTestComparer);
            CollectionAssert.AreEqual(new Subscription[] { existingOrder2.Subscription, order.Subscription }, (ICollection)Subscriptions.Values, _subscriptionTestComparer);
        }
Example #2
0
        public void Ask_PartialExecutes_AginstUser1Bid_ButAskSharesIsGreaterThanBidShares()
        {
            // Arrange.
            var existingBidOrder = new Order {
                Id = 100, Shares = 100, Subscription = new Subscription {
                    Owned = 0, UserId = _userId1
                }
            };

            var existingLimit = Limits.First();

            existingLimit.BidHead        = existingBidOrder;
            existingBidOrder.ParentLimit = existingLimit;

            Limits.Add(existingLimit);
            Orders.Add(existingBidOrder.Id, existingBidOrder);

            Subscriptions.Add(existingBidOrder.Subscription.UserId, existingBidOrder.Subscription);
            var existingSellerSubscription = new Subscription {
                Owned = 500, UserId = _userId2
            };

            Subscriptions.Add(existingSellerSubscription.UserId, existingSellerSubscription);

            // Act.
            Ask(_userId2, 1, 200);

            // Assert.
            var existingBidOrder2 = new Order {
                Id = 100, Shares = 0, Subscription = new Subscription {
                    Owned = 100, UserId = _userId1
                }
            };

            var existingLimit2 = new Limit {
                Price = 1 * ScaleFactor, BidHead = existingBidOrder2
            };

            existingBidOrder2.ParentLimit = existingLimit2;

            var existingSellerSubscription2 = new Subscription {
                Owned = 400, UserId = _userId2
            };
            var remainingSellersOrder = new Order
            {
                Id           = 1,
                ParentLimit  = existingLimit2,
                Shares       = 100,
                Subscription = existingSellerSubscription2
            };

            existingLimit2.AskHead = remainingSellersOrder;

            Assert.AreEqual(1, Limits.Count);
            Assert.AreEqual(2, Orders.Count);
            Assert.AreEqual(2, Subscriptions.Count);
            CollectionAssert.AreEqual(new Limit[] { existingLimit2 }, Limits, _limitTestComparer);
            CollectionAssert.AreEqual(new Order[] { existingBidOrder2, remainingSellersOrder }, (ICollection)Orders.Values, _orderTestComparer);
            CollectionAssert.AreEqual(new Subscription[] { existingBidOrder2.Subscription, existingSellerSubscription2 }, (ICollection)Subscriptions.Values, _subscriptionTestComparer);
        }
Example #3
0
        void fillClasses(List <Room> rooms)
        {
            for (int i = 1; i < 7; i++)
            {
                for (int j = 1; j < 9; j++)
                {
                    for (int r = 0; r < rooms.Count; r++)
                    {
                        Classes.Add(new WhenClass(i, j, false, rooms[r].ID_Room, rooms[r].Roominess));
                    }
                }
            }

            for (int i = 1; i < 7; i++)
            {
                for (int j = 1; j < 9; j++)
                {
                    for (int r = 0; r < rooms.Count; r++)
                    {
                        Classes.Add(new WhenClass(i, j, true, rooms[r].ID_Room, rooms[r].Roominess));
                    }
                }
            }

            for (int i = 0; i < Orders.Count; i++)
            {
                Limits.Add(Orders[i].Number);
            }
        }
        /// <summary>
        /// Add an array of ProductVariantLimit.
        /// <param name="limits">List<ProductVariantLimit></param>
        /// <returns>ProductVariantListLoadProductRequest</returns>
        /// </summary>
        public ProductVariantListLoadProductRequest AddLimits(List <ProductVariantLimit> limits)
        {
            foreach (ProductVariantLimit e in limits)
            {
                Limits.Add(e);
            }

            return(this);
        }
Example #5
0
        private static void RequestLimit(WildSpawnType role)
        {
            string json = new Request(Session, BackendUrl).GetJson("/client/game/bot/limit/" + role.ToString());

            if (string.IsNullOrEmpty(json))
            {
                Debug.LogError("EmuTarkov.SinglePlayer: Received bot " + role.ToString() + " limit data is NULL, using fallback");
                return;
            }

            Debug.LogError("EmuTarkov.SinglePlayer: Sucessfully received bot " + role.ToString() + " limit data");
            Limits.Add(role, Convert.ToInt32(json));
        }
Example #6
0
        public void Bid_PartialExecutes_AginstUser1Ask_ButBidSharesIsLessThanAskShares()
        {
            // Arrange.
            var existingAskOrder = new Order {
                Shares = 100, Subscription = new Subscription {
                    Owned = 100, UserId = _userId1
                }
            };

            var existingLimit = Limits.First();

            existingLimit.AskHead        = existingAskOrder;
            existingAskOrder.ParentLimit = existingLimit;

            Limits.Add(existingLimit);
            Orders.Add(existingAskOrder.Id, existingAskOrder);
            Subscriptions.Add(existingAskOrder.Subscription.UserId, existingAskOrder.Subscription);

            // Act.
            Bid(_userId2, 1, 40);

            // Assert.
            var existingAskOrder2 = new Order {
                Shares = 60, Subscription = new Subscription {
                    Owned = 60, UserId = _userId1
                }
            };

            var existingLimit2 = new Limit {
                Price = 1 * ScaleFactor, AskHead = existingAskOrder2
            };

            existingAskOrder2.ParentLimit = existingLimit2;

            var newBiddersSubscription = new Subscription {
                Owned = 40, UserId = _userId2
            };

            Assert.AreEqual(1, Limits.Count);
            Assert.AreEqual(1, Orders.Count);
            Assert.AreEqual(2, Subscriptions.Count);
            CollectionAssert.AreEqual(new Limit[] { existingLimit2 }, Limits, _limitTestComparer);
            CollectionAssert.AreEqual(new Order[] { existingAskOrder2 }, (ICollection)Orders.Values, _orderTestComparer);
            CollectionAssert.AreEqual(new Subscription[] { existingAskOrder2.Subscription, newBiddersSubscription }, (ICollection)Subscriptions.Values, _subscriptionTestComparer);
        }
Example #7
0
        private void NewLimit(object sender, RoutedEventArgs e)
        {
            var l = new Limit();

            Limits.Add(l);
        }
Example #8
0
        public void Bid_PartialExecutes_3Limits3Bids1OnEachLimit()
        {
            // Arrange.
            var existingAskOrder = new Order {
                Id = 100, Shares = 100, Subscription = new Subscription {
                    Owned = 100, UserId = _userId1
                }
            };
            var existingAskOrder2 = new Order {
                Id = 200, Shares = 100, Subscription = new Subscription {
                    Owned = 100, UserId = _userId2
                }
            };
            var existingAskOrder3 = new Order {
                Id = 300, Shares = 100, Subscription = new Subscription {
                    Owned = 100, UserId = _userId3
                }
            };

            var existingLimit = Limits.First();
            var limit2        = new Limit {
                Price = 2 * ScaleFactor
            };
            var limit3 = new Limit {
                Price = 3 * ScaleFactor
            };

            existingLimit.AskHead = existingAskOrder;
            limit2.AskHead        = existingAskOrder2;
            limit3.AskHead        = existingAskOrder3;

            existingAskOrder.ParentLimit  = existingLimit;
            existingAskOrder2.ParentLimit = limit2;
            existingAskOrder3.ParentLimit = limit3;

            Limits.Add(limit2);
            Limits.Add(limit3);
            Orders.Add(existingAskOrder.Id, existingAskOrder);
            Orders.Add(existingAskOrder2.Id, existingAskOrder2);
            Orders.Add(existingAskOrder3.Id, existingAskOrder3);

            Subscriptions.Add(existingAskOrder.Subscription.UserId, existingAskOrder.Subscription);
            Subscriptions.Add(existingAskOrder2.Subscription.UserId, existingAskOrder2.Subscription);
            Subscriptions.Add(existingAskOrder3.Subscription.UserId, existingAskOrder3.Subscription);

            // Act.
            Bid(_userId4, 3, 400);

            // Assert.
            var existingAskOrderClone = new Order {
                Id = 100, Shares = 0, Subscription = new Subscription {
                    Owned = 0, UserId = _userId1
                }
            };
            var existingAskOrder2Clone = new Order {
                Id = 200, Shares = 0, Subscription = new Subscription {
                    Owned = 0, UserId = _userId2
                }
            };
            var existingAskOrder3Clone = new Order {
                Id = 300, Shares = 0, Subscription = new Subscription {
                    Owned = 0, UserId = _userId3
                }
            };

            var newAskOrder = new Order {
                Id = 1, Shares = 100, Subscription = new Subscription {
                    Owned = 300, UserId = _userId4
                }
            };

            var existingLimitClone = new Limit {
                Price = 1 * ScaleFactor, AskHead = existingAskOrderClone
            };
            var existingLimit2Clone = new Limit {
                Price = 2 * ScaleFactor, AskHead = existingAskOrder2Clone
            };
            var existingLimit3Clone = new Limit {
                Price = 3 * ScaleFactor, BidHead = newAskOrder, AskHead = existingAskOrder3Clone
            };

            existingAskOrderClone.ParentLimit  = existingLimitClone;
            existingAskOrder2Clone.ParentLimit = existingLimit2Clone;
            existingAskOrder3Clone.ParentLimit = existingLimit3Clone;
            newAskOrder.ParentLimit            = existingLimitClone;

            Assert.AreEqual(3, Limits.Count);
            Assert.AreEqual(4, Orders.Count);
            Assert.AreEqual(4, Subscriptions.Count);
            CollectionAssert.AreEqual(new Limit[] { existingLimitClone, existingLimit2Clone, existingLimit3Clone }, Limits, _limitTestComparer);
            CollectionAssert.AreEqual(new Order[] { existingAskOrderClone, existingAskOrder2Clone, existingAskOrder3Clone, newAskOrder }, (ICollection)Orders.Values, _orderTestComparer);
            CollectionAssert.AreEqual(new Subscription[] { existingAskOrderClone.Subscription, existingAskOrder2Clone.Subscription, existingAskOrder3Clone.Subscription, newAskOrder.Subscription }, (ICollection)Subscriptions.Values, _subscriptionTestComparer);
        }
        public void Find()
        {
            //если выбран определенный город, то фильтрую лимиты и клиентов
            if (CityForFind != null && CityForFind?.Id != AllCity.Id)
            {
                Clients.Clear();

                var listClients =
                    Client.AllClients
                    .Where(c => string.IsNullOrEmpty(LastName) ||
                           c.LastName.Contains(LastName, StringComparison.InvariantCultureIgnoreCase))
                    .Where(c => CityForFind == null || CityForFind.Id == AllCity.Id ||
                           c.City.Id == CityForFind.Id).ToList();

                foreach (var c in listClients)
                {
                    Clients.Add(c);
                }

                SelectedClient = null;

                Limits.Clear();

                var listLimits =
                    MeasurementLimit.AllMeasurementLimits
                    .Where(m => CityForFind == null || CityForFind.Id == AllCity.Id ||
                           m.City.Id == CityForFind.Id).ToList();

                foreach (var m in listLimits)
                {
                    Limits.Add(m);
                }

                SelectedLimit = null;

                MeasurementsWithoutDate.Clear();
                MeasurementsWithDate.Clear();

                var listMeasure =
                    Measurement.AllMeasurements
                    .Where(m => CityForFind == null || CityForFind.Id == AllCity.Id ||
                           m.MeasurementLimit.City.Id == CityForFind.Id).ToList();

                foreach (var m in listMeasure)
                {
                    //формирую списки для показа с назначенными замерами датам и без дат
                    if (m.MeasurementDate == null || DateTime.Equals(m.MeasurementDate, DateTime.MinValue))
                    {
                        MeasurementsWithoutDate.Add(m);
                    }
                    else
                    {
                        MeasurementsWithDate.Add(m);
                    }
                }

                StayLimits = GetCountStayLimits();
            }
            //если не выбран определенный город
            else
            {
                MeasurementsWithoutDate.Clear();
                MeasurementsWithDate.Clear();

                Clients.Clear();
                foreach (var c in Client.AllClients)
                {
                    Clients.Add(c);
                }
                SelectedClient = null;

                Limits.Clear();
                foreach (var m in MeasurementLimit.AllMeasurementLimits)
                {
                    Limits.Add(m);
                }
                SelectedLimit = null;

                foreach (var m in Measurement.AllMeasurements)
                {
                    //формирую списки для показа с назначенными замерами датам и без дат
                    if (m.MeasurementDate == null || DateTime.Equals(m.MeasurementDate, DateTime.MinValue))
                    {
                        MeasurementsWithoutDate.Add(m);
                    }
                    else
                    {
                        MeasurementsWithDate.Add(m);
                    }
                }

                StayLimits = null;
            }
        }
Example #10
0
        public void Ask_PartialExecutes_1Limit3Bids()
        {
            // Arrange.
            var existingBidOrder = new Order {
                Id = 100, Shares = 100, Subscription = new Subscription {
                    Owned = 0, UserId = _userId1
                }
            };
            var existingBidOrder2 = new Order {
                Id = 200, Shares = 100, Subscription = new Subscription {
                    Owned = 0, UserId = _userId2
                }
            };
            var existingBidOrder3 = new Order {
                Id = 300, Shares = 100, Subscription = new Subscription {
                    Owned = 0, UserId = _userId3
                }
            };

            existingBidOrder.Next  = existingBidOrder2;
            existingBidOrder2.Prev = existingBidOrder;
            existingBidOrder2.Next = existingBidOrder3;
            existingBidOrder3.Prev = existingBidOrder2;

            var existingLimit = Limits.First();

            existingLimit.BidHead         = existingBidOrder;
            existingBidOrder.ParentLimit  = existingLimit;
            existingBidOrder2.ParentLimit = existingLimit;
            existingBidOrder3.ParentLimit = existingLimit;

            Limits.Add(existingLimit);
            Orders.Add(existingBidOrder.Id, existingBidOrder);
            Orders.Add(existingBidOrder2.Id, existingBidOrder2);
            Orders.Add(existingBidOrder3.Id, existingBidOrder3);

            Subscriptions.Add(existingBidOrder.Subscription.UserId, existingBidOrder.Subscription);
            Subscriptions.Add(existingBidOrder2.Subscription.UserId, existingBidOrder2.Subscription);
            Subscriptions.Add(existingBidOrder3.Subscription.UserId, existingBidOrder3.Subscription);
            var existingSellerSubscription = new Subscription {
                Owned = 500, UserId = _userId4
            };

            Subscriptions.Add(existingSellerSubscription.UserId, existingSellerSubscription);

            // Act.
            Ask(_userId4, 1, 400);

            // Assert.
            var existingBidOrderClone = new Order {
                Id = 100, Shares = 0, Subscription = new Subscription {
                    Owned = 100, UserId = _userId1
                }
            };
            var existingBidOrder2Clone = new Order {
                Id = 200, Shares = 0, Subscription = new Subscription {
                    Owned = 100, UserId = _userId2
                }
            };
            var existingBidOrder3Clone = new Order {
                Id = 300, Shares = 0, Subscription = new Subscription {
                    Owned = 100, UserId = _userId3
                }
            };

            existingBidOrderClone.Next  = existingBidOrder2Clone;
            existingBidOrder2Clone.Prev = existingBidOrder;
            existingBidOrder2Clone.Next = existingBidOrder3Clone;
            existingBidOrder3Clone.Prev = existingBidOrder2Clone;

            var newAskOrder = new Order {
                Id = 1, Shares = 100, Subscription = new Subscription {
                    Owned = 200, UserId = _userId4
                }
            };

            var existingLimitClone = new Limit {
                Price = 1 * ScaleFactor, BidHead = existingBidOrderClone, AskHead = newAskOrder
            };

            existingBidOrderClone.ParentLimit  = existingLimitClone;
            existingBidOrder2Clone.ParentLimit = existingLimitClone;
            existingBidOrder3Clone.ParentLimit = existingLimitClone;
            newAskOrder.ParentLimit            = existingLimitClone;

            Assert.AreEqual(1, Limits.Count);
            Assert.AreEqual(4, Orders.Count);
            Assert.AreEqual(4, Subscriptions.Count);
            CollectionAssert.AreEqual(new Limit[] { existingLimitClone }, Limits, _limitTestComparer);
            CollectionAssert.AreEqual(new Order[] { existingBidOrderClone, existingBidOrder2Clone, existingBidOrder3Clone, newAskOrder }, (ICollection)Orders.Values, _orderTestComparer);
            CollectionAssert.AreEqual(new Subscription[] { existingBidOrderClone.Subscription, existingBidOrder2Clone.Subscription, existingBidOrder3Clone.Subscription, newAskOrder.Subscription }, (ICollection)Subscriptions.Values, _subscriptionTestComparer);
        }
 /// <summary>
 /// Add a ProductVariantLimit.
 /// - limit: ProductVariantLimit
 /// - Returns: Self
 /// </summary>
 public ProductVariantListLoadProductRequest AddLimit(ProductVariantLimit limit)
 {
     Limits.Add(limit);
     return(this);
 }
Example #12
0
        public Order Ask(long userId, long price, int shares)
        {
            if (userId <= 0 || price <= 0 || shares <= 0)
            {
                // TODO: Return a message or something.
                return(null);
            }

            price = price * ScaleFactor;

            lock (_orderLock)
            {
                // Get the users subscription.
                if (!Subscriptions.TryGetValue(userId, out Subscription subscription))
                {
                    // TODO: Return a message or something.
                    return(null);
                }

                var index          = Limits.Count - 1;
                var originalShares = shares;
                while (index >= 0 && shares > 0)
                {
                    var currentLimit = Limits.ElementAt(index);
                    if (currentLimit.Price < price)
                    {
                        break;
                    }

                    Order order = currentLimit.BidHead;
                    while (order != null && shares > 0)
                    {
                        if (order.Subscription.UserId == userId)
                        {
                            if (order.Next == null)
                            {
                                break;
                            }
                            else
                            {
                                order = order.Next;
                            }
                        }

                        // Always assume the bid will have a subscription even if it's empty.
                        if (order.Shares >= shares)
                        {
                            order.Subscription.Owned += shares;
                            order.Shares             -= shares;
                            shares = 0;
                        }
                        else
                        {
                            order.Subscription.Owned += order.Shares;
                            shares      -= order.Shares;
                            order.Shares = 0;
                        }

                        order = order.Next;
                    }

                    index--;
                }

                if (shares > 0)
                {
                    subscription.Owned -= originalShares - shares;

                    var newOrder = new Order {
                        Id = /*Interlocked.Increment(ref _orderId)*/ _orderId++, Shares = shares, Subscription = subscription
                    };

                    // At this point Limits is guaranteed to have a single Limit.
                    var prevLimit = Limits.ElementAt(index == Limits.Count - 1 ? index : ++index);
                    if (prevLimit.Price == price)
                    {
                        newOrder.ParentLimit = prevLimit;
                        if (prevLimit.AskHead == null)
                        {
                            prevLimit.AskHead = newOrder;
                        }
                        else
                        {
                            newOrder.Next          = prevLimit.AskHead;
                            prevLimit.AskHead.Prev = newOrder;
                            prevLimit.AskHead      = newOrder;
                        }
                    }
                    else
                    {
                        var newLimit = new Limit {
                            AskHead = newOrder, Price = price
                        };
                        newOrder.ParentLimit = newLimit;
                        Limits.Add(newLimit);
                    }

                    Orders.Add(newOrder.Id, newOrder);
                    return(newOrder);
                }
                else
                {
                    subscription.Owned -= originalShares;
                }
            }

            return(null);
        }
Example #13
0
        public Order Bid(long userId, long price, int shares)
        {
            if (userId <= 0 || price <= 0 || shares <= 0)
            {
                return(null);
            }

            price = price * ScaleFactor;

            lock (_orderLock)
            {
                var index          = 0;
                var originalShares = shares;
                while (index < Limits.Count && shares > 0)
                {
                    var currentLimit = Limits.ElementAt(index);
                    if (currentLimit.Price > price)
                    {
                        break;
                    }

                    Order order = currentLimit.AskHead;
                    while (order != null && shares > 0)
                    {
                        if (order.Subscription.UserId == userId)
                        {
                            if (order.Next == null)
                            {
                                break;
                            }
                            else
                            {
                                order = order.Next;
                            }
                        }

                        if (order.Shares >= shares)
                        {
                            order.Subscription.Owned -= shares;
                            order.Shares             -= shares;
                            shares = 0;
                        }
                        else
                        {
                            order.Subscription.Owned -= order.Shares;
                            shares      -= order.Shares;
                            order.Shares = 0;
                        }

                        order = order.Next;
                    }

                    index++;
                }

                if (!Subscriptions.TryGetValue(userId, out Subscription subscription))
                {
                    subscription = new Subscription {
                        Owned = 0, UserId = userId
                    };
                    Subscriptions.Add(subscription.UserId, subscription);
                }

                if (shares > 0)
                {
                    subscription.Owned += originalShares - shares;

                    var newOrder = new Order {
                        Id = /*Interlocked.Increment(ref _orderId)*/ _orderId++, Shares = shares, Subscription = subscription
                    };

                    // At this point Limits is guaranteed to have a single Limit.
                    var prevLimit = Limits.ElementAt(index == 0 ? 0 : --index);
                    if (prevLimit.Price == price)
                    {
                        newOrder.ParentLimit = prevLimit;
                        if (prevLimit.BidHead == null)
                        {
                            prevLimit.BidHead = newOrder;
                        }
                        else
                        {
                            newOrder.Next          = prevLimit.BidHead;
                            prevLimit.BidHead.Prev = newOrder;
                            prevLimit.BidHead      = newOrder;
                        }
                    }
                    else
                    {
                        var newLimit = new Limit {
                            BidHead = newOrder, Price = price
                        };
                        newOrder.ParentLimit = newLimit;
                        Limits.Add(newLimit);
                    }

                    Orders.Add(newOrder.Id, newOrder);
                    return(newOrder);
                }
                else
                {
                    subscription.Owned += originalShares;
                }
            }

            return(null);
        }