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);
        }
Esempio n. 2
0
        public sealed override void Update()
        {
            base.Update();
            CommandsConsidered      = GetCommandsConsidered();
            CommandsChainForExecute = GetCommandsForExecute();
            var checkNoCommandsForExecute = new CheckNoCommandsForExecute(CommandsChainForExecute);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNoCommandsForExecute);
        }
Esempio n. 3
0
        private void CheckAndAddCauseNoValidFilters <T>(IFilterCollectionConcrete <T> filtersCollection, T data)
            where T : class, ICommandInputData
        {
            filtersCollection.Validate(data, out var responses);
            var filterNoValidNewCollection = new Filters.Response.FilterNoValidCollection(responses.ToList());

            if (filterNoValidNewCollection.Any())
            {
                var checkNoValidFilters = new CheckNoValidFilters(filterNoValidNewCollection);
                CausesCheckAndAdd.AddInCausesIfNecessary(checkNoValidFilters);
            }
        }
Esempio n. 4
0
        protected override void CausesCheckAdd()
        {
            var source        = InputData.EntitySource;
            var toDataEntity  = InputData.ToDataEntity;
            var amountWantPut = InputData.AmountWantPut;

            CausesCheckAndAdd.AddInCausesIfNecessary(new CheckAmountRequiredMoreAmountInSource(source, amountWantPut));
            CausesCheckAndAdd.AddInCausesIfNecessary(new CheckCannotStack(source, toDataEntity));
            CausesCheckAndAdd.AddInCausesIfNecessary(new CheckStackOverflow(source, toDataEntity, amountWantPut));

            CheckAndAddCauseNoValidFilters();
        }
Esempio n. 5
0
        protected override List <ICommand> GetCommandsConsidered()
        {
            var entitySource  = InputData.EntitySource;
            var slot          = InputData.SlotLeftTop;
            var amountWantPut = InputData.AmountWantPut;

            var resultList = new List <ICommand>();

            // MoveInEmptySlots
            var moveInEmptySlots = (MoveCommand)CreateCommand <MoveCommand>().EnterData(new MoveInputData(entitySource, slot, amountWantPut));

            resultList.Add(moveInEmptySlots);
            if (!moveInEmptySlots.IsCanTryStackOrSwap)
            {
                return(resultList);
            }

            if (moveInEmptySlots.CausesFailure.IsCause <CauseIsMoreOneNoValidEntityInSlots>())
            {
                return(resultList);
            }

            // There is only one entity in slots
            var otherEntity = moveInEmptySlots.CausesFailure.GetCauseFirstOrDefault <CauseIsSingleNoValidEntityInSlots>().Entity;
            var moveInStack = (StackCommand)CreateCommand <StackCommand>(FilterCollection)
                              .EnterData(new StackInputData(entitySource, otherEntity, amountWantPut));

            resultList.Add(moveInStack);

            if (!moveInStack.IsNeedOtherPrimaryAction)
            {
                return(resultList);
            }

            var checkTakeNoFullStack = new CheckTakeNoFullStack(entitySource, amountWantPut);

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

            // Swap
            var swap = CreateCommand <SwapCommand>(FilterCollection).EnterData(new SwapInputData(entitySource, slot, otherEntity));

            resultList.Add(swap);
            return(resultList);
        }
Esempio n. 6
0
        protected override void CausesCheckAdd()
        {
            var dataEntity = InputData.DataEntity;
            var slot       = InputData.Slot;

            var checkOutBorder = new CheckOutBorder(dataEntity, slot);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkOutBorder);

            var checkNoValidEntitiesInTargetSlots = new CheckNoValidEntitiesInTargetSlots(dataEntity, slot);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNoValidEntitiesInTargetSlots);

            // filters
            CheckAndAddCauseNoValidFilters();
        }
        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);
        }
        protected override void CausesCheckAdd()
        {
            // IntersectionOfPositions
            var checkIntersectionOfPositions = new CheckIntersectionOfPositions(InputData.EntitySource, InputData.SlotTo,
                                                                                InputData.EntityTarget, InputData.SlotNewPositionEntityTarget);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkIntersectionOfPositions);

            // NoValidEntitiesInTargetSlots
            var dataEntitiesValidate = new[] { InputData.EntitySource, InputData.EntityTarget };

            var checkNoValidEntitiesInTargetSlotsEntityDisplacing = new CheckNoValidEntitiesInTargetSlots(InputData.EntitySource, InputData.SlotTo,
                                                                                                          dataEntitiesValidate);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNoValidEntitiesInTargetSlotsEntityDisplacing);

            var checkNoValidEntitiesInTargetSlotsEntityTarget = new CheckNoValidEntitiesInTargetSlots(InputData.EntityTarget, InputData.SlotNewPositionEntityTarget,
                                                                                                      dataEntitiesValidate);

            CausesCheckAndAdd.AddInCausesIfNecessary(checkNoValidEntitiesInTargetSlotsEntityTarget);

            CheckAndAddCauseNoValidFilters();
        }
Esempio n. 9
0
 protected Command()
 {
     _causesCollection = new CausesCollection();
     CausesCheckAndAdd = new CausesCheckAndAdd(_causesCollection);
 }
Esempio n. 10
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);
        }