private List<GEItem> abortingOffers; //holds the offers that will be aborted from abortingOffers public GrandExchange() { offersToRemove = new List<GEItem>(); removingOffers = new List<GEItem>(); offersToAdd = new List<GEItem>(); addingOffers = new List<GEItem>(); offersToAbort = new List<GEItem>(); abortingOffers = new List<GEItem>(); /* Load grand exchange items */ Console.WriteLine("Checking SQLite Database Grand Exchange Items Counts"); try { string buyerCount = "0"; string sellerCount = "0"; string abortedBuyerCount = "0"; string abortedSellerCount = "0"; SQLiteDatabase db = new SQLiteDatabase(Constants.databaseName); DataTable dt = db.ExecuteQuery("SELECT Count(*) FROM grandExchangeBuying"); if (dt.Rows.Count == 0) buyerCount = "0"; else buyerCount = dt.Rows[0].ItemArray[0].ToString(); dt = db.ExecuteQuery("SELECT Count(*) FROM grandExchangeSelling"); if (dt.Rows.Count == 0) sellerCount = "0"; else sellerCount = dt.Rows[0].ItemArray[0].ToString(); dt = db.ExecuteQuery("SELECT Count(*) FROM grandExchangeBuying WHERE aborted=1"); if (dt.Rows.Count == 0) abortedBuyerCount = "0"; else abortedBuyerCount = dt.Rows[0].ItemArray[0].ToString(); dt = db.ExecuteQuery("SELECT Count(*) FROM grandExchangeSelling WHERE aborted=1"); if (dt.Rows.Count == 0) abortedSellerCount = "0"; else abortedSellerCount = dt.Rows[0].ItemArray[0].ToString(); db.CloseDatabase(); Console.WriteLine("Total GrandExchange Buyers Count: " + buyerCount); Console.WriteLine("Total GrandExchange Sellers Count: " + sellerCount); Console.WriteLine("Total GrandExchange Aborted Buyers Count: " + abortedBuyerCount); Console.WriteLine("Total GrandExchange Aborted Sellers Count: " + abortedSellerCount); } catch (Exception e) { misc.WriteError(e.Message); } Event startGEEvent = new Event(5000); startGEEvent.setAction(() => { processSales(); }); Server.registerEvent(startGEEvent); /* * Processes all the removing offers to SQL database on a new thread. * This will improve the proformance of the server */ new Thread(removeAddUpdateOffersToGrandExchangeDatabase).Start(); }
protected void processSales() { OrderedDictionary soldBoughtItems = new OrderedDictionary(); /* * First of all the old system was not possible to do with SQL * Would of required to do a SQL Query for each itemId sold and bought by all players * That would of ment over 10,000 items x 2 (buyers+sellers) so 20,000 queries to process. * * The new system I created to fix this. * Works like this it everytime this runs matches up 1 GE Buying/Selling auction. * Then loops it 100 times or until no more matches are possible. * * TODO: Match Buyer's of larger item quantity with Sellers with larger quanity instead of Sellers with * any item quantity. (this requires editing the query below to a better suited query). */ BuyOffer buyer = null; SellOffer seller = null; int itemId; int itemAmount; int price; int sold; int bought; int collectedItem; int collectedGold; int overpaid; byte slot; long playerHash; SQLiteDatabase db = new SQLiteDatabase(Constants.databaseName); for (int processNumSales = 0; processNumSales < 100; processNumSales++) { soldBoughtItems.Clear(); //Can't do more then 1 sale at a time, LIMIT 100 wont work since it will pick duplicate sellers. DataTable dt = db.ExecuteQuery("SELECT S.itemId AS sell_itemId, S.amount AS sell_amount, S.price AS sell_price, S.sold AS sell_sold, S.collectedItem AS sell_collectedItem, S.collectedGold AS sell_collectedGold, S.overpaid AS sell_overpaid, S.slot AS sell_slot, S.playerHash AS sell_playerHash, B.itemId AS buy_itemId, B.amount AS buy_amount, B.price AS buy_price, B.bought AS buy_bought, B.collectedItem AS buy_collectedItem, B.collectedGold AS buy_collectedGold, B.overpaid AS buy_overpaid, B.slot AS buy_slot, B.playerHash AS buy_playerHash FROM grandExchangeBuying AS B, grandExchangeSelling AS S ON B.itemId = S.itemId AND B.aborted = 0 AND S.aborted = 0 AND B.price >= S.price AND S.sold < S.amount AND B.bought < B.amount ORDER BY B.price DESC LIMIT 1"); if (dt.Rows.Count == 0) { db.CloseDatabase(); return; } else { itemId = Convert.ToInt32(dt.Rows[0]["sell_itemId"]); itemAmount = Convert.ToInt32(dt.Rows[0]["sell_amount"]); price = Convert.ToInt32(dt.Rows[0]["sell_price"]); sold = Convert.ToInt32(dt.Rows[0]["sell_sold"]); collectedItem = Convert.ToInt32(dt.Rows[0]["sell_collectedItem"]); collectedGold = Convert.ToInt32(dt.Rows[0]["sell_collectedGold"]); overpaid = Convert.ToInt32(dt.Rows[0]["sell_overpaid"]); slot = Convert.ToByte(dt.Rows[0]["sell_slot"]); playerHash = (long)dt.Rows[0]["sell_playerHash"]; seller = new SellOffer(itemId, itemAmount, price, sold, collectedItem, collectedGold, overpaid, slot, playerHash); itemId = Convert.ToInt32(dt.Rows[0]["buy_itemId"]); itemAmount = Convert.ToInt32(dt.Rows[0]["buy_amount"]); price = Convert.ToInt32(dt.Rows[0]["buy_price"]); bought = Convert.ToInt32(dt.Rows[0]["buy_bought"]); collectedItem = Convert.ToInt32(dt.Rows[0]["buy_collectedItem"]); collectedGold = Convert.ToInt32(dt.Rows[0]["buy_collectedGold"]); overpaid = Convert.ToInt32(dt.Rows[0]["buy_overpaid"]); slot = Convert.ToByte(dt.Rows[0]["buy_slot"]); playerHash = (long)dt.Rows[0]["buy_playerHash"]; buyer = new BuyOffer(itemId, itemAmount, price, bought, collectedItem, collectedGold, overpaid, slot, playerHash); } if (seller == null || buyer == null) continue; int amountToBuy = buyer.getTotalAmount() - buyer.getAmountTraded(); int amountToSell = seller.getTotalAmount() - seller.getAmountTraded(); // This check will never happen. SQL Query will not allow it.. Just old code I left in. if (amountToBuy <= 0 || amountToSell <= 0) continue; int amount = (amountToBuy > amountToSell) ? amountToSell : amountToBuy; // Buys from Seller a random amount of a item. amount = misc.random(1, amount); // Buyer will pay minimum what the seller wants. int amountBuyerOverpaid = (buyer.getPriceEach() - seller.getPriceEach()) * amount; // buyer is paying more than the seller wants, therefore MAY recieve this amount as a refund. bool buyerKeepsRefund = misc.random(1) == 0; // if 0, the buyer gets a refund, if its 1...the seller gets more. buyer.setAmountTraded(buyer.getAmountTraded() + amount); seller.setAmountTraded(seller.getAmountTraded() + amount); /** * How much refunded gold Buyer gets in addition to his previous refunded gold. * or * How much of the Buyer's overpaid gold that they couldn't keep goes as extra profit to the Seller. */ if (buyerKeepsRefund && amountBuyerOverpaid > 0) buyer.setAmountOverpaid(buyer.getAmountOverpaid() + amountBuyerOverpaid); else if (!buyerKeepsRefund && amountBuyerOverpaid > 0) seller.setAmountOverpaid(seller.getAmountOverpaid() + amountBuyerOverpaid); // Shows amount of Item Buyer bought in Slot 1 minus how much he already took out. if (buyer.getAmountItemsLeftToCollect() > 0) buyer.setSlot1(new Item(buyer.getItem(), buyer.getAmountItemsLeftToCollect())); //Shows amount of Gold Buyer has in Slot 2 from previous refunded money plus how much he maybe will be refunded this sale. if ((buyer.getAmountCollectedGold() < buyer.getAmountOverpaid()) && (buyer.getAmountOverpaid() - buyer.getAmountCollectedGold()) > 0) buyer.setSlot2(new Item(995, (buyer.getAmountOverpaid() - buyer.getAmountCollectedGold()))); //Shows amount of Gold Seller in Slot 2 has minus how much he already took out. if (seller.getAmountGoldLeftToCollect() > 0) seller.setSlot2(new Item(995, seller.getAmountGoldLeftToCollect())); Player buyerP = Server.getPlayerForName(buyer.getPlayerName()); Player sellerP = Server.getPlayerForName(seller.getPlayerName()); //Update both Buyer and Seller's Slots in the database try { db.ExecuteNonQuery("UPDATE grandExchangeBuying SET bought = " + buyer.getAmountTraded() + ", overpaid = " + buyer.getAmountOverpaid() + " WHERE slot = " + buyer.getSlot() + " AND playerHash = " + buyer.getPlayerHash()); db.ExecuteNonQuery("UPDATE grandExchangeSelling SET sold = " + seller.getAmountTraded() + ", overpaid = " + seller.getAmountOverpaid() + " WHERE slot = " + seller.getSlot() + " AND playerHash = " + seller.getPlayerHash()); } catch (Exception e) { //Instantly skip this sale if any database errors occurred. //If one of those queries works and other fails then you will have a dupe -_-. //TODO: A fix would be to keep retrying both queries until both successful. misc.WriteError("[GrandExchange SQL Error]: " + e.Message); continue; } /* * TODO: Fix When player is offline and logs in after item is bought. He won't see messages below.. * Add something to Packets.cs (sendLogin() method) to process this. * Maybe a (boolean in playerSaves for GE update?) */ if (buyerP != null) { buyerP.getPackets().sendMessage("One or more of your Grand Exchange offers has been updated."); buyerP.getPackets().updateGEProgress(buyer); if (buyerP.getGESession() != null) { Item[] items = { buyer.getSlot1(), buyer.getSlot2() }; buyerP.getPackets().sendItems(-1, -1757, 523 + buyer.getSlot(), items); } } /* * TODO: Fix When player is offline and logs in after item is bought. He won't see messages below.. * Add something to Packets.cs (sendLogin() method) to process this. * Maybe a (boolean in playerSaves for GE update?) */ if (sellerP != null) { sellerP.getPackets().sendMessage("One or more of your Grand Exchange offers has been updated."); sellerP.getPackets().updateGEProgress(seller); if (sellerP.getGESession() != null) { Item[] items = { seller.getSlot1(), seller.getSlot2() }; sellerP.getPackets().sendItems(-1, -1757, 523 + seller.getSlot(), items); } } } }