public ItemRepackageEntry GetItemToRepackage(int itemID, int ownerID, int locationID) { MySqlConnection connection = null; MySqlDataReader reader = Database.PrepareQuery(ref connection, $"SELECT invItems.singleton, invItems.nodeID, IF(valueInt IS NULL, valueFloat, valueInt) AS damage, insuranceID, invItems.typeID, upgrades.itemID, invItems.locationID AS hasUpgrades FROM invItems LEFT JOIN invItems upgrades ON upgrades.locationID = invItems.itemID AND upgrades.flag >= {(int) Flags.RigSlot0} AND upgrades.flag <= {(int) Flags.RigSlot7} LEFT JOIN chrShipInsurances ON shipID = invItems.itemID LEFT JOIN invItemsAttributes ON invItemsAttributes.itemID = invItems.itemID AND invItemsAttributes.attributeID = @damageAttributeID WHERE invItems.itemID = @itemID AND invItems.locationID = @locationID AND invItems.ownerID = @ownerID", new Dictionary <string, object>() { { "@locationID", locationID }, { "@ownerID", ownerID }, { "@itemID", itemID }, { "@damageAttributeID", (int)Attributes.damage } } ); using (connection) using (reader) { if (reader.Read() == false) { return(null); } return(new ItemRepackageEntry() { ItemID = itemID, NodeID = reader.GetInt32(1), Singleton = reader.GetBoolean(0), Damage = reader.GetDoubleOrDefault(2), HasContract = reader.IsDBNull(3) == false, TypeID = reader.GetInt32(4), HasUpgrades = reader.IsDBNull(5) == false, LocationID = reader.GetInt32(6) }); } }
public Dictionary <int, ItemQuantityEntry> PrepareItemForOrder(MySqlConnection connection, int typeID, int locationID1, int locationID2, int quantity, int ownerID1) { MySqlDataReader reader = Database.PrepareQuery(ref connection, "SELECT invItems.itemID, quantity, singleton, nodeID, IF(valueInt IS NULL, valueFloat, valueInt) AS damage FROM invItems LEFT JOIN invItemsAttributes ON invItemsAttributes.itemID = invItems.itemID AND invItemsAttributes.attributeID = @damageAttributeID WHERE typeID = @typeID AND (locationID = @locationID1 OR locationID = @locationID2) AND ownerID = @ownerID1", new Dictionary <string, object>() { { "@locationID1", locationID1 }, { "@locationID2", locationID2 }, { "@ownerID1", ownerID1 }, { "@typeID", typeID }, { "@damageAttributeID", (int)Attributes.damage } } ); Dictionary <int, ItemQuantityEntry> itemIDToQuantityLeft = new Dictionary <int, ItemQuantityEntry>(); int quantityLeft = quantity; using (reader) { while (reader.Read() == true) { int itemID = reader.GetInt32(0); int itemQuantity = reader.GetInt32(1); // singletons cannot be sold if (reader.GetBoolean(2) == true) { continue; } ItemQuantityEntry entry = new ItemQuantityEntry() { ItemID = itemID, OriginalQuantity = itemQuantity, Quantity = itemQuantity - Math.Min(itemQuantity, quantityLeft), NodeID = reader.GetInt32(3), Damage = reader.GetDoubleOrDefault(4) }; itemIDToQuantityLeft[itemID] = entry; quantityLeft -= Math.Min(itemQuantity, quantityLeft); if (quantityLeft == 0) { break; } } } if (quantityLeft > 0) { // there's not enough items for this sale!! return(null); } // preeliminary check, are items damaged? foreach ((int _, ItemQuantityEntry entry) in itemIDToQuantityLeft) { if (entry.Damage > 0) { throw new RepairBeforeSelling(this.TypeManager[typeID]); } } // now iterate all the itemIDs, the ones that have a quantity of 0 must be moved to the correct container foreach ((int itemID, ItemQuantityEntry entry) in itemIDToQuantityLeft) { // if the item is loaded in any node do not update the item // that's the responsibility of the other node if (entry.NodeID != 0) { continue; } // the item is just gone, remove it if (entry.Quantity == 0) { Database.PrepareQuery(ref connection, "DELETE FROM invItems WHERE itemID = @itemID", new Dictionary <string, object>() { { "@itemID", itemID } } ).Close(); } else { // reduce the item quantity available Database.PrepareQuery(ref connection, "UPDATE invItems SET quantity = @quantity WHERE itemID = @itemID", new Dictionary <string, object>() { { "@itemID", itemID }, { "@quantity", entry.Quantity } } ).Close(); } } // everything should be up to date, return the list of changes done so the market can notify the required nodes return(itemIDToQuantityLeft); }