def create_phase_battles(phase_id): phase = Phase.query.get(phase_id) songs = shuffled(phase.songs) for battle_songs in partition(songs, 4): if len(battle_songs) == 4: create_battle(phase, battle_songs) else: extend_battles(phase.battles, battle_songs) db.session.add(phase) flash('Bitwy dla fazy {} zostały utworzone.'.format(phase_id), 'success') return redirect(url_for('phases.read_phase_battles', phase_id = phase_id))
def finish_battle(battle_id): winners_count = int(request.form["winners"]) song_ids = request.form.getlist("songs") song_points = request.form.getlist("points") songs = sorted(shuffled(zip(song_ids, song_points)), key=itemgetter(1), reverse=True) winner_song_ids = list(map(songs[:winners_count], itemgetter(0))) battle = Battle.query.get(battle_id) battle.finish() db.session.add(battle) winner_songs = Song.query.filter(Song.id.in_(winner_song_ids)).all() next_phase = battle.phase.next_phase next_phase.songs.extend(winner_songs) db.session.add(next_phase) flash("Bitwa została zakończona.", "success") return redirect(url_for("battles.read_battle", battle_id=battle_id))