public void Deserialise() { var wrapped = new WrappedObjectState(Market); using (var session = Database.OpenSession()) { session.Store(wrapped, string.Concat(wrapped.Value.GetType().Name, "/market")); session.SaveChanges(); } using (var session = Database.OpenSession()) { var buyOrder = new BuyOrder {Id = Universe.BuyOrder.ObjectId}; var sellOrder = new SellOrder { Id = Universe.SellOrder.ObjectId }; var resolver = new IdResolutionContext(new IObject[] { buyOrder, sellOrder, new ItemInfo(ItemCode.Veldnium), new TradingStation { Id = Universe.TradingStation.ObjectId }, new Corporation { Id = Universe.MSCorp.ObjectId }, new Corporation { Id = Universe.AppleCorp.ObjectId } }); var state = session.Load<WrappedObjectState>(wrapped.Id).Value; Assert.That(state, Is.Not.Null); Assert.That(state.Settlements, Is.Not.Empty); var market = MarketPlace.Builder.Build(Market, null).Materialise(resolver); Assert.That(market, Is.InstanceOf<MarketPlace>()); Assert.That(market.BuyOrders, Is.Not.Empty); Assert.That(market.SellOrders, Is.Not.Empty); Assert.That(market.Settlements, Is.Not.Empty); } }
public void Save() { var buyOrder = new BuyOrder { Id = Universe.BuyOrder.ObjectId }; var sellOrder = new SellOrder { Id = Universe.SellOrder.ObjectId }; var resolver = new IdResolutionContext(new IObject[] { buyOrder, sellOrder, new ItemInfo(ItemCode.Veldnium), new TradingStation { Id = Universe.TradingStation.ObjectId }, new Corporation { Id = Universe.MSCorp.ObjectId }, new Corporation { Id = Universe.AppleCorp.ObjectId } }); var market = MarketPlace.Builder.Build(Market, new StarCluster()).Materialise(resolver); Assert.That(market, Is.InstanceOf<MarketPlace>()); var state = market.Save(); var json = Database.Conventions.CreateSerializer().Serialize(state); Console.WriteLine(json); }
private bool BuyFrom(SellOrder sellOrder, Settlement settlement) { uint commonBlockSize; if (!GetBlockSize(sellOrder, out commonBlockSize)) return Quantity == 0; var targetQuantity = Math.Min(Quantity, sellOrder.Quantity); var numberOfBlocks = targetQuantity/commonBlockSize; var buyQuantity = numberOfBlocks*commonBlockSize; var item = new Item(sellOrder.Item, buyQuantity, Owner); settlement.Add(sellOrder, buyQuantity * sellOrder.PricePerUnit, item); sellOrder.Item.Quantity -= buyQuantity; if (sellOrder.Item.Quantity == 0) { sellOrder.Remove(); } Item.Quantity -= buyQuantity; return Quantity == 0; }
public void ChooseCheapestSellOrder() { var expensiveItem = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 10, Location = _sc }; var expensive = new SellOrder { Item = expensiveItem, Location = _tradingStation, PricePerUnit = 100 }; var cheapItem = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 10, Location = _sc }; var cheapest = new SellOrder { Item = cheapItem, Location = _tradingStation, PricePerUnit = 80 }; _sc.MarketPlace.Add(expensive); _sc.MarketPlace.Add(cheapest); _sc.MarketPlace.AuctionLength = 10; var item = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 10, Location = _sc }; var a = new BuyOrder { Item = item, Location = _tradingStation, PricePerUnit = 120 }; var settlement = a.List(_sc.MarketPlace); settlement.Success.ShouldBe(true); settlement.Item.Quantity.ShouldBe(10u); settlement.Sum.ShouldBe(800); _sc.MarketPlace.Auctions.ShouldNotContain(a); _sc.MarketPlace.Auctions.ShouldNotContain(cheapest); }
public void ListBuyOrderWithOpposingSellOrder() { var selling = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 10, Location = _sc }; var sellOrder = new SellOrder { Item = selling, Location = _tradingStation, PricePerUnit = 100 }; _sc.MarketPlace.Add(sellOrder); _sc.MarketPlace.AuctionLength = 10; var item = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 10, Location = _sc }; var a = new BuyOrder { Item = item, Location = _tradingStation, PricePerUnit = 120 }; var settlement = a.List(_sc.MarketPlace); settlement.Success.ShouldBe(true); settlement.Sum.ShouldBe(1000); settlement.Item.Quantity.ShouldBe(10u); _sc.MarketPlace.Auctions.ShouldNotContain(a); _sc.MarketPlace.Auctions.ShouldNotContain(sellOrder); }
public void ListSellOrderWithIncompatibleBlockSizes() { var selling = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 150, Location = _sc }; var sellOrder = new SellOrder { Item = selling, Location = _tradingStation, PricePerUnit = 100, BlockSize = 150 }; _sc.MarketPlace.Add(sellOrder); _sc.MarketPlace.AuctionLength = 10; var item = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 180, Location = _sc }; var a = new BuyOrder { Item = item, Location = _tradingStation, PricePerUnit = 120, BlockSize = 100 }; var settlement = a.List(_sc.MarketPlace); settlement.Success.ShouldBe(false); settlement.Sum.ShouldBe(0); }
public void ListBuyOrderWithPartialBuy() { var selling = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 150, Location = _sc }; var sellOrder = new SellOrder { Item = selling, Location = _tradingStation, PricePerUnit = 100, BlockSize = 10 }; _sc.MarketPlace.Add(sellOrder); _sc.MarketPlace.AuctionLength = 10; var item = new Item(new ItemInfo(ItemCode.Veldnium)) { Owner = new Corporation(), Quantity = 200, Location = _sc }; var a = new BuyOrder { Item = item, Location = _tradingStation, PricePerUnit = 120, BlockSize = 100 }; var settlement = a.List(_sc.MarketPlace); settlement.Success.ShouldBe(true); settlement.Item.Quantity.ShouldBe(100u); settlement.Sum.ShouldBe(100 * 100); settlement.Timer = _sc.MarketPlace.SettlementDelay; _sc.MarketPlace.Auctions.ShouldContain(sellOrder); _sc.MarketPlace.Auctions.ShouldContain(a); _sc.MarketPlace.Settlements.ShouldContain(settlement); }