diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index d302515..2f1795b 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -194,9 +194,23 @@ var xhr = new XMLHttpRequest(); xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); +<<<<<<< HEAD xhr.send(value); +======= + 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); + } + } + } +>>>>>>> webserve } - var round = 0; var MAX_ROUND = 10; diff --git a/flask/testapp/views.py b/flask/testapp/views.py index f4a274b..321b3bf 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -1,38 +1,45 @@ from flask import render_template, request from testapp import app +from collections import Counter +# ユーザーごとの点数を格納する辞書 +user_score = {} + +# 各データの保存 data = [] +# playerの数 +players = 50 + @app.route("/form", methods=["GET", "POST"]) def index(): if request.method == "GET": 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)) # バイナリデータではなくテキストとして取得する + # 全員分のデータが集まった時 + if len(data) >= players: + # リスト内の要素の出現回数をカウントする + counter = Counter(data) + # 最も多く出現する要素を取得する + most_common_element = counter.most_common(1)[0][0] + return most_common_element + + # ユーザーごとのデータを取得または作成 + if request.form.get("username"): + username = request.form.get("username") + 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]) # 必要な処理を行う(例:データベースへの書き込みなど) 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)