From d14fe3f972f14890ec81005afdd5b6455a191bad Mon Sep 17 00:00:00 2001 From: Made Baruna Date: Mon, 19 Apr 2021 01:12:31 +0800 Subject: [PATCH] Update character list from wish history - close #36 --- src/functions/character.js | 38 --------------- src/routes/characters/[id].svelte | 69 ++++++++++++++++++++++++++-- src/routes/characters/index.svelte | 74 +++++++++++++++++++++++++++++- src/routes/wish/_summary.svelte | 31 ++++++++++++- 4 files changed, 167 insertions(+), 45 deletions(-) delete mode 100644 src/functions/character.js diff --git a/src/functions/character.js b/src/functions/character.js deleted file mode 100644 index 7069cda0..00000000 --- a/src/functions/character.js +++ /dev/null @@ -1,38 +0,0 @@ -import { getAccountPrefix } from "../stores/account"; -import { readSave, updateSave } from "../stores/saveManager"; - -const bannerCategories = ['beginners', 'standard', 'character-event', 'weapon-event']; - -function readLocalData(path) { - const prefix = getAccountPrefix(); - const data = readSave(`${prefix}${path}`); - if (data !== null) { - const counterData = JSON.parse(data); - const pullData = counterData.pulls || []; - - return pullData; - } - - return null; -} - -export function processCharacters() { - const characters = {}; - for (const id of bannerCategories) { - const data = readLocalData(`wish-counter-${id}`); - if (data === null) continue; - - for (const item of data) { - if (item.type === 'character') { - if (characters[item.id] === undefined) { - characters[item.id] = 0; - } - - characters[item.id]++; - } - } - } - - const prefix = getAccountPrefix(); - updateSave(`${prefix}characters`, JSON.stringify(characters)); -} \ No newline at end of file diff --git a/src/routes/characters/[id].svelte b/src/routes/characters/[id].svelte index aeb53559..ce25df2c 100644 --- a/src/routes/characters/[id].svelte +++ b/src/routes/characters/[id].svelte @@ -17,7 +17,7 @@ import Icon from '../../components/Icon.svelte'; import Button from '../../components/Button.svelte'; import { getAccountPrefix } from '../../stores/account'; - import { readSave } from '../../stores/saveManager'; + import { readSave, updateSave } from '../../stores/saveManager'; import { characters } from '../../data/characters'; import { itemGroup } from '../../data/itemGroup'; @@ -26,6 +26,44 @@ let constellationDiv; + const defaultChars = { + amber: { + default: 1, + wish: 0, + manual: 0, + }, + kaeya: { + default: 1, + wish: 0, + manual: 0, + }, + lisa: { + default: 1, + wish: 0, + manual: 0, + }, + traveler_geo: { + default: 1, + wish: 0, + manual: 0, + }, + traveler_anemo: { + default: 1, + wish: 0, + manual: 0, + }, + barbara: { + default: 1, + wish: 0, + manual: 0, + }, + xiangling: { + default: 1, + wish: 0, + manual: 0, + }, + }; + const numberFormat = Intl.NumberFormat('en', { maximumFractionDigits: 2, minimumFractionDigits: 0, @@ -36,6 +74,7 @@ const book = itemGroup[bookId]; const materials = character.ascension[1].items; + let chars = {}; let constellationCount = -1; let manualCount = 0; let editConstallation = false; @@ -49,11 +88,12 @@ const data = readSave(`${prefix}characters`); if (data !== null) { const constellation = JSON.parse(data); + chars = constellation; if (constellation[id]) { - constellationCount = constellationCount[id].default + constellationCount[id].wish - 1; - manualCount = constellationCount[id].manual; + constellationCount = constellation[id].default + constellation[id].wish - 1; + manualCount = constellation[id].manual; } else { - constellationCount = 0; + constellationCount = -1; } } } @@ -64,6 +104,27 @@ function saveConstellationCount() { editConstallation = false; + if (chars[id]) { + chars[id].manual = manualCount; + } else if (defaultChars[id]) { + chars[id] = { + ...defaultChars[id], + manual: manualCount, + }; + } else { + chars[id] = { + default: 0, + wish: 0, + manual: manualCount, + }; + } + + if (chars[id].default + chars[id].wish + chars[id].manual === 0) { + delete chars[id]; + } + + const prefix = getAccountPrefix(); + updateSave(`${prefix}characters`, JSON.stringify(chars)); } function scrollToView(view) { diff --git a/src/routes/characters/index.svelte b/src/routes/characters/index.svelte index e9c87bd7..ccceb815 100644 --- a/src/routes/characters/index.svelte +++ b/src/routes/characters/index.svelte @@ -8,7 +8,7 @@ import { characters } from '../../data/characters'; import { getAccountPrefix } from '../../stores/account'; - import { readSave } from '../../stores/saveManager'; + import { readSave, updateSave } from '../../stores/saveManager'; let sortBy = ''; let sortOrder = false; @@ -71,12 +71,82 @@ } } + function processWishes() { + const chars = { + amber: { + default: 1, + wish: 0, + manual: 0, + }, + kaeya: { + default: 1, + wish: 0, + manual: 0, + }, + lisa: { + default: 1, + wish: 0, + manual: 0, + }, + traveler_geo: { + default: 1, + wish: 0, + manual: 0, + }, + traveler_anemo: { + default: 1, + wish: 0, + manual: 0, + }, + barbara: { + default: 1, + wish: 0, + manual: 0, + }, + xiangling: { + default: 1, + wish: 0, + manual: 0, + }, + }; + + const bannerCategories = ['beginners', 'standard', 'character-event', 'weapon-event']; + const prefix = getAccountPrefix(); + for (const id of bannerCategories) { + const data = readSave(`${prefix}wish-counter-${id}`); + if (data !== null) { + showConstellation = true; + const counterData = JSON.parse(data); + const pullData = counterData.pulls || []; + for (const pull of pullData) { + if (pull.type === 'character') { + if (chars[pull.id] === undefined) { + chars[pull.id] = { + default: 0, + wish: 0, + manual: 0, + }; + } + chars[pull.id].wish++; + } + } + } + } + + if (showConstellation) { + constellation = chars; + updateSave(`${prefix}characters`, JSON.stringify(chars)); + } + } + function getConstellation() { const prefix = getAccountPrefix(); const data = readSave(`${prefix}characters`); if (data !== null) { constellation = JSON.parse(data); showConstellation = true; + } else { + processWishes(); } } @@ -144,7 +214,7 @@ > {#if constellation[id]} - C{Math.min(constellation[id] - 1, 6)} + C{Math.max(0, (constellation[id].default + constellation[id].wish + constellation[id].manual) - 1)} {/if} {char.element.name} diff --git a/src/routes/wish/_summary.svelte b/src/routes/wish/_summary.svelte index 484f24cb..9054f4f0 100644 --- a/src/routes/wish/_summary.svelte +++ b/src/routes/wish/_summary.svelte @@ -9,7 +9,7 @@ import { bannerTypes } from '../../data/bannerTypes'; import { getAccountPrefix } from '../../stores/account'; - import { readSave, updateTime, fromRemote } from '../../stores/saveManager'; + import { readSave, updateTime, fromRemote, updateSave } from '../../stores/saveManager'; import SummaryItem from './_summaryItem.svelte'; import Icon from '../../components/Icon.svelte'; import { mdiEarth } from '@mdi/js'; @@ -43,6 +43,18 @@ monthlyData = {}; + // collected characters stuff + let updateCollectedCharacters = false; + let collectedCharacters = {}; + const collectedCharactersData = readSave(`${prefix}characters`); + if (collectedCharactersData !== null) { + collectedCharacters = JSON.parse(collectedCharactersData); + for (const collectedId of Object.keys(collectedCharacters)) { + collectedCharacters[collectedId].wish = 0; + } + updateCollectedCharacters = true; + } + for (let type of types) { const path = `wish-counter-${type.id}`; const data = readSave(`${prefix}${path}`); @@ -70,6 +82,19 @@ rarity = characters[pull.id].rarity; itemName = characters[pull.id].name; currentType = 'character'; + + // collected characters stuff + if (updateCollectedCharacters) { + if (collectedCharacters[pull.id]) { + collectedCharacters[pull.id].wish += 1; + } else { + collectedCharacters[pull.id] = { + default: 0, + manual: 0, + wish: 1, + }; + } + } } else if (pull.type === 'weapon') { rarity = weaponList[pull.id].rarity; itemName = weaponList[pull.id].name; @@ -134,6 +159,10 @@ } } + if (updateCollectedCharacters) { + updateSave(`${prefix}characters`, JSON.stringify(collectedCharacters)); + } + console.log(avg); loading = false; }