diff --git a/src/web/components/config-editor/components/sections/randomize-config-section.vue b/src/web/components/config-editor/components/sections/randomize-config-section.vue index b62141e..9d594d1 100644 --- a/src/web/components/config-editor/components/sections/randomize-config-section.vue +++ b/src/web/components/config-editor/components/sections/randomize-config-section.vue @@ -8,6 +8,7 @@ import ConfigBounds from "@/web/components/config-editor/components/inputs/confi import InitialConfigSection from "@/web/components/config-editor/components/sections/initial-config-section.vue"; import Flag from "@/web/components/config-editor/components/inputs/flag.vue"; import InputHeader from "@/web/components/config-editor/components/base/input-header.vue"; +import RandomizeConfigSnippets from "@/web/components/config-editor/components/widgets/randomize-config-snippets.vue"; const configStore = useConfigStore(); const { randomTypesConfig, typesConfig } = configStore; @@ -234,6 +235,11 @@ const randomizeTypesConfig = () => { +
+
+
+

Snippets

+ diff --git a/src/web/components/config-editor/components/widgets/randomize-config-snippets.vue b/src/web/components/config-editor/components/widgets/randomize-config-snippets.vue new file mode 100644 index 0000000..0f3f567 --- /dev/null +++ b/src/web/components/config-editor/components/widgets/randomize-config-snippets.vue @@ -0,0 +1,52 @@ + + + + + diff --git a/src/web/store/config.ts b/src/web/store/config.ts index d886998..1187d72 100644 --- a/src/web/store/config.ts +++ b/src/web/store/config.ts @@ -90,6 +90,13 @@ export const useConfigStore = defineStore("config", () => { setWorldConfigRaw(newConfig); } + const setRandomTypesConfig = (newConfig: RandomTypesConfig) => { + const buf = fullCopyObject(newConfig); + for (const i in newConfig) { + (randomTypesConfig.value[i as keyof RandomTypesConfig] as number) = buf[i as keyof RandomTypesConfig] as number; + } + } + const exportConfig = () => { return { worldConfig: worldConfigRaw, @@ -262,6 +269,7 @@ export const useConfigStore = defineStore("config", () => { setWorldConfig, randomizeTypesConfig, setDefaultTypesConfig, + setRandomTypesConfig, appendType, addTypesFromConfig, removeTypeFromConfig, diff --git a/src/web/store/local.ts b/src/web/store/local.ts new file mode 100644 index 0000000..120f266 --- /dev/null +++ b/src/web/store/local.ts @@ -0,0 +1,42 @@ +import { defineStore } from "pinia"; + +type StoredData = Record | unknown[]; + +export const useLocalStore = defineStore("local", () => { + const get = (alias: string) => { + return JSON.parse(localStorage.getItem(alias) as string); + } + + const set = (alias: string, value: StoredData) => { + localStorage.setItem(alias, JSON.stringify(value)); + } + + const remove = (alias: string) => { + localStorage.removeItem(alias); + } + + const addToArray = (alias: string, value: StoredData): number => { + const old = (get(alias) ?? []) as unknown[]; + set(alias, [...old, value]); + return old.length; + } + + const getFromArray = (alias: string, index: number): StoredData => { + const old = get(alias); + return old[index]; + } + + const removeFromArray = (alias: string, index: number) => { + const old = get(alias); + set(alias, [...old.slice(0, index), ...old.slice(index + 1)]); + } + + return { + set, + get, + remove, + addToArray, + getFromArray, + removeFromArray, + } +}); diff --git a/src/web/store/snippets.ts b/src/web/store/snippets.ts new file mode 100644 index 0000000..705642c --- /dev/null +++ b/src/web/store/snippets.ts @@ -0,0 +1,39 @@ +import type { RandomTypesConfig } from "@/lib/types/config"; +import { defineStore } from "pinia"; +import { useLocalStore } from "./local"; +import { ref } from "vue"; + +type StoredRandomTypesConfig = { + name: string; + value: RandomTypesConfig; +}; + +export const useSnippetsStore = defineStore("snippets", () => { + const localStore = useLocalStore(); + const randomTypesConfigList = ref(localStore.get('randomTypesConfig') ?? []); + + const updateRandomTypesConfigList = () => { + randomTypesConfigList.value = localStore.get('randomTypesConfig') ?? []; + } + + const getRandomTypesConfigItem = (index: number): StoredRandomTypesConfig => { + return localStore.getFromArray('randomTypesConfig', index) as StoredRandomTypesConfig; + } + + const addRandomTypesConfig = (config: RandomTypesConfig, name: string) => { + localStore.addToArray('randomTypesConfig', { name, value: config}); + updateRandomTypesConfigList(); + } + + const removeRandomTypesConfig = (index: number) => { + localStore.removeFromArray('randomTypesConfig', index); + updateRandomTypesConfigList(); + } + + return { + randomTypesConfigList, + getRandomTypesConfigItem, + addRandomTypesConfig, + removeRandomTypesConfig, + } +});