public static DynamicJsonObject ToJson(this IIndexedTime indexedTime) { dynamic coolObject = new DynamicJsonObject(); coolObject.id = indexedTime.Index; coolObject.realTime = indexedTime.Time.RealTime; coolObject.gameTime = indexedTime.Time.GameTime; return(coolObject); }
public static XmlNode ToXml(this IIndexedTime indexedTime, XmlDocument document) { var element = indexedTime.Time.ToXml(document); var attribute = document.CreateAttribute("id"); attribute.InnerText = indexedTime.Index.ToString(); element.Attributes.Append(attribute); return(element); }
public void Generate(TimingMethod method) { var allHistory = new List <List <IndexedTimeSpan> >(); foreach (var segment in Run) { allHistory.Add(new List <IndexedTimeSpan>()); } foreach (var attempt in Run.AttemptHistory) { var ind = attempt.Index; var historyStartingIndex = -1; foreach (var segment in Run) { var currentIndex = Run.IndexOf(segment); IIndexedTime history = segment.SegmentHistory.FirstOrDefault(x => x.Index == ind); if (history != null) { if (history.Time[method] != null) { allHistory[Run.IndexOf(segment)].Add(new IndexedTimeSpan(history.Time[method].Value, historyStartingIndex)); historyStartingIndex = currentIndex; } } else { historyStartingIndex = currentIndex; } } } var weightedLists = new List <List <KeyValuePair <double, TimeSpan> > >(); var overallStartingIndex = -1; foreach (var currentList in allHistory) { var nullSegment = false; var curIndex = allHistory.IndexOf(currentList); var curPBTime = Run[curIndex].PersonalBestSplitTime[method]; var previousPBTime = overallStartingIndex >= 0 ? Run[overallStartingIndex].PersonalBestSplitTime[method] : null; var finalList = new List <TimeSpan>(); var matchingSegmentHistory = currentList.Where(x => x.Index == overallStartingIndex); if (matchingSegmentHistory.Count() > 0) { finalList = matchingSegmentHistory.Select(x => x.Time).ToList(); overallStartingIndex = curIndex; } else if (curPBTime != null && previousPBTime != null) { finalList.Add(curPBTime.Value - previousPBTime.Value); overallStartingIndex = curIndex; } else { nullSegment = true; } if (!nullSegment) { var tempList = finalList.Select((x, i) => new KeyValuePair <double, TimeSpan>(GetWeight(i, finalList.Count), x)).ToList(); var weightedList = new List <KeyValuePair <double, TimeSpan> >(); if (tempList.Count > 1) { tempList = tempList.OrderBy(x => x.Value).ToList(); var totalWeight = tempList.Aggregate(0.0, (s, x) => (s + x.Key)); var smallestWeight = tempList[0].Key; var rangeWeight = totalWeight - smallestWeight; var aggWeight = 0.0; foreach (var value in tempList) { aggWeight += value.Key; weightedList.Add(new KeyValuePair <double, TimeSpan>(ReWeight(aggWeight, smallestWeight, rangeWeight), value.Value)); } weightedList = weightedList.OrderBy(x => x.Value).ToList(); } else { weightedList.Add(new KeyValuePair <double, TimeSpan>(1.0, tempList[0].Value)); } weightedLists.Add(weightedList); } else { weightedLists.Add(null); } } TimeSpan?goalTime = null; if (Run[Run.Count - 1].PersonalBestSplitTime[method].HasValue) { goalTime = Run[Run.Count - 1].PersonalBestSplitTime[method].Value; } var runSum = TimeSpan.Zero; var outputSplits = new List <TimeSpan>(); var percentile = 0.5; var percMax = 1.0; var percMin = 0.0; var loopProtection = 0; do { runSum = TimeSpan.Zero; outputSplits.Clear(); percentile = 0.5 * (percMax - percMin) + percMin; foreach (var weightedList in weightedLists) { if (weightedList != null) { var curValue = TimeSpan.Zero; if (weightedList.Count > 1) { for (var n = 0; n < weightedList.Count; n++) { if (weightedList[n].Key > percentile) { curValue = Calculate(percentile, weightedList[n].Value, weightedList[n].Key, weightedList[n - 1].Value, weightedList[n - 1].Key); break; } if (weightedList[n].Key == percentile) { curValue = weightedList[n].Value; break; } } } else { curValue = weightedList[0].Value; } outputSplits.Add(curValue); runSum += curValue; } else { outputSplits.Add(TimeSpan.Zero); } } if (runSum > goalTime) { percMax = percentile; } else { percMin = percentile; } loopProtection += 1; } while (!(runSum - goalTime).Equals(TimeSpan.Zero) && loopProtection < 50 && goalTime != null); TimeSpan totalTime = TimeSpan.Zero; TimeSpan?useTime = TimeSpan.Zero; for (var ind = 0; ind < Run.Count; ind++) { totalTime += outputSplits[ind]; if (outputSplits[ind] == TimeSpan.Zero) { useTime = null; } else { useTime = totalTime; } var time = new Time(Run[ind].Comparisons[Name]); time[method] = useTime; Run[ind].Comparisons[Name] = time; } }
public static string ToJson(this IIndexedTime indexedTime) { return(JsonConvert.SerializeObject(indexedTime)); }
public void Generate(TimingMethod method) { var allHistory = new List <List <double> >(); foreach (var segment in Run) { allHistory.Add(new List <double>()); } for (var ind = 1; ind <= Run.AttemptHistory.Count; ind++) { var ignoreNextHistory = false; foreach (var segment in Run) { IIndexedTime history = segment.SegmentHistory.FirstOrDefault(x => x.Index == ind); if (history != null) { if (history.Time[method] == null) { ignoreNextHistory = true; } else if (!ignoreNextHistory) { allHistory[Run.IndexOf(segment)].Add(history.Time[method].Value.Ticks); } else { ignoreNextHistory = false; } } else { break; } } } var weightedLists = new List <List <KeyValuePair <double, double> > >(); var forceMedian = false; foreach (var curList in allHistory) { if (curList.Count == 0) { forceMedian = true; break; } var tempList = curList.Select((x, i) => new KeyValuePair <double, double>(GetWeight(i, curList.Count), x)).ToList(); var weightedList = new List <KeyValuePair <double, double> >(); if (tempList.Count > 1) { tempList = tempList.OrderBy(x => x.Value).ToList(); var totalWeight = tempList.Aggregate(0.0, (s, x) => (s + x.Key)); var smallestWeight = tempList[0].Key; var rangeWeight = totalWeight - smallestWeight; var aggWeight = 0.0; foreach (var value in tempList) { aggWeight += value.Key; weightedList.Add(new KeyValuePair <double, double>(ReWeight(aggWeight, smallestWeight, rangeWeight), value.Value)); } weightedList = weightedList.OrderBy(x => x.Value).ToList(); } else { weightedList.Add(new KeyValuePair <double, double>(1.0, tempList[0].Value)); } weightedLists.Add(weightedList); } var goalTime = TimeSpan.Zero; if (Run[Run.Count - 1].PersonalBestSplitTime[method].HasValue) { goalTime = Run[Run.Count - 1].PersonalBestSplitTime[method].Value; } var runSum = TimeSpan.Zero; var outputSplits = new List <double>(); var percentile = 0.5; var percMax = 1.0; var percMin = 0.0; var loopProtection = 0; do { runSum = TimeSpan.Zero; outputSplits.Clear(); percentile = 0.5 * (percMax - percMin) + percMin; foreach (var weightedList in weightedLists) { var curValue = 0.0; if (weightedList.Count > 1) { for (var n = 0; n < weightedList.Count; n++) { if (weightedList[n].Key > percentile) { curValue = Calculate(percentile, weightedList[n].Value, weightedList[n].Key, weightedList[n - 1].Value, weightedList[n - 1].Key); break; } if (weightedList[n].Key == percentile) { curValue = weightedList[n].Value; break; } } } else { curValue = weightedList[0].Value; } outputSplits.Add(curValue); runSum += TimeSpan.FromTicks(Convert.ToInt64(curValue)); } if (runSum > goalTime) { percMax = percentile; } else { percMin = percentile; } loopProtection += 1; } while (!(runSum - goalTime).Equals(TimeSpan.Zero) && loopProtection < 50 && !forceMedian); TimeSpan?totalTime = TimeSpan.Zero; for (var ind = 0; ind < Run.Count; ind++) { if (ind >= outputSplits.Count) { totalTime = null; } if (totalTime != null) { totalTime += TimeSpan.FromTicks(Convert.ToInt64(outputSplits[ind])); } var time = new Time(Run[ind].Comparisons[Name]); time[method] = totalTime; Run[ind].Comparisons[Name] = time; } }