/// <summary> /// Creates and starts a new draft /// </summary> /// <param name="name">the name which will identify the draft</param> /// <param name="decklist">the number of each card and its data that will be used to populate the packs</param> /// <param name="participantNames">the names fo the participants</param> /// <param name="packSize">how many cards will initially fill each pack</param> /// <param name="numRounds">the number of rounds in the draft</param> private Draft(string name, Dictionary <CardData, int> decklist, List <string> participantNames, int packSize, int numRounds) { _name = name; _decklist = decklist; _created = DateTime.Now; _cardList = new List <Card>(); _cardLookup = new Dictionary <string, Card>(); //create all cards and populate _cardLookup foreach (var kvp in decklist) { for (int i = 0; i < kvp.Value; i++) { //the unique ID is just {database ID}{draft name}{copy number} string uniqueID = kvp.Key.DBID + _name + i.ToString(); Card c = new Card(kvp.Key, uniqueID); _cardList.Add(c); _cardLookup[uniqueID] = c; } } //shuffle cards using draftname as the seed - it is not properly random so that if something goes wrong, a draft can be restarted and the players can re-pick the same cards Random rng = new Random(name.GetHashCode()); int n = _cardList.Count; while (n > 1) { n--; int k = rng.Next(n + 1); Card v = _cardList[k]; _cardList[k] = _cardList[n]; _cardList[n] = v; } //create participants _participants = new LinkedList <Participant>(participantNames.Select(pname => new Participant(pname, this))); foreach (Participant p in _participants) { _waitingPacks[p] = new Queue <Pack>(_participants.Count); } NumRoundsRemaining = numRounds; //since we want the draft to start with passing to the right, we set this to false, so that when the first round starts, it will be set to true PassRight = false; //num packs will be num rounds * num participants int numPacks = numRounds * _participants.Count; _remainingPacks = new Queue <Pack>(numPacks); //enqueue packs to _remainingPacks and associate the card list to each pack in _cardLists for (int packIndex = 0; packIndex < numPacks; packIndex++) { List <Card> currPackList = AllCards.GetRange(packIndex * packSize, packSize); RemainingPacks.Enqueue(new Pack(currPackList)); } //start the round StartNewRound(); }
private void StartNewRound() { lock (_newRoundLock) { //if there are no more rounds, end the draft if (NumRoundsRemaining == 0) { EndDraft(); } else { //toggle passign rules PassRight = !PassRight; //populate current packs foreach (Participant p in Participants) { SetParticipantCurrentPack(p, RemainingPacks.Dequeue()); } //decrement remaining rounds NumRoundsRemaining--; BroadCastNewRound(); } } }