-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
120 lines (96 loc) · 4.02 KB
/
app.py
File metadata and controls
120 lines (96 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from flask import Flask, render_template, jsonify, request, make_response
from datetime import datetime, timedelta
import tbapy
app = Flask(__name__)
tba = tbapy.TBA("ZF8araneSrIcZkQilOAmdvm7ULbcyOcQrBKfan0Yh3AghfoXuPgBDqxlRZ4Jv2Kv")
year = datetime.now().strftime("%Y")
def sort_matches(matches):
return sorted(matches, key=lambda k: k["predicted_time"])
def get_next_and_previous_match(matches):
previous_match = ""
next_predicted_match = ""
for match in matches:
if match["actual_time"]:
previous_match = match["key"]
else:
next_predicted_match = match["key"]
break
if not previous_match:
return None, next_predicted_match
if not next_predicted_match:
return previous_match, None
return previous_match, next_predicted_match
def check_next_match(matches):
for match in matches:
if not match["actual_time"]:
return True
return False
def get_team_alliance(match, team_number):
for team in match["alliances"]["blue"]["team_keys"]:
if f"frc{team_number}" == team:
return "blue"
for team in match["alliances"]["red"]["team_keys"]:
if f"frc{team_number}" == team:
return "red"
return None
@app.route("/")
def home():
if not request.cookies.get("credits"):
resp = make_response(render_template("main.html", credit_mode="On"))
resp.set_cookie("credits", "On")
return resp
return render_template("main.html", credit_mode=request.cookies.get("credits"))
@app.route("/credits/<string:mode>")
def set_credits(mode):
resp = make_response("")
resp.set_cookie("credits", mode)
return resp
@app.route("/<int:team_number>/")
def team_select(team_number):
team_object = tba.team(team_number)
if "Errors" in team_object:
return render_template("errors/team_missing.html")
return render_template("event_select.html", events=tba.team_events(team=team_number, year=year), team=team_number)
@app.route("/<int:team_number>/event/<string:event>/")
def view_team(team_number, event):
matches = tba.team_matches(team_number, event=event, year=year, simple=True)
if not matches:
return render_template("errors/no_matches_seeded.html", team=team_number)
if not check_next_match(matches):
return render_template("errors/current_no_matches.html", team=team_number)
event_object = tba.event(event=event)
team_object = tba.team(team_number)
return render_template("viewer.html", team=team_object, event=event_object)
@app.route("/<int:team_number>/event/<string:event>/next_previous_match/")
def next_match_info(team_number, event):
matches = sort_matches(tba.team_matches(team_number, event=event, year=year, simple=True))
previous_key, next_key = get_next_and_previous_match(matches)
standings = tba.team_status(team_number, event)
if previous_key:
previous_match = tba.match(key=previous_key)
previous_match["our_team_alliance"] = get_team_alliance(previous_match, team_number)
if next_key:
next_match = tba.match(key=next_key)
next_match["delay"] = next_match["predicted_time"] - datetime.now().timestamp()
next_match["our_team_alliance"] = get_team_alliance(next_match, team_number)
if previous_key and next_key:
return jsonify([previous_match, next_match, standings])
elif previous_key:
return jsonify([previous_match, None, standings])
elif next_key:
return jsonify([None, next_match, standings])
else:
return [None, None, standings]
@app.route("/<int:team_number>/event/<string:event>/match_info/")
def match_info(team_number, event):
matches = tba.team_matches(team_number, event=event, year=year, simple=True)
return jsonify(matches)
@app.route("/demo")
def demo():
team_number = 2036
event = "2025code"
event_object = tba.event(event=event)
team_object = tba.team(team_number)
return render_template("viewer.html", team=team_object, event=event_object)
if __name__ == '__main__':
app.run()