def get_teams_report(): """ produce a compared report of TOP 24 teams by: 1. Total PPG a. Shooting PPG (with top / bottom amount) b. Crossings PPG (with which defences are over 75%) c. End-Game PPG d. Auton PPG 2. Crossing success percentage (per defense) 3. Collections per game (with floor / HP amount) 4. Defensive records """ def filter_and_sort(stats, argument, amount=24): filtered_stats = [(t, s[argument]) for (t, s) in stats] return sorted(filtered_stats, key=lambda s: s[1], reverse=True)[:24] teams_stats = dict.fromkeys(_db_get_teams()) for team in teams_stats.keys(): try: team_matches = _db_get_match_stats(team=team) stats = statsmgr.run_handlers(team_matches) except Exception, ex: return jsonify(status='ERROR', msg=traceback.format_exc()) crossing_breakdown = {defense_stats['name']: min(float(defense_stats['successful']), 2) for defense_stats in stats['breaching']} shooting_ppg = float(stats['shooting']['low']['successful']) * 3 + float(stats['shooting']['high']['successful']) * 5 crossing_ppg = sum([v * 5 for v in crossing_breakdown.values()]) auton_ppg = float(stats['auton']['reach']['percentage'][:-1]) / 100 * 2 + float(stats['auton']['cross']['percentage'][:-1]) / 100 * 10 + float(stats['auton']['score']['percentage'][:-1]) / 100 * 10 end_game_ppg = float(stats['end_game']['challenge']['percentage'][:-1]) / 100 * 5 + float(stats['end_game']['scale']['percentage'][:-1]) / 100 * 15 teams_stats[team] = dict( shooting_ppg = shooting_ppg, crossing_breakdown = crossing_breakdown, crossing_ppg = crossing_ppg, auton_ppg = auton_ppg, end_game_ppg = end_game_ppg, total_ppg = (shooting_ppg + crossing_ppg + auton_ppg + end_game_ppg), collections_pg = float(stats['collection']['amount']), defencive_records = len(stats['defences']) )
return jsonify(status='OK', teams=teams, matches=matches) @app.route('/stats/team/<int:team_number>') def get_team_stats(team_number): """ gets a team's statistics """ try: matches = _db_get_match_stats(team=team_number) except Exception, ex: return jsonify(status='ERROR', msg=traceback.format_exc()) if not matches: return jsonify(status='ERROR', team_number=team_number, msg=("No matches for team %d." % team_number)) try: stats = statsmgr.run_handlers(matches) except Exception, ex: return jsonify(status='ERROR', msg=traceback.format_exc()) return jsonify(status='OK', team=team_number, stats=stats) @app.route('/stats/id/<int:match_id>') def get_id_stats(match_id): """ gets a team's statistics """ try: match = _db_get_match_stats(id=match_id) except Exception, ex: return jsonify(status='ERROR', msg=traceback.format_exc()) if not match: return jsonify(status='ERROR', id=match_id,