Ejemplo n.º 1
0
        public void Set(MaterialCommodityData.CatType cat, string fdname, int num, double price)
        {
            MaterialCommodities mc = GetNewCopyOf(cat, fdname);

            mc.Count = num;
            if (price > 0)
            {
                mc.Price = price;
            }
        }
Ejemplo n.º 2
0
        public void Craft(string fdname, int num)
        {
            int index = List.FindIndex(x => x.Details.FDName.Equals(fdname, StringComparison.InvariantCultureIgnoreCase));

            if (index >= 0)
            {
                MaterialCommodities mc = new MaterialCommodities(List[index]); // new clone of
                List[index] = mc;                                              // replace ours with new one
                mc.Count    = Math.Max(mc.Count - num, 0);
            }
        }
        public void Set(string cat, string fdname, int num, double price, SQLiteConnectionUser conn)
        {
            MaterialCommodities mc = GetNewCopyOf(cat, fdname, conn);

            mc.Count = num;
            if (price > 0)
            {
                mc.Price = price;
            }

            //log.WriteLine("MC Set:" + cat + " " + fdname + " " + num + " " + mc.count);
        }
Ejemplo n.º 4
0
        public void Set(string cat, string fdname, int num, double price, SQLiteConnectionUser conn, bool ignorecatonsearch = false)
        {
            MaterialCommodities mc = GetNewCopyOf(cat, fdname, conn, ignorecatonsearch);

            mc.count = num;
            if (price > 0)
            {
                mc.price = price;
            }

            //System.Diagnostics.Debug.WriteLine("Set:" + cat + " " + fdname + " " + num + " " + mc.count);
        }
Ejemplo n.º 5
0
        public void Set(MaterialCommodityData.CatType cat, string fdname, int num, double price)
        {
            MaterialCommodities mc = GetNewCopyOf(cat, fdname);

            mc.Count = num;
            if (price > 0)
            {
                mc.Price = price;
            }

            //log.WriteLine("MC Set:" + cat + " " + fdname + " " + num + " " + mc.count);
        }
Ejemplo n.º 6
0
 public void Clear(bool commodity)
 {
     for (int i = 0; i < List.Count; i++)
     {
         MaterialCommodities mc = List[i];
         if (commodity == mc.Details.IsCommodity)
         {
             List[i]       = new MaterialCommodities(List[i]); // new clone of it we can change..
             List[i].Count = 0;                                // and clear it
         }
     }
 }
Ejemplo n.º 7
0
        public void Craft(string fdname, int num)
        {
            int index = list.FindIndex(x => x.fdname.Equals(fdname, StringComparison.InvariantCultureIgnoreCase));

            if (index >= 0)
            {
                MaterialCommodities mc = new MaterialCommodities(list[index]); // new clone of
                list[index] = mc;                                              // replace ours with new one
                mc.count    = Math.Max(mc.count - num, 0);
                //System.Diagnostics.Debug.WriteLine("craft:" + fdname + " " + num + " " + mc.count);
            }
        }
Ejemplo n.º 8
0
        // return shopping list/count given receipe list, list of current materials.

        static public List <Tuple <MaterialCommodities, int> > GetShoppingList(List <Tuple <Recipes.Recipe, int> > wantedrecipes, List <MaterialCommodities> list)
        {
            var shoppingList = new List <Tuple <MaterialCommodities, int> >();
            var totals       = TotalList(list);

            foreach (Tuple <Recipes.Recipe, int> want in wantedrecipes)
            {
                Recipes.Recipe r      = want.Item1;
                int            wanted = want.Item2;

                for (int i = 0; i < r.Ingredients.Length; i++)
                {
                    string ingredient = r.Ingredients[i].Shortname;

                    int mi = list.FindIndex(x => x.Details.Shortname.Equals(ingredient));                                                       // see if we have any in list

                    MaterialCommodities matc = mi != -1 ? list[mi] : new MaterialCommodities(MaterialCommodityData.GetByShortName(ingredient)); // if not there, make one
                    if (mi == -1)                                                                                                               // if not there, make an empty total entry
                    {
                        totals[matc.Details.FDName] = 0;
                    }

                    int got  = totals[matc.Details.FDName];     // what we have left from totals
                    int need = r.Amount[i] * wanted;
                    int left = got - need;

                    if (left < 0)                                                                                                                                // if not enough
                    {
                        int shopentry = shoppingList.FindIndex(x => x.Item1.Details.Shortname.Equals(ingredient));                                               // have we already got it in the shopping list

                        if (shopentry >= 0)                                                                                                                      // found, update list with new wanted total
                        {
                            shoppingList[shopentry] = new Tuple <MaterialCommodities, int>(shoppingList[shopentry].Item1, shoppingList[shopentry].Item2 - left); // need this more
                        }
                        else
                        {
                            shoppingList.Add(new Tuple <MaterialCommodities, int>(matc, -left));  // a new shop entry with this many needed
                        }

                        totals[matc.Details.FDName] = 0;            // clear count
                    }
                    else
                    {
                        totals[matc.Details.FDName] -= need;        // decrement total
                    }
                }
            }

            shoppingList.Sort(delegate(Tuple <MaterialCommodities, int> left, Tuple <MaterialCommodities, int> right) { return(left.Item1.Details.Name.CompareTo(right.Item1.Details.Name)); });

            return(shoppingList);
        }
Ejemplo n.º 9
0
 public void Clear(bool commodity)
 {
     for (int i = 0; i < list.Count; i++)
     {
         MaterialCommodities mc = list[i];
         if (commodity == (mc.category == MaterialCommodities.CommodityCategory))
         {
             list[i]       = new MaterialCommodities(list[i]); // new clone of it we can change..
             list[i].count = 0;                                // and clear it
             //System.Diagnostics.Debug.WriteLine("Clear:" + mc.fdname);
         }
     }
 }
Ejemplo n.º 10
0
 public void Clear(bool commodity)
 {
     //log.Write("MC Clear");
     for (int i = 0; i < list.Count; i++)
     {
         MaterialCommodities mc = list[i];
         if (commodity == mc.Details.IsCommodity)
         {
             list[i]       = new MaterialCommodities(list[i]); // new clone of it we can change..
             list[i].Count = 0;                                // and clear it
             //log.Write(mc.fdname + ",");
         }
     }
     //log.Write("", true);
 }
Ejemplo n.º 11
0
        // ignore cat is only used if you don't know what it is
        public void Change(string cat, string fdname, int num, long price, SQLiteConnectionUser conn, bool ignorecatonsearch = false)
        {
            MaterialCommodities mc = GetNewCopyOf(cat, fdname, conn, ignorecatonsearch);

            double costprev = mc.Count * mc.Price;
            double costnew  = num * price;

            mc.Count = Math.Max(mc.Count + num, 0);

            if (mc.Count > 0 && num > 0)                    // if bought (defensive with mc.count)
            {
                mc.Price = (costprev + costnew) / mc.Count; // price is now a combination of the current cost and the new cost. in case we buy in tranches
            }
            //log.WriteLine("MC Change:" + cat + " " + fdname + " " + num + " " + mc.count);
        }
Ejemplo n.º 12
0
        static public List <MaterialCommodities> GetShoppingList(List <Tuple <Recipe, int> > target, List <MaterialCommodities> list)
        {
            List <MaterialCommodities> shoppingList = new List <MaterialCommodities>();

            foreach (Tuple <Recipe, int> want in target)
            {
                Recipe r      = want.Item1;
                int    wanted = want.Item2;
                for (int i = 0; i < r.ingredients.Length; i++)
                {
                    string ingredient = r.ingredients[i];
                    int    mi         = list.FindIndex(x => x.shortname.Equals(ingredient));
                    int    got        = (mi >= 0) ? list[mi].scratchpad : 0;
                    int    need       = r.count[i] * wanted;

                    if (got < need)
                    {
                        int shopentry = shoppingList.FindIndex(x => x.shortname.Equals(ingredient));
                        if (shopentry >= 0)
                        {
                            shoppingList[shopentry].scratchpad += (need - got);
                        }
                        else
                        {
                            MaterialCommodityDB db = MaterialCommodityDB.GetCachedMaterialByShortName(ingredient);
                            if (db != null)                                           // MUST be there, its well know, but will check..
                            {
                                MaterialCommodities mc = new MaterialCommodities(db); // make a new entry
                                mc.scratchpad = (need - got);
                                shoppingList.Add(mc);
                            }
                        }
                        if (mi >= 0)
                        {
                            list[mi].scratchpad = 0;
                        }
                    }
                    else
                    {
                        if (mi >= 0)
                        {
                            list[mi].scratchpad -= need;
                        }
                    }
                }
            }
            return(shoppingList);
        }
Ejemplo n.º 13
0
        // ignore cat is only used if you don't know what it is
        public void Change(DateTime utc, MaterialCommodityData.CatType cat, string fdname, int num, long price, bool ignorecatonsearch = false)
        {
            MaterialCommodities mc = GetNewCopyOf(cat, fdname, ignorecatonsearch);

            double costprev = mc.Count * mc.Price;
            double costnew  = num * price;

            //if (mc.Count == 0 && num < 0) System.Diagnostics.Debug.WriteLine("{0} Error, removing {1} {2} but nothing counted", utc, fdname, num);

            mc.Count = Math.Max(mc.Count + num, 0);

            if (mc.Count > 0 && num > 0)                    // if bought (defensive with mc.count)
            {
                mc.Price = (costprev + costnew) / mc.Count; // price is now a combination of the current cost and the new cost. in case we buy in tranches
            }
            //System.Diagnostics.Debug.WriteLine("In {0} At {1} MC Change {2} {3} {4} = {5}", System.Threading.Thread.CurrentThread.Name, utc, cat, fdname, num,mc.Count);
        }
Ejemplo n.º 14
0
        // ifnorecatonsearch is used if you don't know if its a material or commodity.. for future use.

        private MaterialCommodities GetNewCopyOf(string cat, string fdname, SQLiteConnectionUser conn, bool ignorecatonsearch = false)
        {
            int index = list.FindIndex(x => x.fdname.Equals(fdname, StringComparison.InvariantCultureIgnoreCase) && (ignorecatonsearch || x.category.Equals(cat, StringComparison.InvariantCultureIgnoreCase)));

            if (index >= 0)
            {
                list[index] = new MaterialCommodities(list[index]);    // fresh copy..
                return(list[index]);
            }
            else
            {
                MaterialCommodityDB mcdb = MaterialCommodityDB.EnsurePresent(cat, fdname, conn); // get a MCDB of this
                MaterialCommodities mc   = new MaterialCommodities(mcdb);                        // make a new entry
                list.Add(mc);
                return(mc);
            }
        }
Ejemplo n.º 15
0
        // ifnorecatonsearch is used if you don't know if its a material or commodity.. for future use.

        private MaterialCommodities GetNewCopyOf(MaterialCommodityData.CatType cat, string fdname, bool ignorecatonsearch = false)
        {
            int index = List.FindIndex(x => x.Details.FDName.Equals(fdname, StringComparison.InvariantCultureIgnoreCase) && (ignorecatonsearch || x.Details.Category == cat));

            if (index >= 0)
            {
                List[index] = new MaterialCommodities(List[index]);    // fresh copy..
                return(List[index]);
            }
            else
            {
                MaterialCommodityData mcdb = MaterialCommodityData.EnsurePresent(cat, fdname); // get a MCDB of this
                MaterialCommodities   mc   = new MaterialCommodities(mcdb);                    // make a new entry
                List.Add(mc);
                return(mc);
            }
        }
Ejemplo n.º 16
0
        // ifnorecatonsearch is used if you don't know if its a material or commodity.. for future use.

        private MaterialCommodities GetNewCopyOf(string cat, string fdname, SQLiteConnectionUser conn, bool ignorecatonsearch = false)
        {
            int index = list.FindIndex(x => x.Details.FDName.Equals(fdname, StringComparison.InvariantCultureIgnoreCase) && (ignorecatonsearch || x.Details.Category.Equals(cat, StringComparison.InvariantCultureIgnoreCase)));

            if (index >= 0)
            {
                list[index] = new MaterialCommodities(list[index]);    // fresh copy..
                return(list[index]);
            }
            else
            {
                MaterialCommodityData mcdb = MaterialCommodityData.EnsurePresent(cat, fdname); // get a MCDB of this
                MaterialCommodities   mc   = new MaterialCommodities(mcdb);                    // make a new entry
                list.Add(mc);

                //log.WriteLine("MC Made:" + cat + " " + fdname + " >> " + mc.fdname + mc.name );

                return(mc);
            }
        }
Ejemplo n.º 17
0
 public MaterialCommodities(MaterialCommodities c)
 {
     Count        = c.Count;   // clone these
     Price        = c.Price;
     this.Details = c.Details; // can copy this, its fixed
 }
Ejemplo n.º 18
0
        //return maximum can make, how many made, needed string.

        static public Tuple <int, int, string> HowManyLeft(List <MaterialCommodities> list, Recipe r, List <MaterialCommodities> shoppinglist = null, int tomake = 0)
        {
            int           max    = int.MaxValue;
            StringBuilder needed = new StringBuilder(64);

            for (int i = 0; i < r.ingredients.Length; i++)
            {
                string ingredient = r.ingredients[i];

                int mi   = list.FindIndex(x => x.shortname.Equals(ingredient));
                int got  = (mi >= 0) ? list[mi].scratchpad : 0;
                int sets = got / r.count[i];

                max = Math.Min(max, sets);

                int need = r.count[i] * tomake;

                if (got < need)
                {
                    if (shoppinglist != null)
                    {
                        int shopentry = shoppinglist.FindIndex(x => x.shortname.Equals(ingredient));
                        if (shopentry >= 0)
                        {
                            shoppinglist[shopentry].scratchpad += (need - got);
                        }
                        else
                        {
                            MaterialCommodityDB db = MaterialCommodityDB.GetCachedMaterialByShortName(ingredient);
                            if (db != null)                                           // MUST be there, its well know, but will check..
                            {
                                MaterialCommodities mc = new MaterialCommodities(db); // make a new entry
                                mc.scratchpad = (need - got);
                                shoppinglist.Add(mc);
                            }
                        }
                    }

                    string dispName;
                    if (mi > 0)
                    {
                        dispName = (list[mi].category == MaterialCommodityDB.MaterialEncodedCategory || list[mi].category == MaterialCommodityDB.MaterialManufacturedCategory) ? " " + list[mi].name : list[mi].shortname;
                    }
                    else
                    {
                        MaterialCommodityDB db = MaterialCommodityDB.GetCachedMaterialByShortName(ingredient);
                        dispName = (db.category == MaterialCommodityDB.MaterialEncodedCategory || db.category == MaterialCommodityDB.MaterialManufacturedCategory) ? " " + db.name : db.shortname;
                    }
                    string s = (need - got).ToStringInvariant() + dispName;
                    if (needed.Length == 0)
                    {
                        needed.Append("Need:" + s);
                    }
                    else
                    {
                        needed.Append("," + s);
                    }
                }
            }

            int made = 0;

            if (max > 0 && tomake > 0)             // if we have a set, and use it up
            {
                made = Math.Min(max, tomake);      // can only make this much
                StringBuilder usedstr = new StringBuilder(64);

                for (int i = 0; i < r.ingredients.Length; i++)
                {
                    int mi = list.FindIndex(x => x.shortname.Equals(r.ingredients[i]));
                    System.Diagnostics.Debug.Assert(mi != -1);
                    int used = r.count[i] * made;
                    list[mi].scratchpad -= used;
                    string dispName = (list[mi].category == MaterialCommodityDB.MaterialEncodedCategory || list[mi].category == MaterialCommodityDB.MaterialManufacturedCategory) ? " " + list[mi].name : list[mi].shortname;
                    usedstr.AppendPrePad(used.ToStringInvariant() + dispName, ",");
                }

                needed.AppendPrePad("Used: " + usedstr.ToString(), ", ");
            }

            return(new Tuple <int, int, string>(max, made, needed.ToNullSafeString()));
        }