# Untitled

a guest
Dec 30th, 2020
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def generateTeamsService(players):
2.     if (len(players) < 2):
3.         return {
4.             'error': 'Too few players signed up.'
5.         }
6.
7.     # Get the "percent rank" for each player passed in
8.     subquery = db.session.query(
10.         func.percent_rank().over(
11.             order_by=Trueskillrating.rating.desc()
12.         ).label('pct-rnk'),
13.     ).filter_by(matchtype='pickup-fortress1').subquery()
14.     query = db.session.query(subquery).filter(
15.         func.lower(subquery.c.username).in_([p.lower() for p in players])
16.     )
17.     percent_ranked_ratings = query.all()
18.
19.     s_tiers, a_tiers, b_tiers, c_tiers = ([] for i in range(4))
20.
21.     s_tier = .1 # top 10% of rankings
22.     a_tier = .25 # 10-25%
23.     b_tier = .6 # 25-60%
24.     c_tier = 1 # 60-100
25.
26.     # Shuffle the players so that players within a tier are in a random order. We will sort by tier next.
27.     random.shuffle(percent_ranked_ratings)
28.
29.     for rating in percent_ranked_ratings:
30.         percent_rank = rating[1]
31.         if (percent_rank <= s_tier):
32.             s_tiers.append(rating)
33.         elif (percent_rank <= a_tier):
34.             a_tiers.append(rating)
35.         elif (percent_rank <= b_tier):
36.             b_tiers.append(rating)
37.         elif (percent_rank <= c_tier):
38.             c_tiers.append(rating)
39.
40.     # Separate s, a, and b tier players for selecting captain, if we have enough from those tiers.
41.     semi_sorted_ratings_sab = s_tiers + a_tiers + b_tiers
42.     semi_sorted_ratings_all = s_tiers + a_tiers + b_tiers + c_tiers
43.
44.     # If we have enough players in s, a and b tiers, randomly choose captains from there
45.     if (len(semi_sorted_ratings_sab) > 3):
46.         captain_1_index = random.randrange(0, len(semi_sorted_ratings_sab), 2)
47.         captain_2_index = captain_1_index + 1
50.     else:
51.         captain_1_index = random.randrange(0, len(semi_sorted_ratings_all), 2)
52.         captain_2_index = captain_1_index + 1
55.
56.     team_2 = []
57.     team_1 = []
58.     counter = 2
59.
60.     # Assign players to teams using abbaabb... approach.
61.     while (len(semi_sorted_ratings_all) > 0):
62.         if (counter <= 2):
64.         elif (counter <= 4):
66.         elif (counter == 5):
67.             counter = 0
68.         counter += 1
69.
70.     # Figure out which team our captians are in since we assigned teams after choosing captains.
71.     if captain_1 in team_1:
72.         team_1_captain = captain_1
73.         team_2_captain = captain_2
74.     else:
75.         team_1_captain = captain_2
76.         team_2_captain = captain_1
77.
78.     return {
79.         'team_1': {
80.             'captain': team_1_captain,
81.             'players': team_1
82.         },
83.         'team_2': {
84.             'captain': team_2_captain,
85.             'players': team_2
86.         }
87.     }
RAW Paste Data