Meteorで簡単なリアルタイムマルチプレイヤー数学ゲームを作成しました。ここで試すことができます:http://mathplay.meteor.com
ローカルで(異なるブラウザを使用して)プレイする場合、すべてが正常に機能します。しかし、インターネットを介して友達とプレイすると、クライアントの同期がとれることがよくあります。あるプレーヤーに対してアクティブとしてリストされている質問は、実際には別のプレーヤーによってすでに解決されています。
私の推測では、サーバー専用であるはずの一部のコードは、代わりにクライアントの1つで実行されます。この動作をデバッグする方法に関する提案はありますか?
ユーザーが回答を送信すると、クライアントで何が起こるかを次に示します。
Template.number_input.events[okcancel_events('#answertextbox')] = make_okcancel_handler({
ok: function (text, event) {
question = Questions.findOne({ order_number: Session.get("current_question_order_number") });
if (question.answer == document.getElementById('answertextbox').value) {
console.log('True');
Questions.update(question._id, {$set: {text: question.text.substr(0, question.text.length - 1) + question.answer, player: Session.get("player_name")}});
callGetNewQuestion();
}
else {
console.log('False');
}
document.getElementById('answertextbox').value = "";
document.getElementById('answertextbox').focus();
}
});
callGetNewQuestion()は、クライアントとサーバーの両方でこれをトリガーします。
getNewQuestion: function () {
var nr1 = Math.round(Math.random() * 100);
var nr2 = Math.round(Math.random() * 100);
question_string = nr1 + " + " + nr2 + " = ?";
question_answer = (nr1 + nr2);
current_order_number = Questions.find({}).count() + 1;
current_question_id = Questions.insert({ order_number: current_order_number, text: question_string, answer: question_answer });
return Questions.findOne({_id: current_question_id});//current_question_id;
},
完全なソースコードは参照用にここにあります:https ://github.com/tomsoderlund/MathPlay