-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
173 lines (116 loc) · 4.65 KB
/
app.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from flask import Flask, request, jsonify, abort, render_template, send_from_directory
import errors
from functools import wraps
from cribbage_scorer import cribbage_scorer as cs
from datetime import datetime
from flask_cors import CORS
from flask import Response
from app_utils import translate_card
application = Flask(__name__)
CORS(application)
MAX_DATA_LIMIT = 10*1024
@application.route("/")
def webapp():
return render_template('index.html')
@application.route("/.well-known/ai-plugin.json")
def plugin_manifest():
with open("./.well-known/ai-plugin.json") as f:
text = f.read()
return text
@application.route("/openapi.yaml")
def openapi():
with open("./openapi.yaml") as f:
text = f.read()
return Response(text, mimetype="text/yaml")
@application.route("/legal")
def legal():
return render_template('legal.html')
@application.route("/privacy")
def privacy():
return render_template('privacy.html')
@application.route('/js/<path:path>')
def send_js(path):
return send_from_directory('templates/js', path)
def lists_to_tuples(list_of_lists):
out_list = []
for a_list in list_of_lists:
if len(a_list) > 2:
raise Exception("Error: Card ID list can not be converted to tuple as too many members.")
out_list.append(tuple(a_list))
return out_list
def limit_content_length(max_length=MAX_DATA_LIMIT):
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
cl = request.content_length
if cl is not None and cl > max_length:
abort(413)
return f(*args, **kwargs)
return wrapper
return decorator
@application.errorhandler(errors.InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
@application.route("/health", methods=["GET"])
def health_check():
return { "status": "UP", "current_time" : f"{datetime.now().astimezone().isoformat()}"}
@application.route("/score/cut", methods=["POST"])
@limit_content_length()
def cutcalcscore():
required_fields = ["players", "cut_card", "dealer"]
try:
req_data = request.get_json()
except:
errors.check_for_required_fields(None,required_fields)
errors.check_for_required_fields(req_data,required_fields)
players = req_data["players"]
cut_card = tuple(req_data["cut_card"])
dealer = req_data["dealer"]
scores, msg = cs.cut_calc_score(cut_card, players, dealer)
return {"type": "cut", "score": scores, "message": msg}
@application.route("/score/play/set", methods=["POST"])
def playcalcscorewhole():
req_data = request.get_json()
errors.check_for_required_fields(req_data, ["players", "played_cards"])
players = req_data["players"]
played_cards = lists_to_tuples(req_data["played_cards"])
scores, current_count, play_log = cs.play_calc_score_set(played_cards, players)
return {"type": "playwhole", "score": scores, "current_count": current_count, "play_log": play_log}
@application.route("/score/play/ongoing", methods=["POST"])
def playcalcscore():
req_data = request.get_json()
errors.check_for_required_fields(req_data, ["played_cards"])
played_cards = lists_to_tuples(req_data["played_cards"])
if "last_card" in req_data:
last_card = req_data["last_card"]
else:
last_card = False
current_count, scores, play_log = cs.play_score_ongoing(played_cards, last_card)
return {"type": "play", "score": scores, "count": current_count, "play_log": play_log}
@application.route("/score/show", methods=["POST"])
def showcalcscore():
req_data = request.get_json()
errors.check_for_required_fields(req_data, ["starter", "hand", "crib"])
starter = tuple(req_data["starter"])
hand = lists_to_tuples(req_data["hand"])
crib = req_data["crib"]
score, msg = cs.show_calc_score(starter, hand, crib)
return {"type": "show", "score": score, "message": msg}
@application.route("/score_hand_show", methods=["POST"])
def show_calc_score_open_api():
req_data = request.get_json()
errors.check_for_required_fields(req_data, ["starter", "hand", "isCrib"])
errors.check_for_fields_populated(req_data, "hand", 4)
errors.check_for_dupes(req_data)
hand = []
for card in req_data["hand"]:
card_tuple = translate_card(card)
hand.append(card_tuple)
starter_tuple = translate_card(req_data["starter"])
crib = req_data["isCrib"]
score, msg = cs.show_calc_score(starter_tuple, hand, crib)
return {"type": "show", "score": score, "message": msg}
if __name__ == "__main__":
application.run(port=5000, host="0.0.0.0")