public void TypeTest()
        {
            AbstractFactory factory = new PointBottleFactory();

            Assert.IsInstanceOfType(factory, typeof(AbstractFactory));
            Assert.IsInstanceOfType(factory, typeof(PointBottleFactory));

            factory = new SpecialBottleFactory();

            Assert.IsInstanceOfType(factory, typeof(SpecialBottleFactory));
        }
        public void BottleInfoGetTest()
        {
            AbstractFactory factory = new PointBottleFactory();

            var bottle = factory.CreateBottle("NukeCola");

            Assert.AreEqual(int.Parse(bottle.GetBottleInfo()), 5);

            factory = new SpecialBottleFactory();

            bottle = factory.CreateBottle("GinOfDestruction");

            Assert.AreEqual(int.Parse(bottle.GetBottleInfo()), 10);
        }
        public void CreateBottleTest()
        {
            AbstractFactory factory = new PointBottleFactory();

            var bottle = factory.CreateBottle("Vodka");

            Assert.IsNull(bottle);

            bottle = factory.CreateBottle("Cola");

            Assert.IsInstanceOfType(bottle, typeof(Cola));



            factory = new SpecialBottleFactory();
            bottle  = factory.CreateBottle("Cola");

            Assert.IsNull(bottle);

            bottle = factory.CreateBottle("Vodka");

            Assert.IsInstanceOfType(bottle, typeof(Vodka));
        }
Пример #4
0
        public void GetBottleData()
        {
            Task <string>   task;
            string          result;
            JArray          array;
            Bottle          bottle;
            AbstractFactory pointBottleFactory   = new PointBottleFactory();
            AbstractFactory specialBottleFactory = new SpecialBottleFactory();
            List <Bottle>   toDelete;

            while (!_cancelationTokenSourceBottles.Token.IsCancellationRequested)
            {
                task   = Facade.Connector.GetAction("Bottle");
                result = task.Result;
                array  = JArray.Parse(result);
                lock (GroundBottles)
                {
                    foreach (var item in GroundBottles)
                    {
                        item.MarkedForDelete = true;
                    }
                    foreach (var item in array)
                    {
                        bottle = GroundBottles.FirstOrDefault(x => x.BottleId == item["bottleId"].Value <int>());
                        if (bottle == null)
                        {
                            string bottleType = BottleTypeIntToString(item["bottleType"].Value <int>());
                            bottle = pointBottleFactory.CreateBottle(bottleType);
                            if (bottle != null)
                            {
                                bottle.PositionX       = item["posX"].Value <int>();
                                bottle.PositionY       = item["posY"].Value <int>();
                                bottle.BottleId        = item["bottleId"].Value <int>();
                                bottle.Image.Location  = new Point(bottle.PositionX * 16, bottle.PositionY * 16);
                                bottle.IsNewlySpawned  = true;
                                bottle.MarkedForDelete = false;
                                GroundBottles.Add(bottle);
                            }
                            else
                            {
                                bottle = specialBottleFactory.CreateBottle(bottleType);
                                if (bottle != null)
                                {
                                    bottle.PositionX       = item["posX"].Value <int>();
                                    bottle.PositionY       = item["posY"].Value <int>();
                                    bottle.BottleId        = item["bottleId"].Value <int>();
                                    bottle.Image.Location  = new Point(bottle.PositionX * 16, bottle.PositionY * 16);
                                    bottle.IsNewlySpawned  = true;
                                    bottle.MarkedForDelete = false;
                                    GroundBottles.Add(bottle);
                                }
                            }
                        }
                        else
                        {
                            bottle.MarkedForDelete = false;
                        }
                    }
                    toDelete = GroundBottles.Where(x => x.MarkedForDelete == true).ToList();
                    foreach (Control item in Controls)
                    {
                        if (item is PictureBox pictureBox && toDelete.Exists(x => x.Image == (item as PictureBox)))
                        {
                            Controls.Remove(pictureBox);
                        }
                    }
                    GroundBottles.RemoveAll(x => x.MarkedForDelete == true);
                }
                _cancelationTokenSourceBottles.Token.WaitHandle.WaitOne(5000);
            }
        }