From 8fb8418becd18beb6d086668ccca6f3f178bcd79 Mon Sep 17 00:00:00 2001 From: Jimmy Gaussen Date: Mon, 26 Jul 2021 00:53:25 +0200 Subject: [PATCH 1/3] feat: corruption loadout import/export --- Synergism.css | 12 ++++++++++++ src/Corruptions.ts | 30 ++++++++++++++++++++++++++++-- src/History.ts | 16 ++++++++++++++++ src/ImportExport.ts | 23 +++-------------------- src/Utility.ts | 24 ++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 22 deletions(-) diff --git a/Synergism.css b/Synergism.css index 0da30cf5b..1f5ea824f 100644 --- a/Synergism.css +++ b/Synergism.css @@ -2462,6 +2462,18 @@ p[id$="BlessingsTotal"] { min-width: 1212px; } +#historyAscendTable button { + font-size: 14px; + padding: 3px 4px; + border: 1px solid pink; + margin: 0 5px; + line-height: 18px; +} + +#historyAscendTable button:hover { + background-color: #444; +} + .runeBlessingPurchaseSettings { display: flex; justify-content: center; diff --git a/src/Corruptions.ts b/src/Corruptions.ts index 4ecad93a7..1d317aaaa 100644 --- a/src/Corruptions.ts +++ b/src/Corruptions.ts @@ -201,10 +201,15 @@ export const corruptionLoadoutTableCreate = () => { } if (i === 0) { let cell = row.insertCell(); - //empty + let btn = document.createElement("button"); + btn.className = "corrSave" + btn.textContent = "Import" + btn.onclick = () => corruptionLoadoutImportPrompt(); + cell.appendChild(btn); + cell.title = "Import a loadout configuration string to be used on your next ascension" cell = row.insertCell(); - let btn = document.createElement("button"); + btn = document.createElement("button"); btn.className = "corrLoad" btn.textContent = "Zero" btn.onclick = () => corruptionLoadoutSaveLoad(false, i); @@ -253,6 +258,27 @@ const corruptionLoadoutSaveLoad = (save = true, loadout = 1) => { } } +const corruptionLoadoutImportPrompt = async () => { + const importPrompt = await Prompt( + [ + 'Please enter comma-separated corruption values. Current loadout:', + player.prototypeCorruptions.join(','), + ].join('\n') + ); + + if (!importPrompt) { + return Alert('Okay, maybe next time.'); + } + const importLoadout = importPrompt.split(',').map(Number); + if (importLoadout.length !== 13) { + return Alert('Invalid format! Corruption loadouts should be 13 comma-separated numbers.'); + } + + player.prototypeCorruptions = importLoadout; + corruptionLoadoutTableUpdate(); + corruptionStatsUpdate(); +}; + async function corruptionLoadoutGetNewName(loadout = 0) { const maxChars = 9 // eslint-disable-next-line diff --git a/src/History.ts b/src/History.ts index 59899fade..b33f0662f 100644 --- a/src/History.ts +++ b/src/History.ts @@ -2,6 +2,7 @@ import { player, format, formatTimeShort } from './Synergism'; import Decimal, { DecimalSource } from 'break_infinity.js'; import { antSacrificePointsToMultiplier } from './Ants'; import { Synergism } from './Events'; +import { copyToClipboard } from './Utility'; // The categories are the different tables & storages for each type. export type Category = 'ants' | 'reset' | 'ascend'; @@ -339,6 +340,19 @@ const resetHistoryRenderRow = ( rowContentHtml += ``; row.innerHTML = rowContentHtml; + if (data.kind === "ascend") { + const loadoutButton = row.querySelector('button[data-loadout]'); + if (loadoutButton) { + loadoutButton.addEventListener('click', async () => { + const loadout = loadoutButton.getAttribute('data-loadout'); + await copyToClipboard(loadout); + + loadoutButton.textContent = 'Copied!'; + setTimeout(() => loadoutButton.textContent = 'Copy', 10000); + }); + } + } + return row; } @@ -384,7 +398,9 @@ const resetHistoryFormatCorruptions = (data: ResetHistoryEntryAscend): [string, if (corruptionIdx in data.usedCorruptions && data.usedCorruptions[corruptionIdx] !== 0) { corruptions += ` ${resetHistoryCorruptionTitles[i]}${data.usedCorruptions[corruptionIdx]}`; } + } + corruptions += ` ` if (data.currentChallenge !== undefined) { score += ` / C${data.currentChallenge}`; } diff --git a/src/ImportExport.ts b/src/ImportExport.ts index 5d880bfce..e4119c997 100644 --- a/src/ImportExport.ts +++ b/src/ImportExport.ts @@ -1,6 +1,6 @@ import { player, saveSynergy, blankSave, reloadShit, format } from './Synergism'; import { testing, version } from './Config'; -import { getElementById } from './Utility'; +import { copyToClipboard, getElementById } from './Utility'; import LZString from 'lz-string'; import { achievementaward } from './Achievements'; import { Player } from './types/Synergism'; @@ -75,25 +75,8 @@ export const exportSynergism = async () => { const toClipboard = getElementById('saveType').checked; const save = localStorage.getItem('Synergysave2'); - if ('clipboard' in navigator && toClipboard) { - await navigator.clipboard.writeText(save) - .catch(e => console.error(e)); - } else if (toClipboard) { - // Old browsers (legacy Edge, Safari 13.0) - const textArea = document.createElement('textarea'); - textArea.value = save; - textArea.setAttribute('style', 'top: 0; left: 0; position: fixed;'); - - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - try { - document.execCommand('copy'); - } catch (_) { - console.error("Failed to copy savegame to clipboard."); - } - - document.body.removeChild(textArea); + if (toClipboard) { + await copyToClipboard(save); } else { const a = document.createElement('a'); a.setAttribute('href', 'data:text/plain;charset=utf-8,' + save); diff --git a/src/Utility.ts b/src/Utility.ts index 4ba374902..ba757d52c 100644 --- a/src/Utility.ts +++ b/src/Utility.ts @@ -102,3 +102,27 @@ export const btoa = (s: string) => { return null; } } + +export const copyToClipboard = async (text: string) => { + if ('clipboard' in navigator) { + await navigator.clipboard.writeText(text) + .catch(e => console.error(e)); + + return; + } + // Old browsers (legacy Edge, Safari 13.0) + const textArea = document.createElement('textarea'); + textArea.value = text; + textArea.setAttribute('style', 'top: 0; left: 0; position: fixed;'); + + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + try { + document.execCommand('copy'); + } catch (_) { + console.error("Failed to copy savegame to clipboard."); + } + + document.body.removeChild(textArea); +}; \ No newline at end of file From 74c05655063f6898c76af833d7cd7722fbb616b2 Mon Sep 17 00:00:00 2001 From: Jimmy Gaussen Date: Mon, 26 Jul 2021 00:59:16 +0200 Subject: [PATCH 2/3] fix history export button layout --- src/History.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/History.ts b/src/History.ts index b33f0662f..d8f10b550 100644 --- a/src/History.ts +++ b/src/History.ts @@ -315,8 +315,7 @@ const resetHistoryRenderRow = ( const corruptions = resetHistoryFormatCorruptions(data); if (corruptions !== null) { - extra.push(corruptions[0]); - extra.push(corruptions[1]); + extra.push(...corruptions); } } @@ -390,7 +389,7 @@ export const resetHistoryTogglePerSecond = () => { } // Helper function to format the corruption display in the ascension table. -const resetHistoryFormatCorruptions = (data: ResetHistoryEntryAscend): [string, string] => { +const resetHistoryFormatCorruptions = (data: ResetHistoryEntryAscend): [string, string, string] => { let score = "Score: " + format(data.corruptionScore, 0, false); let corruptions = ""; for (let i = 0; i < resetHistoryCorruptionImages.length; ++i) { @@ -400,10 +399,10 @@ const resetHistoryFormatCorruptions = (data: ResetHistoryEntryAscend): [string, } } - corruptions += ` ` if (data.currentChallenge !== undefined) { score += ` / C${data.currentChallenge}`; } + const exportCorruptionButton = ``; - return [score, corruptions]; + return [score, corruptions, exportCorruptionButton]; } From 87602a3ea956d73244da4bde21a5490a05c0b856 Mon Sep 17 00:00:00 2001 From: Jimmy Gaussen Date: Mon, 26 Jul 2021 01:03:55 +0200 Subject: [PATCH 3/3] fix export button timeout text --- src/History.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/History.ts b/src/History.ts index d8f10b550..b7de85589 100644 --- a/src/History.ts +++ b/src/History.ts @@ -347,7 +347,7 @@ const resetHistoryRenderRow = ( await copyToClipboard(loadout); loadoutButton.textContent = 'Copied!'; - setTimeout(() => loadoutButton.textContent = 'Copy', 10000); + setTimeout(() => loadoutButton.textContent = 'Copy layout', 10000); }); } }