def scoreboard(offset): teams = Team.query.filter_by(active=True, is_practice=False).all() teams = sorted(teams, key=cmp_to_key(util.compare_teams)) stage = app.load_stage() params = { 'title': 'Scoreboard', 'stage': stage, 'offset': offset, 'end': len(teams) } if app.config['LEGO_APP_TYPE'] in ('bristol', 'uk'): template = 'scoreboard_{!s}.html'.format(app.config['LEGO_APP_TYPE']) else: raise Exception('Unsupported value for LEGO_APP_TYPE: {!s}' \ .format(app.config['LEGO_APP_TYPE'])) stages = ('round_1', 'round_2', 'quarter_final', 'semi_final', 'final') for i, s in enumerate(stages): if stage >= i: params[s] = True if stage == 0: if app.config['LEGO_APP_TYPE'] == 'bristol': quotient = len(teams) // 3 remainder = len(teams) % 3 # remainder == 0 if not remainder: params['first'] = teams[:quotient] params['second'] = teams[quotient:(quotient * 2)] params['third'] = teams[(quotient * 2):] elif remainder == 1: params['first'] = teams[:(quotient + 1)] params['second'] = teams[quotient + 1:(quotient * 2) + 1] params['third'] = teams[(quotient * 2) + 1:] # remainder == 2 else: params['first'] = teams[:(quotient)] params['second'] = teams[quotient:(quotient * 2) + 1] params['third'] = teams[(quotient * 2) + 1:] return render_template(template, **params) else: params['teams'] = teams[offset:offset + 10] return render_template(template, **params) # force offset to 0 if we refreshed in the middle of a cycle if params['offset'] != 0: return redirect(url_for('scoreboard')) if app.config['LEGO_APP_TYPE'] == 'bristol': params['first'] = teams else: params['teams'] = teams params['no_pagination'] = True return render_template(template, **params)
def set_score(self, score): stage = app.load_stage() app.logger.debug('Stage: %s', stage) app.logger.debug('Team: %s', str(self.__dict__)) # score is a tuple holding the total score and a breakdown of all previous scores score_total, score_breakdown = score # first round if stage == 0: if self.attempt_1 is None: self.attempt_1 = score_total self.attempt_1_breakdown = score_breakdown elif self.attempt_2 is None: self.attempt_2 = score_total self.attempt_2_breakdown = score_breakdown elif self.attempt_3 is None: self.attempt_3 = score_total self.attempt_3_breakdown = score_breakdown else: raise Exception('All attempts have been made for this stage.') # second round elif stage == 1: if self.round_2 is None: self.round_2 = score_total self.round_2_breakdown = score_breakdown else: raise Exception('All attempts have been made for this stage.') # quarter finals elif stage == 2: if self.quarter is None: self.quarter = score_total self.quarter_breakdown = score_breakdown else: raise Exception('All attempts have been made for this stage.') # semi finals elif stage == 3: if self.semi is None: self.semi = score_total self.semi_breakdown = score_breakdown else: raise Exception('All attempts have been made for this stage.') # finals elif stage == 4: if self.final is None: self.final = score_total self.final_breakdown = score_breakdown else: raise Exception('All attempts have been made for this stage.') else: raise Exception('Invalid value for stage.')
def __gt__(self, other): stage = app.load_stage() if stage == 4: if (self.final or -1) > (other.final or -1): return True if (self.final or -1) < (other.final or -1): return False if stage >= 3: if (self.semi or -1) > (other.semi or -1): return True if (self.semi or -1) < (other.semi or -1): return False if stage >= 2: if (self.quarter or -1) > (other.quarter or -1): return True if (self.quarter or -1) < (other.quarter or -1): return False if stage >= 1: if (self.round_2 or -1) > (other.round_2 or -1): return True if (self.round_2 or -1) < (other.round_2 or -1): return False if stage >= 0: s_attempt = [a if a is not None else -1 for a in self.attempts] o_attempt = [a if a is not None else -1 for a in other.attempts] s_attempt.sort(reverse=True) o_attempt.sort(reverse=True) for s, o in zip(s_attempt, o_attempt): if s > o: return True if s < o: return False # we want to order by score highest to lowest # but if we fall back to this, we order by lowest number to highest if self.number < other.number: return True return False
def highest_score(self): stage = app.load_stage() if stage == 0: return max(self.attempt_1 or 0, self.attempt_2 or 0, self.attempt_3 or 0) if stage == 1: return self.round_2 or 0 if stage == 2: return self.quarter or 0 if stage == 3: return self.semi or 0 if stage == 4: return self.final or 0
def top_ten(): teams = Team.query.filter_by(is_practice=False).all() teams = sorted(teams, key=cmp_to_key(util.compare_teams)) teams = teams[0:10] stage = app.load_stage() params = { 'title': 'Scoreboard', 'stage': stage, 'teams': teams, } template = 'top_ten.html' stages = ('round_1', 'round_2', 'quarter_final', 'semi_final', 'final') for i, s in enumerate(stages): if stage >= i: params[s] = True return render_template(template, **params)
def admin_stage(): ''' For moving the stage forward. ''' if not current_user.is_admin: return abort(403) stages = ('First Round', 'Second Round (UK Final only)', 'Quarter Final', 'Semi Final', 'Final') stage = app.load_stage() current_stage = stages[stage] form = StageForm() if form.validate_on_submit(): new_stage = int(form.stage.data) cur_file_path = os.path.dirname(os.path.abspath(__file__)) if new_stage <= stage: flash('Unable to go back a stage.') elif app.config['LEGO_APP_TYPE'] == 'bristol' and new_stage == 1: flash('Round 2 only available during UK Final.') else: set_active_teams(new_stage) with open(os.path.join(cur_file_path, 'tmp', '.stage'), 'w') as fh: fh.write(str(new_stage)) flash('Stage updated to: {!s}'.format(stages[int(new_stage)])) return redirect(url_for('admin_stage')) return render_template('admin/stage.html', title='Manage Stage', form=form, current_stage=current_stage)