public async Task <DomainResult> Handle(UpdateRouteCommand command, CancellationToken cancellationToken)
        {
            var origin = await _pointRepository.FindAsync(command.OriginPointId);

            var destination = await _pointRepository.FindAsync(command.DestinationPointId);

            if (origin is null || destination is null)
            {
                return(DomainResult.Failure <string>("Origin or Destination not found"));
            }

            var route = await _routeRepository.FindAsync(command.Id);

            if (route is null)
            {
                return(DomainResult.Failure <string>("Route not found"));
            }

            var arePointsChanged = route.ArePointsChanged(origin, destination);

            route.Update(origin, destination);

            if (arePointsChanged && await _routeRepository.AlreadyExistsAsync(x => route.IsTheSame(x)))
            {
                return(DomainResult.Failure <string>("Route already exists", HttpStatusCode.Conflict));
            }

            await _routeRepository.UpdateAsync(route);

            await _mediator.Publish(new RouteUpdatedEvent(route));

            return(DomainResult.Ok());
        }
Esempio n. 2
0
        public DomainResult BuyPlayer(string playerTypeId)
        {
            var playerBuyConfig = AllowedPlayers.FirstOrDefault(ap => ap.PlayerTypeId.Equals(playerTypeId));

            if (playerBuyConfig == null)
            {
                return(DomainResult.Error(new CanNotUsePlayerInThisRaceError(playerTypeId)));
            }

            var amountOfPlayerTypeToBuy = Players.Count(p => p.PlayerTypeId.Equals(playerTypeId));

            if (amountOfPlayerTypeToBuy >= playerBuyConfig.MaximumPlayers)
            {
                return(DomainResult.Error(new TeamFullError(playerBuyConfig.MaximumPlayers)));
            }

            if (playerBuyConfig.Cost.MoreThan(TeamMoney))
            {
                return(DomainResult.Error(new FewMoneyInTeamChestError(playerBuyConfig.Cost.Value, TeamMoney.Value)));
            }

            var newTeamMoney     = TeamMoney.Minus(playerBuyConfig.Cost);
            var orderedPositions = Players.Select(p => p.PlayerPositionNumber).OrderBy(p => p).ToList();
            var travers          = FindFirstFreeNumber(orderedPositions, 1);

            var playerBought = _teamState.BoughtEvent(TeamId, playerTypeId, travers, Guid.NewGuid(),
                                                      newTeamMoney);

            Apply(new List <IDomainEvent> {
                playerBought
            });
            return(DomainResult.Ok(playerBought));
        }
Esempio n. 3
0
        public DomainResult ChooseSkill(SkillReadModel newSkill)
        {
            if (!FreeSkillPoints.Any())
            {
                return(DomainResult.Error(new NoLevelUpsAvailable()));
            }
            if (CurrentSkills.Any(s => s.SkillId == newSkill.SkillId))
            {
                return(DomainResult.Error(
                           new CanNotPickSkillTwice(CurrentSkills.Select(s => s.SkillId))));
            }

            foreach (var freeSkillPoint in FreeSkillPoints)
            {
                if (!HasPlayerFreeSkillForChosenSkill(newSkill, freeSkillPoint))
                {
                    return(DomainResult.Error(new SkillNotPickable(freeSkillPoint)));
                }
            }

            var newFreeSkills = GetMinimalSkillToRemove(FreeSkillPoints, newSkill);

            var domainEvent = new SkillChosen(PlayerId, newSkill, newFreeSkills);

            Apply(domainEvent);
            return(DomainResult.Ok(domainEvent));
        }
Esempio n. 4
0
        public DomainResult ProgressMatch(PlayerProgression playerProgression)
        {
            if (!_isStarted)
            {
                return(DomainResult.Error(new MatchDidNotStartYet()));
            }
            if (_isFinished)
            {
                return(DomainResult.Error(new MatchAllreadyFinished()));
            }

            var playerIsNotInHomeOrGuestTeam =
                !HomeTeamPlayers.Contains(playerProgression.PlayerId) &&
                !GuestTeamPlayers.Contains(playerProgression.PlayerId);

            if (playerIsNotInHomeOrGuestTeam)
            {
                return(DomainResult.Error(new PlayerWasNotPartOfTheTeamWhenStartingTheMatch(playerProgression.PlayerId)));
            }

            var playerProgressions = PlayerProgressions.Append(playerProgression);
            var gameResult         = CreateGameResult(playerProgressions);

            var matchResultUploaded = new MatchProgressed(MatchId, playerProgression, gameResult);

            return(DomainResult.Ok(matchResultUploaded));
        }
Esempio n. 5
0
        public async Task <DomainResult <ObjectId> > Handle(CreateConnectionCommand command, CancellationToken cancellationToken)
        {
            var origin = await _pointRepository.FindAsync(command.OriginPointId);

            var destination = await _pointRepository.FindAsync(command.DestinationPointId);

            if (origin is null || destination is null)
            {
                return(DomainResult.Failure <ObjectId>("Origin or Destination not found"));
            }

            var connection = new Connection(origin, destination, command.Time, command.Cost);

            var alreadyExists = await _connectionRepository.AlreadyExistsAsync(x => connection.IsTheSame(x));

            if (alreadyExists)
            {
                return(DomainResult.Failure <ObjectId>("Connection already exists.", HttpStatusCode.Conflict));
            }

            await _connectionRepository.CreateAsync(connection);

            await _mediator.Publish(new ConnectionCreatedEvent(connection));

            return(DomainResult.Ok(connection.Id));
        }
Esempio n. 6
0
        public DomainResult Pass()
        {
            var newStarPlayerPoints = StarPlayerPoints + 1;
            var domainEvents        = CreateLevelUpEvents(new PlayerPassed(PlayerId, newStarPlayerPoints), newStarPlayerPoints);

            Apply(domainEvents);
            return(DomainResult.Ok(domainEvents));
        }
Esempio n. 7
0
        public DomainResult Move()
        {
            var newStarPlayerPoints = StarPlayerPoints + 3;
            var domainEvents        = CreateLevelUpEvents(new PlayerMadeTouchdown(PlayerId, newStarPlayerPoints), newStarPlayerPoints);

            Apply(domainEvents);
            return(DomainResult.Ok(domainEvents));
        }
Esempio n. 8
0
        public DomainResult NominateForMostValuablePlayer()
        {
            var newStarPlayerPoints = StarPlayerPoints + 5;
            var domainEvents        = CreateLevelUpEvents(new PlayerWasNominatedMostValuablePlayer(PlayerId, newStarPlayerPoints), newStarPlayerPoints);

            Apply(domainEvents);
            return(DomainResult.Ok(domainEvents));
        }
 public DomainResult ChangeId(string identity)
 {
     if (identity == null)
     {
         return(DomainResult.Error(new CanNotUSeNullId()));
     }
     return(DomainResult.Ok(new IdChanged(identity)));
 }
Esempio n. 10
0
        public DomainResult Block()
        {
            var newStarPlayerPoints = StarPlayerPoints + 2;
            var domainEvents        = CreateLevelUpEvents(new PlayerMadeCasualty(PlayerId, newStarPlayerPoints), newStarPlayerPoints);

            Apply(domainEvents);
            return(DomainResult.Ok(domainEvents));
        }
Esempio n. 11
0
        public DomainResult RemovePlayer(Guid playerId)
        {
            var playerReadModel = Players.Single(p => p.PlayerId == playerId);
            var playerBuyConfig = AllowedPlayers.Single(ap => ap.PlayerTypeId.Equals(playerReadModel.PlayerTypeId));
            var newTeamMoney    = TeamMoney.Plus(playerBuyConfig.Cost);

            var playerRemovedFromDraft = new PlayerRemoved(TeamId, playerId, newTeamMoney);

            Apply(playerRemovedFromDraft);
            return(DomainResult.Ok(playerRemovedFromDraft));
        }
Esempio n. 12
0
        public static DomainResult Create(
            Guid matchId,
            TeamReadModel teamAtHome,
            TeamReadModel teamAsGuest)
        {
            if (teamAtHome.TeamId == teamAsGuest.TeamId)
            {
                return(DomainResult.Error(new TeamsCanNotBeTheSame(teamAtHome.TeamId, teamAsGuest.TeamId)));
            }

            return(DomainResult.Ok(new MatchCreated(matchId, teamAtHome.TeamId, teamAsGuest.TeamId)));
        }
Esempio n. 13
0
 public DomainResult AddTeam(TeamReadModel teamId)
 {
     if (SeasonIsStarted)
     {
         return(DomainResult.Error(new SeasonAllreadyStarted()));
     }
     if (Teams.Any(t => t == teamId.TeamId))
     {
         return(DomainResult.Error(new CanNotAddTeamTwice(teamId.TeamId)));
     }
     return(DomainResult.Ok(new TeamAddedToSeason(SeasonId, teamId.TeamId)));
 }
Esempio n. 14
0
        public DomainResult RegisterLevelUpSkillPointRoll(FreeSkillPoint freeSkillPoint)
        {
            if (ChoosableSkillPoints < 1)
            {
                return(DomainResult.Error(new PlayerIsNotLeveledUpAndHasNoFreeSkillPoints()));
            }

            var playerLevelUpPossibilitiesChosen = new PlayerLevelUpPossibilitiesChosen(PlayerId, freeSkillPoint);

            Apply(playerLevelUpPossibilitiesChosen);
            return(DomainResult.Ok(playerLevelUpPossibilitiesChosen));
        }
Esempio n. 15
0
        public async Task <DomainResult <string> > Handle(CreateUserSessionCommand command, CancellationToken cancellationToken)
        {
            var user = await _accountRepository.GetUserByEmailAndPassword(command.Email, command.Password);

            if (user is null)
            {
                return(DomainResult.Failure <string>("User not found", HttpStatusCode.Unauthorized));
            }

            var token = _jwtAuthService.CreateJwtToken(user);

            return(DomainResult.Ok(token));
        }
Esempio n. 16
0
 public static DomainResult Draft(
     string raceId,
     string teamName,
     string trainerName,
     IEnumerable <AllowedPlayer> allowedPlayers)
 {
     return(DomainResult.Ok(new TeamDraftCreated(
                                Guid.NewGuid(),
                                raceId,
                                teamName,
                                trainerName,
                                allowedPlayers,
                                new GoldCoins(1000000))));
 }
        public DomainResult <BestRoutePath> FindBestPath(Route route)
        {
            var path      = _dijkstra.FindBestPath(route.Origin.Id, route.Destination.Id);
            var wayPoints = FindWayPoints(path, route.Origin, route.Destination);

            if (wayPoints.Any() is false || path.Any() is false)
            {
                return(DomainResult.Failure <BestRoutePath>("Theres is no exist a valid path for this route"));
            }

            var weight = path.Last().Distance;
            var result = new BestRoutePath(route.Id, route.Origin, route.Destination, wayPoints, weight);

            return(DomainResult.Ok(result));
        }
Esempio n. 18
0
        public static DomainResult Create(
            Guid playerId,
            Guid teamId,
            int playerPositionNumber,
            AllowedPlayer allowedPlayer)
        {
            var playerConfig = new PlayerConfig(
                allowedPlayer.PlayerTypeId,
                allowedPlayer.PlayerStats,
                allowedPlayer.StartingSkills,
                allowedPlayer.SkillsOnDefault,
                allowedPlayer.SkillsOnDouble);
            var playerCreated = new PlayerCreated(playerId, teamId, playerPositionNumber, playerConfig);

            return(DomainResult.Ok(playerCreated));
        }
        public async Task <DomainResult> Handle(InactiveRouteCommand command, CancellationToken cancellationToken)
        {
            var route = await _routeRepository.FindAsync(command.Id);

            if (route is null)
            {
                return(DomainResult.Failure <string>("Route not found"));
            }

            route.Inactive();

            await _routeRepository.UpdateAsync(route);

            await _mediator.Publish(new RouteInactivatedEvent(route));

            return(DomainResult.Ok());
        }
        public async Task <DomainResult> Handle(InactivePointCommand command, CancellationToken cancellationToken)
        {
            var point = await _pointRepository.FindAsync(command.Id);

            if (point is null)
            {
                return(DomainResult.Failure <string>("Point not found"));
            }

            point.Inactive();

            await _pointRepository.UpdateAsync(point);

            await _mediator.Publish(new PointInactivatedEvent(point));

            return(DomainResult.Ok());
        }
Esempio n. 21
0
        public DomainResult Finish()
        {
            if (!_isStarted)
            {
                return(DomainResult.Error(new MatchDidNotStartYet()));
            }
            if (_isFinished)
            {
                return(DomainResult.Error(new MatchAllreadyFinished()));
            }

            var gameResult = CreateGameResult(PlayerProgressions);

            var matchResultUploaded = new MatchFinished(MatchId, PlayerProgressions, gameResult);

            return(DomainResult.Ok(matchResultUploaded));
        }
        public async Task <DomainResult <ObjectId> > Handle(CreatePointCommand command, CancellationToken cancellationToken)
        {
            var point = Point.Create(command);

            var alreadyExists = await _pointRepository.AlreadyExistsAsync(x => x.IsTheSame(point));

            if (alreadyExists)
            {
                return(DomainResult.Failure <ObjectId>("Point already exists", HttpStatusCode.Conflict));
            }

            await _pointRepository.CreateAsync(point);

            await _mediator.Publish(new PointCreatedEvent(point));

            return(DomainResult.Ok(point.Id));
        }
Esempio n. 23
0
        public async Task <DomainResult> Handle(InactiveConnectionCommand command, CancellationToken cancellationToken)
        {
            var connection = await _connectionRepository.FindAsync(command.Id);

            if (connection is null)
            {
                return(DomainResult.Failure <string>("Connection not found"));
            }

            connection.Inactive();

            await _connectionRepository.UpdateAsync(connection);

            await _mediator.Publish(new ConnectionInactivatedEvent(connection));

            return(DomainResult.Ok());
        }
Esempio n. 24
0
        public DomainResult CommitDraft()
        {
            if (Players.Count() < 11)
            {
                return(DomainResult.Error(new TeamDoesNeedMorePlayersToFinish(Players.Count())));
            }

            var domainEvents = new List <IDomainEvent>();

            domainEvents.Add(new TeamCreated(TeamId, RaceId, TeamName, TrainerName, AllowedPlayers, TeamMoney));
            domainEvents.AddRange(Players.Select(player => new PlayerBought(
                                                     TeamId,
                                                     player.PlayerTypeId,
                                                     player.PlayerPositionNumber,
                                                     player.PlayerId,
                                                     TeamMoney)));

            Apply(domainEvents);

            return(DomainResult.Ok(domainEvents));
        }
Esempio n. 25
0
        public async Task <DomainResult> Handle(UpdatedConnectionCommand command, CancellationToken cancellationToken)
        {
            var origin = await _pointRepository.FindAsync(command.OriginPointId);

            var destination = await _pointRepository.FindAsync(command.DestinationPointId);

            if (origin is null || destination is null)
            {
                return(DomainResult.Failure <string>("Origin or Destination not found"));
            }

            if (origin.Equals(destination))
            {
                return(DomainResult.Failure <string>("You can't create a connection to the same origin and destination"));
            }

            var connection = await _connectionRepository.FindAsync(command.Id);


            if (connection is null)
            {
                return(DomainResult.Failure <string>("Connection not found"));
            }

            var arePointsChanged = connection.ArePointsChanged(origin, destination);

            connection.Update(origin, destination, command.Time, command.Cost);

            if (arePointsChanged && await _connectionRepository.AlreadyExistsAsync(x => connection.IsTheSame(x)))
            {
                return(DomainResult.Failure <string>("Connection already exists.", HttpStatusCode.Conflict));
            }

            await _connectionRepository.UpdateAsync(connection);

            await _mediator.Publish(new ConnectionUpdatedEvent(connection));

            return(DomainResult.Ok());
        }
Esempio n. 26
0
        public DomainResult StartSeason()
        {
            if (Teams.Count() < 2)
            {
                return(DomainResult.Error(new SeasonMustHaveAtLeast2Teams(Teams.Count())));
            }
            if (TeamCountIsUneven())
            {
                return(DomainResult.Error(new CanNotStartSeasonWithUnevenTeamCount(Teams.Count())));
            }
            if (SeasonIsStarted)
            {
                return(DomainResult.Error(new CanNotStartSeasonASecondTime()));
            }

            var matchPairingService = new MatchPairingService();
            var gameDays            = matchPairingService.ComputePairings(Teams).ToList();

            var seasonStarted = new SeasonStarted(SeasonId, gameDays, DateTimeOffset.UtcNow);

            return(DomainResult.Ok(seasonStarted));
        }
        public async Task <DomainResult> Handle(UpdatePointCommand command, CancellationToken cancellationToken)
        {
            var point = await _pointRepository.FindAsync(command.Id);

            if (point is null)
            {
                return(DomainResult.Failure <string>("Point not found"));
            }

            point.Update(command);

            var alreadyExists = await _pointRepository.AlreadyExistsAsync(x => x.IsTheSame(point));

            if (alreadyExists)
            {
                return(DomainResult.Failure <string>("Point already exists", HttpStatusCode.Conflict));
            }

            await _pointRepository.UpdateAsync(point);

            await _mediator.Publish(new PointUpdatedEvent(point));

            return(DomainResult.Ok());
        }
Esempio n. 28
0
 public static DomainResult Create(Guid identity)
 {
     return(DomainResult.Ok(new EntityCreated(identity)));
 }
Esempio n. 29
0
        public DomainResult Start(TeamReadModel teamAtHome, TeamReadModel teamAsGuest)
        {
            var matchStarted = new MatchStarted(MatchId, teamAtHome.Players, teamAsGuest.Players);

            return(DomainResult.Ok(matchStarted));
        }
Esempio n. 30
0
 public static DomainResult Create(string seasonName)
 {
     return(DomainResult.Ok(new SeasonCreated(Guid.NewGuid(), seasonName, DateTimeOffset.UtcNow)));
 }