Example #1
0
 private void InitializeTables(SQLiteDatabase db)
 {
     db.ExecuteNonQuery("BEGIN EXCLUSIVE");
     for (int i = 0; i < CREATE_Commands.Length; i++)
     {
         db.ExecuteNonQuery(CREATE_Commands[i]);
     }
 }
Example #2
0
        private SQLiteDatabase OpenDB(string fileName)
        {
            if (File.Exists(fileName))
                File.Delete(fileName);

            var db = new SQLiteDatabase(fileName);

            for (int i = 0; i < PRAGMA_Commands.Length; i++)
            {
                db.ExecuteNonQuery(PRAGMA_Commands[i]);
            }

            return db;
        }
        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);
                    }
                }
            }
        }
        /**
         * Updates a Slot1 and Slot2 of any GEItem in the database instantly.
         * @param GEItem
         */
        public void updateGEItemSlotsDatabase(GEItem geItem)
        {
            try
            {
                SQLiteDatabase db = new SQLiteDatabase(Constants.databaseName);

                /**
                 * Each time you do in GESession.cs [checkOffer or abortOffer]
                 * A Slot1 and Slot2 is generated but those Slot's themselves are never saved, less data like this.
                 * Only thing thats saved is how much to generate of those Slots next time you call [checkOffer].
                 * Even generated Slots check against how much they can truly take out.
                 */
                if(geItem is BuyOffer)
                    db.ExecuteNonQuery("UPDATE grandExchangeBuying SET collectedItem = " + geItem.getAmountCollectedItem() + ", collectedGold = " + geItem.getAmountCollectedGold() + " WHERE slot = " + geItem.getSlot() + " AND playerHash = " + geItem.getPlayerHash());
                else if(geItem is SellOffer)
                    db.ExecuteNonQuery("UPDATE grandExchangeSelling SET collectedItem = " + geItem.getAmountCollectedItem() + ", collectedGold = " + geItem.getAmountCollectedGold() + " WHERE slot = " + geItem.getSlot() + " AND playerHash = " + geItem.getPlayerHash());

                db.CloseDatabase();
            }
            catch (Exception e)
            {
                misc.WriteError("GrandExchange Error]: " + e.Message);
            }
        }
        protected void removeAddUpdateOffersToGrandExchangeDatabase()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(100); //100 milliseconds.
                }
                catch (ThreadInterruptedException) { }

                lock (removeOffersLocker)
                {
                    foreach (GEItem geItem in offersToRemove)
                        removingOffers.Add(geItem);
                    offersToRemove.Clear();
                }

                lock (addOffersLocker)
                {
                    foreach (GEItem geItem in offersToAdd)
                        addingOffers.Add(geItem);
                    offersToAdd.Clear();
                }

                lock (abortOffersLocker)
                {
                    foreach (GEItem geItem in offersToAbort)
                        abortingOffers.Add(geItem);
                    offersToAbort.Clear();
                }

                try
                {
                    //this check should go straight to Thread.Sleep.
                    if (removingOffers.Count == 0 && addingOffers.Count == 0 && abortingOffers.Count == 0) continue;

                    //Run this on seperate thread so it won't slow down the process.
                    SQLiteDatabase db = new SQLiteDatabase(Constants.databaseName); //opens new database connection.
                    
                    //Removes all the removingOffers from grandExchangeSelling table
                    //This delayed offer deleter should always be ran before inserter (below) or else it's possible bug may occur.
                    foreach (GEItem geItem in removingOffers)
                    {
                        if (geItem is BuyOffer)
                            db.ExecuteNonQuery("DELETE FROM grandExchangeBuying WHERE `playerHash` = " + geItem.getPlayerHash() + " AND `itemId` = " + geItem.getItem() + " AND `amount` = " + geItem.getTotalAmount() + " AND `price` = " + geItem.getPriceEach() + " AND `slot` = " + geItem.getSlot());
                        else if (geItem is SellOffer)
                            db.ExecuteNonQuery("DELETE FROM grandExchangeSelling WHERE `playerHash` = " + geItem.getPlayerHash() + " AND `itemId` = " + geItem.getItem() + " AND `amount` = " + geItem.getTotalAmount() + " AND `price` = " + geItem.getPriceEach() + " AND `slot` = " + geItem.getSlot());
                    }
                    //Add all new addingOffers to grandExchangeBuying or grandExchangeSelling table.
                    //This delayed offer inserter would cause a possible dupe, if both tables didn't have UNIQUE( slot, playerHash ), fixed now.
                    foreach (GEItem geItem in addingOffers)
                    {
                        if (geItem is BuyOffer)
                            db.ExecuteNonQuery("INSERT INTO grandExchangeBuying(itemId, amount, price, bought, slot, playerHash) VALUES (" + geItem.getItem() + ", " + geItem.getTotalAmount() + ", " + geItem.getPriceEach() + ", " + geItem.getAmountTraded() + ", " + geItem.getSlot() + ", " + geItem.getPlayerHash() + ")");
                        else if(geItem is SellOffer)
                            db.ExecuteNonQuery("INSERT INTO grandExchangeSelling(itemId, amount, price, sold, slot, playerHash) VALUES (" + geItem.getItem() + ", " + geItem.getTotalAmount() + ", " + geItem.getPriceEach() + ", " + geItem.getAmountTraded() + ", " + geItem.getSlot() + ", " + geItem.getPlayerHash() + ")");
                    }

                    //Aborts any Buying Offer or Selling Offer from grandExchangeBuying or grandExchangeSelling table.
                    foreach (GEItem geItem in abortingOffers)
                    {
                        if (geItem is BuyOffer)
                            db.ExecuteNonQuery("UPDATE grandExchangeBuying SET aborted = 1 WHERE slot = " + geItem.getSlot() + " AND playerHash = " + geItem.getPlayerHash());
                        else if (geItem is SellOffer)
                            db.ExecuteNonQuery("UPDATE grandExchangeSelling SET aborted = 1 WHERE slot = " + geItem.getSlot() + " AND playerHash = " + geItem.getPlayerHash());
                    }

                    db.CloseDatabase(); //closes the connection.
                    
                    removingOffers.Clear();
                    addingOffers.Clear();
                    abortingOffers.Clear();

                } catch (Exception e) {
                    misc.WriteError(e.Message);
                }
            }
        }