diff --git a/flask/testapp/static/javascript/beautyVote.js b/flask/testapp/static/javascript/beautyVote.js index 209c8f3..95a5b74 100644 --- a/flask/testapp/static/javascript/beautyVote.js +++ b/flask/testapp/static/javascript/beautyVote.js @@ -2,15 +2,29 @@ class Question{ constructor(){ this.question = 0; - this.answer = 0; + this.answer1 = 0; + this.answer2 = 0; + this.answer3 = 0; + this.answer4 = 0; + this.people1 = 0; + this.people2 = 0; + this.people3 = 0; + this.people4 = 0; this.choiceA = 0; this.choiceB = 0; this.choiceC = 0; this.choiceD = 0; } - join(question, answer, choiceA, choiceB, choiceC, choiceD){ + join(question, answer1, answer2, answer3, answer4, people1, people2, people3, people4, choiceA, choiceB, choiceC, choiceD){ this.question = question; - this.answer = answer; + this.answer1 = answer1; + this.answer2 = answer2; + this.answer3 = answer3; + this.answer4 = answer4; + this.people1 = people1; + this.people2 = people2; + this.people3 = people3; + this.people4 = people4; this.choiceA = choiceA; this.choiceB = choiceB; this.choiceC = choiceC; @@ -20,7 +34,7 @@ class Player{ constructor(){ - this.points = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; + this.points = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; this.choice = 0; this.name = 0; this.number = 0; @@ -28,9 +42,7 @@ calcScore(){ var score = 0; for(var i = 0; i < MAX_ROUND; i ++){ - if(this.points[i] == CORRECT){ - score ++; - } + score += int(this.points[i]); } return score; } @@ -121,18 +133,29 @@ function setQuestion(round){ - question[round].join(questions[round], answer[round], choices[round][A], choices[round][B], choices[round][C], choices[round][D]); + question[round].join(questions[round], answer1[round], answer2[round], answer3[round], answer4[round], people1[round], people2[round], + people3[round], people4[round], choices[round][A], choices[round][B], choices[round][C], choices[round][D]); } function checkAnswer(){ - if(player.choice == question[round].answer){ - player.points[round] = CORRECT; + if(player.choice == question[round].answer1){ + player.points[round] = "4"; + } + else if(player.choice == question[round].answer2){ + player.points[round] = "3"; + } + else if(player.choice == question[round].answer3){ + player.points[round] = "2"; + } + else if(player.choice == question[round].answer4){ + player.points[round] = "1"; } else{ - player.points[round] = WRONG; + player.points[round] = "0"; } round ++; + console.log(player.points[round]) if(round < MAX_ROUND){ setQuestion(round); } @@ -231,11 +254,26 @@ if(xhr.responseText=="WAIT"){ console.log(xhr.responseText); } - //正解選択肢を取得 + //正解選択肢を取得(全ての計算を取得) else{ timer = 0; console.log(xhr.responseText); - question[round].answer = xhr.responseText; + answerTable = xhr.responseText + let splitAnswer = answerTable.split("\n"); + let arr_answer1 = splitAnswer[0].split(" "); // 空白で文字列を分割 + question[round].answer1 = arr_answer1[0]; + question[round].people1 = arr_answer1[1]; + let arr_answer2 = splitAnswer[1].split(" "); // 空白で文字列を分割 + question[round].answer2 = arr_answer2[0]; + question[round].people2 = arr_answer2[1]; + let arr_answer3 = splitAnswer[2].split(" "); // 空白で文字列を分割 + question[round].answer3 = arr_answer3[0]; + question[round].people3 = arr_answer3[1]; + 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) @@ -286,6 +324,11 @@ var C = 2; var D = 3; var E = 4; +var first = 0; +var second = 1; +var third = 2; +var forth = 3; + var CORRECT = 1; var WRONG = 0; @@ -326,7 +369,15 @@ "ジンベイザメは英語で何シャーク?", "心臓がない生き物は?"]; -var answer = [C, A, B, B, C, C, C, C, B, B]; +var answer1 = [0]*MAX_ROUND; +var answer2 = [0]*MAX_ROUND; +var answer3 = [0]*MAX_ROUND; +var answer4 = [0]*MAX_ROUND; + +var people1 = [0]*MAX_ROUND; +var people2 = [0]*MAX_ROUND; +var people3 = [0]*MAX_ROUND; +var people4 = [0]*MAX_ROUND; var choices = [["がっくり", "げっそり", "むっつり", "しっとり"], ["ドナルド・トランプ", "ボリス・ジョンソン", "ウラジーミル・プーチン", "アンゲラ・メルケル"], diff --git a/flask/testapp/views.py b/flask/testapp/views.py index 31cd020..b391f49 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -19,6 +19,9 @@ # ゲーム中の選択肢を保存 choices = [] +# 各回のゲーム結果を保存 +frequent_table = "" + # ユーザーを識別するための番号(実際の初期値は0にする) user_number = 0 @@ -31,7 +34,7 @@ @app.route("/form", methods=["GET", "POST"]) def index(): - global 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 if request.method == "GET": return render_template("testapp/index.html") @@ -64,8 +67,7 @@ print("Received choice:", choices) if len(choices) >= MAX_PLAYER: - most_common_option = calculate_majority(choices) - print("Answer:", most_common_option) + frequent_table = calculate_majority(choices) return "WAIT" # 他の人が終わるまで待機 @@ -74,7 +76,8 @@ return "WAIT" # 全員のデータが集まった場合は,多数派を計算 else: - return str(most_common_option) + print(frequent_table) + return frequent_table # ゲーム終了時,識別番号とスコアを受け取る if request.form.get("number") and request.form.get("score"): @@ -115,9 +118,27 @@ # 多数派の計算 def calculate_majority(get_data): counter = Counter(get_data) + get_frequent_table = "" # 最も多く出現する要素を取得する - get_most_common_option = counter.most_common(1)[0][0] - return get_most_common_option + 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(4) + 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(4) + 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(4) + get_frequent_table += get_fourth_most_common_option + " " + get_fourth_most_common_count + return get_frequent_table # ランキング上位の計算