Example #1
0
        public static IEnumerable <ResourceSelection> FindConsumableResourcesByRule(
            IActor actor,
            IActorTaskContext context,
            IEnumerable <Resource> resources,
            ConsumeCommonRuleType ruleType)
        {
            foreach (var resource in resources)
            {
                var rule = resource.Scheme.Use?.CommonRules?
                           .SingleOrDefault(x => x.Type == ruleType && x.Direction == PersonRuleDirection.Positive);

                if (rule != null)
                {
                    var isAllow = UsePropHelper.CheckPropAllowedByRestrictions(resource, actor, context);
                    if (!isAllow)
                    {
                        continue;
                    }

                    yield return(new ResourceSelection
                    {
                        Resource = resource,
                        Rule = rule
                    });
                }
            }
        }
Example #2
0
        public override CanExecuteCheckResult CanExecute()
        {
            var propVm = _inventoryState.SelectedProp;

            if (propVm == null)
            {
                return(new CanExecuteCheckResult {
                    IsSuccess = false
                });
            }

            var prop = propVm.Prop;

            if (prop == null)
            {
                throw new AppException("Для модели представления не задан предмет.");
            }

            if (prop.Scheme.Use == null)
            {
                throw new AppException("Попытка использовать предмет, для которого нет информации об использовании.");
            }

            var sector = _player.SectorNode.Sector;

            if (sector is null)
            {
                throw new InvalidOperationException();
            }

            var taskContext = new ActorTaskContext(sector);
            var actor       = PlayerState.ActiveActor?.Actor;

            if (actor is null)
            {
                throw new InvalidOperationException();
            }

            var isAllowed = UsePropHelper.CheckPropAllowedByRestrictions(prop, actor, taskContext);

            if (!isAllowed)
            {
                return(new CanExecuteCheckResult {
                    IsSuccess = false
                });
            }

            return(new CanExecuteCheckResult {
                IsSuccess = true
            });
        }
        public void CheckPropAllowedByRestrictions_HasMaxSurivalHazardEffect_UsageIsNoAllowed(
            SurvivalStatType effectStatType, UsageRestrictionRule usageRule)
        {
            // ARRANGE

            var personMock = new Mock <IPerson>();

            var сonditionsModuleMock = new Mock <IConditionsModule>();
            var сonditions           = new[]
            {
                new SurvivalStatHazardCondition(effectStatType, SurvivalStatHazardLevel.Max,
                                                Mock.Of <ISurvivalRandomSource>())
            };

            сonditionsModuleMock.Setup(x => x.Items).Returns(сonditions);
            personMock.Setup(x => x.GetModule <IConditionsModule>(nameof(IConditionsModule)))
            .Returns(сonditionsModuleMock.Object);
            personMock.Setup(x => x.HasModule(nameof(IConditionsModule))).Returns(true);

            var actor = Mock.Of <IActor>(x => x.Person == personMock.Object);

            var propScheme = new TestPropScheme
            {
                Use = new TestPropUseSubScheme
                {
                    Restrictions = Mock.Of <IUsageRestrictions>(x =>
                                                                x.Items == new[] { Mock.Of <IUsageRestrictionItem>(item => item.Type == usageRule) })
                }
            };
            var prop = new Resource(propScheme, 1);

            var context = Mock.Of <IActorTaskContext>();

            // ACT

            var fact = UsePropHelper.CheckPropAllowedByRestrictions(prop, actor, context);

            // ASSERT
            fact.Should().BeFalse();
        }