/// <summary> /// For this method to work correctly the Attribute names **MUST** be separated by a space at the start and end. /// (Dexterity+Cunning) will not work it needs to be ( Dexterity + Cunning ) any other math symbols and numbers do not require /// spaces. /// </summary> /// <param name="player"></param> /// <returns></returns> private string ReplaceStringWithNumber(IActor player, string calculation) { //would like to make this a bit more generic so if new attributes are inserted we don't have to change this method //I think easiest way is to have the expression be separated by spaces, but just so it works with anything let's get rid of //any mathematical signs and then we should just have the name of the attributes we want. string temp = calculation; string[] operators = new string[] { "+", "-", "/", "*", "(", ")", "[", "]", "{", "}", "^", "SQRT", "POW", "." }; foreach (string operand in operators) { temp = temp.Replace(operand, " "); } //need to get rid of repeats and empty spaces string[] attributeList = temp.Split(' '); temp = calculation; foreach (string attributeName in attributeList) { if (!string.IsNullOrEmpty(attributeName)) { if (player.GetAttributes().Any(a => a.Name == attributeName.CamelCaseWord())) { temp = temp.Replace(attributeName, player.GetAttributeValue(attributeName).ToString()); } else if (player.GetSubAttributes().ContainsKey(attributeName)) { temp = temp.Replace(attributeName, player.GetSubAttributes()[attributeName].ToString()); } else if (attributeName.Contains("Rank")) { temp = temp.Replace(attributeName, player.GetAttributeRank(attributeName.Substring(0, attributeName.Length - 4)).ToString()); } } } return temp; }
//TODO: //This method exists in the Skill class, should probably combine them into a math library or something private static string ReplaceStringWithNumber(IActor player, string expression) { //would like to make this a bit more generic so if new attributes are inserted we don't have to change this method //I think easiest way is to have the expression be separated by spaces, but just so it works with anything let's get rid of //any mathematical signs and then we should just have the name of the attributes we want. string temp = expression; string[] operators = new string[] { "+", "-", "/", "*", "(", ")", "[", "]", "{", "}", "^", "SQRT", "POW", "MAX" }; foreach (string operand in operators) { temp = temp.Replace(operand, " "); } //need to get rid of repeats and empty spaces string[] attributeList = temp.Split(' '); //if you cocked your head to the side at this assignment, read over the code again. temp = expression; foreach (string attributeName in attributeList) { if (!string.IsNullOrEmpty(attributeName)) { if (player.GetAttributes().Any(a => a.Name == attributeName.CamelCaseWord())) { temp = temp.Replace(attributeName, player.GetAttributeValue(attributeName).ToString()); } else if (player.GetSubAttributes().ContainsKey(attributeName)) { temp = temp.Replace(attributeName, player.GetSubAttributes()[attributeName].ToString()); } else if (attributeName.Contains("Rank")) { temp = temp.Replace(attributeName, player.GetAttributeRank(attributeName.Substring(0, attributeName.Length - 4)).ToString()); } else if (attributeName.Contains("Bonus")){ //this part does not exist in the Skill class method string bonusName = attributeName.Replace("Bonus", ""); double replacementValue = player.GetBonus((BonusTypes)Enum.Parse(typeof(BonusTypes), bonusName)); temp = temp.Replace(attributeName, replacementValue.ToString()); } } } return temp; }