修复了第一题游戏显示异常的bug

This commit is contained in:
Coldin04 2025-04-25 19:43:10 +08:00
parent 3baa43a147
commit 1447d2f16d

View file

@ -40,13 +40,14 @@
<main v-if="isGameActive"> <main v-if="isGameActive">
<div class="players-info"> <div class="players-info">
<div class="player local" :class="{ 'player-turn': localPlayerTurn }"> <div class="player local">
<h3>我方</h3> <h3>我方</h3>
<p class="score">得分: {{ localScore }}</p> <p class="score">得分: {{ localScore }}</p>
</div> </div>
<div class="player remote" :class="{ 'player-turn': !localPlayerTurn }"> <div class="player remote">
<h3>对方</h3> <h3>对方</h3>
<p class="score">得分: {{ remoteScore }}</p> <p class="score">得分: {{ remoteScore }}</p>
<p v-if="remoteIsAnswering" class="status">对方正在答题...</p>
</div> </div>
</div> </div>
@ -55,8 +56,8 @@
<input type="text" v-model="userAnswer" @keyup.enter="submitAnswer" <input type="text" v-model="userAnswer" @keyup.enter="submitAnswer"
:class="{'correct-answer': answerFeedback === 'correct', :class="{'correct-answer': answerFeedback === 'correct',
'wrong-answer': answerFeedback === 'wrong'}" 'wrong-answer': answerFeedback === 'wrong'}"
:disabled="!isGameActive || !localPlayerTurn" /> :disabled="!isGameActive || waitingForNextQuestion" />
<button @click="submitAnswer" :disabled="!isGameActive || !localPlayerTurn">提交答案</button> <button @click="submitAnswer" :disabled="!isGameActive || waitingForNextQuestion">提交答案</button>
</section> </section>
<div class="feedback-container"> <div class="feedback-container">
@ -122,12 +123,15 @@ const userAnswer = ref<string>('');
const timeLeft = ref<number>(30); const timeLeft = ref<number>(30);
const localScore = ref<number>(0); const localScore = ref<number>(0);
const remoteScore = ref<number>(0); const remoteScore = ref<number>(0);
const localPlayerTurn = ref<boolean>(true);
const answerFeedback = ref<'none' | 'correct' | 'wrong'>('none'); const answerFeedback = ref<'none' | 'correct' | 'wrong'>('none');
const lastCorrectAnswer = ref<string>(''); const lastCorrectAnswer = ref<string>('');
const timerInterval = ref<number | null>(null); const timerInterval = ref<number | null>(null);
const gameMessage = ref<string>(''); const gameMessage = ref<string>('');
const gameMessageType = ref<string>(''); const gameMessageType = ref<string>('');
const waitingForNextQuestion = ref<boolean>(false);
const questionNumber = ref<number>(0);
const remoteIsAnswering = ref<boolean>(false);
const waitingForSettingsAck = ref<boolean>(false);
// //
const resultMessage = computed(() => { const resultMessage = computed(() => {
@ -206,7 +210,6 @@ function connectToPeer() {
const conn = peer.value.connect(remotePeerId.value, { const conn = peer.value.connect(remotePeerId.value, {
reliable: true reliable: true
}); });
handleConnection(conn); handleConnection(conn);
} catch (error) { } catch (error) {
console.error('连接失败:', error); console.error('连接失败:', error);
@ -222,11 +225,6 @@ function handleConnection(conn: DataConnection) {
isConnected.value = true; isConnected.value = true;
connectionStatus.value = '连接成功!'; connectionStatus.value = '连接成功!';
showGameMessage('连接成功!', 'success'); showGameMessage('连接成功!', 'success');
//
if (mode.value === 'join') {
localPlayerTurn.value = false;
}
}); });
conn.on('data', (data: any) => { conn.on('data', (data: any) => {
@ -253,43 +251,70 @@ function handleConnection(conn: DataConnection) {
function startGame() { function startGame() {
if (!isConnected.value || !connection.value) return; if (!isConnected.value || !connection.value) return;
//
timeLeft.value = 30;
localScore.value = 0;
remoteScore.value = 0;
gameEnded.value = false;
waitingForNextQuestion.value = false;
questionNumber.value = 0;
remoteIsAnswering.value = false;
//
waitingForSettingsAck.value = true;
//
showGameMessage('正在等待对方准备...', 'info');
// //
connection.value.send({ connection.value.send({
type: 'game-settings', type: 'game-settings',
difficulty: selectedDifficulty.value difficulty: selectedDifficulty.value
}); });
}
// //
timeLeft.value = 30; function actuallyStartGame() {
localScore.value = 0; //
remoteScore.value = 0;
isGameActive.value = true; isGameActive.value = true;
gameEnded.value = false; waitingForSettingsAck.value = false;
localPlayerTurn.value = mode.value === 'host'; //
//
generateNewQuestion();
// //
startTimer(); startTimer();
// //
if (connection.value) {
connection.value.send({ connection.value.send({
type: 'game-started' type: 'game-started'
}); });
} }
showGameMessage('游戏开始!', 'success');
//
if (mode.value === 'host') {
generateNewQuestion();
}
}
// //
function generateNewQuestion() { function generateNewQuestion() {
try { try {
currentQuestion.value = generateQuestion(parseInt(selectedDifficulty.value)); currentQuestion.value = generateQuestion(parseInt(selectedDifficulty.value));
questionNumber.value++;
waitingForNextQuestion.value = false;
userAnswer.value = '';
answerFeedback.value = 'none';
remoteIsAnswering.value = false;
// //
if (localPlayerTurn.value && connection.value) { if (mode.value === 'host' && connection.value) {
connection.value.send({ connection.value.send({
type: 'new-question', type: 'new-question',
question: currentQuestion.value.question, question: currentQuestion.value.question,
answer: currentQuestion.value.answer answer: currentQuestion.value.answer,
questionNumber: questionNumber.value
}); });
} }
} catch (error) { } catch (error) {
@ -300,7 +325,7 @@ function generateNewQuestion() {
// //
function submitAnswer() { function submitAnswer() {
if (!isGameActive.value || !localPlayerTurn.value) return; if (!isGameActive.value || waitingForNextQuestion.value) return;
const isCorrect = currentQuestion.value.answer.toString() === userAnswer.value; const isCorrect = currentQuestion.value.answer.toString() === userAnswer.value;
lastCorrectAnswer.value = currentQuestion.value.answer.toString(); lastCorrectAnswer.value = currentQuestion.value.answer.toString();
@ -312,21 +337,25 @@ function submitAnswer() {
answerFeedback.value = 'wrong'; answerFeedback.value = 'wrong';
} }
//
waitingForNextQuestion.value = true;
// //
if (connection.value) { if (connection.value) {
connection.value.send({ connection.value.send({
type: 'answer-result', type: 'answer-result',
isCorrect, isCorrect,
score: localScore.value score: localScore.value,
questionNumber: questionNumber.value
}); });
} }
// //
localPlayerTurn.value = false; if (mode.value === 'host') {
setTimeout(() => {
//
generateNewQuestion(); generateNewQuestion();
userAnswer.value = ''; }, 1500);
}
// //
setTimeout(() => { setTimeout(() => {
@ -338,8 +367,15 @@ function submitAnswer() {
function startTimer() { function startTimer() {
clearTimerInterval(); clearTimerInterval();
//
if (timeLeft.value <= 0) {
timeLeft.value = 30;
}
timerInterval.value = window.setInterval(() => { timerInterval.value = window.setInterval(() => {
if (timeLeft.value > 0) {
timeLeft.value--; timeLeft.value--;
}
if (timeLeft.value <= 0) { if (timeLeft.value <= 0) {
clearTimerInterval(); clearTimerInterval();
@ -379,6 +415,14 @@ function resetGame() {
gameEnded.value = false; gameEnded.value = false;
userAnswer.value = ''; userAnswer.value = '';
answerFeedback.value = 'none'; answerFeedback.value = 'none';
waitingForNextQuestion.value = false;
questionNumber.value = 0;
remoteIsAnswering.value = false;
waitingForSettingsAck.value = false;
if (timerInterval.value !== null) {
clearInterval(timerInterval.value);
timerInterval.value = null;
}
} }
// //
@ -389,6 +433,20 @@ function handleReceivedData(data: any) {
case 'game-settings': case 'game-settings':
// //
selectedDifficulty.value = data.difficulty; selectedDifficulty.value = data.difficulty;
//
if (connection.value) {
connection.value.send({
type: 'settings-ack'
});
}
break;
case 'settings-ack':
//
if (waitingForSettingsAck.value) {
actuallyStartGame();
}
break; break;
case 'game-started': case 'game-started':
@ -405,21 +463,23 @@ function handleReceivedData(data: any) {
question: data.question, question: data.question,
answer: data.answer answer: data.answer
}; };
// questionNumber.value = data.questionNumber;
localPlayerTurn.value = true; waitingForNextQuestion.value = false;
userAnswer.value = '';
answerFeedback.value = 'none';
remoteIsAnswering.value = false;
break; break;
case 'answer-result': case 'answer-result':
// //
remoteScore.value = data.score; remoteScore.value = data.score;
remoteIsAnswering.value = true;
if (data.isCorrect) { if (data.isCorrect) {
showGameMessage('对方答对了!', 'info'); showGameMessage('对方答对了!', 'info');
} else { } else {
showGameMessage('对方答错了!', 'info'); showGameMessage('对方答错了!', 'info');
} }
//
localPlayerTurn.value = true;
generateNewQuestion();
break; break;
case 'game-ended': case 'game-ended':