Beispiel #1
0
        private void INVENTORY_ITEM_TEXT(byte[] data)
        {
            if (Trading == false || TheInventory.GettingInventoryItems == true || Gambling || claimingPrize)
                return;
            //if getting storage items still, pause here...
            int i=0;
            string ItemDescription = "";
            string TempItemDescription = "";

            TempItemDescription=System.Text.ASCIIEncoding.ASCII.GetString(data,4,data.Length-4).Trim();
            // remove bad some chars (eg color tags)
            TempItemDescription = TempItemDescription.Replace((char)10, ' ');

            TheLogger.Debug("Beginning FOR loop (1)\n");
            for (i=0;i<TempItemDescription.Length;i++)
            {
                if (!(TempItemDescription[i]<32 || TempItemDescription[i]>126))
                {
                    ItemDescription = ItemDescription+TempItemDescription[i];
                }
            }
            TheLogger.Debug("FOR loop (1) completed\n");
            TradeItem MyTradeItem = new TradeItem();
            MyTradeItem.pos = 0;

            // Get all the keys in the hashtable and sort them
            ArrayList keys = new ArrayList(PartnerTradeItemsList.Keys);
            keys.Sort();

            foreach (object obj in keys)
            {
                MyTradeItem = (TradeItem)PartnerTradeItemsList[obj];
                if (MyTradeItem.name == "")
                {
                    break;
                }
            }
            TheLogger.Debug("Beginning TRIM(1) of " + ItemDescription + "\n");
            ItemDescription = ItemDescription.Trim();
            if (ItemDescription.Contains(" - "))
            {
                if (ItemDescription.ToLower().Contains("extract"))
                {
                 	MyTradeItem.name = ItemDescription.Substring(0, ItemDescription.LastIndexOf(" - ")).Trim();
                }
                else
                {
                 	MyTradeItem.name = ItemDescription.Substring(0, ItemDescription.IndexOf(" - ")).Trim();
                }
            }
            else
            {
            MyTradeItem.name = ItemDescription;
            MyTradeItem.name = MyTradeItem.name.Replace((char)10, ' ');
            MyTradeItem.name = MyTradeItem.name.Substring(0, MyTradeItem.name.IndexOf("Weight:"));
            MyTradeItem.name = MyTradeItem.name.Trim();
            }

            if (Settings.IsTradeBot == true && !(Donating || PutOnSale))
            {
                if (MyTradeItem.name.ToLower() != "gold coins")
                {
                    if (CheckBuyingItems(ref MyTradeItem))
                    {
                        MyTradeItem.validTradeItem = true;
                    }
                    else
                    {
                        TheTCPWrapper.Send(CommandCreator.SEND_PM(TradeHandler.username, "!!!I am not buying " + MyTradeItem.name + ".  You will need to remove this item before I will accept the trade!!!"));
                        MyTradeItem.validTradeItem = false;
                    }
                }
                else
                {
                    MyTradeItem.validTradeItem = true;
                    MyTradeItem.pricepurchase = 1;
                    MyTradeItem.pricepurchasemembers = 1;
                }
            }

            MyTradeItem.weight = int.Parse(ItemDescription.Substring(ItemDescription.IndexOf("Weight:") + 8, ItemDescription.Length - (ItemDescription.IndexOf("Weight:") + 8 + 4)));
            MyTradeItem.KnownItemsSqlID = TheMySqlManager.GetKnownItemsSQLID(MyTradeItem);
            PartnerTradeItemsList[MyTradeItem.pos] = MyTradeItem;

            //double check if this was needed or not..... testing
            Inventory.inventory_item MyInventoryItem = new Inventory.inventory_item();
            MyInventoryItem.name = MyTradeItem.name;
            MyInventoryItem.imageid = MyTradeItem.imageid;
            MyInventoryItem.name = MyTradeItem.name;
            MyInventoryItem.is_resource = false;
            MyInventoryItem.is_reagent = false;
            MyInventoryItem.is_stackable = false;
            MyInventoryItem.use_with_inventory = false;
            MyInventoryItem.description = "";
            MyInventoryItem.weight = MyTradeItem.weight;
            TheMySqlManager.updateknownitems(MyInventoryItem, MyTradeItem.KnownItemsSqlID);
        }
Beispiel #2
0
        public int GetStorageSQLID(TradeHandler.TradeItem MyTradeItem)
        {
            MySqlConnection MyConnection = new MySqlConnection("Server=" + MainClass.SqlServer + ";Port=" + MainClass.SqlPort.ToString() + ";Database=" + MainClass.SqlDatabase + ";Uid=" + MainClass.SqlUsername + ";Pwd=" + MainClass.SqlPassword + ";");
            try
            {
                MyConnection.Open();
            }
            catch (Exception myException)
            {
            Console.WriteLine(myException.Message); Environment.Exit(0);
            }
            TradeHandler.TradeItem MyTempTradeItem = new TradeHandler.TradeItem();
            MyTempTradeItem = MyTradeItem;
            // Add Item it the Global Items List and to the Inventory List
            // First check if the Item exists in the global Database, if not Add it.
            int id = -1;
            int count = -1;
            //string sql = "SELECT id FROM knownitems WHERE imageid = ?imageid and name = ?name order by imageid, name";
            string sql = "SELECT id FROM knownitems WHERE lower(name) = ?name order by name";
            MySqlCommand cmd = new MySqlCommand(sql, MyConnection);
            cmd.Parameters.AddWithValue("?imageid", MyTempTradeItem.imageid);
            cmd.Parameters.AddWithValue("?name", MyTempTradeItem.name.ToLower());
            TheLogger.Debug(sql + "\n");
            MySqlDataReader reader = cmd.ExecuteReader();
            TheLogger.Debug("### cmd.ExecuteReader() in GetStorageSQLID\n");

            try
            {
                while (reader.Read())
                {
                    id = reader.GetInt16(0);
                    count++;
                }
            }
            catch (MySqlException oMySQLException)
            {
                myErrorHandler.errorWriter(oMySQLException);
            }
            catch (Exception oException)
            {
                myErrorHandler.errorWriter(oException);
            }
            finally
            {
                reader.Close();
                TheLogger.Debug("### reader.Close() in GetStorageSQLID\n");
            }
            if (id != -1 && count == 0)
            {
                MyConnection.Close();
                return id;//we found a perfect match
            }
            count = -1;
            sql = "SELECT id FROM knownitems WHERE imageid = ?imageid and lower(name) = ?name order by imageid, name";
            cmd = new MySqlCommand(sql, MyConnection);
            cmd.Parameters.AddWithValue("?imageid", -1);
            cmd.Parameters.AddWithValue("?name", MyTempTradeItem.name.ToLower());
            TheLogger.Debug(sql + "\n");
            reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    id = reader.GetInt16(0);
                    count++;
                }
            }
            catch (MySqlException oMySQLException)
            {
                myErrorHandler.errorWriter(oMySQLException);
            }
            catch (Exception oException)
            {
                myErrorHandler.errorWriter(oException);
            }
            finally
            {
                reader.Close();
            }
            if (id != -1 && count == 0)
            {
                //update the imageid here from -1 to the right one
                // imageid = -1 update it
                Inventory.inventory_item MyInventoryItem = new Inventory.inventory_item();
                MyInventoryItem.imageid = MyTradeItem.imageid;
                MyInventoryItem.name = MyTradeItem.name;
                MyInventoryItem.description = "";
                MyInventoryItem.weight = MyTradeItem.weight;
                MyInventoryItem.is_resource = false;
                MyInventoryItem.is_reagent = false;
                MyInventoryItem.is_stackable = false;
                MyInventoryItem.use_with_inventory = false;
                updateknownitems(MyInventoryItem, id);
                MyConnection.Close();
                return id;//we found a perfect match
            }
            if (id == -1)
            {
            if(MyTradeItem.imageid == 0) TheLogger.ErrorLog("Possibly invalid Item: " + MyTradeItem.name + " added by " + Settings.botid + " in GetStorageSQLID()");
                sql = "INSERT INTO knownitems (name,description,imageid,weight,is_resource,is_reagent,is_stackable,use_with_inventory) VALUES (?name,?description,?imageid,?weight,?is_resource,?is_reagent,?is_stackable,?use_with_inventory)";
                cmd.Parameters.Clear();
                cmd = new MySqlCommand(sql, MyConnection);
                cmd.Parameters.AddWithValue("?name", MyTradeItem.name);
                cmd.Parameters.AddWithValue("?description", " ");
                cmd.Parameters.AddWithValue("?imageid", MyTradeItem.imageid);
                cmd.Parameters.AddWithValue("?weight", MyTradeItem.weight);
                cmd.Parameters.AddWithValue("?is_resource", -1);
                cmd.Parameters.AddWithValue("?is_reagent", -1);
                cmd.Parameters.AddWithValue("?is_stackable", -1);
                cmd.Parameters.AddWithValue("?use_with_inventory", -1);
                try
                {
                    cmd.ExecuteNonQuery();
                    id = GetKnownItemsSQLID(MyTradeItem);
                }
                catch (MySqlException oMySQLException)
                {
                    TheLogger.Log("ExecSql Error in " + oMySQLException.TargetSite + " due to : " + oMySQLException.Message + "\n");
                    TheLogger.Log("ExecSql by SQL : " + sql
                        + "\n");
                    myErrorHandler.errorWriter(oMySQLException);
                }
                catch (Exception oException)
                {
                    TheLogger.Log("ExecSql Error in " + oException.TargetSite + " due to : " + oException.Message + "\n");
                    TheLogger.Log("ExecSql by SQL : " + sql + "\n");
                    myErrorHandler.errorWriter(oException);
                }
                finally
                {
                    //reader.Close();
                }
                reader.Close();
                MyConnection.Close();
                return id;
            }
            reader.Close();
            MyConnection.Close();
            return id;
        }
Beispiel #3
0
        public uint PutItemsOnTrade(int SQLID, uint quantity, bool fromInventory)
        {
            //totalCalculated = false;
            itemTraded = true;
            bool itemFound = false;
            uint reservedAmount = TheMySqlManager.ReservedAmount(SQLID);
            uint reservedAmountForUser = TheMySqlManager.ReservedAmountForUser(SQLID, username);
            Inventory.inventory_item MyInventoryItem = new Inventory.inventory_item();
            Storage.StorageItem MyStorageItem = new Storage.StorageItem();
            int totalOnHand = 0;
            int itemIndex = 0;
            uint position = 0;
            if (fromInventory)
            {
                for (int i = 0; i < InventorySnapshop.Count; i++)
                {
                    MyInventoryItem = (Inventory.inventory_item)InventorySnapshop[i];
                    if (MyInventoryItem.SqlID == SQLID && MyInventoryItem.pos < 36)
                    {
                        itemFound = true;
                        totalOnHand += (int)MyInventoryItem.quantity;
                        itemIndex = i;
                        position = MyInventoryItem.pos;
                    }
                }
            }
            else
            {
                for (int i = 0; i < StorageSnapshot.Count; i++)
                {
                    MyStorageItem = (Storage.StorageItem)StorageSnapshot[i];
                    if (MyStorageItem.knownItemsID == SQLID && itemFound == false)
                    {
                        itemFound = true;
                        totalOnHand = (int)MyStorageItem.quantity;
                        itemIndex = i;
                        position = MyStorageItem.pos;
                    }
                }
            }

            totalOnHand = totalOnHand - (int)reservedAmount + (int)reservedAmountForUser;
            if (totalOnHand < 0)
            {
                totalOnHand = 0;
            }
            //Console.WriteLine("SQLID: " + SQLID);
            //Console.WriteLine("Position: " + position);
            //Console.WriteLine("Quantity: " + quantity);
            //Console.WriteLine("reservedAmount: " + reservedAmount);
            //Console.WriteLine("reservedAmountForUser: "******"Item Found: " + itemFound);
            //Console.WriteLine("Total On Hand: " + totalOnHand);
            //Console.WriteLine("Item index: " + itemIndex);
            uint amountToTrade = 0;
            if (itemFound && totalOnHand > 0)
            {
                if (totalOnHand > quantity)
                {
                    amountToTrade = quantity;
                }
                else
                {
                    amountToTrade = (uint)totalOnHand;
                }
                uint quantityLeftOver = (uint)totalOnHand - amountToTrade;
                //Console.WriteLine("Quantity LeftOver: " + quantityLeftOver);
                if (fromInventory)
                {
                    MyInventoryItem = (Inventory.inventory_item)InventorySnapshop[itemIndex];

            if (MyInventoryItem.is_stackable)
            {
                        MyInventoryItem.quantity = quantityLeftOver;
                        InventorySnapshop[itemIndex] = MyInventoryItem;
            }
            else
            {
            uint toClear = amountToTrade;
            for (int i = 0; i < InventorySnapshop.Count; i++)
                    {
                            MyInventoryItem = (Inventory.inventory_item)InventorySnapshop[i];
                            if (MyInventoryItem.SqlID == SQLID && MyInventoryItem.pos < 36 && MyInventoryItem.quantity > 0)
                            {
                    MyInventoryItem.quantity = 0;
                    InventorySnapshop[i] = MyInventoryItem;
                    toClear--;
                            }
                if (toClear == 0) break;
                    }

            }
                }
                else //from storage
                {
                    MyStorageItem = (Storage.StorageItem)StorageSnapshot[itemIndex];
                    MyStorageItem.quantity = quantityLeftOver;
                    StorageSnapshot[itemIndex] = MyStorageItem;
                }
                if (position > 255)
                {
                    TheTCPWrapper.Send(CommandCreator.PUT_OBJECT_ON_TRADE((ushort)position, amountToTrade));
                }
                else
                {
                    TheTCPWrapper.Send(CommandCreator.PUT_OBJECT_ON_TRADE((byte)position, amountToTrade, fromInventory));
                }
            }

            Console.WriteLine("Amount to trade: " + amountToTrade);
            return amountToTrade;
        }