-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
115 lines (86 loc) · 2.91 KB
/
main.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
from flask import Flask, jsonify, make_response, request, render_template, redirect
from flask_cors import CORS
from Repository.QuestionDAO import QuestionDAO
from Service.QuestionService import QuestionService
import settings
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
cors = CORS(app, resources={r"*": {"origins": "*"}})
@app.route('/hello')
def hello():
return jsonify({'message': 'hello'})
## エンドポイント
@app.route('/start', methods=['GET', 'POST'])
def start():
instance = QuestionDAO()
game_id = instance.get_game_id()
return jsonify({'game_id': game_id})
@app.route('/question', methods=['GET', 'POST'])
def question():
if not request.json:
return make_response('', 400)
body = request.json
game_id = body['game_id']
if not game_id:
return make_response('auth error', 400)
question_id = body['question_id']
instance = QuestionDAO()
response = instance.find_question(question_id)
return jsonify(response)
@app.route('/question/answer', methods=['GET', 'POST'])
def question_answer():
if not request.json:
return make_response('not json', 402)
body = request.json
game_id = body['game_id']
if not game_id:
return make_response('auth error', 400)
question_id = body['question_id']
result = body['result']
if result not in [0, 1]:
return make_response('"result" is 0 or 1.', 401)
instance = QuestionDAO()
# question_id を確認する
flag = instance.find_question(question_id)
if(flag == []):
return jsonify({'message': 'Invalid "question_id"'})
try:
response = instance.insert_answer(game_id, question_id, result)
return jsonify({'message': 'OK'})
except:
return jsonify({'message': 'ERROR'})
@app.route('/result', methods=['GET', 'POST'])
def result():
if not request.json:
return make_response('not json', 402)
body = request.json
game_id = body['game_id']
if not game_id:
return make_response('auth error', 401)
instance = QuestionService()
result_circle_list = instance.calc_point(game_id)
if type(result_circle_list) == list:
# 正常時
return jsonify({'ranking': result_circle_list})
elif type(result_circle_list) == str:
# 異常時
# 回答が完了していない
# 回答に誤りがある
# 不正なgame_idである
return jsonify({"message": result_circle_list})
@app.route('/end', methods=['GET', 'POST'])
def end():
if not request.json:
return make_response('', 400)
body = request.json
game_id = body['game_id']
if not game_id:
return make_response('auth error', 400)
instance = QuestionDAO()
try:
response = instance.deleat_gameid(game_id)
return jsonify({'message': 'OK'})
except:
return jsonify({'message': 'ERROR'})
if __name__ == '__main__':
app.run()