SyntaxSquad/DADProject#14 added protection to logout inside a game and going to another page
This commit is contained in:
@@ -4,7 +4,9 @@ import { ref } from 'vue'
|
||||
export const useBiscaStore = defineStore('bisca', () => {
|
||||
const isDealing = ref(false)
|
||||
const isGameRunning = ref(false)
|
||||
const isGameOver = ref(false)
|
||||
const gameType = ref(3)
|
||||
const isLoggingOut = ref(false)
|
||||
|
||||
const deck = ref([])
|
||||
const playerHand = ref([])
|
||||
@@ -15,6 +17,11 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
|
||||
const currentTurn = ref('player')
|
||||
|
||||
const playerTricks = ref(0)
|
||||
const opponentTricks = ref(0)
|
||||
|
||||
const winner = ref(null)
|
||||
|
||||
const table = ref({
|
||||
playerCard: null,
|
||||
opponentCard: null,
|
||||
@@ -27,9 +34,13 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
isDealing.value = true
|
||||
isGameRunning.value = true
|
||||
gameType.value = type
|
||||
isGameOver.value = false
|
||||
isLoggingOut.value = false
|
||||
|
||||
playerScore.value = 0
|
||||
opponentScore.value = 0
|
||||
playerTricks.value = 0
|
||||
opponentTricks.value = 0
|
||||
|
||||
deck.value = []
|
||||
playerHand.value = []
|
||||
@@ -55,6 +66,66 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
isDealing.value = false
|
||||
}
|
||||
|
||||
const resolveTrick = () => {
|
||||
const pCard = table.value.playerCard
|
||||
const oCard = table.value.opponentCard
|
||||
const trumpSuit = trumpCard.value.suit
|
||||
let trickWinner = ''
|
||||
|
||||
if (pCard.suit === trumpSuit && oCard.suit !== trumpSuit) trickWinner = 'player'
|
||||
else if (oCard.suit === trumpSuit && pCard.suit !== trumpSuit) trickWinner = 'opponent'
|
||||
else if (pCard.suit === oCard.suit) {
|
||||
trickWinner = getStrength(pCard.rank) > getStrength(oCard.rank) ? 'player' : 'opponent'
|
||||
} else {
|
||||
trickWinner = table.value.firstToPlay
|
||||
}
|
||||
|
||||
const points = pCard.points + oCard.points
|
||||
|
||||
if (trickWinner === 'player') {
|
||||
playerScore.value += points
|
||||
playerTricks.value++
|
||||
} else {
|
||||
opponentScore.value += points
|
||||
opponentTricks.value++
|
||||
}
|
||||
|
||||
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
||||
|
||||
if (
|
||||
deck.value.length === 0 &&
|
||||
playerHand.value.length === 0 &&
|
||||
opponentHand.value.length === 0
|
||||
) {
|
||||
endGame()
|
||||
return
|
||||
}
|
||||
|
||||
if (deck.value.length > 0) {
|
||||
if (trickWinner === 'player') {
|
||||
playerHand.value.push(deck.value.pop())
|
||||
opponentHand.value.push(deck.value.pop())
|
||||
} else {
|
||||
opponentHand.value.push(deck.value.pop())
|
||||
playerHand.value.push(deck.value.pop())
|
||||
}
|
||||
}
|
||||
|
||||
currentTurn.value = trickWinner
|
||||
if (trickWinner === 'opponent') {
|
||||
setTimeout(botPlayCard, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
const endGame = () => {
|
||||
isGameRunning.value = false
|
||||
isGameOver.value = true
|
||||
|
||||
if (playerScore.value > opponentScore.value) winner.value = 'player'
|
||||
else if (opponentScore.value > playerScore.value) winner.value = 'opponent'
|
||||
else winner.value = 'draw'
|
||||
}
|
||||
|
||||
const dealInitialCards = async () => {
|
||||
const cardsToDeal = gameType.value === 9 ? 9 : 3
|
||||
|
||||
@@ -108,6 +179,16 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
return strengthMap[rank] || 0
|
||||
}
|
||||
|
||||
const quitGame = () => {
|
||||
if (!isGameRunning.value) return
|
||||
opponentScore.value = 120
|
||||
playerScore.value = 0
|
||||
winner.value = 'opponent'
|
||||
|
||||
isGameRunning.value = false
|
||||
isGameOver.value = true
|
||||
}
|
||||
|
||||
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
|
||||
return {
|
||||
@@ -117,11 +198,18 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
deck,
|
||||
playerHand,
|
||||
opponentHand,
|
||||
isLoggingOut,
|
||||
trumpCard,
|
||||
playerScore,
|
||||
opponentScore,
|
||||
currentTurn,
|
||||
table,
|
||||
playerTricks,
|
||||
opponentTricks,
|
||||
isGameOver,
|
||||
winner,
|
||||
startGame,
|
||||
quitGame,
|
||||
resolveTrick,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user