예제 #1
0
        public static IChallenge ToEntity(this ChallengeModel model)
        {
            var challenge = new Challenge(
                ChallengeId.FromGuid(model.Id),
                new ChallengeName(model.Name),
                Game.FromValue(model.Game),
                new BestOf(model.BestOf),
                new Entries(model.Entries),
                new ChallengeTimeline(new DateTimeProvider(model.Timeline.CreatedAt), new ChallengeDuration(TimeSpan.FromTicks(model.Timeline.Duration))),
                model.ScoringItems.ToEntity());

            foreach (var participant in model.Participants.Select(participant => participant.ToEntity()))
            {
                challenge.Register(participant);
            }

            if (model.Timeline.StartedAt.HasValue)
            {
                challenge.Start(new DateTimeProvider(model.Timeline.StartedAt.Value));
            }

            if (model.SynchronizedAt.HasValue)
            {
                challenge.Synchronize(new DateTimeProvider(model.SynchronizedAt.Value));
            }

            if (model.Timeline.ClosedAt.HasValue)
            {
                challenge.Close(new DateTimeProvider(model.Timeline.ClosedAt.Value));
            }

            challenge.ClearDomainEvents();

            return(challenge);
        }
예제 #2
0
        public void Close_DomainEvents_ShouldHaveCountOfParticipantWithScore(Dictionary <UserId, decimal?> scoreboard)
        {
            // Arrange
            var factory  = new ChallengePayoutFactory();
            var instance = factory.CreateInstance();

            var challenge = new Challenge(
                new ChallengeId(),
                instance.GetChallengePayout(new ChallengePayoutEntries(scoreboard.Count / 2), new EntryFee(100, CurrencyType.Money)));

            var challengeScoreboard = new ChallengeScoreboard(challenge.Payout, scoreboard);
            var participantCount    = scoreboard.Count(participant => participant.Value != null);

            // Act
            challenge.Close(challengeScoreboard);

            // Assert
            challenge.DomainEvents.Where(domainEvent => domainEvent is ChallengeParticipantPayoutDomainEvent).Should().HaveCount(participantCount);
        }
예제 #3
0
        public IChallenge FakeChallenge(ChallengeModel model)
        {
            var challengeFaker = new ChallengeFaker();

            challengeFaker.CustomInstantiator(
                faker =>
            {
                faker.User().Reset();

                var name = new ChallengeName(model.Name);

                var game = Game.FromValue(model.Game);

                var entries = new Entries(model.Entries);

                var bestOf = new BestOf(model.BestOf);

                var duration = new ChallengeDuration(TimeSpan.FromTicks(model.Timeline.Duration));

                var state = ChallengeState.FromValue(model.State);

                var utcNowDate = DateTime.UtcNow.Date;

                var createdAt = faker.Date.Recent(1, utcNowDate);

                var startedAt = faker.Date.Between(createdAt, utcNowDate);

                var endedAt = startedAt + duration;

                var synchronizationBuffer = endedAt + TimeSpan.FromHours(2);

                var closedAt = faker.Date.Soon(1, synchronizationBuffer);

                var synchronizedAt = faker.Date.Between(synchronizationBuffer, closedAt);

                var timeline = new ChallengeTimeline(new DateTimeProvider(startedAt), duration);

                var scoring = new Scoring
                {
                    [new StatName("StatName1")] = new StatWeighting(0.00015F),
                    [new StatName("StatName2")] = new StatWeighting(1),
                    [new StatName("StatName3")] = new StatWeighting(0.77F),
                    [new StatName("StatName4")] = new StatWeighting(100),
                    [new StatName("StatName5")] = new StatWeighting(-3)
                };

                var challenge = new Challenge(
                    model.Id.ConvertTo <ChallengeId>(),
                    name,
                    game,
                    bestOf,
                    entries,
                    timeline,
                    scoring);

                var participantFaker = new ParticipantFaker(game, createdAt, startedAt);

                participantFaker.UseSeed(faker.Random.Int());

                var participants = participantFaker.Generate(this.ParticipantCount(state, challenge.Entries));

                participants.ForEach(participant => challenge.Register(participant));

                if (state != ChallengeState.Inscription)
                {
                    challenge.Start(new DateTimeProvider(startedAt));

                    participants.ForEach(
                        participant =>
                    {
                        var matchFaker = new MatchFaker(challenge.Scoring, synchronizedAt);

                        matchFaker.UseSeed(faker.Random.Int());

                        var matches = matchFaker.Generate(this.MatchCount(state, challenge.BestOf));

                        participant.Snapshot(matches, new DateTimeProvider(synchronizedAt));
                    });

                    challenge.Synchronize(new DateTimeProvider(synchronizedAt));

                    if (state == ChallengeState.Ended || state == ChallengeState.Closed)
                    {
                        challenge.Start(new DateTimeProvider(startedAt - duration));
                    }

                    if (state == ChallengeState.Closed)
                    {
                        challenge.Close(new DateTimeProvider(closedAt));
                    }
                }

                return(challenge);
            });

            return(challengeFaker.Generate());
        }