Beispiel #1
0
        /// <summary>
        /// Saves a armour object.
        /// </summary>
        /// <param name="item">
        /// Armour obejct to save.
        /// </param>
        /// <param name="room">
        /// Eventual room in which the armour is placed.
        /// </param>
        /// <returns>
        /// Id of armour saved.
        /// </returns>
        public async Task <int> Save(ArmourModel item, RoomModel room = null)
        {
            _gameDataAccess.StartTransaction();

            var model = new Armour
            {
                ArmourID   = item.Id,
                ArmourType = item.Type
            };

            if (item.ActiveEffect != null)
            {
                model.EffectLevel = item.ActiveEffect.Level;
                model.EffectType  = item.ActiveEffect.Type;
            }
            //if item is in the room, not character inventory
            if (room != null)
            {
                model.RoomID = room.Id;
                //if room has a shop that sells armours
                if (room.ShopEvent?.BuyableArmours != null)
                {
                    //if item is in this shop
                    if (room.ShopEvent.BuyableArmours.Contains(item))
                    {
                        model.Buyable = 1;
                    }
                    else
                    {
                        model.Buyable = 0;
                    }
                }
                else
                {
                    model.Buyable = 0;
                }
            }

            var exists = await _gameDataAccess.Get(model);

            if (exists == null)
            {
                int id = await _gameDataAccess.Add(model);

                _gameDataAccess.Save();
                return(id);
            }
            else
            {
                await _gameDataAccess.Update(model);

                _gameDataAccess.Save();
                return(model.ArmourID.Value);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Removes saved armour.
        /// </summary>
        /// <param name="item">
        /// Armour to remove.
        /// </param>
        public async Task Remove(ArmourModel item)
        {
            //to not nest transactions
            bool transaction;

            if (_gameDataAccess.Transaction == false)
            {
                _gameDataAccess.StartTransaction();
                transaction = true;
            }
            else
            {
                transaction = false;
            }
            await _gameDataAccess.Remove(new Armour { ArmourID = item.Id });

            if (transaction)
            {
                _gameDataAccess.Save();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets armour object.
        /// </summary>
        /// <param name="item">
        /// Model of weapon to get.
        /// </param>
        public async Task <ArmourModel> Get(ArmourModel item)
        {
            //in case armour was not yet saved
            if (item.Id == null)
            {
                return(null);
            }
            var save = new Armour
            {
                ArmourID = item.Id
            };

            //get saved state of an armour with given id
            save = await _gameDataAccess.Get(save);

            var result = new ArmourModel
            {
                Id   = save.ArmourID,
                Type = save.ArmourType
            };

            //get a prototype for the armour
            var model = await _protDataAccess.Get(new ArmourProt { ArmourType = result.Type });

            result.Defence = model.Defence;
            result.Price   = model.Price;
            result.Level   = model.Level;

            //if armour has a saved effect
            if (save.EffectType != null)
            {
                //get effect object
                result.ActiveEffect = await Get(new EffectModel { Type = save.EffectType });

                result.ActiveEffect.Level = save.EffectLevel.Value;
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Fills shop with armours.
        /// </summary>
        /// <param name="room">
        /// Room with shop to fill.
        /// </param>
        /// <returns>
        /// Room with shop filled with armours.
        /// </returns>
        private async Task <RoomModel> FillArmours(RoomModel room)
        {
            var armourModel = new Armour {
                RoomID = room.Id
            };
            var armours = await _gameDataAccess.GetByRoom(armourModel);

            var freeArmours    = new List <ArmourModel>();
            var buyableArmours = new List <ArmourModel>();

            foreach (var armour in armours)
            {
                var model = new ArmourModel
                {
                    Id = armour.ArmourID,
                };
                model = await Get(model);

                if (armour.Buyable.Value == 1)
                {
                    buyableArmours.Add(model);
                }
                else
                {
                    freeArmours.Add(model);
                }
            }

            if (buyableArmours.Count > 0)
            {
                room.ShopEvent.BuyableArmours = buyableArmours;
            }
            if (freeArmours.Count > 0)
            {
                room.ShopEvent.FreeArmours = freeArmours;
            }

            return(room);
        }