diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index 1b144cf..e0a8c52 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -49,62 +49,23 @@ timer ++; switch(scene){ + case START_SCENE: inputName(); break; + case GAME_SCENE: + //ゲーム画面を表示 + showQuestion(); + + //制限時間を過ぎた場合にはEを選択 if(timer > TIME_LIMIT){ player.choice = E; - sendChoice(player.choice) // 変更 - checkAnswer(); + sendChoice() break; } - - - - fill("silver"); - setAlp(50); - fRect(50, 50, 855, 100, "black"); - fText("Q.", 110, 100, 50, "white"); - fRect(45, 295, 865, 560, "white"); - fRect(50, 300, 400, 250, "black"); - fRect(500, 300, 400, 250, "black"); - fRect(50, 600, 400, 250, "black"); - fRect(500, 600, 400, 250, "black"); - - sRect(45, 880, 865, 40, "black"); - fRect(45, 880, (timer/900)*865, 40, "red"); - setAlp(100); - - fText("A.", 100, 425, 40, "white"); - fText("B.", 550, 425, 40, "white"); - fText("C.", 100, 725, 40, "white"); - fText("D.", 550, 725, 40, "white"); - - fText(question[round].question, 480, 100, 30, "white"); - fText(question[round].choiceA, 275, 425, 25, "white"); - fText(question[round].choiceB, 725, 425, 25, "white"); - fText(question[round].choiceC, 275, 725, 25, "white"); - fText(question[round].choiceD, 725, 725, 25, "white"); - - fText(int(timer/30), 900, 1100, 50, "red"); - - setAlp(50); - - fText((round+1) + "回戦", 100, 1000, 50, "red"); - for(var i = 0; i < MAX_ROUND; i ++){ - if(player.points[i] == CORRECT){ - fText("O", 480 + 50*i, 1000, 50, "red"); - } - else if(player.points[i] == WRONG) { - fText("X", 480 + 50*i, 1000, 50, "blue"); - } - else{ - fText("*", 480 + 50*i, 1000, 50, "black"); - } - } - + if(tapC == 1){ tapC ++; @@ -126,24 +87,32 @@ } } break; + + //集計結果待ち + case WAIT_SCENE: + WaitOthers() // 他のプレイヤーが終わるまで待機する画面 + if(timer > 30){ + timer = 0; + CheckOthers() // 他のプレイヤーが終わるまで待機する処理 + } + break; + + + //最終集計結果待ち + case WAIT_RESULT: + if(timer > 30){ + timer = 0; + CheckResult(); //順位表を受け取るための処理 + } + //結果表示 case RESULT_SCENE: showResult(); break; - - // nemoto追記 - case WAIT_SCENE: - WaitOthers() // 他のプレイヤーが終わるまで待機する画面(nemoto追記) - if(timer > 30){ - timer = 0; - CheckOthers() // 他のプレイヤーが終わるまで待機する処理(nemoto追記) - } - + } } - - function setQuestion(round){ question[round].join(questions[round], answer[round], choices[round][A], choices[round][B], choices[round][C], choices[round][D]); } @@ -162,16 +131,17 @@ } } - +//スコアと識別番号をサーバーに送信する処理 function postForm(){ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); + //識別番号とスコアを送信 xhr.send('number='+player.number + '&score=' + player.calcScore()); //変更 - xhr.onreadystatechange = function() { //受信用 + //WAITコードを受信 + 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); @@ -180,13 +150,16 @@ } } +//選択をサーバーに送信する処理 function sendChoice(){ timer = 0; - scene = WAIT_SCENE; //nemoto追記 + scene = WAIT_SCENE; xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send('choice=' + player.choice); //変更 - xhr.onreadystatechange = function() { //受信用 + //選択を送信 + xhr.send('choice=' + player.choice); + //WAITコードを受信 + xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { if(xhr.responseText!="Data received successfully"){ console.log(xhr.responseText); @@ -198,11 +171,14 @@ } } +//ユーザーネームをサーバーに送信する処理 function sendName(){ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send('username=' + player.name); //変更 - xhr.onreadystatechange = function() { //受信用 + //ユーザーネームを送信 + xhr.send('username=' + player.name); + //識別番号を受信 + xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { if(xhr.responseText!="Data received successfully"){ console.log("識別番号:" + xhr.responseText); @@ -215,25 +191,29 @@ } } -// 追記(nemoto) +// 集計結果待機中処理 function CheckOthers(){ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); - xhr.send('checkothers=' + "check"); //変更 - xhr.onreadystatechange = function() { //受信用 + //待機コード送信 + xhr.send('checkothers=' + "check"); + xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { if(xhr.responseText=="WAIT"){ console.log(xhr.responseText); } + //正解選択肢を取得 else{ timer = 0; console.log(xhr.responseText); question[round].answer = xhr.responseText; + //正解判定 checkAnswer(); if(round < MAX_ROUND) scene = GAME_SCENE + //最終ラウンド終了時 else{ - scene = RESULT_SCENE + scene = WAIT_RESULT postForm(); } } @@ -241,6 +221,25 @@ } } +//最終集計結果(順位表)待機処理 +function CheckResult(){ + xhr.open('POST', '/form'); + xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); + //待機コードを送信 + xhr.send('checkResult=' + "check"); + xhr.onreadystatechange = function() { + if (xhr.readyState === 4 && xhr.status === 200) { + if(xhr.responseText=="WAIT"){ + console.log(xhr.responseText); + } + //順位表を受信 + else{ + orderTable = xhr.responseText; + scene = RESULT_SCENE; + } + } + } +} var xhr = new XMLHttpRequest(); @@ -265,9 +264,12 @@ var GAME_SCENE = 2; var RESULT_SCENE = 3; var WAIT_SCENE = 4; // nemoto追記 +var WAIT_RESULT = 5; var scene = START_SCENE; +var orderTable = ""; + var my_order = 8; var ranking_order = ["ルフィ", "ゾロ", "ジンベエ", "サンジ", "ロビン", "ウソップ", "フランキー", "ブルック", "ナミ", "チョッパー"] diff --git a/flask/testapp/static/javascript/resultScreen.js b/flask/testapp/static/javascript/resultScreen.js index 89ed316..25d99b2 100644 --- a/flask/testapp/static/javascript/resultScreen.js +++ b/flask/testapp/static/javascript/resultScreen.js @@ -1,15 +1,65 @@ +function showQuestion(){ + fill("silver"); + setAlp(50); + fRect(50, 50, 855, 100, "black"); + fText("Q.", 110, 100, 50, "white"); + fRect(45, 295, 865, 560, "white"); + fRect(50, 300, 400, 250, "black"); + fRect(500, 300, 400, 250, "black"); + fRect(50, 600, 400, 250, "black"); + fRect(500, 600, 400, 250, "black"); + + sRect(45, 880, 865, 40, "black"); + fRect(45, 880, (timer/900)*865, 40, "red"); + setAlp(100); + + fText("A.", 100, 425, 40, "white"); + fText("B.", 550, 425, 40, "white"); + fText("C.", 100, 725, 40, "white"); + fText("D.", 550, 725, 40, "white"); + + fText(question[round].question, 480, 100, 30, "white"); + fText(question[round].choiceA, 275, 425, 25, "white"); + fText(question[round].choiceB, 725, 425, 25, "white"); + fText(question[round].choiceC, 275, 725, 25, "white"); + fText(question[round].choiceD, 725, 725, 25, "white"); + + fText(int(timer/30), 900, 1100, 50, "red"); + + setAlp(50); + + fText((round+1) + "回戦", 100, 1000, 50, "red"); + for(var i = 0; i < MAX_ROUND; i ++){ + if(player.points[i] == CORRECT){ + fText("O", 480 + 50*i, 1000, 50, "red"); + } + else if(player.points[i] == WRONG) { + fText("X", 480 + 50*i, 1000, 50, "blue"); + } + else{ + fText("*", 480 + 50*i, 1000, 50, "black"); + } + } +} + function showResult(){ fill("silver"); setAlp(50); fRect(50, 50, 855, 100, "black"); fText("結果発表 ", 480, 100, 80, "white"); - fText(player.name+"さんのスコアは" + player.calcScore() + "点です!", 480, 200, 50, "gold"); - //fText("あなたの順位は...", 230, 200, 50, "black"); - fText(my_order+"位!", 480, 300, 110, "red"); fRect(45, 370, 865, 700, "white"); fText("成績上位者", 300, 410, 50, "black"); fText("スコア", 670, 410, 50, "black"); + + if(scene == RESULT_SCENE){ + fText(player.name+"さんのスコアは" + player.calcScore() + "点です!", 480, 200, 50, "gold"); + fTextN(orderTable, 480, 720, 600, 50, "silver"); + } + + //fText("あなたの順位は...", 230, 200, 50, "black"); + //fText(my_order+"位!", 480, 300, 110, "red"); + /* for (var i = 0; i < MAX_ORDER; i++){ if(i+1==1){ fText((i+1)+"位", 100, 480+60*i, 50, "gold"); @@ -25,7 +75,7 @@ } fText(ranking_order[i], 300, 480+60*i, 50, "white"); fText(scoreing_order[i], 670, 480+60*i, 50, "white"); - } + }*/ } diff --git a/flask/testapp/views.py b/flask/testapp/views.py index da17261..e001b08 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -1,8 +1,6 @@ from flask import render_template, request from testapp import app from collections import Counter -import random -import time # ユーザーごとの点数を格納する辞書 user_score = {} @@ -25,16 +23,18 @@ order_table = "" + + @app.route("/form", methods=["GET", "POST"]) def index(): - global user_number, data_count, order_table + global user_number, data_count, order_table, most_common_option if request.method == "GET": return render_template("testapp/index.html") + if request.method == "POST": - # ゲーム開始時にユーザー数を出しておき,それぞれに識別番号を振る if request.form.get("username"): - # ユーザー名を取得し,ユーザー情報を登録←クラス化したほうがいいかも? + # ユーザー名を取得し,ユーザー情報を登録 username = request.form.get("username") user[user_number] = username print("Received name:", username, "UserNumber:", user_number) @@ -50,22 +50,20 @@ choices.append(request.form.get("choice")) print("Received choice:", choices) - if len(choices) >= MAX_PLAYER: # nemoto変更 - print("集計...") - - return "WAIT" # nemoto追記 - - # 他の人が終わるまで待機(nemoto追記) - if request.form.get("checkothers"): - if len(choices) < MAX_PLAYER: - return "WAIT" - else: - most_common_option = calculate_majority(choices) # nemoto追記 + if len(choices) >= MAX_PLAYER: + most_common_option = calculate_majority(choices) print("Answer:", most_common_option) + return "WAIT" + + # 他の人が終わるまで待機 + if request.form.get("checkothers"): + if len(choices) < user_number: + return "WAIT" + # 全員のデータが集まった場合は,多数派を計算 + else: return str(most_common_option) - - - # ゲーム終了時,識別番号とスコアを受け取り,ランキング上位を返す(作成途中) + + # ゲーム終了時,識別番号とスコアを受け取る if request.form.get("number") and request.form.get("score"): # 識別番号を取得 user_number = int(request.form.get("number")) @@ -81,26 +79,27 @@ data[user_number], ) data_count += 1 - if data_count >= MAX_PLAYER: - scores_order, user_number_order = calculate_order(data) # nemoto追記 + + # すべてのデータが集まった場合,順位表を作成 otaki + if data_count >= user_number: + scores_order, user_number_order = calculate_order(data) print(scores_order, user_number_order) order_table = create_order_table(scores_order, user_number_order) - # 戻り値でランキング上位を返す?(仮) + return "WAIT" - - if request.form.get("waitResult"): + + # 最終結果を集計 + if request.form.get("checkResult"): + #  順位表が作成した場合はそれを返す if order_table == "": return "WAIT" else: return order_table - - - # 必要な処理を行う(例:データベースへの書き込みなど) return "WAIT" -# 多数派の計算(nemoto追記) +# 多数派の計算 def calculate_majority(get_data): counter = Counter(get_data) # 最も多く出現する要素を取得する @@ -108,25 +107,23 @@ return get_most_common_option -# ランキング上位の計算(nemoto追記) +# ランキング上位の計算 def calculate_order(get_data): get_sorted_numbers_with_indices = sorted( enumerate(get_data), key=lambda x: x[1], reverse=True ) get_scores = [item[1] for item in get_sorted_numbers_with_indices] get_labels = [item[0] for item in get_sorted_numbers_with_indices] - print("スコア:" + str(get_scores)) - print("識別番号:" + str(get_labels)) + return get_scores, get_labels +# 順位表の作成 otaki def create_order_table(scores, labels): order_table = "" - print(user) for i in range(MAX_PLAYER): order_table += ( - str(i) + "位 " + user[int(labels[i])] + " " + str(scores[i]) + "pt\n" + str(i + 1) + "位 " + user[int(labels[i])] + " " + str(scores[i]) + "pt\n" ) - print(order_table) return order_table