public async Task <BaseResponse> Handle(PassRoundCommand request, CancellationToken cancellationToken)
        {
            var match = _ctx.Matches
                        .Include(x => x.MatchUsers)
                        .ThenInclude(x => x.User)
                        .FirstOrDefault(x => x.Id == request.MatchId);

            if (match == null)
            {
                return(new BaseResponse("Match not found.", false));
            }

            if (!match.CanPass(request.UserId))
            {
                return(new BaseResponse("Not allowed to pass.", false));
            }

            var user = match.GetUser(request.UserId);

            match.Videos.Add(new Video
            {
                VideoIndex = match.Videos.Count,
                UserIndex  = user.Index,
                UserId     = user.UserId,
                Empty      = true,
            });

            var host     = match.GetHost();
            var opponent = match.GetOpponent();

            host.SetGoFlagUpdatePassLock(host.CanGo, false, false);
            opponent.SetGoFlagUpdatePassLock(opponent.CanGo, false, false);

            match.Round++;

            match.Turn = match.GetTurnName();
            var finished = CopyCatHelper.CompleteCopyCat(match);

            match.LastUpdate = DateTime.Now;

            await _ctx.SaveChangesAsync(cancellationToken);

            if (finished)
            {
                _notifications.QueueNotification(
                    $"{user.User.DisplayName} passed round {match.Round}, you won!",
                    new[] { match.Id.ToString() }.DefaultJoin(),
                    NotificationMessageType.MatchHistory,
                    match.GetOtherUserIds(user.UserId));
            }
            else
            {
                _notifications.QueueNotification(
                    $"{user.User.DisplayName} passed round {match.Round}.",
                    new[] { match.Id.ToString() }.DefaultJoin(),
                    NotificationMessageType.MatchActive,
                    match.GetOtherUserIds(user.UserId));
            }

            return(new BaseResponse("Round passed.", true));
        }
Пример #2
0
        public void UpdateMatch(UpdateSettings command)
        {
            var user = _match.GetUser(command.UserId);

            _match.Videos.Add(new Video
            {
                VideoIndex = _match.Videos.Count,
                UserIndex  = user.Index,
                UserId     = user.UserId,
                VideoPath  = CdnUrlHelper.CreateVideoUrl(_routing.Cdn, _match.Id.ToString(), command.Video),
                ThumbPath  = CdnUrlHelper.CreateThumbUrl(_routing.Cdn, _match.Id.ToString(), command.Thumb)
            });

            var tempRound = _match.Round;

            var host     = _match.GetHost();
            var opponent = _match.GetOpponent();

            if (_match.Round % 2 == 1)
            {
                if (_match.IsTurn(MatchRole.Host))
                {
                    host.Points++;
                    host.SetGoFlagUpdatePassLock(false, false, true);
                    opponent.SetGoFlagUpdatePassLock(true, true, false, pass: true);
                }
                else
                {
                    opponent.Points++;
                    host.SetGoFlagUpdatePassLock(false, true, false);
                    opponent.SetGoFlagUpdatePassLock(true, false, true);
                    _match.Round++;
                }
            }
            else if (_match.IsTurn(MatchRole.Host))
            {
                host.Points++;
                host.SetGoFlagUpdatePassLock(true, false, true);
                opponent.SetGoFlagUpdatePassLock(false, true, false);
                _match.Round++;
            }
            else
            {
                host.SetGoFlagUpdatePassLock(true, true, false, pass: true);
                opponent.SetGoFlagUpdatePassLock(false, false, true);
                opponent.Points++;
            }

            if (tempRound == _match.Round)
            {
                if (IsNullOrEmpty(_match.Chain))
                {
                    _match.Chain = command.Move.Trim();
                }
                else
                {
                    _match.Chain += $"|{command.Move.Trim()}";
                }
            }

            CopyCatHelper.CompleteCopyCat(_match);

            _match.Turn       = _match.GetTurnName();
            _match.LastUpdate = DateTime.Now;
        }