Example #1
0
        public static int CalculateScore(this IScorable scorable)
        {
            var baseScore = scorable.Stars * 50 + scorable.Awesomes * 5 + scorable.AdminScore + 2;

            scorable.Score = baseScore / ((DateTime.UtcNow - scorable.CreateTime).Days + 1);
            return(scorable.Score);
        }
Example #2
0
        public override void Collided(ICollidable i_Collidable)
        {
            Bullet bullet  = i_Collidable as Bullet;
            bool   dispose = true;

            if (bullet != null)
            {
                if (TeamColor == Color.Blue)
                {
                    dispose = randomCalculationIfIShouldDie();
                }
            }
            else
            {
                IScorable iScorable = i_Collidable as IScorable;
                if (iScorable != null)
                {
                    addScoresToMyShooter(iScorable);
                }
            }

            if (dispose)
            {
                IsAlive = false;
                Visible = false;
            }
        }
Example #3
0
        /// <summary>
        /// Try to simplify a list of scorables.
        /// </summary>
        /// <param name="scorables">The simplified list of scorables.</param>
        /// <param name="scorable">The single scorable representing the list, if possible.</param>
        /// <returns>True if it were possible to reduce the list of scorables to a single scorable, false otherwise.</returns>
        public static bool TryReduce <Item, Score>(ref IEnumerable <IScorable <Item, Score> > scorables, out IScorable <Item, Score> scorable)
        {
            // only if this is a fixed list of scorables, but never a lazy enumerable
            var list = scorables as IReadOnlyList <IScorable <Item, Score> >;

            if (list != null)
            {
                var itemCount = list.Count;
                int keepCount = 0;
                for (int index = 0; index < itemCount; ++index)
                {
                    var item = list[index];
                    if (Keep(item))
                    {
                        ++keepCount;
                    }
                }

                // empty non-null list is null scorable
                if (keepCount == 0)
                {
                    scorable = NullScorable <Item, Score> .Instance;
                    return(true);
                }
                // single item non-null list is just that scorable
                else if (keepCount == 1)
                {
                    for (int index = 0; index < itemCount; ++index)
                    {
                        var item = list[index];
                        if (Keep(item))
                        {
                            scorable = item;
                            return(true);
                        }
                    }
                }
                // non-null subset of that list is just those scorables
                else if (keepCount < itemCount)
                {
                    var keep      = new IScorable <Item, Score> [keepCount];
                    int keepIndex = 0;
                    for (int index = 0; index < itemCount; ++index)
                    {
                        var item = list[index];
                        if (Keep(item))
                        {
                            keep[keepIndex] = item;
                            ++keepIndex;
                        }
                    }

                    scorables = keep;
                }
            }

            scorable = null;
            return(false);
        }
Example #4
0
        TargetScore IScorable <Item, TargetScore> .GetScore(Item item, object state)
        {
            IScorable <Item, SourceScore> source = this.inner;
            var sourceScore = source.GetScore(item, state);
            var targetScore = this.selector(item, sourceScore);

            return(targetScore);
        }
Example #5
0
        public DispatchTests()
        {
            // TODO: not working
            //methods.Setup(m => m.ToString()).Returns("methods");

            this.resolver =
                new ActivityResolver(
                    new DictionaryResolver(new Dictionary <Type, object>()
            {
                { typeof(IActivity), this.activity },
                { typeof(IMethods), this.methods.Object },
            }, new NullResolver()));

            luisOne
            .Setup(l => l.BuildUri(It.IsAny <string>()))
            .Returns <string>(q => new UriBuilder()
            {
                Path = q
            }.Uri);

            luisOne
            .Setup(l => l.QueryAsync(It.IsAny <Uri>(), token))
            .Returns <Uri, CancellationToken>(async(u, t) =>
            {
                var text = u.LocalPath.Substring(1);
                return(luisOneByText[text]);
            });

            luisTwo
            .Setup(l => l.BuildUri(It.IsAny <string>()))
            .Returns <string>(q => new UriBuilder()
            {
                Path = q
            }.Uri);

            luisTwo
            .Setup(l => l.QueryAsync(It.IsAny <Uri>(), token))
            .Returns <Uri, CancellationToken>(async(u, t) =>
            {
                var text = u.LocalPath.Substring(1);
                return(luisTwoByText[text]);
            });

            Func <ILuisModel, ILuisService> make = model =>
            {
                if (model.SubscriptionKey == KeyOne && model.ModelID == ModelOne)
                {
                    return(luisOne.Object);
                }
                if (model.SubscriptionKey == KeyTwo && model.ModelID == ModelTwo)
                {
                    return(luisTwo.Object);
                }
                throw new NotImplementedException();
            };

            this.scorable = new AttributeScorable(typeof(IMethods), make);
        }
Example #6
0
        protected virtual bool OnStage(FoldStage stage, IScorable <IResolver, object> scorable, IResolver item, object state, object score)
        {
            switch (stage)
            {
            case FoldStage.AfterFold: return(true);

            case FoldStage.StartPost: continueAfterPost = false; return(true);

            case FoldStage.AfterPost: return(continueAfterPost);

            default: throw new NotImplementedException();
            }
        }
Example #7
0
        public override bool OnStageHandler(FoldStage stage, IScorable <Item, Score> scorable, Item item, object state, Score score)
        {
            switch (stage)
            {
            case FoldStage.AfterFold: return(OnFold(scorable, item, state, score));

            case FoldStage.StartPost: return(true);

            case FoldStage.AfterPost: return(false);

            default: throw new NotImplementedException();
            }
        }
Example #8
0
        public override async Task <Token <Item, Score> > PrepareAsync(Item item, CancellationToken token)
        {
            Score  maximumScore = default(Score);
            object maximumState = null;
            IScorable <Item, Score> maximumScorable = null;

            foreach (var scorable in this.scorables)
            {
                var state = await scorable.PrepareAsync(item, token);

                if (scorable.HasScore(item, state))
                {
                    var  score = scorable.GetScore(item, state);
                    bool better;
                    if (maximumScorable == null)
                    {
                        better = true;
                    }
                    else
                    {
                        var compare = this.comparer.Compare(score, maximumScore);
                        better = compare > 0;
                    }

                    if (better)
                    {
                        maximumScore    = score;
                        maximumState    = state;
                        maximumScorable = scorable;

                        if (!OnFold(scorable, item, state, score))
                        {
                            break;
                        }
                    }
                }
            }

            if (maximumScorable != null)
            {
                return(new Token <Item, Score>()
                {
                    Scorable = maximumScorable, State = maximumState
                });
            }
            else
            {
                return(null);
            }
        }
Example #9
0
        /// <summary>
        /// Invoke the scorable calling protocol against a single scorable.
        /// </summary>
        public static async Task <bool> TryPostAsync <Item, Score>(this IScorable <Item, Score> scorable, Item item, CancellationToken token)
        {
            var state = await scorable.PrepareAsync(item, token);

            if (scorable.HasScore(item, state))
            {
                var score = scorable.GetScore(item, state);
                await scorable.PostAsync(item, state, token);

                return(true);
            }

            return(false);
        }
Example #10
0
        /// <summary>
        /// True if the scorable is non-null, false otherwise.
        /// </summary>
        public static bool Keep <Item, Score>(IScorable <Item, Score> scorable)
        {
            // complicated because IScorable<Item, Score> is variant on generic parameter,
            // so generic type arguments of NullScorable<,> may not match generic type
            // arguments of IScorable<,>
            var type = scorable.GetType();

            if (type.IsGenericType)
            {
                var definition = type.GetGenericTypeDefinition();
                if (typeof(NullScorable <,>).IsAssignableFrom(definition))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #11
0
        private bool OnFold(IScorable <Item, Score> scorable, Item item, object state, Score score)
        {
            if (this.comparer.Compare(score, this.traits.Minimum) < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(score));
            }

            var maximum = this.comparer.Compare(score, this.traits.Maximum);

            if (maximum > 0)
            {
                throw new ArgumentOutOfRangeException(nameof(score));
            }
            else if (maximum == 0)
            {
                return(false);
            }

            return(true);
        }
Example #12
0
 public SelectScoreScorable(IScorable <Item, SourceScore> scorable, Func <Item, SourceScore, TargetScore> selector)
     : base(scorable)
 {
     SetField.NotNull(out this.selector, nameof(selector), selector);
 }
Example #13
0
 /// <summary>
 /// Project the item of a scorable using a lambda expression.
 /// </summary>
 public static IScorable <SourceItem, Score> SelectItem <SourceItem, TargetItem, Score>(this IScorable <TargetItem, Score> scorable, Func <SourceItem, TargetItem> selector)
 {
     return(new SelectItemScorable <SourceItem, TargetItem, Score>(scorable, selector));
 }
Example #14
0
 /// <summary>
 /// Project the score of a scorable using a lambda expression.
 /// </summary>
 public static IScorable <Item, TargetScore> SelectScore <Item, SourceScore, TargetScore>(this IScorable <Item, SourceScore> scorable, Func <Item, SourceScore, TargetScore> selector)
 {
     return(new SelectScoreScorable <Item, SourceScore, TargetScore>(scorable, selector));
 }
Example #15
0
 public static IScorable <Item, Score> WhereScore <Item, Score>(this IScorable <Item, Score> scorable, Func <Item, Score, bool> predicate)
 {
     return(new WhereScoreScorable <Item, Score>(scorable, predicate));
 }
Example #16
0
 protected DelegatingScorable(IScorable <Item, Score> inner)
 {
     SetField.NotNull(out this.inner, nameof(inner), inner);
 }
Example #17
0
 public static IScorable <IResolver, double> Normalize(this IScorable <IResolver, Binding> scorable)
 {
     return(scorable.SelectScore((r, b) => 1.0));
 }
Example #18
0
 public static IDialog <T> WithScorable <T, Score>(this IDialog <T> antecedent, IScorable <Score> scorable)
 {
     return(new WithScorableDialog <T, Score>(antecedent, scorable));
 }
Example #19
0
 public static IScorable <IResolver, IntentRecommendation> When(this IScorable <IResolver, Binding> scorable, ILuisModel model, LuisIntentAttribute intent, ILuisService service = null)
 {
     service = service ?? new LuisService(model);
     return(new LuisIntentScorable <Binding, Binding>(service, model, intent, scorable));
 }
Example #20
0
 public static IScorable <IResolver, double> Normalize(this IScorable <IResolver, Match> scorable)
 {
     return(scorable.SelectScore((r, m) => RegexMatchScorable.ScoreFor(m)));
 }
Example #21
0
        public override async Task PostAsync <T>(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            Score             maximumScore    = default(Score);
            object            maximumState    = null;
            IScorable <Score> maximumScorable = null;

            Func <IScorable <Score>, Delegate, Task <bool> > UpdateAsync = async(scorable, frame) =>
            {
                var state = await scorable.PrepareAsync(item, frame);

                Score score;
                if (scorable.TryScore(state, out score))
                {
                    if (this.comparer.Compare(score, this.traits.Minimum) < 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(score));
                    }

                    if (this.comparer.Compare(score, this.traits.Maximum) > 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(score));
                    }

                    var compare = this.comparer.Compare(score, maximumScore);
                    if (maximumScorable == null || compare > 0)
                    {
                        maximumScore    = score;
                        maximumState    = state;
                        maximumScorable = scorable;

                        if (this.comparer.Compare(score, this.traits.Maximum) == 0)
                        {
                            return(false);
                        }
                    }
                }

                return(true);
            };

            bool more = true;

            foreach (var frame in this.Frames)
            {
                var scorable = frame.Target as IScorable <Score>;
                if (scorable != null)
                {
                    more = await UpdateAsync(scorable, frame);

                    if (!more)
                    {
                        break;
                    }
                }
            }

            if (more)
            {
                foreach (var scorable in this.scorables)
                {
                    more = await UpdateAsync(scorable, null);

                    if (!more)
                    {
                        break;
                    }
                }
            }

            if (maximumScorable != null)
            {
                await maximumScorable.PostAsync <T>(this.inner, item, maximumState);

                await base.PollAsync();
            }
            else
            {
                await base.PostAsync <T>(item, cancellationToken);
            }
        }
Example #22
0
 public WithScorableDialog(IDialog <T> antecedent, IScorable <Item, Score> scorable)
     : base(scorable)
 {
     SetField.NotNull(out this.Antecedent, nameof(antecedent), antecedent);
 }
 public ScoringDialogTask(IPostToBot inner, IDialogStack stack, IScorable <IActivity, Score> scorable)
 {
     SetField.NotNull(out this.inner, nameof(inner), inner);
     SetField.NotNull(out this.stack, nameof(stack), stack);
     SetField.NotNull(out this.scorable, nameof(scorable), scorable);
 }
Example #24
0
 public static IScorable <IResolver, double> Normalize(this IScorable <IResolver, IntentRecommendation> scorable)
 {
     return(scorable.SelectScore((r, i) => i.Score ?? 0));
 }
Example #25
0
 public WithScorableDialog(IDialog <T> antecedent, IScorable <Score> scorable)
 {
     SetField.NotNull(out this.Antecedent, nameof(antecedent), antecedent);
     SetField.NotNull(out this.Scorable, nameof(scorable), scorable);
 }
Example #26
0
 public static IScorable <IResolver, Match> When(this IScorable <IResolver, Binding> scorable, Regex regex)
 {
     return(new RegexMatchScorable <Binding, Binding>(regex, scorable));
 }
Example #27
0
 public RegexMatchScorable(Regex regex, IScorable <IResolver, InnerScore> inner)
     : base(inner)
 {
     SetField.NotNull(out this.regex, nameof(regex), regex);
 }
Example #28
0
 public WhereScoreScorable(IScorable <Item, Score> scorable, Func <Item, Score, bool> predicate)
     : base(scorable)
 {
     SetField.NotNull(out this.predicate, nameof(predicate), predicate);
 }
 public LuisIntentScorable(ILuisService service, ILuisModel model, LuisIntentAttribute intent, IScorable <IResolver, InnerScore> inner)
     : base(inner)
 {
     SetField.NotNull(out this.service, nameof(service), service);
     SetField.NotNull(out this.model, nameof(model), model);
     SetField.NotNull(out this.intent, nameof(intent), intent);
 }
Example #30
0
 public SelectItemScorable(IScorable <InnerItem, Score> scorable, Func <OuterItem, InnerItem> selector)
 {
     SetField.NotNull(out this.scorable, nameof(scorable), scorable);
     SetField.NotNull(out this.selector, nameof(selector), selector);
 }