Added game start for single player, 3 and 9 card game types and protected the pages for only logged in players
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export const useBiscaStore = defineStore('bisca', () => {
|
||||
const isDealing = ref(false)
|
||||
const isGameRunning = ref(false)
|
||||
const gameType = ref(3)
|
||||
|
||||
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 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
|
||||
|
||||
playerScore.value = 0
|
||||
opponentScore.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 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 wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
|
||||
return {
|
||||
isDealing,
|
||||
isGameRunning,
|
||||
gameType,
|
||||
deck,
|
||||
playerHand,
|
||||
opponentHand,
|
||||
trumpCard,
|
||||
playerScore,
|
||||
opponentScore,
|
||||
currentTurn,
|
||||
table,
|
||||
startGame,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user