def generateResults(stage = None): response = Match.all() results = [] for match in response: if match['round'] == int(stage): if match['player_a'] == str(-1) or match['player_b'] == str(-1): continue set_count = 0 w_a = 0 w_b = 0 while set_count < match['sets']: set_count += 1 result = generate_result() if result[0] > result[1]: w_a += 1 else: w_b += 1 r = Result(match_id = match['match_id'], set_id = set_count, result_a = result[0], result_b = result[1]) results.append(r.serialize()) db.session.add(r) if w_a == 3 or w_b == 3: break try: db.session.commit() except: print sys.exc_info() return prepare_response(None, "Error while inserting") return prepare_response(True)
def getMatchesInCompetition(stage = None, competitionID = None): if competitionID: response = Match.getByCompetitionID(competitionID) else: response = Match.all() all_matches = {} aux = [] if stage is not None: aux = [match for match in response if match['round'] == int(stage) or match['round'] == int(stage)+1] else: aux = response response = aux for match in response: r = match['round'] match['result'] = Result.getByMatchID(match['match_id']) if r not in all_matches.keys(): all_matches[r] = [] # match['offset'] = 2**(match['round'] - 1) - 1 # match['height'] = 2**match['round'] - 1 # match['offset'] = 2**(len(all_matches) - 1) - 1 # match['height'] = 2**len(all_matches) - 1 match['offset'] = 2**(len(all_matches) - 1) - 1 match['height'] = len(all_matches)**2 - len(all_matches) all_matches[r].append(match) return prepare_response(all_matches)
def check_results(matchID = None): if matchID is None: matches = Match.all() else: matches = Match.getById(matchID) for match in matches: match['results'] = Result.getByMatchID(match['match_id']) calculate_winner(match['player_a'], match['player_b'], match['results'], match['match_id']) return prepare_response(True)