diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index 78b7d89..569ed5b 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -192,9 +192,19 @@ var xhr = new XMLHttpRequest(); xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send(player.choice); + xhr.send('username=' + player.name + '&data=' + player.choice); //変更 + xhr.onreadystatechange = function() { //受信用 + if (xhr.readyState === 4 && xhr.status === 200) { + if(xhr.responseText!="Data received successfully"){ + console.log(xhr.responseText); + my_order=xhr.responseText + } + else{ + console.log(xhr.responseText); + } + } + } } - var round = 0; var MAX_ROUND = 10; diff --git a/flask/testapp/views.py b/flask/testapp/views.py index f4a274b..412e9e8 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -1,7 +1,11 @@ from flask import render_template, request from testapp import app -data = [] +# ユーザーごとのデータを格納する辞書 +user_data = {} +players = 10 +task = 10 +data_count = 0 @app.route("/form", methods=["GET", "POST"]) @@ -10,29 +14,25 @@ return render_template("testapp/index.html") if request.method == "POST": # POSTデータを受け取る - data.append( - request.get_data(as_text=True) - ) # バイナリデータではなくテキストとして取得する - + # data.append(request.get_data(as_text=True)) # バイナリデータではなくテキストとして取得する + + # ユーザーごとのデータを取得または作成 + username = request.form.get("username") + if username not in user_data: + user_data[username] = [] + + # クライアントから送信されたデータを取得 + data = request.form.get("data") + # ユーザーごとのデータに追加 + user_data[username].append(data) + data_count += 1 # 受け取ったデータを印刷する - print("Received data:", data) - - if len(data) > 10: - data.clear() + print("Received data:", user_data[username]) + + # 全員分のデータが集まった時 + if len(data_count) >= task * players: + + return "7" # 必要な処理を行う(例:データベースへの書き込みなど) return "Data received successfully" - - # print("POSTデータ受け取ったので処理します。") - # data = request.form["text"] - # print(data) - # return render_template("testapp/index.html") - - -# @app.route("/test") -# def index2(): -# my_dict = { -# "insert_something1": "views.pyのinsert_something1部分です。", -# "insert_something2": "views.pyのinsert_something2部分です。", -# } -# return render_template("testapp/index.html", my_dict=my_dict)