Add button to import study presets as constants

This commit is contained in:
SpectralFlame 2023-04-25 00:22:40 -05:00 committed by cyip92
parent 9aa7175be0
commit e5eb8761c9
4 changed files with 113 additions and 5 deletions

View File

@ -0,0 +1,98 @@
<script>
import ModalWrapperChoice from "@/components/modals/ModalWrapperChoice";
export default {
name: "ImportTimeStudyConstants",
components: {
ModalWrapperChoice
},
data() {
return {
constantNames: [],
};
},
computed: {
presets: () => player.timestudy.presets.filter(p => p.studies !== ""),
names() {
// Study presets can contain non-alphanumeric characters, which aren't allowed in constants,
// so we replace all of those with underscores
return this.presets.map(p => `TSPreset__${p.name.replaceAll(/[^a-zA-Z_0-9]/gu, "_")}`);
}
},
methods: {
update() {
this.constantNames = [...player.reality.automator.constantSortOrder];
},
importConstants() {
for (let index = 0; index < this.presets.length; index++) {
AutomatorBackend.modifyConstant(this.names[index], this.presets[index].studies);
}
},
hasConflict(constantName) {
return this.constantNames.includes(constantName);
},
missedImports() {
let newCount = 0;
for (const name of this.names) {
if (!this.hasConflict(name)) newCount++;
}
return Math.clampMin(this.constantNames.length + newCount - AutomatorData.MAX_ALLOWED_CONSTANT_COUNT, 0);
},
// Shorten the string to less than 55 characters for UI purposes - but we shorten the middle since the
// beginning and end are both potentially useful to see
shortenString(str) {
if (str.length < 55) return str;
return `${str.substring(0, 12)}...${str.substring(str.length - 40, str.length)}`;
}
}
};
</script>
<template>
<ModalWrapperChoice
@confirm="importConstants"
>
<template #header>
Importing Time Study Presets as Constants
</template>
<div class="c-modal-message__text">
Confirming this modal will import all of your saved Time Study presets as new Automator constants.
Below are all the valid presets which will be imported, with the beginning and end of their contained
studies shown. Some names may be changed due to restrictions on constant name formatting.
<br>
<br>
<div
v-for="i in presets.length"
:key="i"
>
Name: {{ presets[i-1].name }} <b>{{ names[i-1] }}</b>
<br>
{{ shortenString(presets[i-1].studies) }}
<span
v-if="hasConflict(names[i-1])"
class="l-warn-text"
>
<br>
This will overwrite an existing constant!
</span>
<br>
<br>
</div>
<div
v-if="missedImports() > 0"
class="l-warn-text"
>
Due to the limit on constant count, {{ quantify("constant", missedImports()) }} will not be imported!
</div>
</div>
<template #confirm-text>
Import All
</template>
</ModalWrapperChoice>
</template>
<style scoped>
.l-warn-text {
color: var(--color-bad);
}
</style>

View File

@ -32,6 +32,9 @@ export default {
deleteAllConstants() {
if (this.constants.length > 0) Modal.clearAutomatorConstants.show();
},
importPresets() {
Modal.importTSConstants.show();
},
}
};
</script>
@ -58,6 +61,14 @@ export default {
>
Delete all constants
</PrimaryButton>
<br>
<br>
<PrimaryButton
class="c-delete-margin o-primary-btn--subtab-option"
@click="importPresets"
>
Import Time Study Presets
</PrimaryButton>
<div
:key="constants.length"
class="l-definition-container"

View File

@ -60,6 +60,7 @@ import GlyphShowcasePanelModal from "@/components/modals/GlyphShowcasePanelModal
import H2PModal from "@/components/modals/H2PModal";
import ImportAutomatorDataModal from "@/components/modals/ImportAutomatorDataModal";
import ImportSaveModal from "@/components/modals/ImportSaveModal";
import ImportTimeStudyConstants from "@/components/modals/ImportTimeStudyConstants";
import InformationModal from "@/components/modals/InformationModal";
import LoadGameModal from "@/components/modals/LoadGameModal";
import PelleEffectsModal from "@/components/modals/PelleEffectsModal";
@ -252,6 +253,7 @@ Modal.automatorScriptDelete = new Modal(DeleteAutomatorScriptModal);
Modal.automatorScriptTemplate = new Modal(AutomatorScriptTemplate);
Modal.switchAutomatorEditorMode = new Modal(SwitchAutomatorEditorModal);
Modal.clearAutomatorConstants = new Modal(ClearConstantsModal);
Modal.importTSConstants = new Modal(ImportTimeStudyConstants);
Modal.autobuyerEditModal = new Modal(AutobuyerEditModal);
Modal.shop = new Modal(StdStoreModal);
Modal.studyString = new Modal(StudyStringModal);

View File

@ -520,6 +520,7 @@ export const AutomatorBackend = {
// All modifications to constants should go these two methods in order to properly update both the constant prop and
// the sorting order prop while keeping them consistent with each other
modifyConstant(constantName, newValue) {
if (Object.keys(player.reality.automator.constants).length >= AutomatorData.MAX_ALLOWED_CONSTANT_COUNT) return;
player.reality.automator.constants[constantName] = newValue;
if (!player.reality.automator.constantSortOrder.includes(constantName)) {
player.reality.automator.constantSortOrder.push(constantName);
@ -704,11 +705,7 @@ export const AutomatorBackend = {
if (!ignore.constants) {
for (const constant of parsed.constants) {
const alreadyExists = player.reality.automator.constants[constant.key];
const canMakeNew = Object.keys(player.reality.automator.constants).length <
AutomatorData.MAX_ALLOWED_CONSTANT_COUNT;
if (alreadyExists || canMakeNew) {
this.modifyConstant(constant.key, constant.value);
}
if (alreadyExists) this.modifyConstant(constant.key, constant.value);
}
}