def __init__(self, name, matches_input, matches_result, time_begin, time_end): """Init turn class.""" self.name = name self.matches = [] self.time_begin = time_begin self.time_end = time_end for match, result in zip(matches_input, matches_result): self.matches.append(Match(match, result))
def creer_tour(self, indice_de_tour): """ Méthode permettant de créer un tour """ nom = f"Round {indice_de_tour + 1}" dico = { 'nom': nom, 'date_heure_debut': util.encode_date_heure(datetime.now()), 'date_heure_fin': '9999-12-31T12:00:00' } tour = Tour(**dico) tour.liste_de_matchs = [ Match(*paire_de_joueurs) for paire_de_joueurs in self.generer_paires_de_joueurs(indice_de_tour) ] self._liste_de_tours.append(tour) for match in tour.liste_de_matchs: cle = f"{match.paire_de_joueurs[0].id} " cle += f"{match.paire_de_joueurs[1].id}" self._matchs_deja_joues[cle] = 1
def detect_faces(self): headers = { 'Content-Type': 'application/octet-stream', 'Ocp-Apim-Subscription-Key': AzureConfig.KEY, } params = urllib.urlencode({ 'returnFaceId': 'true', 'returnFaceLandmarks': 'false', }) # Sending a post request to obtain all of the face ids and face rectangles in the image image = open(self.path, 'rb') response = requests.post(Image.DETECT_URL % params, data=image, headers=headers) # Constructing all of the image's matches' matches = [] for match in response.json(): matches.append(Match(match['faceId'], match['faceRectangle'], self)) return matches
def matches_post(): post_json = request.get_json() new_match = Match(game_id=post_json["game_id"], date=post_json["date"]) db.session.add(new_match) db.session.commit() if "winner_ids" in post_json.keys(): for winner_id in post_json["winner_ids"]: new_play = Play( match_id=new_match.match_id, player_id=winner_id, did_win=True ) db.session.add(new_play) db.session.commit() if "non_winner_ids" in post_json.keys(): for non_winner_id in post_json["non_winner_ids"]: new_play = Play( match_id=new_match.match_id, player_id=non_winner_id, did_win=False ) db.session.add(new_play) db.session.commit() return jsonify(match_dict(new_match)), 201
def generate(self, seeding=None): """ generate the bracket """ # find all registered bots in the given class bots = Bot.get_by_weightclass_registered(self.weightclass_code, self.event_id, order="id") # defines the order for matches to spread out the byes better, probably a formula for this but didn't take time to figure it out match_ordering = { 2: [1, 2], 4: [1, 3, 2, 4], 8: [1, 8, 5, 4, 3, 6, 7, 2], 16: [1, 16, 9, 8, 5, 12, 13, 4, 3, 14, 11, 6, 7, 10, 15, 2] } # need at least 2 bots to generate a chart if (len(bots) < 2): return False # manual seeding, if passed in (is dumb, assumes # of seeds matches # of bots) if seeding: for i, bot_id in enumerate(seeding): bot = Bot.get_by_id(bot_id) bot.seed_number = i bot.bracket_id = self.id bot.put() else: # generate a random array for the seeding seeds = range(0, len(bots)) shuffle(seeds) # assign the seeds for i, bot in enumerate(bots): bot.seed_number = seeds[i] bot.bracket_id = self.id bot.put() # generate matches if self.format_code.upper() != 'ROUNDROBIN': chart_size = 2 # find the first power of 2 higher than the number of bots in the bracket, this is our chart size num_rounds = 1 while (len(bots) > chart_size): chart_size *= 2 num_rounds += 1 # create first round matches, do our best to avoid a team fighting itself first round # will regenerate up to 5 times until it gives up on avoiding first round team fighting self for _ in xrange(0, 5): matches = [] for i in xrange(0, chart_size / 2): bot1_seed = i bot2_seed = chart_size - 1 - i bot1 = Bot.get_by_bracket_seed(self.event_id, self.id, bot1_seed) bot2 = Bot.get_by_bracket_seed(self.event_id, self.id, bot2_seed) bot1_id = bot1.id bot2_id = bot2.id if bot2 else 0 match = Match(number=match_ordering[chart_size / 2][i], bracket_id=self.id, bracket_side="A", round="A", bot1_id=bot1_id, bot2_id=bot2_id) matches.append(match) conflict = False for match in matches: if match.bot1_id > 0 and match.bot2_id > 0: bot1 = Bot.get_by_id(match.bot1_id) bot2 = Bot.get_by_id(match.bot2_id) if bot1.team_name == bot2.team_name: conflict = True break if not conflict: break [match.put() for match in matches] # create the rest of the A side matches, one round at a time for i in xrange(2, num_rounds + 1): round_letter = chr(63 + (2 * i)) #A,C,E etc for j in xrange(1, chart_size / pow(2, i) + 1): bot1_source = 'W%s%d' % (chr(63 + (2 * i) - 2), (j * 2 - 1) ) # ie WA1 (Winner of A1) bot2_source = 'W%s%d' % (chr(63 + (2 * i) - 2), (j * 2)) match = Match(number=j, bracket_id=self.id, bracket_side="A", round=round_letter, bot1_source_match=bot1_source, bot2_source_match=bot2_source) match.put() # generate B side matches, if necessary if self.format_code.upper( ) == 'DOUBLEELIM' or self.format_code.upper() == 'DOUBLETRUE': num_b_rounds = num_rounds * 2 - 1 for i in xrange(2, num_b_rounds + 1): round_letter = chr(62 + (2 * i)) round_size = int(chart_size / pow(2, math.floor( (i + 2) / 2))) for j in xrange(1, round_size + 1): if i == 2: # only case where a loser moves into bot1 spot bot1_source = 'LA%d' % (j * 2 - 1) bot2_source = 'LA%d' % (j * 2) else: if i % 2 == 1: # means this round's bot2 is sourced from A side # losing source bots need to be from opposite side of chart as to prevent rematches bot1_source = 'W%s%d' % (chr(60 + (2 * i)), j) bot2_source = 'L%s' % (chr(64 + i)) # match order depends how far into B side we are # 3 possibilities: normal, reverse, half shift if i % 7 == 0: # normal bot2_source = '%s%d' % (bot2_source, j) elif i % 5 == 0: # half shift if j < round_size / 2: bot2_source = '%s%d' % ( bot2_source, math.ceil((round_size / 2) + j)) else: bot2_source = '%s%d' % ( bot2_source, math.ceil((0 - (round_size / 2)) + j)) else: # reverse bot2_source = '%s%d' % (bot2_source, round_size + 1 - j) else: bot1_source = 'W%s%d' % (chr(60 + (2 * i)), (j * 2 - 1)) bot2_source = 'W%s%d' % (chr(60 + (2 * 1)), (j * 2)) match = Match(number=j, bracket_id=self.id, bracket_side="B", round=round_letter, bot1_source_match=bot1_source, bot2_source_match=bot2_source) match.put() # insert final A-side match round_letter = chr(63 + (2 * (num_rounds + 1))) bot1_source = 'W%s1' % chr(63 + (2 * num_rounds)) bot2_source = 'W%s1' % chr(60 + (2 * (num_b_rounds + 1))) match = Match(number=1, bracket_id=self.id, bracket_side="A", round=round_letter, bot1_source_match=bot1_source, bot2_source_match=bot2_source) match.put() matches_to_update = Match.get_by_bracket_round(self.id, 'A') for match in matches_to_update: match.check() else: #ROUNDROBIN bot_index = 1 # start at 1 because bot0 can't fight bot0 etc for bot in bots: match_index = 1 for i in xrange(bot_index, len(bots)): match = Match(number=match_index, round=chr(64 + bot_index), bracket_id=self.id, bracket_side="A", bot1_id=bot.id, bot2_id=bots[i].id) match.put() match_index += 1 bot_index += 1 return True
import app.aggregators.team_goals as team_goals_aggregator from app.helpers.aggregation import combine_aggregations from app.models.match import Match from collections import Counter from tests.testing_helper import get_empty_match match1 = Match(get_empty_match()) match2 = Match(get_empty_match()) def test_both_teams_score(): match1.homeTeamGoals = 2 match1.awayTeamGoals = 3 match2.homeTeamGoals = 0 match2.awayTeamGoals = 1 final_aggregation = compute_both_teams_aggregation(match1, match2) assert final_aggregation['gg'] == 1 def test_one_team_did_not_score(): match1.homeTeamGoals = 0 match1.awayTeamGoals = 3 match2.homeTeamGoals = 0 match2.awayTeamGoals = 1 final_aggregation = compute_both_teams_aggregation(match1, match2) assert final_aggregation['ng'] == 2
from tests.testing_helper import get_empty_match from app.models.match import Match from app.constants.outcome import Outcome import app.helpers.outcome as oh match = Match(get_empty_match()) """Tests if expected outcome is equal to actual outcome""" def test_if_outcome_comparison_is_correct(): match.finalOutcome = Outcome.HOME_WIN assert oh.compare(match.finalOutcome, Outcome.HOME_WIN) == 1 def test_if_outcome_comparison_is_not_correct(): match.finalOutcome = Outcome.HOME_WIN assert oh.compare(match.finalOutcome, Outcome.AWAY_WIN) == 0 """Test if double chance matches the expected outcomes""" def test_if_double_chance_comparison_is_correct(): match.finalOutcome = Outcome.HOME_WIN assert oh.double_chance(match.finalOutcome, Outcome.HOME_WIN, Outcome.DRAW) == 1 def test_if_double_chance_comparison_is_not_correct(): match.finalOutcome = Outcome.AWAY_WIN assert oh.double_chance(match.finalOutcome, Outcome.HOME_WIN, Outcome.DRAW) == 0
from collections import Counter import app.aggregators.mixed as mixed_games_aggregator from app.constants.outcome import Outcome from app.helpers.aggregation import combine_aggregations from app.models.match import Match from tests.testing_helper import get_empty_match match1 = Match(get_empty_match()) match2 = Match(get_empty_match()) match3 = Match(get_empty_match()) match4 = Match(get_empty_match()) match5 = Match(get_empty_match()) match6 = Match(get_empty_match()) def test_double_win(): match1.outcome1stHalf = Outcome.HOME_WIN match1.outcome2ndHalf = Outcome.HOME_WIN match2.outcome1stHalf = Outcome.AWAY_WIN match2.outcome2ndHalf = Outcome.AWAY_WIN match3.outcome1stHalf = Outcome.AWAY_WIN match3.outcome2ndHalf = Outcome.AWAY_WIN final_aggregation = compute_aggregation(match1, match2, match3) assert final_aggregation['double-win-t1'] == 1 assert final_aggregation['double-win-t2'] == 2
from tests.testing_helper import get_empty_match from app.models.match import Match import app.helpers.goals as gh actual_match = Match(get_empty_match()) """Goals sum""" def test_goal_sum_is_equal_or_greater_than_expected_sum(): actual_match.homeTeamGoals = 2 actual_match.awayTeamGoals = 1 match_goals_sum = actual_match.homeTeamGoals + actual_match.awayTeamGoals assert gh.compare_sums(match_goals_sum, 3) == 1 def test_goal_sum_is_not_equal_or_greater_than_expected_sum(): actual_match.homeTeamGoals = 0 actual_match.awayTeamGoals = 1 match_goals_sum = actual_match.homeTeamGoals + actual_match.awayTeamGoals assert gh.compare_sums(match_goals_sum, 2) == 0 """Goals sum exact""" def test_goal_sum_is_exact_the_expected_sum(): actual_match.homeTeamGoals = 2 actual_match.awayTeamGoals = 1 match_goals_sum = actual_match.homeTeamGoals + actual_match.awayTeamGoals assert gh.compare_sums(match_goals_sum, 3, is_exact=True) == 1