diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index b0a2999..fb34d9e 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -16,16 +16,6 @@ this.choiceC = choiceC; this.choiceD = choiceD; } - getQustion(){ - return this.question; - } - getAnswer(){ - return this.answer; - } - getChoice(idx){ - return this.choices[idx]; - } - } class Player{ @@ -33,6 +23,7 @@ this.points = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; this.choice = 0; this.name = 0; + this.number = 0; } calcScore(){ var score = 0; @@ -46,7 +37,6 @@ } - function setup(){ canvasSize(960, 1200); lineW(3); @@ -58,12 +48,11 @@ function mainloop(){ switch(scene){ - case 3: + case START_SCENE: inputName(); - sendName(); break; - case 1: + case GAME_SCENE: if(timer > TIME_LIMIT){ player.choice = E; postForm(player.choice) // 送信用 @@ -141,7 +130,7 @@ } } break; - case 2: + case RESULT_SCENE: showResult(); } } @@ -167,7 +156,7 @@ timer = 0; } else{ - scene = 2; + scene = RESULT_SCENE; postForm(); } } @@ -194,7 +183,7 @@ function postForm(){ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send('username=' + player.name + '&score=' + player.calcScore()); //変更 + xhr.send('number='+player.number + '&score=' + player.calcScore()); //変更 xhr.onreadystatechange = function() { //受信用 if (xhr.readyState === 4 && xhr.status === 200) { if(xhr.responseText!="Data received successfully"){ @@ -228,7 +217,18 @@ function sendName(){ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send('username=' + player.name); //変更 + xhr.send('originalname=' + player.name); //変更 + xhr.onreadystatechange = function() { //受信用 + if (xhr.readyState === 4 && xhr.status === 200) { + if(xhr.responseText!="Data received successfully"){ + console.log(xhr.responseText); + player.number = xhr.responseText; + } + else{ + console.log(xhr.responseText); + } + } + } } @@ -250,7 +250,11 @@ var CORRECT = 1; var WRONG = 0; -var scene = 3; +var START_SCENE = 1; +var GAME_SCENE = 2; +var RESULT_SCENE = 3; + +var scene = START_SCENE; var BLOCK_SIZE = 5; var my_order = 8; diff --git a/flask/testapp/static/javascript/resultScreen.js b/flask/testapp/static/javascript/resultScreen.js index 5d05a51..f6130a3 100644 --- a/flask/testapp/static/javascript/resultScreen.js +++ b/flask/testapp/static/javascript/resultScreen.js @@ -1,12 +1,3 @@ - -/* -function setup(){ - canvasSize(960, 1200); - lineW(3); - loadImg(0, "image/bg.png"); - -}*/ - function showResult(){ fill("silver"); setAlp(50); @@ -36,10 +27,6 @@ fText(scoreing_order[i], 670, 480+60*i, 50, "white"); } - if(tapC > 1){ - - } - } function inputName(){ @@ -69,7 +56,8 @@ if(200 <= tapX && tapX <= 760 && 500 <= tapY && tapY <= 700){ player.name = document.getElementById('textBox').value; document.getElementById('textBox').style.display = 'none'; - scene = 1; + scene = GAME_SCENE; + sendName(); } } diff --git a/flask/testapp/views.py b/flask/testapp/views.py index b5c7956..acb5f5c 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -1,20 +1,30 @@ from flask import render_template, request from testapp import app from collections import Counter +import random # ユーザーごとの点数を格納する辞書 user_score = {} -# 各データの保存 -data = [] -user_data = [] # playerの数 -players = 1 +MAX_PLAYER = 50 + +# scoreを保存するデータ +data = [0] * MAX_PLAYER +#  user名を保存するデータ +user = [0] * MAX_PLAYER + +# ゲーム中の選択肢を保存 +choices = [] + +# ユーザーを識別するための番号(実際の初期値は0にする) +user_number = 5 @app.route("/form", methods=["GET", "POST"]) def index(): + global user_number if request.method == "GET": return render_template("testapp/index.html") if request.method == "POST": @@ -23,7 +33,7 @@ data.append( request.get_data(as_text=True) ) # バイナリデータではなくテキストとして取得する - + # 全員分のデータが集まった時 if len(data) >= players: # リスト内の要素の出現回数をカウントする @@ -32,27 +42,52 @@ 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) + choices.append(request.form.get("choice")) + print("Received choice:", choices) + if len(choices) >= user_number: + print("集計...") + choices.clear() + return "一番多い選択肢" - - # ユーザーごとのデータを取得または作成 - if request.form.get("username"): - username = request.form.get("username") - print("Received username:", username) + # ゲーム開始時にユーザー数を出しておき,それぞれに識別番号を振る + if request.form.get("originalname"): - # クライアントから送信されたデータを取得 - if request.form.get("score"): + # ユーザー名を取得し,ユーザー情報を登録←クラス化したほうがいいかも? + username = request.form.get("originalname") + user[user_number] = username + print("Received name:", username, "UserNumber:", user_number) + + user_number += 1 + # クライアントサイドに識別番号を返す + return str(user_number - 1) + + # ゲーム終了時,識別番号とスコアを受け取り,ランキング上位を返す(作成途中) + if request.form.get("number") and request.form.get("score"): + # 識別番号を取得 + user_number = int(request.form.get("number")) + # スコアを取得 score = request.form.get("score") - print("Received score:", score) + data[user_number] = score + + print( + "Received number:", + user_number, + "\nUserName:", + user[user_number], + "\nScore:", + data[user_number], + ) + # 戻り値でランキング上位を返す?(仮) + return "7" """ # ユーザーごとのデータに追加 user_data[username].append(data) data_count += 1 """ # 受け取ったデータを印刷する - #print("Received data:", user_data[username]) + # print("Received data:", user_data[username]) # 必要な処理を行う(例:データベースへの書き込みなど) return "Data received successfully"