Пример #1
0
        public IList<SituationViewModel> StringToSituations(string content)
        {
            var result = new List<SituationViewModel>();

            var doc = new XmlDocument();
            doc.LoadXml(content);

            var rootNode = doc.ChildNodes[1];
            if (rootNode.Name != "situations")
                throw new FormatException("Unable to find situations node");

            foreach (XmlNode node in rootNode.ChildNodes)
            {
                if (node.Name != "situation")
                    throw new FormatException("Encountered unknown node type.");

                var situation = new SituationViewModel();
                situation.Name = node.Attributes["name"].Value;
                situation.SituationValue = Convert.ToDouble(node.Attributes["value"].Value);

                var ballNode = node.ChildNodes[0];
                if(ballNode.Name != "ball")
                    throw new FormatException("Unable to finde ball node");

                situation.Ball.States.SelectedItem = ballNode.Attributes["state"].Value;
                situation.Ball.X = Convert.ToDouble(ballNode.Attributes["x"].Value);
                situation.Ball.Y = Convert.ToDouble(ballNode.Attributes["y"].Value);

                var teamNode = node.ChildNodes[1];
                if(teamNode.Name != "team")
                    throw new FormatException("Encountered unexpected node type");
                loadPlayer(situation.OwnTeam, teamNode);
                
                var opponentsNode = node.ChildNodes[2];
                if (opponentsNode.Name != "opponents")
                    throw new FormatException("Encountered unexpected node type");
                loadPlayer(situation.Opponents, opponentsNode);

                result.Add(situation);
            }

            return result;
        }
Пример #2
0
        public void WriteToXmlString()
        {
            var situation1 = new SituationViewModel();
            situation1.Name = "situation1";
            situation1.SituationValue = 0.5;
            situation1.Ball.X = 1;
            situation1.Ball.Y = 10;
            situation1.OwnTeam[0].States.SelectedItem = "Available";
            situation1.OwnTeam[0].X = 1;
            situation1.OwnTeam[0].Y = 10;
            situation1.OwnTeam[1].States.SelectedItem = "Penalized";
            situation1.OwnTeam[1].X = 2;
            situation1.OwnTeam[1].Y = 20;
            situation1.OwnTeam[2].States.SelectedItem = "Disabled";
            situation1.OwnTeam[2].X = 3;
            situation1.OwnTeam[2].Y = 30;
            situation1.OwnTeam[3].States.SelectedItem = "NotPlaying";
            situation1.OwnTeam[3].X = 4;
            situation1.OwnTeam[3].Y = 40;
            situation1.OwnTeam[4].States.SelectedItem = "Available";
            situation1.OwnTeam[4].X = 5;
            situation1.OwnTeam[4].Y = 50;

            situation1.Opponents[0].States.SelectedItem = "Available";
            situation1.Opponents[0].X = -1;
            situation1.Opponents[0].Y = -10;
            situation1.Opponents[1].States.SelectedItem = "Penalized";
            situation1.Opponents[1].X = -2;
            situation1.Opponents[1].Y = -20;
            situation1.Opponents[2].States.SelectedItem = "Disabled";
            situation1.Opponents[2].X = -3;
            situation1.Opponents[2].Y = -30;
            situation1.Opponents[3].States.SelectedItem = "NotPlaying";
            situation1.Opponents[3].X = -4;
            situation1.Opponents[3].Y = -40;
            situation1.Opponents[4].States.SelectedItem = "Available";
            situation1.Opponents[4].X = -5;
            situation1.Opponents[4].Y = -50;


            var situation2 = new SituationViewModel();
            situation2.Name = "situation2";
            situation2.SituationValue = -0.5;
            situation2.Ball.States.SelectedItem = "NotInGame";
            situation2.Ball.X = -10;
            situation2.Ball.Y = -100;
            situation2.OwnTeam[0].States.SelectedItem = "Available";
            situation2.OwnTeam[0].X = 1;
            situation2.OwnTeam[0].Y = 10;
            situation2.OwnTeam[1].States.SelectedItem = "Penalized";
            situation2.OwnTeam[1].X = 2;
            situation2.OwnTeam[1].Y = 20;
            situation2.OwnTeam[2].States.SelectedItem = "Disabled";
            situation2.OwnTeam[2].X = 3;
            situation2.OwnTeam[2].Y = 30;
            situation2.OwnTeam[3].States.SelectedItem = "NotPlaying";
            situation2.OwnTeam[3].X = 4;
            situation2.OwnTeam[3].Y = 40;
            situation2.OwnTeam[4].States.SelectedItem = "Available";
            situation2.OwnTeam[4].X = 5;
            situation2.OwnTeam[4].Y = 50;

            situation2.Opponents[0].States.SelectedItem = "Available";
            situation2.Opponents[0].X = -1;
            situation2.Opponents[0].Y = -10;
            situation2.Opponents[1].States.SelectedItem = "Penalized";
            situation2.Opponents[1].X = -2;
            situation2.Opponents[1].Y = -20;
            situation2.Opponents[2].States.SelectedItem = "Disabled";
            situation2.Opponents[2].X = -3;
            situation2.Opponents[2].Y = -30;
            situation2.Opponents[3].States.SelectedItem = "NotPlaying";
            situation2.Opponents[3].X = -4;
            situation2.Opponents[3].Y = -40;
            situation2.Opponents[4].States.SelectedItem = "Available";
            situation2.Opponents[4].X = -5;
            situation2.Opponents[4].Y = -50;


            var sut = new ConfigReaderWriter();

            var result = sut.SituationToString(new[] {situation1, situation2});


            var expected = @"<?xml version=""1.0""?>
<situations>
    <situation name=""situation1"" value=""0,5"">
        <ball state=""InGame"" x=""1"" y=""10""/>
        <team>
            <player id=""1"" state=""Available"" x=""1"" y=""10""/>
            <player id=""2"" state=""Penalized"" x=""2"" y=""20""/>
            <player id=""3"" state=""Disabled"" x=""3"" y=""30""/>
            <player id=""4"" state=""NotPlaying"" x=""4"" y=""40""/>
            <player id=""5"" state=""Available"" x=""5"" y=""50""/>
        </team>
        <opponents>
            <player id=""1"" state=""Available"" x=""-1"" y=""-10""/>
            <player id=""2"" state=""Penalized"" x=""-2"" y=""-20""/>
            <player id=""3"" state=""Disabled"" x=""-3"" y=""-30""/>
            <player id=""4"" state=""NotPlaying"" x=""-4"" y=""-40""/>
            <player id=""5"" state=""Available"" x=""-5"" y=""-50""/>
        </opponents>
    </situation>
    <situation name=""situation2"" value=""-0,5"">
        <ball state=""NotInGame"" x=""-10"" y=""-100""/>
        <team>
            <player id=""1"" state=""Available"" x=""1"" y=""10""/>
            <player id=""2"" state=""Penalized"" x=""2"" y=""20""/>
            <player id=""3"" state=""Disabled"" x=""3"" y=""30""/>
            <player id=""4"" state=""NotPlaying"" x=""4"" y=""40""/>
            <player id=""5"" state=""Available"" x=""5"" y=""50""/>
        </team>
        <opponents>
            <player id=""1"" state=""Available"" x=""-1"" y=""-10""/>
            <player id=""2"" state=""Penalized"" x=""-2"" y=""-20""/>
            <player id=""3"" state=""Disabled"" x=""-3"" y=""-30""/>
            <player id=""4"" state=""NotPlaying"" x=""-4"" y=""-40""/>
            <player id=""5"" state=""Available"" x=""-5"" y=""-50""/>
        </opponents>
    </situation>
</situations>";

            Assert.AreEqual(expected, result);
        }