예제 #1
0
파일: Army.cs 프로젝트: eecs-susu/DotNet
        public static Army <Tank> Deserialize <TContainer>(Serializer <TContainer> serializer, string data)
            where TContainer : SerializerContainer
        {
            var container = serializer.Create();

            container.Deserialize(data);
            Army <Tank> army = null;

            switch ((string)container.Container["TankFactory"])
            {
            case nameof(SovietUnionTankFactory):
                army = new Army <Tank>(new SovietUnionTankFactory());
                break;

            case nameof(UnitedStatesTankFactory):
                army = new Army <Tank>(new UnitedStatesTankFactory());
                break;

            default:
                throw new Exception();     // TODO: make custom exception
            }

            foreach (var tank in (IEnumerable <JToken>)container.Container["Tanks"])
            {
                var type = (TankType)(int)tank["Type"];
                switch (type)
                {
                case TankType.Composite:
                    army.AddCompositeTank();
                    break;

                case TankType.Reactive:
                    army.AddReactiveTank();
                    break;

                case TankType.Light:
                    army.AddLightTank();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(army);
        }
예제 #2
0
        private static void LoadArmies(string path)
        {
            var forces         = File.ReadAllText(path);
            var data           = JObject.Parse(forces);
            var existingArmies = new HashSet <string>();
            var armies         = new List <Army <Tank> >();

            foreach (var army in data["Armies"])
            {
                var side = (string)army["Side"];
                if (existingArmies.Contains(side))
                {
                    throw new TooManyForcesException($"Each side can contains at most 1 army, but '{side}' wants to have more");
                }

                existingArmies.Add(side);
                TankFactory factory = null;
                switch (side)
                {
                case "SovietUnion":
                    factory = new SovietUnionTankFactory();
                    break;

                case "UnitedStates":
                    factory = new UnitedStatesTankFactory();
                    break;

                default:
                    throw new ArgumentException($"There is no such side like '{side}'");
                }

                var tanks = new Army <Tank>(factory);
                foreach (var tank in army["Tanks"])
                {
                    var type = (string)tank["Type"];
                    switch (type)
                    {
                    case "Reactive":
                        tanks.AddReactiveTank();
                        break;

                    case "Light":
                        tanks.AddLightTank();
                        break;

                    case "Composite":
                        tanks.AddCompositeTank();
                        break;

                    default:
                        throw new ArgumentException($"There is no such tank type like '{type}'");
                    }
                }

                var extraTanks = (int)(army["RandomTanks"] ?? 0);
                for (var i = 0; i < extraTanks; ++i)
                {
                    tanks.AddRandomTank();
                }
                armies.Add(tanks);
            }

            foreach (var army in armies)
            {
                Console.WriteLine("---");
                Console.WriteLine(string.Join(' ', army));
            }
        }