public PositioningItem[] Rank(int actualPlayersCount) { var playersCount = CalculatePlayersCount(actualPlayersCount); var codes = new Queue<string>(); for (var i = 0; i < actualPlayersCount ; i++) { codes.Enqueue("Q" + (i + 1)); } if (actualPlayersCount < playersCount) { for (var i = 0; i < (playersCount - actualPlayersCount); i++) { codes.Enqueue("BYE"); } } var items = new List<PositioningItem>(playersCount); var skip = playersCount / 8; var first = new List<int>(); var second = new List<int>(); var third = new List<int>(); for (var i = 0; i < playersCount; i += skip) { items[i] = new PositioningItem() { Index = i, Code = codes.Dequeue() }; first.Add(i); second.Add(i + 2); second.Add(i + 4); third.Add(i + 3); third.Add(i + 5); } var oddPlaces = new List<int>(); for (var i = skip - 1; i < playersCount; i += skip) { oddPlaces.Add(i); } first.AddRange(oddPlaces); var randomizer = new RandomPositionGenerator(oddPlaces.ToArray()); while (randomizer.CanTake()) { var i = randomizer.Take(); items[i] = new PositioningItem() { Index = i, Code = codes.Dequeue() }; } randomizer = new RandomPositionGenerator(second.ToArray()); while (randomizer.CanTake()) { var i = randomizer.Take(); items[i] = new PositioningItem() { Index = i, Code = codes.Dequeue() }; } randomizer = new RandomPositionGenerator(third.ToArray()); while (randomizer.CanTake()) { var i = randomizer.Take(); items[i] = new PositioningItem() { Index = i, Code = codes.Dequeue() }; } var available = Enumerable.Range(0, playersCount).Except(first).Except(second).Except(third); randomizer = new RandomPositionGenerator(available.ToArray()); while (randomizer.CanTake()) { var i = randomizer.Take(); items[i] = new PositioningItem() { Index = i, Code = codes.Dequeue() }; } //for (int i = 0; i < playersCount; i++) //{ // var item = new PositioningItem() {Index = i}; // if (i%2 == 0) // { // item.Code = codes.Dequeue(); // } // items.Add(item); //} //var empty = items.Where(i => string.IsNullOrEmpty(i.Code)); //var randomizer = new RandomPositionGenerator(empty.Select(i=>i.Index).ToArray()); //while (randomizer.CanTake()) //{ // items[randomizer.Take()].Code = codes.Dequeue(); //} return items.ToArray(); }
public PositioningItem[] Rank(int actualPlayersCount, int qPlayers) { var playersCount = PlayersCountCalculator.CalculatePlayersCount(actualPlayersCount); var rankedPlayers = playersCount/4; var items = new List<PositioningItem>(playersCount); for (int i = 0; i < playersCount; i++) { items.Add(new PositioningItem() { Index = i }); } var codes = new Queue<string>(); for (var i = 0; i < actualPlayersCount - qPlayers; i++) { codes.Enqueue("MD" + (i + 1)); } for (int i = 0; i < qPlayers; i++) { codes.Enqueue("Q" + (i + 1)); } if (actualPlayersCount < playersCount) { for (var i = 0; i < (playersCount - actualPlayersCount); i++) { codes.Enqueue("BYE"); } } var vsRanked = new Stack<int>(); items[0].Code = codes.Dequeue(); items.Last().Code = codes.Dequeue(); vsRanked.Push(1); vsRanked.Push(items.Count - 2); var randomizer = default(RandomPositionGenerator); if (rankedPlayers > 2) { var p1 = items.Count / 4; var p2 = (items.Count - 1) - items.Count / 4; randomizer = new RandomPositionGenerator(new[] { p1, p2 }); items[randomizer.Take()].Code = codes.Dequeue(); items[randomizer.Take()].Code = codes.Dequeue(); vsRanked.Push(p1 + 1); vsRanked.Push(p2 - 1); } if (rankedPlayers > 4) { var p = new[] { items.Count / 2-1, items.Count/2, items.Count / 4-1, (items.Count) - items.Count/4, }; randomizer = new RandomPositionGenerator(p); items[randomizer.Take()].Code = codes.Dequeue(); items[randomizer.Take()].Code = codes.Dequeue(); items[randomizer.Take()].Code = codes.Dequeue(); items[randomizer.Take()].Code = codes.Dequeue(); vsRanked.Push(p[0] - 1); vsRanked.Push(p[1] + 1); vsRanked.Push(p[2] - 1); vsRanked.Push(p[3] + 1); } var emptySlots = items.Where(item => string.IsNullOrEmpty(item.Code)); var positions = emptySlots.Select(i => i.Index).Except(vsRanked).ToList(); randomizer = new RandomPositionGenerator(positions.ToArray()); while (randomizer.CanTake()) { items[randomizer.Take()].Code = codes.Dequeue(); } if (rankedPlayers > 4) { positions = new List<int>(); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); randomizer = new RandomPositionGenerator(positions.ToArray()); while (randomizer.CanTake()) { items[randomizer.Take()].Code = codes.Dequeue(); } } if (rankedPlayers > 2) { positions = new List<int>(); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); randomizer = new RandomPositionGenerator(positions.ToArray()); while (randomizer.CanTake()) { items[randomizer.Take()].Code = codes.Dequeue(); } } items[vsRanked.Pop()].Code = codes.Dequeue(); items[vsRanked.Pop()].Code = codes.Dequeue(); return items.ToArray(); }
private void PositionQualifyingDraw(MatchInfo[] matches, int playersCount, int actualPlayersCount, Queue <string> codes) { var skip = playersCount / 8; var first = new List <int>(); var second = new List <int>(); var third = new List <int>(); var against = new Stack <int>(); for (var i = 0; i < playersCount; i += skip) { PositionInMatch(matches, i, codes.Dequeue()); against.Push(i + 1); if (skip > 1) { first.Add(i); } if (skip > 4) { for (var j = 2; j < skip; j *= 2) { second.Add(i + j); third.Add(i + j + 1); } } } var oddPlaces = new List <int>(); if (codes.Count > 0) { if (skip > 2) { for (var i = skip - 1; i < playersCount; i += skip) { oddPlaces.Add(i); against.Push(i - 1); } first.AddRange(oddPlaces); var randomizer = new RandomPositionGenerator(oddPlaces.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), codes.Dequeue()); } randomizer = new RandomPositionGenerator(second.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), codes.Dequeue()); } randomizer = new RandomPositionGenerator(third.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), codes.Dequeue()); } } var available = Enumerable.Range(0, playersCount).Except(first).Except(second).Except(third); if (available.Any() && codes.Count > 0) { var remainderQueue = new Queue <string>(); var code = codes.Dequeue(); while (against.Count > 0 && code != "BYE") { against.Pop(); remainderQueue.Enqueue(code); code = codes.Dequeue(); } if (against.Count > 0) { available = available.Except(against); } if (available.Any() && remainderQueue.Count > 0) { var randomizer = new RandomPositionGenerator(available.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), remainderQueue.Dequeue()); } } while (against.Count > 0) { PositionInMatch(matches, against.Pop(), "BYE"); } } } }
private void RankPlayers(List<CompetitionPosition> positions, int[] randomPositions, List<Player> players, List<int> availablePositions, List<int> rankedPositions) { var randomizer = new RandomPositionGenerator(randomPositions); while (players.Count > 0 && randomizer.CanTake()) { var position = randomizer.Take(); RankPlayer(positions, position, players, availablePositions, rankedPositions); } }
private void PositionMainDraw(int rankedPlayers, MatchInfo[] matches, Queue <string> codes) { var playersCount = matches.Length; var vsRanked = new Stack <int>(); var taken = new List <int>(); PositionInMatch(matches, 0, codes.Dequeue()); PositionInMatch(matches, playersCount - 1, codes.Dequeue()); taken.Add(0); taken.Add(playersCount - 1); vsRanked.Push(1); vsRanked.Push(matches.Length - 2); var randomizer = default(RandomPositionGenerator); if (rankedPlayers > 2) { var p1 = playersCount / 4; var p2 = (playersCount - 1) - playersCount / 4; randomizer = new RandomPositionGenerator(new[] { p1, p2 }); taken.Add(randomizer.Take()); PositionInMatch(matches, taken.Last(), codes.Dequeue()); taken.Add(randomizer.Take()); PositionInMatch(matches, taken.Last(), codes.Dequeue()); vsRanked.Push(p1 + 1); vsRanked.Push(p2 - 1); } if (rankedPlayers > 4) { var p = new[] { playersCount / 2 - 1, playersCount / 2, playersCount / 4 - 1, (playersCount) - playersCount / 4, }; randomizer = new RandomPositionGenerator(p); taken.Add(randomizer.Take()); PositionInMatch(matches, taken.Last(), codes.Dequeue()); taken.Add(randomizer.Take()); PositionInMatch(matches, taken.Last(), codes.Dequeue()); taken.Add(randomizer.Take()); PositionInMatch(matches, taken.Last(), codes.Dequeue()); taken.Add(randomizer.Take()); PositionInMatch(matches, taken.Last(), codes.Dequeue()); vsRanked.Push(p[0] - 1); vsRanked.Push(p[1] + 1); vsRanked.Push(p[2] - 1); vsRanked.Push(p[3] + 1); } var positions = Enumerable.Range(0, playersCount).Except(vsRanked).Except(taken).ToList(); randomizer = new RandomPositionGenerator(positions.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), codes.Dequeue()); } if (rankedPlayers > 4) { positions = new List <int>(); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); randomizer = new RandomPositionGenerator(positions.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), codes.Dequeue()); } } if (rankedPlayers > 2) { positions = new List <int>(); positions.Add(vsRanked.Pop()); positions.Add(vsRanked.Pop()); randomizer = new RandomPositionGenerator(positions.ToArray()); while (randomizer.CanTake()) { PositionInMatch(matches, randomizer.Take(), codes.Dequeue()); } } PositionInMatch(matches, vsRanked.Pop(), codes.Dequeue()); PositionInMatch(matches, vsRanked.Pop(), codes.Dequeue()); }