public double CalculateCost(Dictionary <string, string> addlVariables = null) { if (addlVariables != null) { foreach (KeyValuePair <string, string> kvp in addlVariables) { Variables[kvp.Key] = kvp.Value; } } if (Teacher != null) { Variables["TeachLvl"] = Teacher.experienceLevel.ToString(); // Variables.Add("TeachLvl", Teacher.experienceLevel.ToString()); int classID = -1; if (Teacher.trait == "Pilot") { classID = 0; } else if (Teacher.trait == "Engineer") { classID = 1; } else if (Teacher.trait == "Scientist") { classID = 2; } Variables["TeachClass"] = classID.ToString(); //Variables.Add("TeachClass", classID.ToString()); } Variables["FilledSeats"] = Students.Count.ToString(); // UnityEngine.Debug.Log("FilledSeats: " + Variables["FilledSeats"]); //Variables.Add("FilledSeats", Students.Count.ToString()); // UnityEngine.Debug.Log(ConfigNodeExtensions.GetValueOrDefault(sourceNode, "costBase", "0")); //recalculate the baseCost, seatCost, and teacherCost costBase = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(sourceNode, "costBase", "0"), Variables); costSeat = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(sourceNode, "costSeat", "0"), Variables); if (Teacher != null) { costTeacher = 0; } else { costTeacher = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(sourceNode, "costTeacher", "0"), Variables); } double cost = costBase + costTeacher + (costSeat * Students.Count); // UnityEngine.Debug.Log("Course cost: " + cost); return(Math.Max(cost, 0.0)); }
public void FromConfigNode(ConfigNode node) { double.TryParse(ConfigNodeExtensions.GetValueOrDefault(node, "elapsedTime", "0"), out elapsedTime); bool.TryParse(ConfigNodeExtensions.GetValueOrDefault(node, "Started", "true"), out Started); bool.TryParse(ConfigNodeExtensions.GetValueOrDefault(node, "Completed", "false"), out Completed); string teacherName = ConfigNodeExtensions.GetValueOrDefault(node, "teacher", "NA"); if (teacherName != "NA" && HighLogic.CurrentGame.CrewRoster.Exists(teacherName)) { Teacher = HighLogic.CurrentGame.CrewRoster[teacherName]; } //load students ConfigNode studentNode = node.GetNode("STUDENTS"); if (studentNode != null) { Students.Clear(); foreach (ConfigNode.Value val in studentNode.values) { if (HighLogic.CurrentGame.CrewRoster.Exists(val.value)) { Students.Add(HighLogic.CurrentGame.CrewRoster[val.value]); } } } //load variables ConfigNode variableNode = node.GetNode("VARIABLES"); if (variableNode != null) { foreach (ConfigNode.Value val in variableNode.values) { Variables[val.name] = val.value; } } sourceNode = node.GetNode("SOURCE_NODE"); PopulateFromSourceNode(Variables, sourceNode); }
/// <summary> /// Populates all fields from the provided ConfigNode, parsing all formulas /// </summary> /// <param name="variables"></param> public void PopulateFromSourceNode(Dictionary <string, string> variables, ConfigNode source = null) { if (source == null) { source = sourceNode; } Variables = variables; id = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "id"), variables); name = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "name"), variables); description = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "description"), variables); bool.TryParse(MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "Available", "True"), variables), out Available); List <string> tmpList = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "activePreReqs"), variables).Split(',').ToList(); tmpList.ForEach((s) => s.Trim()); if (!tmpList.TrueForAll(s => s == "")) { activePreReqs = tmpList.ToArray(); } else { activePreReqs = new string[] { } }; //activePreReqs = tmpList.ToArray(); tmpList = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "preReqs"), variables).Split(',').ToList(); tmpList.ForEach((s) => s.Trim()); if (!tmpList.TrueForAll(s => s == "")) { preReqs = tmpList.ToArray(); } else { preReqs = new string[] { } }; tmpList = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "conflicts"), variables).Split(',').ToList(); tmpList.ForEach((s) => s.Trim()); //conflicts = tmpList.ToArray(); if (!tmpList.TrueForAll(s => s == "")) { conflicts = tmpList.ToArray(); } else { conflicts = new string[] { } }; time = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "time", "0"), variables); bool.TryParse(MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "required", "false"), variables), out required); string repeatStr = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "required", "false"), variables).Trim(); switch (repeatStr) { case "NEVER": repeatable = RepeatMode.NEVER; break; case "EXPIRED": repeatable = RepeatMode.EXPIRED; break; case "ALWAYS": repeatable = RepeatMode.ALWAYS; break; default: repeatable = RepeatMode.NEVER; break; } tmpList = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "classes"), variables).Split(',').ToList(); tmpList.ForEach((s) => s.Trim()); //classes = tmpList.ToArray(); if (!tmpList.TrueForAll(s => s == "")) { classes = tmpList.ToArray(); } else { classes = new string[] { } }; minLevel = (int)(MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "minLevel", "0"), variables)); maxLevel = (int)(MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "maxLevel", "5"), variables)); seatMax = (int)(MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "seatMax", "-1"), variables)); seatMin = (int)(MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "seatMin", "0"), variables)); costBase = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "costBase", "0"), variables); costSeat = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "costSeat", "0"), variables); costTeacher = MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "costTeacher", "0"), variables); tmpList = MathParsing.ReplaceMathVariables("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "teachClasses"), variables).Split(',').ToList(); tmpList.ForEach((s) => s.Trim()); if (!tmpList.TrueForAll(s => s == "")) { teachClasses = tmpList.ToArray(); } else { teachClasses = new string[] { } }; teachMinLevel = (int)(MathParsing.ParseMath("FlightSchool", ConfigNodeExtensions.GetValueOrDefault(source, "teachMinLevel", "0"), variables)); //get the REWARD nodes and replace any variables in there too ConfigNode r = source.GetNode("REWARD"); if (r != null) { ReplaceValuesInNode(r, variables); RewardLog = r.GetNode("FLIGHTLOG"); r.TryGetValue("XPAmt", ref rewardXP); } /* Expiry = source.GetNodes("EXPIRY"); * foreach (ConfigNode node in Expiry) * ConfigNodeExtensions.ReplaceValuesInNode(node, variables);*/ /*string logStr = "Course created"; * logStr += "\nID: " + id; * logStr += "\nName: " + name; * logStr += "\nAvailable: " + Available; * logStr += "\nprereqs: " + preReqs.Length; * logStr += "\ntime: " + time; * logStr += "\nrepeatable: " + repeatable; * logStr += "\nteachMin: " + teachMinLevel; * logStr += "\nXP: " + rewardXP; * logStr += "\nLog: "; * if (RewardLog != null) * foreach (ConfigNode.Value v in RewardLog.values) * logStr += "\n" + v.value; * * UnityEngine.Debug.Log(logStr);*/ }