public void GetCycleIterationForNumCards13() { var cc = new AutoSanctionCycleConfig[0]; var result = AutoSanctionDispatcher.GetCycleIterationForNumCards(cc, 0, out AutoSanctionCycleConfig rule); Assert.AreEqual(-1, result); Assert.AreEqual(null, rule); }
// __ Helpers _________________________________________________________ public static AutoSanctionCycleConfig[] GetCycleConfigs(params int[] numCards) { var result = new AutoSanctionCycleConfig[numCards.Length]; for (int i = 0; i < numCards.Length; ++i) { result[i] = new AutoSanctionCycleConfig { Id = 10 + i, Name = "Cycle " + (i + 1), NumYellowCards = numCards[i], Penalty = new PenaltyConfig { Text = "Cycle " + (i + 1) } }; } return(result); }
/// <returns> /// If the number of cards is within the ranges of defined cycles, it returns the cycle for the number of cards. /// If the number of cards matches exactly the boundary of a cycle, it returns the next cycle (signaling a sanction should be applied) /// Examples: /// Cycles 5, 4, 3: 4 cards = 0, 5 cards = 1, 6 cards = 1, 8 cards = 1, 9 cards = 2, 10 cards = 2, 11 cards = 2, 12 cards = 3 /// </returns> public static int GetCycleIterationForNumCards(IEnumerable <AutoSanctionCycleConfig> cycleConfigs, int numCards, out AutoSanctionCycleConfig matchingRule) { matchingRule = null; if (cycleConfigs == null || cycleConfigs.Count() == 0) { return(-1); } int iteration = 0; int ruleAccumulated = 0; foreach (var rule in cycleConfigs) { matchingRule = rule; ruleAccumulated += rule.NumYellowCards; if (ruleAccumulated == numCards) { return(++iteration); } if (ruleAccumulated > numCards) { return(iteration); } iteration++; } // More cards than covered by cycles, keep iterating the last cycle. while (true) { ruleAccumulated += matchingRule.NumYellowCards; if (ruleAccumulated == numCards) { return(++iteration); } if (ruleAccumulated > numCards) { return(iteration); } iteration++; } }