From 084fa3e37ccc5ea495281b41ac33054f778f2489 Mon Sep 17 00:00:00 2001 From: Made Baruna Date: Tue, 18 May 2021 17:50:37 +0800 Subject: [PATCH] Handle save convert from google drive sync --- src/components/DataSync.svelte | 43 ++++++++++++++++++++++++++++++++-- src/routes/wish/index.svelte | 1 + src/stores/saveManager.js | 4 +++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/components/DataSync.svelte b/src/components/DataSync.svelte index 7fa5895e..d9186f27 100644 --- a/src/components/DataSync.svelte +++ b/src/components/DataSync.svelte @@ -89,13 +89,51 @@ } } + function convertTime(data) { + for (const pull of data.pulls) { + pull.time = dayjs.unix(pull.time).format('YYYY-MM-DD HH:mm:ss'); + } + + return data; + } + async function useRemoteData() { - for (const k in remoteSave) { - await updateSave(k, remoteSave[k], true); + // check if old local storage version + if (remoteSave['converted'] === undefined) { + for (const key in remoteSave) { + if (key.endsWith('ar') || key.endsWith('wl')) { + await updateSave(key, Number(remoteSave[key]), true); + } else if (key.endsWith('collectables-updated')) { + await updateSave(key, remoteSave[key] === 'true', true); + } else if ( + key.endsWith('accounts') || + key.endsWith('locale') || + key.endsWith('server') || + key.endsWith('update-time') + ) { + await updateSave(key, remoteSave[key], true); + } else if ( + key.endsWith('wish-counter-character-event') || + key.endsWith('wish-counter-weapon-event') || + key.endsWith('wish-counter-standard') || + key.endsWith('wish-counter-beginners') + ) { + const converted = convertTime(JSON.parse(remoteSave[key])); + await updateSave(key, converted, true); + } else { + await updateSave(key, JSON.parse(remoteSave[key]), true); + } + } + console.log('finished convert from google drive'); + } else { + for (const k in remoteSave) { + await updateSave(k, remoteSave[k], true); + } } synced.set(true); closeModal(); + window.location.reload(); } async function useLocalData() { @@ -269,6 +307,7 @@ document.body.appendChild(fileLink); fileLink.click(); } + diff --git a/src/routes/wish/index.svelte b/src/routes/wish/index.svelte index aa9743e1..ba5b56cf 100644 --- a/src/routes/wish/index.svelte +++ b/src/routes/wish/index.svelte @@ -33,6 +33,7 @@ }; $: if ($fromRemote) { + console.log('update from google drive'); readLocalData(); } diff --git a/src/stores/saveManager.js b/src/stores/saveManager.js index 1d99e54f..e7854f3e 100644 --- a/src/stores/saveManager.js +++ b/src/stores/saveManager.js @@ -101,7 +101,9 @@ const convertSave = async () => { } } - localStorage.setItem(CONVERTED_KEY, dayjs().toISOString()); + const convertedTime = dayjs().toISOString(); + localStorage.setItem(CONVERTED_KEY, convertedTime); + await localforage.setItem(CONVERTED_KEY, convertedTime); if (convertedCount > 0) { window.location.reload();