Beispiel #1
0
        //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()));
        }
        static public string Build(System.Globalization.CultureInfo ct, string padchars, params System.Object[] values)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(64);

            string overrideprefix = string.Empty;

            for (int i = 0; i < values.Length;)
            {
                Object first = values[i];

                if (first is NewPrefix)         // first item is special, a new prefix, override
                {
                    overrideprefix = (first as NewPrefix).prefix;
                    i++;
                }
                else if (first is string)       // normal, string
                {
                    System.Diagnostics.Debug.Assert(i + 2 <= values.Length, "Field Builder missing parameter");

                    string[] fieldnames = ((string)first).Split(';');

                    object value = values[i + 1];
                    i += 2;

                    string pad = padchars;
                    if (fieldnames[0].Length > 0 && fieldnames[0][0] == '<')
                    {
                        fieldnames[0] = fieldnames[0].Substring(1);
                        pad           = "";
                    }

                    if (value != null)
                    {
                        if (value is bool)
                        {
                            if (fieldnames.Length != 2)
                            {
                                sb.AppendPrePad("!!REPORT ERROR IN FORMAT STRING " + first + "!!", (overrideprefix.Length > 0) ? overrideprefix : pad);
                                System.Diagnostics.Debug.WriteLine("*************** FIELD BUILDER ERROR" + first);
                            }
                            else
                            {
                                string s = ((bool)value) ? fieldnames[1] : fieldnames[0];
                                sb.AppendPrePad(s, (overrideprefix.Length > 0) ? overrideprefix : pad);
                                overrideprefix = string.Empty;
                            }
                        }
                        else
                        {
                            string format = fieldnames.Length >= 3 ? fieldnames[2] : "0";

                            string output;
                            if (value is string)
                            {
                                output = (string)value;
                            }
                            else if (value is int)
                            {
                                output = ((int)value).ToString(format, ct);
                            }
                            else if (value is long)
                            {
                                output = ((long)value).ToString(format, ct);
                            }
                            else if (value is double)
                            {
                                output = ((double)value).ToString(format, ct);
                            }
                            else if (value is float)
                            {
                                output = ((float)value).ToString(format, ct);
                            }
                            else if (value is ushort)
                            {
                                output = ((ushort)value).ToString(format, ct);
                            }
                            else if (value is short)
                            {
                                output = ((short)value).ToString(format, ct);
                            }
                            else if (value is uint)
                            {
                                output = ((uint)value).ToString(format, ct);
                            }
                            else if (value is ulong)
                            {
                                output = ((ulong)value).ToString(format, ct);
                            }
                            else if (value is double?)
                            {
                                output = ((double?)value).Value.ToString(format, ct);
                            }
                            else if (value is float?)
                            {
                                output = ((float?)value).Value.ToString(format, ct);
                            }
                            else if (value is int?)
                            {
                                output = ((int?)value).Value.ToString(format, ct);
                            }
                            else if (value is uint?)
                            {
                                output = ((uint?)value).Value.ToString(format, ct);
                            }
                            else if (value is ushort?)
                            {
                                output = ((ushort?)value).Value.ToString(format, ct);
                            }
                            else if (value is short?)
                            {
                                output = ((short?)value).Value.ToString(format, ct);
                            }
                            else if (value is long?)
                            {
                                output = ((long?)value).Value.ToString(format, ct);
                            }
                            else if (value is ulong?)
                            {
                                output = ((ulong?)value).Value.ToString(format, ct);
                            }
                            else if (value is DateTime)
                            {
                                format = fieldnames.Length >= 3 ? fieldnames[2] : "g";
                                output = ((DateTime)value).ToString(format, ct);
                            }
                            else
                            {
                                Type t = value.GetType();
                                if (t.BaseType.Name.Equals("Enum"))
                                {
                                    var ev = Activator.CreateInstance(t);
                                    ev     = value;
                                    output = ev.ToString();
                                }
                                else
                                {
                                    output = "";
                                    System.Diagnostics.Debug.Assert(false);
                                }
                            }

                            // if printed something, text must be non null and of length, and it returns true.  Only adds on prefix and prepad if required
                            if (sb.AppendPrePad(output, fieldnames[0], (overrideprefix.Length > 0) ? overrideprefix : pad))
                            {                                                                   // prefix with fieldnames[0], and prefix with newline if defined, or pad
                                if (fieldnames.Length >= 2 && fieldnames[1].Length > 0)
                                {
                                    sb.Append(fieldnames[1]);
                                }

                                overrideprefix = string.Empty;
                            }
                        }
                    }
                }
                else
                {
                    System.Diagnostics.Debug.Assert(false);
                    return("!!REPORT ERROR IN FORMAT STRING!!");
                }
            }

            return(sb.ToString());
        }