static string ApplyStat(string actionPattern, StatServer stat) { while (actionPattern.Contains("[")) { int startIndex = actionPattern.IndexOf("["); int endIndex = actionPattern.IndexOf("]", startIndex); string strStatType = actionPattern.Substring(startIndex + 1, endIndex - startIndex - 1); Enum statType = EnumTool.Parse(strStatType) as Enum; float statValue = stat.Get(statType); actionPattern = actionPattern.Replace(actionPattern.Substring(startIndex, endIndex - startIndex + 1), statValue.ToString()); } return actionPattern; }
public static float Calc(string expr, StatServer statHolder) { expr = expr.Trim(); Assert.IsTrue(HasValidSquareBrackets(expr), "has invalid brackets \n" + expr); var bracketedTokens = ExportBracketedTokens(expr).Distinct(); Assert.IsTrue(bracketedTokens.All(x => EnumTool.Parse(x) != null), "has invalid enum token \n" + expr); Assert.IsTrue(bracketedTokens.All(x => statHolder.ContainsKey(EnumTool.Parse(x) as Enum))); var statTable = bracketedTokens.ToDictionary(x => string.Format("[{0}]", x), x => statHolder.Get(EnumTool.Parse(x) as Enum)); foreach (var entity in statTable) { expr = expr.Replace(entity.Key, entity.Value.ToString()); } return SimpleCalc.Calc(expr); }