static public List <MaterialCommodities> GetShoppingList(List <Tuple <Recipes.Recipe, int> > target, List <MaterialCommodities> list) { List <MaterialCommodities> shoppingList = new List <MaterialCommodities>(); foreach (Tuple <Recipes.Recipe, int> want in target) { Recipes.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.Details.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.Details.Shortname.Equals(ingredient)); if (shopentry >= 0) { shoppingList[shopentry].scratchpad += (need - got); } else { MaterialCommodityData db = MaterialCommodityData.GetByShortName(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); }
//return maximum can make, how many made, needed string. static public Tuple <int, int, string, string> HowManyLeft(List <MaterialCommodities> list, Recipes.Recipe r, int tomake = 0) { int max = int.MaxValue; System.Text.StringBuilder needed = new System.Text.StringBuilder(64); System.Text.StringBuilder neededlong = new System.Text.StringBuilder(64); for (int i = 0; i < r.ingredients.Length; i++) { string ingredient = r.ingredients[i]; int mi = list.FindIndex(x => x.Details.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) { string dispshort; string displong; if (mi > 0) // if got one.. { dispshort = (list[mi].Details.IsEncodedOrManufactured) ? " " + list[mi].Details.Name : list[mi].Details.Shortname; displong = " " + list[mi].Details.Name; } else { MaterialCommodityData db = MaterialCommodityData.GetByShortName(ingredient); dispshort = (db.Category == MaterialCommodityData.MaterialEncodedCategory || db.Category == MaterialCommodityData.MaterialManufacturedCategory) ? " " + db.Name : db.Shortname; displong = " " + db.Name; } string sshort = (need - got).ToStringInvariant() + dispshort; string slong = (need - got).ToStringInvariant() + " x " + displong + Environment.NewLine; if (needed.Length == 0) { needed.Append("Need:" + sshort); neededlong.Append("Need:" + Environment.NewLine + slong); } else { needed.Append("," + sshort); neededlong.Append(slong); } } } 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 System.Text.StringBuilder usedstrshort = new System.Text.StringBuilder(64); System.Text.StringBuilder usedstrlong = new System.Text.StringBuilder(64); for (int i = 0; i < r.ingredients.Length; i++) { int mi = list.FindIndex(x => x.Details.Shortname.Equals(r.ingredients[i])); System.Diagnostics.Debug.Assert(mi != -1); int used = r.count[i] * made; list[mi].scratchpad -= used; string dispshort = (list[mi].Details.IsEncodedOrManufactured) ? " " + list[mi].Details.Name : list[mi].Details.Shortname; string displong = " " + list[mi].Details.Name; usedstrshort.AppendPrePad(used.ToStringInvariant() + dispshort, ","); usedstrlong.AppendPrePad(used.ToStringInvariant() + " x " + displong, Environment.NewLine); } needed.AppendPrePad("Used: " + usedstrshort.ToString(), ", "); neededlong.Append("Used: " + Environment.NewLine + usedstrlong.ToString()); } return(new Tuple <int, int, string, string>(max, made, needed.ToNullSafeString(), neededlong.ToNullSafeString())); }
// 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); }