예제 #1
0
        private async void PushNewRecord(PlayHistoryDto playHistoryDto)
        {
            var seats = _tableSeatRepository.GetAll().Where(x => x.TableId == playHistoryDto.TableId).ToList();

            if (seats != null)
            {
                var connections = seats.Select(x => x.DeviceConnectionId).ToList();
                await _hubContext.Clients.Clients(connections).SendAsync("newRecord", playHistoryDto);
            }
        }
예제 #2
0
        public LiquidationResultDto GetPlayerHistory(string cardId)
        {
            var card = Repository.Get(cardId);

            var data = _dbContext.PlayHistoryDetailPlayers
                       .Include(x => x.PlayHistoryDetail)
                       .ThenInclude(x => x.PlayHistory)
                       .Where(x => x.PlayerCardId == cardId && x.PlayerType != PlayerTypesEnum.戥脚 && x.PlayHistoryDetail.PlayHistory.IsPlaying == true)
                       .OrderBy(x => x.Id)
                       .ToList();

            var histories = new List <PlayHistoryDto>();

            foreach (var phdp in data)
            {
                var history = histories.FirstOrDefault(x => x.TableId == phdp.PlayHistoryDetail.PlayHistory.TableId && x.Round == phdp.PlayHistoryDetail.PlayHistory.Round);
                if (history == null)
                {
                    history = new PlayHistoryDto()
                    {
                        TableId            = phdp.PlayHistoryDetail.PlayHistory.TableId,
                        Round              = phdp.PlayHistoryDetail.PlayHistory.Round,
                        PlayHistoryDetails = new List <PlayHistoryDetailDto>()
                    };
                    histories.Add(history);
                }
                history.PlayHistoryDetails.Add(new PlayHistoryDetailDto()
                {
                    MohjongActionName = phdp.PlayHistoryDetail.MohjongActionName,
                    Players           = _objectMapper.Map <List <PlayHistoryDetailPlayerDto> >(phdp.PlayHistoryDetail.Players),
                    OperatorCardId    = phdp.PlayHistoryDetail.OperatorCardId,
                    CreationTime      = phdp.PlayHistoryDetail.CreationTime.AddHours(8)
                });
            }


            return(new LiquidationResultDto()
            {
                Win = data.Count(x => x.IsWinner),
                Lose = data.Count(x => x.IsLoser),
                Total = card.Total,
                CheckinTime = histories.First().PlayHistoryDetails.First().CreationTime,
                CheckoutTime = histories.Last().PlayHistoryDetails.Last().CreationTime,
                Histories = histories
            });
        }
예제 #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();
        }