コード例 #1
0
        public void Validate_OK()
        {
            var dut = new CreateActionDtoValidator();
            var validCreateActionDto = new CreateActionDto
            {
                Title       = "ActionTitle",
                Description = "ActionDescription",
                DueTimeUtc  = new DateTime(2020, 1, 1, 1, 1, 1, DateTimeKind.Utc)
            };

            var result = dut.Validate(validCreateActionDto);

            Assert.IsTrue(result.IsValid);
        }
コード例 #2
0
        public void Fail_WhenDescriptionIsTooLong()
        {
            var dut = new CreateActionDtoValidator();

            var inValidCreateActionDto = new CreateActionDto
            {
                Title       = "ActionTitle",
                Description = new string('x', Action.DescriptionLengthMax + 1),
                DueTimeUtc  = new DateTime(2020, 1, 1, 1, 1, 1, DateTimeKind.Utc)
            };

            var result = dut.Validate(inValidCreateActionDto);

            Assert.IsFalse(result.IsValid);
        }
コード例 #3
0
        public void Create(CreateActionDto input)
        {
            var isValidAction = ActionsEnum.All.Contains(input.MahjongActionName);

            if (!isValidAction)
            {
                throw new UserFriendlyException("Invalid action.");
            }

            var tableExist = _tableRepository.GetAll().Any(x => x.Id == input.TableId);

            if (!tableExist)
            {
                throw new UserFriendlyException("Invalid table id.");
            }

            var isValidOperator = _cardRepository.GetAll().Any(x => x.Id == input.OperatorCardId && x.CardType == CardTypes.Staff);

            if (!isValidOperator)
            {
                throw new UserFriendlyException("Invalid operator id.");
            }

            var table = _tableRepository.GetAllIncluding(x => x.Seats).FirstOrDefault(x => x.Id == input.TableId);

            var playHistory = _playHistoryRepository.GetAll().FirstOrDefault(x => x.TableId == input.TableId && x.Round == table.Round && x.IsPlaying == true);

            var isNewRound = playHistory == null;

            var playerEntities = new List <PlayHistoryDetailPlayer>();

            var relatedSeats = table.Seats.Where(x => input.Players.Any(m => m.Position == x.Position)).ToList();

            foreach (var seat in relatedSeats)
            {
                var player = new PlayHistoryDetailPlayer()
                {
                    PlayerCardId = seat.PlayerCardId,
                    PlayerType   = seat.PlayerType,
                    Position     = seat.Position,
                    StaffCardId  = seat.StaffCardId,
                    WinOrLose    = input.Players.FirstOrDefault(x => x.Position == seat.Position).WinOrLose,
                    Bonus        = input.Players.FirstOrDefault(x => x.Position == seat.Position).Bonus ?? 1
                };
                playerEntities.Add(player);
            }


            if (isNewRound)
            {
                playHistory = new PlayHistory()
                {
                    IsPlaying = true,
                    TableId   = table.Id,
                    Round     = table.Round
                };

                _playHistoryRepository.Insert(playHistory);
                CurrentUnitOfWork.SaveChanges();
            }

            var newPlayHistoryDetail = new PlayHistoryDetail()
            {
                OperatorCardId    = input.OperatorCardId,
                MohjongActionName = input.MahjongActionName,
                Players           = playerEntities,
                PlayHistoryId     = playHistory.Id
            };

            _playHistoryDetailRepository.Insert(newPlayHistoryDetail);

            var actionDto      = _objectMapper.Map <PlayHistoryDetailDto>(newPlayHistoryDetail);
            var playHistoryDto = new PlayHistoryDto()
            {
                TableId            = table.Id,
                Round              = table.Round,
                PlayHistoryDetails = new List <PlayHistoryDetailDto>()
                {
                    actionDto
                }
            };

            //結束當前回合的標誌Action
            if (ActionsEnum.RoundEndActions.Contains(input.MahjongActionName))
            {
                table.Round += 1;
            }

            PushNewRecord(playHistoryDto);

            foreach (var seat in relatedSeats)
            {
                var player = playerEntities.FirstOrDefault(x => x.Position == seat.Position);


                var winners = playerEntities.Where(x => x.WinOrLose == "Win").Count();
                var amount  = EvaluateWinOrLoseAmount(input.MahjongActionName, table, player.WinOrLose, winners);

                if (player.PlayerType == PlayerTypesEnum.戥脚)
                {
                    var staffCardId = string.IsNullOrEmpty(seat.StaffCardId) ? seat.PlayerCardId : seat.StaffCardId;
                    var staffCard   = _cardRepository.Get(staffCardId);
                    staffCard.Total += amount;
                }
                else
                {
                    var playerCard = _cardRepository.Get(player.PlayerCardId);
                    playerCard.Total += amount;

                    if (player.IsWinner)
                    {
                        //計算Commission
                        var commission = EvaluateCommission(input.MahjongActionName, table, player.Bonus);

                        playerCard.Commission += commission;

                        PushCommission(table.Id, player.Position, playerCard.Commission);
                    }
                }

                //計算代打輸贏
                if (seat.PlayerType == PlayerTypesEnum.代打)
                {
                    if (ActionsEnum.RoundEndActions.Contains(input.MahjongActionName))
                    {
                        seat.Round += 1;
                    }

                    seat.HelpPlayAmount += amount;

                    PushHelpPlayInfo(table.Id, seat.Position, seat.Round, seat.HelpPlayAmount);
                }
            }

            CurrentUnitOfWork.SaveChanges();
        }