예제 #1
0
        public Duel CreateDuel(string userId, string image, string photoId, int challengeId)
        {
            var user = _dbService.ById <User>(userId, false);

            // load current duel if it exists
            var currentDuel = _dbService.Collection <Duel>().FirstOrDefault(Duel.IsCurrentDuelOf(userId));

            if (currentDuel != null && !currentDuel.IsPublic)
            {
                throw new InvalidOperationException("У вас уже есть текущая дуэль");
            }
            if (photoId.Length > 300)
            {
                throw new ArgumentException("PhotoId is too long");
            }
            if (!_socialService.CheckImageUrl(image))
            {
                throw new ArgumentException("Image url is invalid");
            }
            if (!_contentService.HasChallengeId(challengeId))
            {
                throw new ArgumentException("Wrong challengeId");
            }
            if (!user.ChallengeIds.Contains(challengeId))
            {
                throw new ArgumentException("This challengeId was not assigned to this user");
            }

            // create new duel object
            var duel = new Duel
            {
                ChallengeId = challengeId,
                Creator     = new Duellist
                {
                    Image   = image,
                    PhotoId = photoId,
                    Time    = Utils.Now(),
                    User    = user.ToMeta(),
                    Voters  = new List <UserMeta>()
                },
                Opponent   = null,
                Status     = DuelStatus.Created,
                IsPublic   = false,
                TimeStart  = 0,
                TimeFinish = 0,
                Id         = Utils.RandomString(8)
            };

            // write to database
            _dbService.Update(duel);

            // return
            return(duel);
        }