示例#1
0
        /// <summary>
        /// XML dosyasını okuyarak level oluşturur
        /// </summary
        public static Level Load(LevelConfiguration levelConf)
        {
            var level = new Level()
            {
                Number     = levelConf.LevelNumber,
                PointRange = new PointRange(levelConf.Range[0], levelConf.Range[1]),
                PillCount  = levelConf.PillCount
            };

            level.Diseases = new ReadOnlyDictionary <DiseaseType, Disease>
                                 (levelConf.Diseases.Select(x => new Disease()
            {
                Point = x.InitialPoint,
                Range = new PointRange(x.Range[0], x.Range[1]),
                Type  = (DiseaseType)Enum.Parse(typeof(DiseaseType), x.Type)
            }).ToDictionary(x => x.Type, x => x));

            return(level);
        }
示例#2
0
        private void LoadLevels()
        {
            if (!Directory.Exists(LevelXmlPath))
            {
                Console.WriteLine($"Level path'i ({LevelXmlPath}) bulunamadi.");
                return;
            }

            var levelConfs = new List <LevelConfiguration>();

            foreach (var file in Directory.GetFiles(LevelXmlPath, "*.xml"))
            {
                XDocument document = null;
                try
                {
                    document = XDocument.Load(file);
                }
                catch (Exception)
                {
                    Console.WriteLine($"{file} okunamadi.");
                    return;
                }

                var levelConf = new LevelConfiguration();
                levelConfs.Add(levelConf);

                //Number
                var numberValue = document.Root.Attribute("Number")?.Value;
                if (numberValue == null)
                {
                    Console.WriteLine("Number null.");
                    return;
                }
                int number;
                if (!int.TryParse(numberValue, out number))
                {
                    Console.WriteLine("Number okunamadi.");
                    return;
                }
                levelConf.LevelNumber = number;

                //PillCount
                var pillCountValue = document.Root.Element("PillCount")?.Value;
                if (pillCountValue == null)
                {
                    Console.WriteLine("PillCount null.");
                    return;
                }
                int pillCount;
                if (!int.TryParse(pillCountValue, out pillCount))
                {
                    Console.WriteLine("PillCount okunamadi.");
                    return;
                }
                levelConf.PillCount = pillCount;

                //Range
                var rangeValue = document.Root.Element("Range")?.Value;
                if (rangeValue == null)
                {
                    Console.WriteLine("Range null.");
                    return;
                }
                int[] range      = new int[2];
                var   rangeSplit = rangeValue.Split(',');
                for (int i = 0; i < 2; i++)
                {
                    int rangeSplitInt;
                    if (!int.TryParse(rangeSplit[i], out rangeSplitInt))
                    {
                        Console.WriteLine("Range okunamadi.");
                        return;
                    }
                    range[i] = rangeSplitInt;
                }
                levelConf.Range = range;

                //HealthyThreshold
                var healthyThresholdValue = document.Root.Element("HealthyThreshold")?.Value;
                if (healthyThresholdValue == null)
                {
                    Console.WriteLine("HealthyThreshold null.");
                    return;
                }
                int healthyThreshold;
                if (!int.TryParse(healthyThresholdValue, out healthyThreshold))
                {
                    Console.WriteLine("HealthyThreshold okunamadi.");
                    return;
                }
                levelConf.HealthyThreshold = healthyThreshold;

                //Diseases
                var diseaseElements = document.Root.Element("Diseases")?.Elements("Disease");
                if (diseaseElements == null)
                {
                    Console.WriteLine("Diseases null");
                    return;
                }
                levelConf.Diseases = new List <DiseaseConfiguration>();
                foreach (var diseaseElement in diseaseElements)
                {
                    //Type
                    var disease = new DiseaseConfiguration();
                    levelConf.Diseases.Add(disease);

                    var typeValue = diseaseElement.Attribute("Type")?.Value;
                    if (typeValue == null)
                    {
                        Console.WriteLine("Type null.");
                        return;
                    }
                    disease.Type = typeValue;

                    //Range
                    var dRangeValue = diseaseElement.Element("Range")?.Value;
                    if (dRangeValue == null)
                    {
                        Console.WriteLine("Range null.");
                        return;
                    }
                    int[] dRange      = new int[2];
                    var   dRangeSplit = dRangeValue.Split(',');
                    for (int i = 0; i < 2; i++)
                    {
                        int dRangeSplitInt;
                        if (!int.TryParse(dRangeSplit[i], out dRangeSplitInt))
                        {
                            Console.WriteLine("Range okunamadi.");
                            return;
                        }
                        dRange[i] = dRangeSplitInt;
                    }
                    disease.Range = dRange;


                    //InitialPoint
                    var initialPointValue = diseaseElement.Element("InitialPoint")?.Value;
                    if (initialPointValue == null)
                    {
                        Console.WriteLine("InitialPoint null.");
                        return;
                    }
                    int initialPoint;
                    if (!int.TryParse(initialPointValue, out initialPoint))
                    {
                        Console.WriteLine("InitialPoint okunamadi.");
                        return;
                    }
                    disease.InitialPoint = initialPoint;
                }
            }

            _levels = new List <Level>();
            foreach (var levelConf in levelConfs)
            {
                _levels.Add(Level.Load(levelConf));
            }
        }