コード例 #1
0
ファイル: DataManager.cs プロジェクト: CWMlolzlz/Challenges
 public static XmlElement NumericalGoalToXMLElement(NumericalGoal goal, XmlDocument doc)
 {
     //<goal name='Population' failValue='0' passValue='2000' passOnce='true' failOnce='false'/>
     XmlElement goalElem = doc.CreateElement("goal");
     goalElem.SetAttribute ("name", goal.ValueID.ToString());
     goalElem.SetAttribute ("failValue", goal.FailValue);
     goalElem.SetAttribute ("passValue", goal.PassValue);
     goalElem.SetAttribute ("failOnce", goal.FailOnce.ToString());
     goalElem.SetAttribute ("passOnce", goal.PassOnce.ToString());
     goalElem.SetAttribute ("hasAlreadyFailed", goal.HasAlreadyFailed.ToString());
     goalElem.SetAttribute ("hasAlreadyPassed", goal.HasAlreadyPassed.ToString());
     return goalElem;
 }
コード例 #2
0
ファイル: DataManager.cs プロジェクト: CWMlolzlz/Challenges
        public static Challenge ChallengeNodeToChallenge(XmlNode challengeNode, bool loadStates)
        {
            XmlAttributeCollection challengeAtts = challengeNode.Attributes;
            FastList<IGoal> goalsToAdd = new FastList<IGoal> ();
            FastList<IReward> rewardsToAdd = new FastList<IReward> ();
            FastList<IReward> penaltiesToAdd = new FastList<IReward> ();

            int years = -1, months = -1;
            foreach (XmlNode node in challengeNode.ChildNodes) {
                //Debug.PrintMessage(node.Name);
                if (node.Name == "goal") {
                    string name = node.Attributes ["name"].Value;
                    float passValue = float.Parse (node.Attributes ["passValue"].Value);
                    float failValue = float.Parse (node.Attributes ["failValue"].Value);
                    NumericalGoal goal = new NumericalGoal (StringToValueID (name), GoalTypeExtension.FromComparison (failValue, passValue), failValue, passValue);
                    if (node.Attributes ["passOnce"] != null) {
                        goal.PassOnce = bool.Parse (node.Attributes ["passOnce"].Value);
                    }
                    if (node.Attributes ["failOnce"] != null) {
                        goal.FailOnce = bool.Parse (node.Attributes ["failOnce"].Value);
                    }
                    if (loadStates && node.Attributes["hasAlreadyFailed"] != null) {
                        goal.HasAlreadyFailed = bool.Parse(node.Attributes ["hasAlreadyFailed"].Value);
                    }
                    if (loadStates && node.Attributes["hasAlreadyPassed"] != null) {
                        goal.HasAlreadyPassed = bool.Parse(node.Attributes ["hasAlreadyPassed"].Value);
                    }
                    goalsToAdd.Add (goal);
                } else if (node.Name == "deadline") {
                    if (node.Attributes ["years"] != null)
                        years = int.Parse (node.Attributes ["years"].Value);
                    if (node.Attributes ["months"] != null)
                        months = int.Parse (node.Attributes ["months"].Value);

                } else if (node.Name == "reward") {
                    foreach (XmlNode rewardNode in node.ChildNodes) {
                        if (rewardNode.Name == "boost") {
                            Boost newBoost = new Boost ();
                            newBoost.ValueID = Data.StringToValueID(rewardNode.Attributes ["name"].Value);
                            newBoost.Value = float.Parse (rewardNode.Attributes ["value"].Value);

                            if (rewardNode.Attributes ["years"] != null){
                                newBoost.Years = int.Parse (rewardNode.Attributes ["years"].Value);
                            }
                            if (rewardNode.Attributes ["months"] != null){
                                newBoost.Months = int.Parse (rewardNode.Attributes ["months"].Value);
                            }
                            rewardsToAdd.Add (newBoost);
                        } else if (rewardNode.Name == "payment") {
                            rewardsToAdd.Add(new Payment (int.Parse (rewardNode.Attributes ["amount"].Value)));
                        }
                    }
                } else if (node.Name == "penalty") {
                    foreach (XmlNode rewardNode in node.ChildNodes) {
                        if (rewardNode.Name == "payment") {
                            penaltiesToAdd.Add(new Payment (int.Parse(rewardNode.Attributes ["amount"].Value)));
                        }
                    }
                }
            }
            Challenge newChallenge = new Challenge (challengeAtts ["name"].Value, challengeAtts ["desc"].Value, goalsToAdd.ToArray (), rewardsToAdd.ToArray(), penaltiesToAdd.ToArray());
            //Debug.PrintMessage(challengeNode.OuterXml);
            if (challengeAtts ["requires"] != null) {
                newChallenge.PassTolerance = int.Parse (challengeAtts ["requires"].Value);
            }
            if (challengeAtts ["failTolerance"] != null) {
                newChallenge.FailTolerance = int.Parse (challengeAtts ["failTolerance"].Value);
            }
            if (loadStates && challengeAtts ["startDate"] != null) {
                newChallenge.MapStartTime = DateTime.Parse (challengeAtts ["startDate"].Value);
            }
            if (years >= 0) {
                newChallenge.Years = years;
            }
            if (months >= 0) {
                newChallenge.Months = months;
            }
            return newChallenge;
        }