From c6c8903c5b393b4fbf447ef0255952bafbd60f3b Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Sun, 28 Apr 2024 17:13:04 +0200 Subject: [PATCH] redefine state in pinia --- src/main.ts | 6 +++ src/store/state.ts | 123 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/store/state.ts diff --git a/src/main.ts b/src/main.ts index 149aeac..ae79b6d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,6 @@ import { createApp } from 'vue' +import { createPinia } from 'pinia' +import piniaPluginPersistedState from 'pinia-plugin-persistedstate' import { store, key } from './store' import router from './router' import i18n from './i18n' @@ -7,7 +9,11 @@ import App from './App.vue' import 'bootstrap/dist/css/bootstrap.min.css' import 'bootstrap' +const pinia = createPinia() + .use(piniaPluginPersistedState) + createApp(App) + .use(pinia) .use(store, key) .use(router) .use(i18n) diff --git a/src/store/state.ts b/src/store/state.ts new file mode 100644 index 0000000..ebabb4d --- /dev/null +++ b/src/store/state.ts @@ -0,0 +1,123 @@ +import DifficultyLevel from '@/services/enum/DifficultyLevel' +import PlayerColor from '@/services/enum/PlayerColor' +import Slot from '@/services/enum/Slot' +import { defineStore } from 'pinia' +import { name } from '@/../package.json' + +export const useStateStore = defineStore(`${name}.store`, { + state: () => { + return { + language: 'en', + baseFontSize: 1.0, + setup: { + playerSetup: { + playerCount: 1, + botCount: 1, + playerColors: [PlayerColor.RED,PlayerColor.ORANGE,PlayerColor.BLUE,PlayerColor.GREEN] + }, + difficultyLevel: DifficultyLevel.L2_STANDARD + }, + rounds: [] + } as State + }, + actions: { + prepareRound(round: Round) { + // merge with state already stored in existing round persistence + const existingRound = this.rounds.find(item => item.round==round.round) + if (existingRound) { + if (existingRound.turns.length > 0) { + round.turns = existingRound.turns + } + if (existingRound.initialLunaStates.length > 0) { + round.initialLunaStates = existingRound.initialLunaStates + } + } + this.rounds = this.rounds.filter(item => item.round!=round.round) + this.rounds.push(round) + }, + turnPlayer(turn: Turn) { + const round = getRound(this, turn.round) + round.turns = round.turns.filter(item => item.round==turn.round + && (item.turn!=turn.turn || item.player!=turn.player)) + round.turns.push(turn) + if (turn.passed) { + // if passed: remove all stored later turns for this player + round.turns = round.turns.filter(item => item.round==turn.round + && (item.turn<=turn.turn || item.player!=turn.player)) + } + }, + turnBot(turn: Turn) { + const round = getRound(this, turn.round) + round.turns = round.turns.filter(item => item.round==turn.round + && (item.turn!=turn.turn || item.bot!=turn.bot)) + round.turns.push(turn) + if (turn.passed) { + // if passed: remove all stored later turns for this bot + round.turns = round.turns.filter(item => item.round==turn.round + && (item.turn<=turn.turn || item.bot!=turn.bot)) + } + }, + resetGame() { + this.rounds = [] + } + }, + persist: true +}) + +function getRound(state: State, roundNo: number) : Round { + let round = state.rounds.find(item => item.round==roundNo) + if (!round) { + console.log(`Persistence for round ${roundNo} not found.`) + round = {round: roundNo, turns: [], initialLunaStates: []} + } + return round +} + +export interface State { + language: string + baseFontSize: number + setup: Setup + rounds: Round[] +} +export interface Setup { + playerSetup: PlayerSetup + difficultyLevel: DifficultyLevel +} +export interface PlayerSetup { + playerCount: number + botCount: number + playerColors: PlayerColor[] +} +export interface Round { + round: number + initialLunaStates: LunaStatePersistence[] + turns: Turn[] +} +export interface Turn { + round: number + turn: number + player?: number + bot?: number + passed?: boolean + claimFirstPlayer?: boolean + lunaState?: LunaStatePersistence +} +export interface LunaStatePersistence { + cardDeck: CardDeckPersistence + heliumCount: number + researchSteps: number +} +export interface CardDeckPersistence { + pile: string[] + grade2: string[] + leftMajoritySlot?: string + rightMajoritySlot?: string + slots: CardSlotPersistence[] + discard: string[], + availableSlots: Slot[] +} +export interface CardSlotPersistence { + slot: Slot + card: string + flipped: boolean +}