internal TransactionList GetItemTransactionList(Station source, Station destination, ItemType type) { TransactionList list = new TransactionList(); Trade[] forSale = source.ItemsForSale[type].ToArray(); Trade[] wanted = destination.ItemsWanted[type].ToArray(); int buyIndex = 0; int sellIndex = 0; int buyAmount = 0; int sellAmount = 0; TransactionItem purchase = null; TransactionItem sale = null; Transaction currentTransaction = null; bool finished = false; int minQtyNeeded = 0; while (!finished) { // Source station has more than destination wants if ((forSale[buyIndex].Quantity - buyAmount) > (wanted[sellIndex].Quantity - sellAmount)) { // Set the amount (qty) of the transaction int amount = (wanted[sellIndex].Quantity - sellAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Set the buy amount up by the amount that can be sold buyAmount += amount; // reset the sell amount sellAmount = 0; sellIndex++; } // Source station has less than destination wants else if ((forSale[buyIndex].Quantity - buyAmount) < (wanted[sellIndex].Quantity - sellAmount)) { // Set the amount (qty) of the transaction int amount = (forSale[buyIndex].Quantity - buyAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Set the buy amount up by the amount that can be sold sellAmount += amount; // reset the buy amount buyAmount = 0; buyIndex++; } else { // Set the amount (qty) of the transaction int amount = (wanted[sellIndex].Quantity - sellAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Reset both buy and sell amount buyAmount = 0; sellAmount = 0; buyIndex++; sellIndex++; } if (currentTransaction == null) { currentTransaction = new Transaction(purchase, sale); } else { currentTransaction.AddPurchase(purchase); if (sale != null) { currentTransaction.AddSale(sale); } } purchase = null; sale = null; if ((wanted.Length <= sellIndex) || (forSale.Length <= buyIndex) || (wanted[sellIndex].UnitPrice <= forSale[buyIndex].UnitPrice)) { finished = true; } // If minimum quantity is achieved, add the transaction if ((finished) || ((minQtyNeeded <= 0) && (!((wanted[sellIndex].UnitPrice == wanted[Math.Max(sellIndex - 1, 0)].UnitPrice) && (forSale[buyIndex].UnitPrice == forSale[Math.Max(buyIndex - 1, 0)].UnitPrice))) )) { if (currentTransaction.Profit >= 0.0f) { list.Add(currentTransaction); } currentTransaction = null; minQtyNeeded = 0; } } return(list); }
public void AddPurchase(TransactionItem item) { AddPurchase(item.TradeItem, item.Quantity); }
public Transaction(TransactionItem purchase, TransactionItem sale) { if (purchase.Type != sale.Type) { throw new ArgumentException("Item types of the trade are not the same"); } Init(purchase.Type); this.AddPurchase(purchase); this.AddSale(sale); }
internal TransactionList GetItemTransactionList(Station source, Station destination, ItemType type) { TransactionList list = new TransactionList(); Trade[] forSale = source.ItemsForSale[type].ToArray(); Trade[] wanted = destination.ItemsWanted[type].ToArray(); int buyIndex = 0; int sellIndex = 0; int buyAmount = 0; int sellAmount = 0; TransactionItem purchase = null; TransactionItem sale = null; Transaction currentTransaction = null; bool finished = false; int minQtyNeeded = 0; while (!finished) { // Source station has more than destination wants if ((forSale[buyIndex].Quantity - buyAmount) > (wanted[sellIndex].Quantity - sellAmount)) { // Set the amount (qty) of the transaction int amount = (wanted[sellIndex].Quantity - sellAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Set the buy amount up by the amount that can be sold buyAmount += amount; // reset the sell amount sellAmount = 0; sellIndex++; } // Source station has less than destination wants else if ((forSale[buyIndex].Quantity - buyAmount) < (wanted[sellIndex].Quantity - sellAmount)) { // Set the amount (qty) of the transaction int amount = (forSale[buyIndex].Quantity - buyAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Set the buy amount up by the amount that can be sold sellAmount += amount; // reset the buy amount buyAmount = 0; buyIndex++; } else { // Set the amount (qty) of the transaction int amount = (wanted[sellIndex].Quantity - sellAmount); // Create trades purchase = new TransactionItem(forSale[buyIndex], amount); if (minQtyNeeded > 0) { minQtyNeeded -= amount; } else { if (wanted[sellIndex].MinQuantity > amount) { sale = new TransactionItem(wanted[sellIndex]); minQtyNeeded = wanted[sellIndex].MinQuantity - amount; } else { sale = new TransactionItem(wanted[sellIndex], amount); } } // Reset both buy and sell amount buyAmount = 0; sellAmount = 0; buyIndex++; sellIndex++; } if (currentTransaction == null) { currentTransaction = new Transaction(purchase, sale); } else { currentTransaction.AddPurchase(purchase); if (sale != null) { currentTransaction.AddSale(sale); } } purchase = null; sale = null; if ((wanted.Length <= sellIndex) || (forSale.Length <= buyIndex) || (wanted[sellIndex].UnitPrice <= forSale[buyIndex].UnitPrice)) { finished = true; } // If minimum quantity is achieved, add the transaction if ((finished) || ((minQtyNeeded <= 0) && (!((wanted[sellIndex].UnitPrice == wanted[Math.Max(sellIndex-1, 0)].UnitPrice) && (forSale[buyIndex].UnitPrice == forSale[Math.Max(buyIndex-1, 0)].UnitPrice))) )) { if (currentTransaction.Profit >= 0.0f) { list.Add(currentTransaction); } currentTransaction = null; minQtyNeeded = 0; } } return list; }