Пример #1
0
        public HubResponse CreateDraft(string name, int roundLimits, bool requireWatchers)
        {
            var user = _db.Users.Find(Context.User.GetUserId());

            //Should never happen, but an additional check just in case
            var broadcaster = _db.Broadcasters.FirstOrDefault(b => b.Name == user.TwitchUsername);

            if (broadcaster == null)
            {
                throw new Exception("Sorry, this feature is only for broadcasters on twitch.tv (With a valid mtgbot.tv client application).");
            }

            var newCubeDraft = new CubeDraft
            {
                BroadcasterId   = broadcaster.Id,
                Created         = DateTime.Now,
                Name            = name,
                RoundTime       = roundLimits,
                RequireWatchers = requireWatchers
            };

            try
            {
                _db.CubeDrafts.Add(newCubeDraft);
                _db.SaveChanges();
            }
            catch (DbEntityValidationException)
            {
                return(HubResponse.ErrorResponse("There was a problem saving this cube draft. If this problem persists, please contact the developer."));
            }

            string username = null;


            try
            {
                username = BotService.StartCubeDraft(newCubeDraft.Id);
            }
            catch (Exception ex)
            {
                Log.Error("Error calling BotService.StartCubeDraft(" + newCubeDraft.Id + ")", ex);
            }

            if (username == null)
            {
                return(HubResponse.ErrorResponse("Unable to create draft. There is no bot available to support this draft. Please try again later."));
            }

            Groups.Add(Context.ConnectionId, String.Format("draft/{0}/clients", newCubeDraft.Id));
            return(HubResponse.SuccessResponse(new {
                Username = username,
                DraftId = newCubeDraft.Id
            }));
        }
Пример #2
0
        public HubResponse PairNextRound(int draftId)
        {
            try
            {
                var user = _db.Users.Find(Context.User.GetUserId());

                var draft = _db.CubeDrafts
                            .Include("Broadcaster").Single(c => c.Id == draftId);

                _draftService.EnsureBroadcaster(draft, user);

                Log.DebugFormat("Attempting to pair next round for draftId='{0}'", draftId);
                var tournament = GetTournament(draftId);
                //TODO: Remove
                //tournament.Matches.Clear();
                //tournament.CurrentRound = 0;
                var pairings = tournament.PairNextRound();

                Log.DebugFormat("Parings {0}", String.Join(", ", pairings));

                foreach (var match in pairings)
                {
                    match.ToDB(_db, draftId);
                }

                try
                {
                    BotService.NotifyPairings(draftId, pairings);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return
                        (HubResponse.ErrorResponse("There was a problem notifiying players of the next round. Please try again later."));
                }

                Clients.Group(String.Format("draft/{0}/clients", draftId))
                .newPairings(tournament.CurrentRound, pairings);

                return(HubResponse.SuccessResponse());
            }
            catch (InvalidOperationException ex)
            {
                return(HubResponse.ErrorResponse(ex.Message));
            }
            catch (Exception ex)
            {
                return(HubResponse.ErrorResponse("An unhandled exception occurred: " + ex.Message));
            }
        }
Пример #3
0
        public HubResponse EndTournament(int draftId)
        {
            var user = _db.Users.Find(Context.User.GetUserId());

            var draft = _db.CubeDrafts
                        .Include("Broadcaster").Single(c => c.Id == draftId);

            _draftService.EndTournament(draft, user);

            Clients.Group(String.Format("draft/{0}/clients", draftId)).statusUpdate(CubeDraftStatus.ProductHandIn);

            BotService.UpdateCubeStatus(draft.Id);

            return(HubResponse.SuccessResponse());
        }