Exemplo n.º 1
0
        private Drafter FindNextPlayer(Drafter drafter)
        {
            var curPick = drafter.CurrentPick - 1;

            var nextPosition = (curPick <= 15 || curPick >= 30) ? drafter.Position - 1 : drafter.Position + 1;

            if (nextPosition < 1)
            {
                nextPosition = Players.Count;
            }
            if (nextPosition > Players.Count)
            {
                nextPosition = 1;
            }

            return(Players.Single(d => d.Position == nextPosition));
        }
Exemplo n.º 2
0
        //If a draft is in progress and failed for whatever reason, get back to original state
        public void Recover()
        {
            //MyEventLog.WriteEntry("Recovery for draft id '" + _draft.Id + "' started", EventLogEntryType.Warning);

            var allPicks = _draft.CubeDraftPicks.Where(p => p.Pick == 1 || p.Pick == 16 || p.Pick == 31).Select(p => p.Picks);
            var cardIds  = _draft.CubeDraftCards.Select(c => c.CardId).ToList();

            var allCardObjects = _db.Cards.Include("CardSet").Where(c => cardIds.Contains(c.Id)).ToArray();

            foreach (var pick in allPicks.SelectMany(c => c.Split(',')))
            {
                cardIds.Remove(Convert.ToInt32(pick));
            }

            var cardObjects = _db.Cards.Include("CardSet").Where(c => cardIds.Contains(c.Id)).ToArray();
            var cards       = cardIds.Select(c => cardObjects.Single(co => co.Id == c)).Shuffle().ToArray();

            var players = _draft.CubeDraftPlayers.Where(p => p.Confirmed).ToList();

            if (players.Count < 2 || players.Count > 10)
            {
                throw new InvalidOperationException("Invalid # of players. Players must be between 2 and 10");
            }

            //if (players.Count * 45 > cards.Length)
            //    throw new InvalidOperationException("Pool is not large enough for all players");

            var packPosition = -1;

            foreach (var player in players.Shuffle())
            {
                var playerPicks       = player.CubeDraftPicks.Where(p => p.PickId != null).Select(p => p.PickId);
                var playerCardObjects = _db.Cards.Include("CardSet").Where(c => playerPicks.Contains(c.Id)).ToArray();

                var packs = new List <Pack>();

                for (var i = player.CubeDraftPicks.Count(); i <= 30; i += 15)
                {
                    packs.Add(new Pack(cards.Skip(++packPosition * 15).Take(15).ToArray(), "Cube Draft"));
                }

                var newDrafter = new Drafter(
                    _draft,
                    player,
                    player.Position,
                    new DraftCollection(packs),
                    playerCardObjects.ToList()
                    );

                //Add Queued Picks to Drafters
                var picks =
                    player.CubeDraftPicks.Where(p => p.PickId == null)
                    .OrderBy(p => p.Pick)
                    .Select(pick => pick.Picks.Split(',').Select(p => Convert.ToInt32(p))).ToArray();

                foreach (var pickIds in picks)
                {
                    newDrafter.QueuedPicks.Enqueue(pickIds.Select(c => allCardObjects.Single(co => co.Id == c)).ToList());
                }

                //TODO: Check queued picks here

                if (picks.Length > 0)
                {
                    newDrafter.FixQueuedPicks();
                }

                Players.Add(newDrafter);
            }

            var max = _draft.CubeDraftPicks.Select(c => c.Pick).DefaultIfEmpty().Max();

            _currentPack = (int)Math.Ceiling((decimal)max / 15);

            if (!Players.All(p => p.Picks.Count % 15 == 0 && p.Picks.Count > 0))
            {
                return;
            }

            //All players are done picking their cards

            //We done here?
            if (_currentPack == 3)
            {
                _draft.Status           = CubeDraftStatus.ProductHandOut;
                _db.Entry(_draft).State = EntityState.Modified;
                _db.SaveChanges();
                _hubContext.Clients.Group(String.Format("draft/{0}/clients", _draft.Id)).statusUpdate(CubeDraftStatus.ProductHandOut);
                BotService.UpdateCubeStatus(_draft.Id);
                return;
            }

            //Update the current pack, and signal to open next packs
            _currentPack++;
            Players.ForEach(p => p.OpenNextPack());

            _hubContext.Clients.Group(String.Format("draft/{0}/clients", _draft.Id)).draftStarted();
        }