class Question{
constructor(){
this.question = 0;
this.answer = 0;
this.choiceA = 0;
this.choiceB = 0;
this.choiceC = 0;
this.choiceD = 0;
}
join(question, answer, choiceA, choiceB, choiceC, choiceD){
this.question = question;
this.answer = answer;
this.choiceA = choiceA;
this.choiceB = choiceB;
this.choiceC = choiceC;
this.choiceD = choiceD;
}
}
class Player{
constructor(){
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;
for(var i = 0; i < MAX_ROUND; i ++){
if(this.points[i] == CORRECT){
score ++;
}
}
return score;
}
}
function setup(){
canvasSize(960, 1200);
lineW(3);
timer = 0;
setQuestion(round);
}
function mainloop(){
timer ++;
switch(scene){
case START_SCENE:
inputName();
break;
case GAME_SCENE:
if(timer > TIME_LIMIT){
player.choice = E;
sendChoice(player.choice) // 変更
checkAnswer();
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 ++;
if(50 <= tapX && tapX <= 450 && 300 <= tapY && tapY <= 550){
player.choice = A;
sendChoice();
}
else if(500 <= tapX && tapX <= 900 && 300 <= tapY && tapY <= 550){
player.choice = B;
sendChoice();
}
else if(50 <= tapX && tapX <= 450 && 600 <= tapY && tapY <= 850){
player.choice = C;
sendChoice();
}
else if(500 <= tapX && tapX <= 900 && 600 <= tapY && tapY <= 850){
player.choice = D;
sendChoice();
}
}
break;
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]);
}
function checkAnswer(){
if(player.choice == question[round].answer){
player.points[round] = CORRECT;
}
else{
player.points[round] = WRONG;
}
round ++;
if(round < MAX_ROUND){
setQuestion(round);
}
}
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() { //受信用
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 sendChoice(){
timer = 0;
scene = WAIT_SCENE; //nemoto追記
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);
}
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); //変更
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);
}
}
}
}
// 追記(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() { //受信用
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
postForm();
}
}
}
}
}
var xhr = new XMLHttpRequest();
var round = 0; //変更
var MAX_ROUND = 5;
var timer = 0;
var TIME_LIMIT = 900;
var A = 0;
var B = 1;
var C = 2;
var D = 3;
var E = 4;
var CORRECT = 1;
var WRONG = 0;
var START_SCENE = 1;
var GAME_SCENE = 2;
var RESULT_SCENE = 3;
var WAIT_SCENE = 4; // nemoto追記
var scene = START_SCENE;
var my_order = 8;
var ranking_order = ["ルフィ", "ゾロ", "ジンベエ", "サンジ", "ロビン", "ウソップ", "フランキー", "ブルック", "ナミ", "チョッパー"]
var scoreing_order = [3000000000,1110000000, 1100000000, 1032000000, 930000000, 500000000, 394000000, 383000000, 366000000, 1000]
var MAX_ORDER = 10;
var question = new Array(MAX_ROUND);
for(var i = 0; i < MAX_ROUND+1; i ++){
question[i] = new Question();
}
var player = new Player();
var questions = ["「にっこり」の対義語は?",
"もっとも年齢が高いのは?",
"中国語で「勉強」意味は?",
"焼き肉の「テッポウ」はどこの部位?",
"アフリカ大陸でもっとも面積が大きい国は?",
"実在する生き物は?",
"カスタネットの名前の由来となったものは?",
"名前が人物に由来しているのは?",
"ジンベイザメは英語で何シャーク?",
"心臓がない生き物は?"];
var answer = [C, A, B, B, C, C, C, C, B, B];
var choices = [["がっくり", "げっそり", "むっつり", "しっとり"],
["ドナルド・トランプ", "ボリス・ジョンソン", "ウラジーミル・プーチン", "アンゲラ・メルケル"],
["喧嘩っ早い", "無理やり", "細かい", "意地悪な"],
["食道", "直腸", "こめかみ", "胃袋"],
["エチオピア", "リビア", "アルジェリア", "ケニア"],
["キリンコウモリ", "ライオンコウモリ", "ウサギコウモリ", "オオカミコウモリ"],
["ハサミの刃", "貝の殻", "栗の実", "両手"],
["そら豆", "えんどう豆", "いんげん豆", "ひよこ豆"],
["エレファント", "ホエール", "ダイナソー", "メガ"],
["カニ", "ナマコ", "タコ", "ホタテ"]];
// 送信用
// var form = document.createElement('form');
// var request = document.createElement('input');