diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index e8d3a75..b0a2999 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -60,6 +60,7 @@ switch(scene){ case 3: inputName(); + sendName(); break; case 1: @@ -120,23 +121,22 @@ if(50 <= tapX && tapX <= 450 && 300 <= tapY && tapY <= 550){ player.choice = A; - postForm(player.choice) // 送信用 + sendChoice(); checkAnswer(); } else if(500 <= tapX && tapX <= 900 && 300 <= tapY && tapY <= 550){ player.choice = B; - postForm(player.choice) // 送信用 + sendChoice(); checkAnswer(); - //document.getElementById('textBox').style.display = 'block'; } else if(50 <= tapX && tapX <= 450 && 600 <= tapY && tapY <= 850){ player.choice = C; - postForm(player.choice) // 送信用 + sendChoice(); checkAnswer(); } else if(500 <= tapX && tapX <= 900 && 600 <= tapY && tapY <= 850){ player.choice = D; - postForm(player.choice) // 送信用 + sendChoice(); checkAnswer(); } } @@ -168,6 +168,7 @@ } else{ scene = 2; + postForm(); } } @@ -190,11 +191,10 @@ // form.submit(); // } -function postForm(value){ - var xhr = new XMLHttpRequest(); +function postForm(){ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send('username=' + player.name + '&data=' + player.choice); //変更 + xhr.send('username=' + player.name + '&score=' + player.calcScore()); //変更 xhr.onreadystatechange = function() { //受信用 if (xhr.readyState === 4 && xhr.status === 200) { if(xhr.responseText!="Data received successfully"){ @@ -208,6 +208,33 @@ } } +function sendChoice(){ + xhr.open('POST', '/form'); + xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); + xhr.send('choice=' + 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); + } + } + } +} + +function sendName(){ + xhr.open('POST', '/form'); + xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); + xhr.send('username=' + player.name); //変更 +} + + + +var xhr = new XMLHttpRequest(); + var round = 0; var MAX_ROUND = 10; diff --git a/flask/testapp/templates/testapp/index.html b/flask/testapp/templates/testapp/index.html index fb09098..b310a32 100644 --- a/flask/testapp/templates/testapp/index.html +++ b/flask/testapp/templates/testapp/index.html @@ -39,7 +39,7 @@
- +
diff --git a/flask/testapp/views.py b/flask/testapp/views.py index 321b3bf..b5c7956 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -7,9 +7,10 @@ # 各データの保存 data = [] +user_data = [] # playerの数 -players = 50 +players = 1 @app.route("/form", methods=["GET", "POST"]) @@ -17,9 +18,12 @@ 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: # リスト内の要素の出現回数をカウントする @@ -27,19 +31,28 @@ # 最も多く出現する要素を取得する most_common_element = counter.most_common(1)[0][0] return most_common_element + """ + if request.form.get("choice"): + choice = request.form.get("choice") + print("Received choice:", choice) + # ユーザーごとのデータを取得または作成 if request.form.get("username"): username = request.form.get("username") - user_data[username] = [] + print("Received username:", username) # クライアントから送信されたデータを取得 - data = request.form.get("data") - # ユーザーごとのデータに追加 - user_data[username].append(data) - data_count += 1 - # 受け取ったデータを印刷する - print("Received data:", user_data[username]) + if request.form.get("score"): + score = request.form.get("score") + print("Received score:", score) + """ + # ユーザーごとのデータに追加 + user_data[username].append(data) + data_count += 1 + """ + # 受け取ったデータを印刷する + #print("Received data:", user_data[username]) # 必要な処理を行う(例:データベースへの書き込みなど) return "Data received successfully"