Esempio n. 1
0
        protected override List <ICommand> GetCommandsConsidered()
        {
            var commands = new List <ICommand>();

            var checkNotEnoughAmount = new CheckNotEnoughAmount(InputData.DataInventory, InputData.Id, InputData.AmountWantRemove);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNotEnoughAmount);

            if (checkNotEnoughAmount.IsActual())
            {
                return(commands);
            }

            var entitiesWithId = DatabaseReadOnly.GetEntitiesWithId(InputData.DataInventory, InputData.Id);
            var lostRemove     = InputData.AmountWantRemove;

            foreach (var dataEntity in entitiesWithId)
            {
                var amountInEntity = dataEntity.Amount;
                var remove         = Math.Min(lostRemove, amountInEntity);
                var removeCommand  = CreateCommand <RemoveCommand>().EnterData(new RemoveInputData(dataEntity, remove));
                commands.Add(removeCommand);
                lostRemove -= remove;
                if (lostRemove == 0)
                {
                    break;
                }
            }
            return(commands);
        }
 public SwapPrimaryInputData([NotNull] DataEntity entityDisplacing, [NotNull] DataSlot slotNewPositionEntityDisplacing,
                             [NotNull] DataEntity entityTarget, [NotNull] DataSlot slotNewPositionEntityTarget) : base(entityDisplacing, slotNewPositionEntityDisplacing)
 {
     SlotNewPositionEntityTarget     = slotNewPositionEntityTarget;
     SlotOldPositionEntityDisplacing = DatabaseReadOnly.GetSlotOrNull(entityDisplacing);
     EntityTarget = entityTarget;
 }
Esempio n. 3
0
        public override IEnumerable <CauseFailureCommand> GetActualCauses()
        {
            var result = new List <CauseFailureCommand>();

            var checkOutBorderFirst  = new CheckOutBorder(First, SlotFirst);
            var checkOutBorderSecond = new CheckOutBorder(Second, SlotSecond);

            result.AddRange(checkOutBorderFirst.GetActualCauses());
            result.AddRange(checkOutBorderSecond.GetActualCauses());

            if (checkOutBorderFirst.IsActual() || checkOutBorderSecond.IsActual())
            {
                return(result);
            }

            var slotsNewPositionEntity      = DatabaseReadOnly.GetSlotsForEntityInInventory(First, SlotFirst).ToArray();
            var slotsNewPositionOtherEntity = DatabaseReadOnly.GetSlotsForEntityInInventory(Second, SlotSecond).ToArray();
            var isIntersection = slotsNewPositionEntity.Intersect(slotsNewPositionOtherEntity).Any();

            if (isIntersection)
            {
                result.Add(new CauseIntersectionOfPositions(First, SlotFirst, Second, SlotSecond));
            }
            return(result);
        }
Esempio n. 4
0
        private void PutHandInSlotTryData([NotNull] DataSlot slotLeftTop)
        {
            if (slotLeftTop.DataInventory == null)
            {
                throw GetExceptionUnBindInventoryComponent();
            }

            var entitySource  = HandData.EntitySource;
            var amountWantPut = HandData.DataEntityHand.Amount;

            HandData.ReturnAmountInSource();
            var slotLeftTopPrevious = DatabaseReadOnly.GetSlotOrNull(entitySource);

            if (slotLeftTopPrevious != slotLeftTop)
            {
                var command = Commands.CreateForHand <PutCommand>()
                              .EnterData(new PutInputData(entitySource, slotLeftTop, amountWantPut));
                var success = command.ExecuteTry();

                if (!success)
                {
                    var noValidOnlyForFilters = FactoryCommandNoValidOnlyForFilters.Create(command);

                    // event
                    if (noValidOnlyForFilters.IsCommandNoValidOnlyForFilters)
                    {
                        NoValidFiltersEvent.Invoke(noValidOnlyForFilters.GetImpossibleOnlyForThisFilters());
                    }
                }
            }
            HandData.Clear();
            StatusWasChanged.Invoke();
        }
Esempio n. 5
0
        public bool TakeEntityOnPositionInHandTry(DataSlot slot, Vector2 positionCreateHand)
        {
            if (slot == null)
            {
                throw new ArgumentNullException(nameof(slot));
            }
            var entitySource = DatabaseReadOnly.GetEntityOrNull(slot);

            if (entitySource == null)
            {
                return(false);
            }
            if (Debug.DebugMode)
            {
                _point = Debug.PointForDebug.InstantiateOnRootCanvas().transform;
                _point.SetAsLastSibling();
            }

            TakeEntityInHand(entitySource);
            var entityComponent = _bindComponentToDataDbRead.GetEntityComponent(entitySource);

            _handView = new HandView(entityComponent, HandData.DataEntityHand);
            _handView.PositionSet(positionCreateHand);
            return(true);
        }
Esempio n. 6
0
        public override IEnumerable <CauseFailureCommand> GetActualCauses()
        {
            var result = new List <CauseFailureCommand>();

            var checkOutBorder = new CheckOutBorder(Entity, Slot);

            result.AddRange(checkOutBorder.GetActualCauses());

            // cant get TargetSlots if IsOutBorder
            if (checkOutBorder.IsActual())
            {
                return(result);
            }

            var targetSlots     = DatabaseReadOnly.GetSlotsForEntityInInventory(Slot.DataInventory, Slot.Vector2Int, Entity).ToList();
            var noValidEntities = DatabaseReadOnly.EntitiesInTargetSlotsWithoutExcluded(Entity, AmountWantPut, targetSlots,
                                                                                        ValidEntities).ToList();

            if (noValidEntities.Count == 1)
            {
                var entity = noValidEntities.Single();
                var cause  = new CauseIsSingleNoValidEntityInSlots(entity, targetSlots);
                result.Add(cause);
            }

            if (noValidEntities.Count > 1)
            {
                var cause = new CauseIsMoreOneNoValidEntityInSlots(noValidEntities, targetSlots);
                result.Add(cause);
            }
            return(result);
        }
Esempio n. 7
0
        private List <DataSlot> BusySlotsAdd(List <DataSlot> busySlots, DataEntity entity, DataSlot slot)
        {
            var slotsBusy = DatabaseReadOnly.GetSlotsForEntityInInventory(entity, slot);

            busySlots.AddRange(slotsBusy);
            return(busySlots);
        }
Esempio n. 8
0
 public SwapInputData([NotNull] DataEntity entityDisplacing,
                      [NotNull] DataSlot slotNewPositionEntityDisplacing, [NotNull] DataEntity entityTarget, [CanBeNull] IComparer <DataSlot> comparer = null)
     : base(entityDisplacing, slotNewPositionEntityDisplacing)
 {
     SlotOldPositionEntityDisplacing = DatabaseReadOnly.GetSlotOrNull(entityDisplacing);
     EntityTarget = entityTarget;
     Comparer     = comparer ?? DatabaseReadOnly.ComparerSlotsDefault;
 }
Esempio n. 9
0
        public override IEnumerable <CauseFailureCommand> GetActualCauses()
        {
            var result      = new List <CauseFailureCommand>();
            var isOutBorder = DatabaseReadOnly.IsOutBorderInventory(DataSlot.DataInventory, DataEntity, DataSlot.Vector2Int);

            if (isOutBorder)
            {
                result.Add(new CauseIsOutBorder(DataEntity, DataSlot));
            }
            return(result);
        }
Esempio n. 10
0
        public override IEnumerable <CauseFailureCommand> GetActualCauses()
        {
            var result        = new List <CauseFailureCommand>();
            var amountCanTake = DatabaseReadOnly.GetEntitiesWithId(InventoryFrom, IdEntity).Sum(entity => entity.Amount);

            if (amountCanTake < AmountWantTake)
            {
                result.Add(new CauseNoRequiredAmountInSourceInventory(InventoryFrom, IdEntity, AmountWantTake, amountCanTake));
            }
            return(result);
        }
        public override IEnumerable <CauseFailureCommand> GetActualCauses()
        {
            var result         = new List <CauseFailureCommand>();
            var entitiesWithId = DatabaseReadOnly.GetEntitiesWithId(DataInventory, Id);
            var sumInInventory = entitiesWithId.Sum(entity => entity.Amount);

            if (sumInInventory < AmountWantRemove)
            {
                result.Add(new CauseNotEnoughAmount(DataInventory, Id, AmountWantRemove));
            }
            return(result);
        }
Esempio n. 12
0
        public override IEnumerable <CauseFailureCommand> GetActualCauses()
        {
            var result = new List <CauseFailureCommand>();

            var isEntitiesMayBeStack = DatabaseReadOnly.IsEntitiesMayBeStack(Source, Target);

            if (!isEntitiesMayBeStack)
            {
                result.Add(new CauseEntitiesCannotStack(Source, Target));
            }
            return(result);
        }
Esempio n. 13
0
        private IEnumerable <DataSlot> GetWishSlotsForSwap()
        {
            var entityDisplacing = InputData.EntitySource;
            var entityTarget     = InputData.EntityTarget;
            var comparer         = InputData.Comparer;

            var displacingInventory = entityDisplacing.DataInventory;

            var displacingEntityPreviousPosition =
                DatabaseReadOnly.GetSlotsForEntityInInventory(displacingInventory,
                                                              DatabaseReadOnly.GetSlotOrNull(entityDisplacing).Vector2Int, entityDisplacing).ToArray();

            var wishSlots = new List <DataSlot>();

            if (displacingInventory.TypeInventory == DataInventory.TypeInventoryEnum.GridSupportMultislotEntity)
            {
                int widthEntityForWishSlots  = entityTarget.Dimensions.x;
                int heightEntityForWishSlots = entityTarget.Dimensions.y;

                Vector2Int maxCoordinateWishSlots = new Vector2Int
                {
                    x = displacingEntityPreviousPosition.Max(slot => slot.Vector2Int.x),
                    y = displacingEntityPreviousPosition.Max(slot => slot.Vector2Int.y)
                };

                Vector2Int minCoordinateWishSlots = new Vector2Int
                {
                    x = displacingEntityPreviousPosition.Min(slot => slot.Vector2Int.x) -
                        widthEntityForWishSlots + 1,
                    y = displacingEntityPreviousPosition.Min(slot => slot.Vector2Int.y) -
                        heightEntityForWishSlots + 1
                };

                var wishSlotsPositions = Vector2IntExtension.GetArea(minCoordinateWishSlots, maxCoordinateWishSlots);
                wishSlots = DatabaseReadOnly.GetSlots(displacingInventory, wishSlotsPositions).ToList();
            }
            else
            {
                wishSlots.Add(DatabaseReadOnly.GetSlotOrNull(entityDisplacing));
            }

            wishSlots = wishSlots.OrderBy(slot => slot, comparer).ToList();
            return(wishSlots);
        }
        protected override List <ICommand> GetCommandsConsidered()
        {
            var resultList = new List <ICommand>();

            var slotsFree = DatabaseReadOnly.SlotsFree(InputData.Inventory).ToArray();
            var busySlots = new List <DataSlot>();

            foreach (var entity in InputData.Entities)
            {
                foreach (var slot in slotsFree)
                {
                    if (busySlots.Contains(slot))
                    {
                        continue;
                    }

                    var command = CreateCommand <CreateCommand>();
                    command.EnterData(new CreateInputData(entity, slot));
                    if (command.IsCanExecute)
                    {
                        var slotsBusy = DatabaseReadOnly.GetSlotsForEntityInInventory(entity, slot);
                        busySlots.AddRange(slotsBusy);
                    }

                    resultList.Add(command);

                    if (command.IsCanExecute)
                    {
                        break;
                    }
                }
            }

            var checkNotFreeSlotsInventoryForCreateEntities = new CheckNotFreeSlotsInventoryForCreateEntities(InputData.Inventory, InputData.Entities
                                                                                                              , resultList);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNotFreeSlotsInventoryForCreateEntities);

            return(resultList);
        }
Esempio n. 15
0
        protected override List <ICommand> GetCommandsConsidered()
        {
            var idEntity      = InputData.IdEntity;
            var fromInventory = InputData.FromInventory;
            var toInventory   = InputData.ToInventory;
            var amount        = InputData.Amount;

            var resultList = new List <ICommand>();

            var checkNoRequiredAmountInSourceInventory = new CheckNoRequiredAmountInSourceInventory(fromInventory, idEntity, amount);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNoRequiredAmountInSourceInventory);
            if (checkNoRequiredAmountInSourceInventory.IsActual())
            {
                return(resultList);
            }

            var entitiesWithId   = DatabaseReadOnly.GetEntitiesWithId(fromInventory, idEntity);
            var totalLeftToPut   = amount;
            var slotsToInventory = DatabaseReadOnly.GetInventoryStructure(toInventory).Slots;

            var busySlots = new List <DataSlot>();

            foreach (var entityFrom in entitiesWithId)
            {
                var takeFromThisEntity      = Math.Min(totalLeftToPut, entityFrom.Amount);
                var leftToPutFromThisEntity = takeFromThisEntity;
                foreach (var slot in slotsToInventory)
                {
                    if (busySlots.Contains(slot))
                    {
                        continue;
                    }

                    var wasPut = 0;

                    // empty slots try
                    var      moveCommand = (MoveCommand)CreateCommand <MoveCommand>().EnterData(new MoveInputData(entityFrom, slot, leftToPutFromThisEntity));
                    ICommand command     = moveCommand;
                    if (moveCommand.IsCanExecute)
                    {
                        wasPut    = moveCommand.InputData.AmountWantPut;
                        busySlots = BusySlotsAdd(busySlots, entityFrom, slot);
                    }
                    else
                    {
                        if (moveCommand.IsCanTryStackOrSwap)
                        {
                            // stack
                            var entityInSlot = moveCommand.CausesFailure.GetCauseFirstOrDefault <CauseIsSingleNoValidEntityInSlots>().Entity;
                            var stackCommand = CreateCommand <StackCommand>(FilterCollection);
                            stackCommand.EnterData(new StackInputData(entityFrom, entityInSlot, leftToPutFromThisEntity));
                            command = stackCommand;
                            if (stackCommand.IsCanExecute)
                            {
                                wasPut    = stackCommand.InputData.AmountWantPut;
                                busySlots = BusySlotsAdd(busySlots, entityFrom, slot);
                            }
                            else
                            {
                                var isCanTryWithOtherAmount = stackCommand.IsCanTryWithOtherAmount();
                                if (isCanTryWithOtherAmount.sucess)
                                {
                                    var amountCanPut            = isCanTryWithOtherAmount.amountCanPut;
                                    var stackCommandOtherAmount = stackCommand.EnterData(new StackInputData(entityFrom, entityInSlot, amountCanPut));

                                    if (stackCommandOtherAmount.IsCanExecute)
                                    {
                                        wasPut    = stackCommandOtherAmount.InputData.AmountWantPut;
                                        busySlots = BusySlotsAdd(busySlots, entityFrom, slot);
                                        command   = stackCommandOtherAmount;
                                    }
                                }
                            }
                        }
                    }

                    resultList.Add(command);
                    leftToPutFromThisEntity -= wasPut;
                    totalLeftToPut          -= wasPut;

                    if (leftToPutFromThisEntity == 0)
                    {
                        break;
                    }
                }
                if (totalLeftToPut == 0)
                {
                    break;
                }
            }

            var checkTargetInventoryIsOverflow = new CheckTargetInventoryIsOverflow(InputData.ToInventory,
                                                                                    InputData.IdEntity, InputData.Amount, totalLeftToPut);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkTargetInventoryIsOverflow);

            return(resultList);
        }
Esempio n. 16
0
 private int GetSumCoins(DataInventory inventory) => DatabaseReadOnly.GetEntitiesWithId(inventory, InputData.IdCoins).Sum(data => data.Amount);