216 lines
4.9 KiB
JavaScript
216 lines
4.9 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
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([])
|
|
const opponentHand = ref([])
|
|
const trumpCard = ref(null)
|
|
const playerScore = ref(0)
|
|
const opponentScore = ref(0)
|
|
|
|
const currentTurn = ref('player')
|
|
|
|
const playerTricks = ref(0)
|
|
const opponentTricks = ref(0)
|
|
|
|
const winner = ref(null)
|
|
|
|
const table = ref({
|
|
playerCard: null,
|
|
opponentCard: null,
|
|
firstToPlay: null,
|
|
})
|
|
|
|
const startGame = async (type = 3) => {
|
|
if (isDealing.value) return
|
|
|
|
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 = []
|
|
opponentHand.value = []
|
|
trumpCard.value = null
|
|
|
|
currentTurn.value = 'player'
|
|
|
|
table.value = {
|
|
playerCard: null,
|
|
opponentCard: null,
|
|
firstToPlay: null,
|
|
}
|
|
|
|
deck.value = createDeck()
|
|
|
|
trumpCard.value = deck.value[0]
|
|
|
|
await wait(500)
|
|
|
|
await dealInitialCards()
|
|
|
|
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
|
|
|
|
const dealSpeed = gameType.value === 9 ? 200 : 500
|
|
|
|
for (let i = 0; i < cardsToDeal; i++) {
|
|
if (deck.value.length > 0) {
|
|
const card = deck.value.pop()
|
|
playerHand.value.push(card)
|
|
await wait(dealSpeed)
|
|
}
|
|
|
|
if (deck.value.length > 0) {
|
|
const card = deck.value.pop()
|
|
opponentHand.value.push(card)
|
|
await wait(dealSpeed)
|
|
}
|
|
}
|
|
}
|
|
|
|
const createDeck = () => {
|
|
const suits = ['c', 'e', 'o', 'p']
|
|
const ranks = [1, 2, 3, 4, 5, 6, 7, 11, 12, 13]
|
|
let newDeck = []
|
|
|
|
for (const suit of suits) {
|
|
for (const rank of ranks) {
|
|
newDeck.push({
|
|
id: `${suit}-${rank}`,
|
|
suit,
|
|
rank,
|
|
points: getPoints(rank),
|
|
strength: getStrength(rank),
|
|
})
|
|
}
|
|
}
|
|
return newDeck.sort(() => Math.random() - 0.5)
|
|
}
|
|
|
|
const getPoints = (rank) => {
|
|
if (rank === 1) return 11
|
|
if (rank === 7) return 10
|
|
if (rank === 13) return 4
|
|
if (rank === 11) return 3
|
|
if (rank === 12) return 2
|
|
return 0
|
|
}
|
|
|
|
const getStrength = (rank) => {
|
|
const strengthMap = { 1: 10, 7: 9, 13: 8, 11: 7, 12: 6, 6: 5, 5: 4, 4: 3, 3: 2, 2: 1 }
|
|
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 {
|
|
isDealing,
|
|
isGameRunning,
|
|
gameType,
|
|
deck,
|
|
playerHand,
|
|
opponentHand,
|
|
isLoggingOut,
|
|
trumpCard,
|
|
playerScore,
|
|
opponentScore,
|
|
currentTurn,
|
|
table,
|
|
playerTricks,
|
|
opponentTricks,
|
|
isGameOver,
|
|
winner,
|
|
startGame,
|
|
quitGame,
|
|
resolveTrick,
|
|
}
|
|
})
|