def main(roundNr):
    changeToUserDirectory()
    if 'turnierName' not in session or not os.path.exists(session['turnierName']):
        return flask.redirect(flask.url_for('new'))

    changeToTurnierDirectory(session['turnierName'])

    turnier = Turnier(roundNr)
    spieler = turnier.getSpieler()
    ranking = spieler.getRanking()
    rankedSpieler = [sub[0] for sub in ranking]

    currentRound = turnier.getLastRound()

    begegnungen = '!'.join(currentRound.getUnfinishedBegegnungenFlat())

    currentRoundNumber = currentRound.getNumberOfRound()
    if 'expertMode' in session and session['expertMode'] or currentRoundNumber <= 0:
        textToEdit = Turnier.getDefiningTextFor(currentRoundNumber)
    else:
        textToEdit = False

    return render_template('ranking.html', ranking=ranking, runde=currentRoundNumber,
                           currentRound=currentRound, isLastRound=turnier.allRoundsWhereReadIn(),
                           canExport=turnier.xmlResultCanBeCreated() and currentRound.isComplete(),
                           thereAreFreilose=spieler.freiloseNeeded(),
                           spielerList=rankedSpieler,
                           begegnungen=begegnungen, text=textToEdit)
def edit(roundNumber):
    changeToTurnierDirectory(session['turnierName'])
    if not Turnier.roundExists(roundNumber):
        flash("Die Runde {} kann nicht editiert werden!".format(roundNumber), 'info')
        return flask.redirect(flask.url_for('main'))

    if request.method == 'POST':
        if request.form['action'] == 'Löschen':
            Turnier.remove(roundNumber)
        else:
            textToWrite = request.form['text']
            Turnier.writeRound(roundNumber, textToWrite)

        return flask.redirect(flask.url_for('main'))

    return render_template('edit.html', runde=roundNumber,
                           text=Turnier.getDefiningTextFor(roundNumber))
def editSingle(roundNumber, a, b):
    changeToTurnierDirectory(session['turnierName'])
    result = '{}:{}  {} {} {} {} {}'.format(request.form['setWon'], request.form['setLost'],
                                            request.form['set1'], request.form['set2'], request.form['set3'],
                                            request.form['set4'], request.form['set5'])
    result = result.strip(' :')
    wholeRoundDef = Turnier.getDefiningTextFor(roundNumber)
    # Vertausche Spieler
    aMatchResult = ttSchweizer.parseMatchResult(result, '{} <> {}'.format(a,b), result, roundNumber)
    if aMatchResult:
        mt = aMatchResult.turned()
        resultTurned = '{}:{} '.format(mt.gamesWonByPlayerA, mt.gamesWonByPlayerB)
        resultTurned += ' '.join(mt.gamePoints)
    else:
        return flask.redirect(flask.url_for('main'))

    for p1, p2, res in ((a,b,result),(b,a,resultTurned)):
        wholeRoundDef = re.sub('{a}\s*<>\s*{b}\s*!.*'.format(a=p1, b=p2),
                           '{} <> {} ! {}'.format(p1, p2, res),
                           wholeRoundDef)

    Turnier.writeRound(roundNumber, wholeRoundDef)

    return flask.redirect(flask.url_for('main'))