// make sure values has name lower case. public void Update(DateTime utc, MaterialCommodityMicroResourceType.CatType cat, List <Tuple <string, int> > values, int cnum = 0) { var curlist = items.GetLastValues((x) => x.Details.Category == cat && x.Counts[cnum] > 0); // find all of this cat with a count >0 var varray = new int[MaterialCommodityMicroResource.NoCounts]; // all set to zero var vsets = new bool[MaterialCommodityMicroResource.NoCounts]; // all set to false, change vsets[cnum] = true; // set the cnum to set. foreach (var v in values) { varray[cnum] = v.Item2; // set cnum value Change(utc, cat, v.Item1, varray, vsets, 0); // set entry } foreach (var c in curlist) { if (values.Find(x => x.Item1.Equals(c.Details.FDName, StringComparison.InvariantCultureIgnoreCase)) == null) // if not in updated list { var mc = new MaterialCommodityMicroResource(c); // clone it mc.Counts[cnum] = 0; // zero cnum items.Add(c.Details.FDName.ToLowerInvariant(), mc); System.Diagnostics.Debug.WriteLine("{0} Found {1} not in update list, zeroing", utc, mc.Details.FDName); } } }
// change entry 0 public void Change(DateTime utc, MaterialCommodityMicroResourceType.CatType cat, string fdname, int num, long price, int cnum = 0, bool setit = false) { var vsets = new bool[MaterialCommodityMicroResource.NoCounts]; // all set to false, change vsets[cnum] = setit; // set cnum to change/set var varray = new int[MaterialCommodityMicroResource.NoCounts]; // all set to zero varray[cnum] = num; // set value on cnum Change(utc, cat, fdname, varray, vsets, price); }
// counts/set array can be of length 1 to maximum number of counts // to set a value, set count/set=1 for that entry // to change a value, set count/set = 0 for that entry // to leave a value, set count=0,set=0 for that entry // set means set to value, else add to value public bool Change(DateTime utc, MaterialCommodityMicroResourceType.CatType cat, string fdname, int[] counts, bool[] set, long price) { fdname = fdname.ToLowerInvariant(); MaterialCommodityMicroResource mc = items.GetLast(fdname); // find last entry, may return null if none stored if (mc == null) // not stored, make new { MaterialCommodityMicroResourceType mcdb = MaterialCommodityMicroResourceType.EnsurePresent(cat, fdname); // get a MCDB of this mc = new MaterialCommodityMicroResource(mcdb); } else { mc = new MaterialCommodityMicroResource(mc); // copy constructor, new copy of it } double costprev = mc.Counts[0] * mc.Price; double costofnew = counts[0] * price; bool changed = false; for (int i = 0; i < counts.Length; i++) { int newcount = set[i] ? counts[i] : Math.Max(mc.Counts[i] + counts[i], 0); // don't let it go below zero if changing if (newcount != mc.Counts[i]) { changed = true; // System.Diagnostics.Debug.WriteLine("MCMRLIST {0} Gen {1} Changed {2}:{3} Entry {4} {5} -> {6} {7}", utc.ToString(), items.Generation, mc.Details.Category, mc.Details.FDName, i, mc.Counts[i], newcount, mc.Counts[i]<newcount ? "+++" : "---"); // System.Diagnostics.Debug.WriteLine(Environment.StackTrace); mc.Counts[i] = newcount; } } if (changed) // only store back a new entry if material change to counts { if (mc.Counts[0] > 0 && counts[0] > 0) // if bought (defensive with mc.counts) { mc.Price = (costprev + costofnew) / mc.Counts[0]; // price is now a combination of the current cost and the new cost. in case we buy in tranches } items[fdname] = mc; // and set fdname to mc - this allows for any repeat adds due to frontier data repeating stuff in things like cargo } else { // System.Diagnostics.Debug.WriteLine("{0} Not changed {1} {2}", utc.ToString(), mc.Details.FDName, mc.Count); } return(changed); }
// ensure a category has the same values as in values, for cnum entry. // All others in the same cat not mentioned in values go to zero // make sure values has name lower case. public int Update(DateTime utc, MaterialCommodityMicroResourceType.CatType cat, List <Tuple <string, int> > values, int cnum = 0) { var curlist = items.GetLastValues((x) => x.Details.Category == cat && x.Counts[cnum] > 0); // find all of this cat with a count >0 var varray = new int[MaterialCommodityMicroResource.NoCounts]; // all set to zero var vsets = new bool[MaterialCommodityMicroResource.NoCounts]; // all set to false, change, with 0 in the varray, no therefore no change vsets[cnum] = true; // but set the cnum to set int changed = 0; //System.Diagnostics.Debug.WriteLine("Perform update for " + cat.ToString()); foreach (var v in values) { varray[cnum] = v.Item2; // set cnum value if (Change(utc, cat, v.Item1, varray, vsets, 0)) // set entry { //System.Diagnostics.Debug.WriteLine("MCMRLIST {0} updated {1} {2} to {3} (entry {4})", utc.ToString(), cat, v.Item1, v.Item2 , cnum); changed++; // indicated changed } } foreach (var c in curlist) //go thru the non zero list of cat { if (values.Find(x => x.Item1.Equals(c.Details.FDName, StringComparison.InvariantCultureIgnoreCase)) == null) // if not in updated list { var mc = new MaterialCommodityMicroResource(c); // clone it mc.Counts[cnum] = 0; // zero cnum items[c.Details.FDName.ToLowerInvariant()] = mc; //System.Diagnostics.Debug.WriteLine("MCMRLIST {0} Found {1}:{2} not in update list, zeroing", utc.ToString(), mc.Details.Category, mc.Details.FDName); changed++; } } if (changed > 0) { System.Diagnostics.Debug.WriteLine("MCMRLIST {0} {1} fixed {2}", utc.ToString(), cat, changed); } return(changed); }
// counts/set array can be of length 1 to maximum number of counts // to set a value, set count/set=1 for that entry // to change a value, set count/set = 0 for that entry // to leave a value, set count=0,set=0 for that entry // set means set to value, else add to value public void Change(DateTime utc, MaterialCommodityMicroResourceType.CatType cat, string fdname, int[] counts, bool[] set, long price) { fdname = fdname.ToLowerInvariant(); MaterialCommodityMicroResource mc = items.GetLast(fdname); // find last entry, may return null if none stored if (mc == null) // not stored, make new { MaterialCommodityMicroResourceType mcdb = MaterialCommodityMicroResourceType.EnsurePresent(cat, fdname); // get a MCDB of this mc = new MaterialCommodityMicroResource(mcdb); } else { mc = new MaterialCommodityMicroResource(mc); // copy constructor, new copy of it } double costprev = mc.Counts[0] * mc.Price; double costofnew = counts[0] * price; bool changed = false; for (int i = 0; i < counts.Length; i++) { int newcount = set[i] ? counts[i] : Math.Max(mc.Counts[i] + counts[i], 0); // don't let it go below zero if changing changed |= newcount != mc.Counts[i]; // have we changed? we are trying to minimise deltas to the gen dictionary, so don't add if nothing changed mc.Counts[i] = newcount; } if (changed) // only store back a new entry if material change to counts { if (mc.Counts[0] > 0 && counts[0] > 0) // if bought (defensive with mc.counts) { mc.Price = (costprev + costofnew) / mc.Counts[0]; // price is now a combination of the current cost and the new cost. in case we buy in tranches } items.Add(fdname, mc); // and add a new fresh mc to the dictionary // System.Diagnostics.Debug.WriteLine("{0} Changed {1} {2} {3}", utc, items.Generation, mc.Details.FDName, mc.Count); } else { // System.Diagnostics.Debug.WriteLine("{0} Not changed {1} {2}", utc, mc.Details.FDName, mc.Count); } }