Exemplo n.º 1
0
        public static async Task<ContentResult> Add(SocialGamificationAssetContext db, Match match, Actor actor)
        {
            // Add actors to this match
            var matchActor = new MatchActor { MatchId = match.Id, ActorId = actor.Id };

            db.MatchActors.Add(matchActor);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                return HttpResponseHelper.ErrorContentResult(e.Message, StatusCodes.Status500InternalServerError);
            }

            for (var i = 1; i <= match.TotalRounds; ++i)
            {
                // Add round(s) entry for each Actor
                var matchRound = new MatchRound { MatchActorId = matchActor.Id, RoundNumber = i };

                db.MatchRounds.Add(matchRound);

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbEntityValidationException e)
                {
                    return HttpResponseHelper.ErrorContentResult(e.Message, StatusCodes.Status500InternalServerError);
                }
            }

            return null;
        }
        private async Task<QuickMatchResult> QuickMatch(QuickMatch quickMatch, IEnumerable<Group> groups)
        {
            Tournament tournament;
            ContentResult error;
            if (quickMatch.Tournament.HasValue && quickMatch.Tournament != Guid.Empty)
            {
                tournament = await _context.Tournaments.FindAsync(quickMatch.Tournament);
                if (tournament == null)
                {
                    return new QuickMatchResult
                    {
                        match = null,
                        error = HttpResponseHelper.BadRequest("Invalid Tournament.")
                    };
                }
            }
            else
            {
                tournament = new Tournament { OwnerId = session.Player.Id };

                _context.Tournaments.Add(tournament);

                error = await SaveChangesAsync();
                if (error != null)
                {
                    return new QuickMatchResult { match = null, error = error };
                }
            }

            // Create Match
            var match = new Match { TournamentId = tournament.Id, TotalRounds = quickMatch.Rounds };

            _context.Matches.Add(match);

            error = await SaveChangesAsync();
            if (error != null)
            {
                return new QuickMatchResult { match = match, error = error };
            }

            foreach (var actor in groups)
            {
                error = await MatchActor.Add(_context, match, actor);
                if (error != null)
                {
                    return new QuickMatchResult { match = match, error = error };
                }
            }

            return new QuickMatchResult { match = match, error = null };
        }