SyntaxSquad/DADProject#16 fix: Surrender if leave
This commit is contained in:
@@ -91,7 +91,7 @@
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, onMounted, onUnmounted, watch, inject } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
|
||||
import { useSocketStore } from '@/stores/socket'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { toast } from 'vue-sonner'
|
||||
@@ -284,6 +284,32 @@ const handlePlayCard = (card) => {
|
||||
wsStore.playCard(gameId.value, card.id)
|
||||
}
|
||||
|
||||
const handleBeforeUnload = (event) => {
|
||||
if (shouldShowBoard.value && !gameOver.value) {
|
||||
event.preventDefault();
|
||||
event.returnValue = '';
|
||||
}
|
||||
};
|
||||
|
||||
onBeforeRouteLeave((to, from, next) => {
|
||||
if (gameOver.value || !shouldShowBoard.value) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
const answer = window.confirm(
|
||||
"If you leave, you will be register has 'losing' or 'surrendered', Are you sure you want to leave?"
|
||||
);
|
||||
|
||||
if (answer) {
|
||||
wsStore.surrender(gameId.value);
|
||||
wsStore.disconnect();
|
||||
next();
|
||||
} else {
|
||||
next(false);
|
||||
}
|
||||
});
|
||||
|
||||
// --- FIX: SURRENDER LOGIC ---
|
||||
const handleSurrender = () => {
|
||||
if(confirm('Are you sure you want to surrender? You will lose.')) {
|
||||
@@ -317,6 +343,8 @@ onMounted(async () => {
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('beforeunload', handleBeforeUnload)
|
||||
|
||||
if (timerInterval.value) clearInterval(timerInterval.value)
|
||||
wsStore.disconnect()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user