def scoreboard_lisp(level):
    if not level.isdigit():
        return render_template("not_found.html")
    if int(level) not in [1, 2, 3]:
        return render_template("not_found.html")

    robots = r.get_registry()['ROBOTS'].get_all_robots()

    if not robots:
        return render_template("not_found.html")

    # add additional parameters to be displayed on scoreboard
    robots = ScoreBoard.add_scoreboard_params(robots)

    # filter robots
    filtered_robots = ScoreBoard.filter_robots_level(robots, int(level))

    # key used for sorting
    score_name = "LS" + level

    # sort based on name then this level's lowest score
    sorted_robots = sorted(list(filtered_robots), key=lambda k: k['name'])
    sorted_robots = sorted(list(sorted_robots), key=lambda k: k[score_name])

    return render_template("scoreboard_lisp.html",
                           robots=sorted_robots,
                           level=level,
                           score_name=score_name)
def scoreboard_lisp(level):
    if not level.isdigit():
        return render_template("not_found.html")
    if int(level) not in [1, 2, 3]:
        return render_template("not_found.html")

    robots = r.get_registry()['ROBOTS'].get_all_robots()

    if not robots:
        return render_template("not_found.html")

    # add additional parameters to be displayed on scoreboard
    robots = ScoreBoard.add_scoreboard_params(robots)

    # filter robots
    filtered_robots = ScoreBoard.filter_robots_level(robots, int(level))

    # key used for sorting
    score_name = "LS" + level

    # sort based on name then this level's lowest score
    sorted_robots = sorted(list(filtered_robots), key=lambda k: k['name'])
    sorted_robots = sorted(list(sorted_robots), key=lambda k: k[score_name])

    return render_template(
        "scoreboard_lisp.html",
        robots=sorted_robots,
        level=level,
        score_name=score_name
    )
def scoreboard_brd(division):
    robots = r.get_registry()['ROBOTS'].get_all_robots_division(
        division
    )

    if not robots:
        return render_template("not_found.html")

    # add additional parameters to be displayed on scoreboard
    robots = ScoreBoard.add_scoreboard_params(robots)

    # sort based on name then total score
    sorted_robots = sorted(
        list(robots),
        key=lambda k: k['name']
    )
    sorted_robots = sorted(
        list(sorted_robots),
        key=lambda k: k['TFS']
    )

    return render_template(
        "scoreboard_brd_gpmp.html",
        robots=sorted_robots,
        scoreboard_name=get_division_label(division)
    )
def prize_winners():
    robots = r.get_registry()['ROBOTS'].get_all_robots()

    # calculate additional score parameters
    robots = ScoreBoard.add_scoreboard_params(robots)

    # gpmp_winner[place] dict
    gpmp_winners = ScoreBoard.get_gpmp_winners(robots)

    # lisp_winners[level][category][place] dict
    lisp_winners = ScoreBoard.get_lisp_winners(robots)

    # brd_winners[division][category][place] dict
    brd_winners = ScoreBoard.get_brd_winners(robots)

    return render_template("prizes.html",
                           gpmp_winners=gpmp_winners,
                           lisp_winners=lisp_winners,
                           brd_winners=brd_winners)
def prize_winners():
    robots = r.get_registry()['ROBOTS'].get_all_robots()

    # calculate additional score parameters
    robots = ScoreBoard.add_scoreboard_params(robots)

    # gpmp_winner[place] dict
    gpmp_winners = ScoreBoard.get_gpmp_winners(robots)

    # lisp_winners[level][category][place] dict
    lisp_winners = ScoreBoard.get_lisp_winners(robots)

    # brd_winners[division][category][place] dict
    brd_winners = ScoreBoard.get_brd_winners(robots)

    return render_template(
        "prizes.html",
        gpmp_winners=gpmp_winners,
        lisp_winners=lisp_winners,
        brd_winners=brd_winners
    )
def scoreboard_gpmp():
    robots = r.get_registry()['ROBOTS'].get_all_robots()

    # add additional parameters to be displayed on scoreboard
    robots = ScoreBoard.add_scoreboard_params(robots)

    # sort based on name then total score
    sorted_robots = sorted(list(robots), key=lambda k: k['name'])
    sorted_robots = sorted(list(sorted_robots), key=lambda k: k['TFS'])
    for robot in sorted_robots:
        robot['completed'] = list(robot['completed'])
    return jsonify({
        'robots': sorted_robots
    })
def scoreboard_gpmp():
    robots = r.get_registry()['ROBOTS'].get_all_robots()

    if not robots:
        return render_template("not_found.html")

    # add additional parameters to be displayed on scoreboard
    robots = ScoreBoard.add_scoreboard_params(robots)

    # sort based on name then total score
    sorted_robots = sorted(list(robots), key=lambda k: k['name'])
    sorted_robots = sorted(list(sorted_robots), key=lambda k: k['TFS'])

    return render_template(
        "scoreboard_brd_gpmp.html",
        robots=sorted_robots,
        scoreboard_name="Grand Performance"
    )
def prize_winners():
    robots = r.get_registry()['ROBOTS'].get_all_robots()

    # calculate additional score parameters
    robots = ScoreBoard.add_scoreboard_params(robots)

    # gpmp_winner[place] dict
    gpmp_winners = ScoreBoard.get_gpmp_winners(robots)

    # lisp_winners[level][category][place] dict
    lisp_winners = ScoreBoard.get_lisp_winners(robots)

    # brd_winners[division][category][place] dict
    brd_winners = ScoreBoard.get_brd_winners(robots)

    divisions = ['junior', 'walking', 'high_school', 'senior']
    levels = [1,2,3]
    categories = ['unique','custom']

    # initialize csv file writer
    si = StringIO.StringIO()
    cw = csv.writer(si)

    # write GPMP winner to file
    entry = ''
    first_place = 1
    if gpmp_winners.get(first_place):
        entry += (
            gpmp_winners.get(first_place).get('name') +
            ' (' + gpmp_winners.get(first_place).get('school') + ')'
        )
    else:
        entry += 'N/A'
    cw.writerow(['Grand Performance Mastery Prize Winner (GPMP)',entry])

    cw.writerow('\n')
    cw.writerow('\n')

    # write BRD winners to file
    cw.writerow(['Best Robots in Division Prize Winners (BRD)'])
    cw.writerow(['Division', 'Unique', 'Customized'])

    for division in divisions:
        row = []
        row.append(division)
        for category in categories:
            entry = ''
            for place in [1,2,3]:
                entry += str(place) + ': '
                if brd_winners.get(division).get(category).get(place):
                    entry += (
                        brd_winners.get(division).get(category).get(place).get('name') +
                        ' (' + brd_winners.get(division).get(category).get(place).get('school') + ')'
                    )
                else:
                    entry += 'N\A'
                entry += '      '

            row.append(entry)
        cw.writerow(row)

    cw.writerow('\n')
    cw.writerow('\n')

    # write LISP winners to file
    cw.writerow(['Lowest Individual Score Prize Winners (LISP)'])
    cw.writerow(['Level', 'Unique', 'Customized'])

    for level in levels:
        row = []
        row.append('L' + str(level))
        for category in categories:
            entry = ''
            for place in [1]:
                entry += str(place) + ': '
                if lisp_winners.get(level).get(category).get(place):
                    entry += (
                        lisp_winners.get(level).get(category).get(place).get('name') +
                        ' (' + lisp_winners.get(level).get(category).get(place).get('school') + ')'
                    )
                else:
                    entry += 'N\A'
                entry += '      '

            row.append(entry)
        cw.writerow(row)

    output = make_response(si.getvalue())
    si.close()
    output.headers['Content-Disposition'] = 'attachment; filename=prizes.csv'
    output.headers['Content-type'] = 'text/csv'
    return output