52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import type { ExtendedCharacter, Zone } from '@/types'
|
|
|
|
export const useZoneStore = defineStore('zone', {
|
|
state: () => {
|
|
return {
|
|
zone: null as Zone | null,
|
|
characters: [] as ExtendedCharacter[],
|
|
characterLoaded: false
|
|
}
|
|
},
|
|
getters: {
|
|
getCharacterById: (state) => {
|
|
return (id: number) => state.characters.find((char) => char.id === id)
|
|
},
|
|
getCharacterCount: (state) => {
|
|
return state.characters.length
|
|
},
|
|
isZoneSet: (state) => {
|
|
return state.zone !== null
|
|
}
|
|
},
|
|
actions: {
|
|
setZone(zone: Zone | null) {
|
|
this.zone = zone
|
|
},
|
|
setCharacters(characters: ExtendedCharacter[]) {
|
|
this.characters = characters
|
|
},
|
|
addCharacter(character: ExtendedCharacter) {
|
|
this.characters.push(character)
|
|
},
|
|
updateCharacter(updatedCharacter: ExtendedCharacter) {
|
|
const index = this.characters.findIndex((char) => char.id === updatedCharacter.id)
|
|
if (index !== -1) {
|
|
this.characters[index] = { ...this.characters[index], ...updatedCharacter }
|
|
}
|
|
},
|
|
removeCharacter(character_id: number) {
|
|
this.characters = this.characters.filter((char) => char.id !== character_id)
|
|
},
|
|
setCharacterLoaded(loaded: boolean) {
|
|
this.characterLoaded = loaded
|
|
},
|
|
reset() {
|
|
this.zone = null
|
|
this.characters = []
|
|
this.characterLoaded = false
|
|
}
|
|
}
|
|
})
|