import {characterRuleBookApi, ruleBookApi} from "@/mammon"; import { CharacterRuleBookResponse, CreateRuleBookRequest, RuleBookResponse, SetCharacterRuleBookRequest } from "@/generated/mammon"; import {defineStore} from "pinia"; import {ref, triggerRef} from "vue"; export type RuleBook = RuleBookResponse; export const useRuleBooksStore = defineStore('rule-books', () => { const ruleBooks = ref([]); const addRuleBook = (ruleBook: RuleBook) => { ruleBooks.value.push(ruleBook); triggerRef(ruleBooks); return ruleBook; }; const replaceRuleBook = (ruleBook: RuleBook) => { const index = ruleBooks.value.findIndex(rb => rb.ruleBookId === ruleBook.ruleBookId); if (index !== -1) { ruleBooks.value[index] = ruleBook; } triggerRef(ruleBooks); return ruleBook; }; const findById = (ruleBookId: string): RuleBook | undefined => ruleBooks.value.find(rb => rb.ruleBookId === ruleBookId); const create = (ruleBook: CreateRuleBookRequest) => ruleBookApi.createRuleBook(ruleBook).then(response => addRuleBook(response.data)); const update = (ruleBookId: string, ruleBook: CreateRuleBookRequest) => ruleBookApi.updateRuleBook(ruleBookId, ruleBook).then(response => replaceRuleBook(response.data)); const duplicate = (ruleBook: RuleBook) => create({ name: `${ruleBook.name} (copy)`, usedForAcquisitions: ruleBook.usedForAcquisitions, ledgerRefs: [...ruleBook.ledgerRefs], script: ruleBook.script, }); const remove = (ruleBookId: string) => ruleBookApi.deleteRuleBook(ruleBookId).then(() => { ruleBooks.value = ruleBooks.value.filter(rb => rb.ruleBookId !== ruleBookId); }); const refresh = () => ruleBookApi.findAllRuleBooks().then(response => ruleBooks.value = response.data); refresh(); return {ruleBooks, findById, create, update, duplicate, remove, refresh}; }) export type CharacterRuleBook = CharacterRuleBookResponse; export const useCharacterRuleBooksStore = defineStore('character-rule-books', () => { const characterRuleBooks = ref([]); const replaceCharacterRuleBook = (characterRuleBook: CharacterRuleBook) => { const index = characterRuleBooks.value.findIndex(crb => crb.character.characterId === characterRuleBook.character.characterId); if (index !== -1) { characterRuleBooks.value[index] = characterRuleBook; } else { characterRuleBooks.value.push(characterRuleBook); } triggerRef(characterRuleBooks); return characterRuleBook; }; const findByCharacterId = (characterId: number): CharacterRuleBook | undefined => characterRuleBooks.value.find(crb => crb.character.characterId === characterId); const setForCharacter = (characterId: number, ruleBook: SetCharacterRuleBookRequest) => characterRuleBookApi.setCharacterRuleBookForCharacter(characterId, ruleBook) .then(response => replaceCharacterRuleBook(response.data)); const refresh = () => characterRuleBookApi.findAllCharacterRuleBooks().then(response => characterRuleBooks.value = response.data); refresh(); return {characterRuleBooks, findByCharacterId, setForCharacter, refresh}; }) export const fetchScriptDefinitions = (): Promise => ruleBookApi.getScriptDefinitions({responseType: 'text'}).then(response => response.data);