diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index 20ab9e8..1460e28 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -24,6 +24,7 @@ constructor(){ this.points = Array(MAX_ROUND).fill(0); this.choice = 0; + this.score = 0; this.name = 0; this.number = 0; } @@ -41,6 +42,7 @@ canvasSize(960, 1200); lineW(3); timer = 0; + scene = START_SCENE; setQuestion(round); } @@ -97,10 +99,26 @@ //集計結果待ち case WAIT_SCENE: - WaitOthers() // 他のプレイヤーが終わるまで待機する画面 + showQuestion(); if(timer > 30){ timer = 0; - CheckOthers() // 他のプレイヤーが終わるまで待機する処理 + CheckOthers(); // 他のプレイヤーが終わるまで待機する処理 + } + break; + + case ROUND_RESULT: + showQuestion(); + if(timer > 90){ + timer = 0; + round ++; + if(round < MAX_ROUND){ + setQuestion(round); + scene = GAME_SCENE; + } + else{ + scene = WAIT_RESULT; + postForm(); + } } break; @@ -111,6 +129,7 @@ timer = 0; CheckResult(); //順位表を受け取るための処理 } + //結果表示 case RESULT_SCENE: showResult(); @@ -157,6 +176,7 @@ } + function setQuestion(round){ question[round].join(questions[round], realAnswer[round],choices[round][A], choices[round][B], choices[round][C], choices[round][D]); } @@ -182,27 +202,8 @@ break; } } -/* - if(player.choice == question[round].answer[0]){ - player.points[round] = "4"; - } - else if(player.choice == question[round].answer[1]){ - player.points[round] = "3"; - } - else if(player.choice == question[round].answer[2]){ - player.points[round] = "2"; - } - else if(player.choice == question[round].answer[3]){ - player.points[round] = "1"; - } - else{ - player.points[round] = "0"; - }*/ - round ++; + player.score = player.calcScore(); console.log(player.points[round]) - if(round < MAX_ROUND){ - setQuestion(round); - } } //スコアと識別番号をサーバーに送信する処理 @@ -210,13 +211,7 @@ xhr.open('POST', '/form'); xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); //識別番号とスコアを送信 - xhr.send('number='+player.number + '&score=' + player.calcScore()); //変更 - //WAITコードを受信 - xhr.onreadystatechange = function() { - if (xhr.readyState === 4 && xhr.status === 200) { - console.log(xhr.responseText); - } - } + xhr.send('number='+player.number + '&score=' + player.score); //変更 } //選択をサーバーに送信する処理 @@ -241,11 +236,18 @@ xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8'); //ユーザーネームを送信 xhr.send('username=' + player.name); + timer = 0; //識別番号を受信 xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { - console.log("識別番号:" + xhr.responseText); - player.number = xhr.responseText; + if(xhr.responseText == "Capasity ERROR"){ + scene = RESULT_SCENE; + } + else{ + console.log("識別番号:" + xhr.responseText); + player.number = xhr.responseText; + scene = WAIT_START; + } } } } @@ -313,35 +315,12 @@ question[round].answer[i] = arr_answer[0]; question[round].people[i] = arr_answer[1]; } - /* - // 1番目に多かった答えと人数 - let arr_answer1 = splitAnswer[0].split(" "); - question[round].answer1 = arr_answer1[0]; - question[round].people1 = arr_answer1[1]; - // 2番目に多かった答えと人数 - let arr_answer2 = splitAnswer[1].split(" "); - question[round].answer2 = arr_answer2[0]; - question[round].people2 = arr_answer2[1]; - // 3番目に多かった答えと人数 - let arr_answer3 = splitAnswer[2].split(" "); - question[round].answer3 = arr_answer3[0]; - question[round].people3 = arr_answer3[1]; - // 4番目に多かった答えと人数 - let arr_answer4 = splitAnswer[3].split(" "); - question[round].answer4 = arr_answer4[0]; - question[round].people4 = arr_answer4[1]; - console.log(question[round].answer4) - console.log(question[round].people4)*/ - //正解判定 - checkAnswer(); - if(round < MAX_ROUND) - scene = GAME_SCENE - //最終ラウンド終了時 - else{ - scene = WAIT_RESULT - postForm(); - } } + + //正解判定 + checkAnswer(); + timer = 0; + scene = ROUND_RESULT; } } } @@ -371,7 +350,7 @@ var round = 0; //変更 -var MAX_ROUND = 5; +var MAX_ROUND = 3; var CHOICE_NUMBER = 4; var max_player = 0; // 最大のplayer数(変更不要) var current_player = 0; @@ -396,8 +375,9 @@ var WAIT_RESULT = 5; var WAIT_START = 6; // nemoto追記 var CHECK_RESULT = 7; +var ROUND_RESULT = 8; -var scene = START_SCENE; +var scene = 0; var orderTable = ""; diff --git a/flask/testapp/static/javascript/resultScreen.js b/flask/testapp/static/javascript/resultScreen.js index 605ccde..591f1cf 100644 --- a/flask/testapp/static/javascript/resultScreen.js +++ b/flask/testapp/static/javascript/resultScreen.js @@ -9,8 +9,6 @@ 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"); @@ -24,22 +22,30 @@ 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"); - } + fText("現在の点数: " + player.score + " Pt", 700, 1000, 50, "black"); + + if(scene == GAME_SCENE){ + setAlp(50); + sRect(45, 880, 865, 40, "black"); + fRect(45, 880, (timer/900)*865, 40, "red"); + setAlp(100); + fText(int(timer/30), 900, 1100, 50, "red"); } + if(scene == ROUND_RESULT){ + fText(CheckPeople(0, round) +"人", 275, 425, 50, "black"); + fText(CHOICE_NUMBER-question[round].answer[A] + "Pt", 350, 500, 50, "black"); + fText(CheckPeople(1, round)+"人", 725, 425, 50, "black"); + fText(CHOICE_NUMBER-question[round].answer[B] + "Pt", 850, 500, 50, "black"); + fText(CheckPeople(2, round)+"人", 275, 725, 50, "black"); + fText(CHOICE_NUMBER-question[round].answer[C] + "Pt", 350, 800, 50, "black"); + fText(CheckPeople(3, round)+"人", 725, 725, 50, "black"); + fText(CHOICE_NUMBER-question[round].answer[A] + "Pt", 850, 800, 50, "black"); + } + if(scene == WAIT_SCENE){ + fText("集計中...", 500, 500, 80, "black"); + } } function inputName(){ @@ -69,8 +75,6 @@ if(200 <= tapX && tapX <= 760 && 500 <= tapY && tapY <= 700){ player.name = document.getElementById('textBox').value; document.getElementById('textBox').style.display = 'none'; - scene = WAIT_START; - timer = 0; sendName(); } } @@ -83,7 +87,6 @@ fRect(50, 50, 855, 100, "black"); - fText("美人投票ゲーム", 480, 100, 80, "white"); setAlp(50); fText("あなたの名前を入力してください", 480, 200, 50, "black"); @@ -112,29 +115,6 @@ fTextN("ルール1\nルール2\nルール3\nルール4", 480, 950, 200, 50, "white"); } -// 他のプレイヤーの終わりを待つ画面(nemoto変更) -function WaitOthers(){ - 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("集計中...", 500, 500, 80, "black"); -} - function showResult(){ fill("silver"); setAlp(50); @@ -147,9 +127,7 @@ fText("スコア", 730, 410, 50, "black"); if(scene == RESULT_SCENE){ - fText(player.name+"さんのスコアは" + player.calcScore() + "点です!", 480, 200, 50, "gold"); - // fTextN(orderTable, 500, 770, 600, 50, "silver"); - + fText(player.name+"さんのスコアは" + player.calcScore() + "点です!", 480, 200, 50, "gold"); let splitOrder = orderTable.split("\n"); for (var i = 0; i < splitOrder.length; i++){ let arr = splitOrder[i].split(" "); // 空白で文字列を分割 @@ -175,57 +153,7 @@ } } - -// 他の設問の結果発表(nemoto追記) -function showOtherResult(show_round){ - - 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"); - - 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[show_round].question, 480, 100, 30, "white"); - fText(question[show_round].choiceA, 275, 425, 25, "white"); - fText(CheckPeople(0, show_round) +"人", 275, 425, 50, "black"); - - fText(CheckPeople(0, show_round) +"人", 275, 425, 50, "black"); - - fText(question[show_round].choiceB, 725, 425, 25, "white"); - fText(CheckPeople(1, show_round)+"人", 725, 425, 50, "black"); - - fText(question[show_round].choiceC, 275, 725, 25, "white"); - fText(CheckPeople(2, show_round)+"人", 275, 725, 50, "black"); - - fText(question[show_round].choiceD, 725, 725, 25, "white"); - fText(CheckPeople(3, show_round)+"人", 725, 725, 50, "black"); - - setAlp(50); - - fText("結果", 100, 1100, 50, "black"); - for(var i = 0; i < MAX_ROUND; i ++){ - if(i == show_round){ - fText((i+1), 480 + 50*i, 1100, 50, "red"); - } - else{ - fText((i+1), 480 + 50*i, 1100, 50, "black"); - } - } -} - function CheckPeople(i, show_round){ - let question_values = [int(question[show_round].answer[0]), int(question[show_round].answer[1]), int(question[show_round].answer[2]), int(question[show_round].answer[3])] let people_values = [int(question[show_round].people[0]), int(question[show_round].people[1]), int(question[show_round].people[2]), int(question[show_round].people[3])] let index = question_values.indexOf(i); diff --git a/flask/testapp/views.py b/flask/testapp/views.py index 5142476..54bb649 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -1,12 +1,15 @@ from flask import render_template, request from testapp import app from collections import Counter +import time # ユーザーごとの点数を格納する辞書 user_score = {} # playerの数 -MAX_PLAYER = 3 +MAX_PLAYER = 2 + +WAIT_TIME = 40 # 何位まで表示するか(nemoto) max_order = 5 @@ -34,10 +37,12 @@ most_common_option = 0 start_flag = False +start, end = 0, 0 + @app.route("/form", methods=["GET", "POST"]) def index(): - global frequent_table, user_number, data_count, order_table, most_common_option, max_order + global frequent_table, user_number, data_count, order_table, most_common_option, max_order, current_player, start, end if request.method == "GET": return render_template("testapp/index.html") @@ -54,6 +59,8 @@ print("Received name:", username, "UserNumber:", user_number) user_number += 1 + current_player = user_number + print(f"現在の参加人数: {current_player}") # クライアントサイドに識別番号を返す return str(user_number - 1) @@ -72,27 +79,36 @@ return str(MAX_PLAYER) + " " + str(user_number) # 全員のデータが集まった場合は,ゲーム開始 else: + start = time.time() + # print(f"START: {start}") return "START" # ゲーム中に選択肢を取得し,集計する if request.form.get("choice"): - if len(choices) >= MAX_PLAYER: + if len(choices) >= current_player: choices.clear() choices.append(request.form.get("choice")) print("Received choice:", choices) - - if len(choices) >= MAX_PLAYER: + """ + if len(choices) >= current_player: frequent_table = calculate_majority(choices) - return "WAIT" + return "WAIT + """ # 他の人が終わるまで待機 if request.form.get("checkothers"): - if len(choices) < MAX_PLAYER: - return "WAIT" - # 全員のデータが集まった場合は,多数派を計算 + end = time.time() + # print(f"END: {end}") + print(f"TIME: {end-start}") + if len(choices) >= current_player or end - start > WAIT_TIME: + start = time.time() + current_player = len(choices) + # print(f"START: {start}") + print(f"現在の参加人数:{current_player}") + return calculate_majority(choices) else: - return frequent_table + return "WAIT" # ゲーム終了時,識別番号とスコアを受け取る if request.form.get("number") and request.form.get("score"): @@ -112,10 +128,12 @@ data_count += 1 # すべてのデータが集まった場合,順位表を作成 otaki - if data_count >= MAX_PLAYER: + if data_count >= current_player: scores_order, user_number_order = calculate_order(data) print(scores_order, user_number_order) - order_table = create_order_table(scores_order, user_number_order, max_order) + order_table = create_order_table( + scores_order, user_number_order, max_order + ) return "WAIT" @@ -125,6 +143,7 @@ if order_table == "": return "WAIT" else: + user_number = 0 return order_table return "WAIT" @@ -135,30 +154,52 @@ counter = Counter(get_data) get_frequent_table = "" # 最も多く出現する要素を取得する - most_common = counter.most_common() # 出現回数が多い順に要素とその出現回数のリストを取得 + most_common = ( + counter.most_common() + ) # 出現回数が多い順に要素とその出現回数のリストを取得 # 最も多く出現する要素 get_most_common_option = str(most_common[0][0]) get_most_common_count = str(most_common[0][1]) get_frequent_table += get_most_common_option + " " + get_most_common_count + "\n" # 2番目に多い要素 - get_second_most_common_option = str(most_common[1][0]) if len(most_common) > 1 else str(4) - get_second_most_common_count = str(most_common[1][1]) if len(most_common) > 1 else str(0) - get_frequent_table += get_second_most_common_option + " " + get_second_most_common_count + "\n" + get_second_most_common_option = ( + str(most_common[1][0]) if len(most_common) > 1 else str(4) + ) + get_second_most_common_count = ( + str(most_common[1][1]) if len(most_common) > 1 else str(0) + ) + get_frequent_table += ( + get_second_most_common_option + " " + get_second_most_common_count + "\n" + ) # 3番目に多い要素 - get_third_most_common_option = str(most_common[2][0]) if len(most_common) > 2 else str(4) - get_third_most_common_count = str(most_common[2][1]) if len(most_common) > 2 else str(0) - get_frequent_table += get_third_most_common_option + " " + get_third_most_common_count + "\n" + get_third_most_common_option = ( + str(most_common[2][0]) if len(most_common) > 2 else str(4) + ) + get_third_most_common_count = ( + str(most_common[2][1]) if len(most_common) > 2 else str(0) + ) + get_frequent_table += ( + get_third_most_common_option + " " + get_third_most_common_count + "\n" + ) # 4番目に多い要素 - get_fourth_most_common_option = str(most_common[3][0]) if len(most_common) > 3 else str(4) - get_fourth_most_common_count = str(most_common[3][1]) if len(most_common) > 3 else str(0) - get_frequent_table += get_fourth_most_common_option + " " + get_fourth_most_common_count + get_fourth_most_common_option = ( + str(most_common[3][0]) if len(most_common) > 3 else str(4) + ) + get_fourth_most_common_count = ( + str(most_common[3][1]) if len(most_common) > 3 else str(0) + ) + get_frequent_table += ( + get_fourth_most_common_option + " " + get_fourth_most_common_count + ) return get_frequent_table # ランキング上位の計算 def calculate_order(get_data): - get_sorted_numbers_with_indices = sorted(enumerate(get_data), key=lambda x: x[1], reverse=True) + 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] @@ -171,13 +212,22 @@ j = 0 # 同率順位の考慮 # 最高"max位"までを表示する - if max >= MAX_PLAYER: - max = MAX_PLAYER + if max >= current_player: + max = current_player for i in range(max): if i + j >= max: # 参照する配列がこれ以上ない時 break - order_table += str(i + 1 + j) + "位" + " " + user[int(labels[i + j])] + " " + str(scores[i + j]) + "pt" + "\n" + order_table += ( + str(i + 1 + j) + + "位" + + " " + + user[int(labels[i + j])] + + " " + + str(scores[i + j]) + + "pt" + + "\n" + ) now_order = i + 1 + j # 今の順位 if i + j + 1 == max: # 先ほどorder_tableに挿入した配列が,最後であった時 break @@ -185,7 +235,14 @@ while str(scores[i + j]) == str(scores[i + 1 + j]): j += 1 order_table += ( - str(now_order) + "位" + " " + user[int(labels[i + j])] + " " + str(scores[i + j]) + "pt" + "\n" + str(now_order) + + "位" + + " " + + user[int(labels[i + j])] + + " " + + str(scores[i + j]) + + "pt" + + "\n" ) if i + j + 1 >= max: # (i+j)と比較する配列(i+j+1)がない時 break