Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: corruption loadout import/export #114

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Synergism.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 28 additions & 2 deletions src/Corruptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,15 @@ export const corruptionLoadoutTableCreate = () => {
}
if (i === 0) {
let cell = row.insertCell();
//empty
let btn = document.createElement("button");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

btn.className = "corrSave"
btn.textContent = "Import"
btn.onclick = () => corruptionLoadoutImportPrompt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addEventListener

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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename

btn.className = "corrLoad"
btn.textContent = "Zero"
btn.onclick = () => corruptionLoadoutSaveLoad(false, i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please addEventListener here too

Expand Down Expand Up @@ -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')
Comment on lines +263 to +266
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just concat the strings

);

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;
Copy link
Member

@KhafraDev KhafraDev Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if one of these is NaN, negative, infinite, etc?

corruptionLoadoutTableUpdate();
corruptionStatsUpdate();
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove semicolon


async function corruptionLoadoutGetNewName(loadout = 0) {
const maxChars = 9
// eslint-disable-next-line
Expand Down
23 changes: 19 additions & 4 deletions src/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -314,8 +315,7 @@ const resetHistoryRenderRow = (

const corruptions = resetHistoryFormatCorruptions(data);
if (corruptions !== null) {
extra.push(corruptions[0]);
extra.push(corruptions[1]);
extra.push(...corruptions);
}
}

Expand All @@ -339,6 +339,19 @@ const resetHistoryRenderRow = (
rowContentHtml += `<td class="history-filler" colspan="${4 - extra.length}"></td>`;

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 layout', 10000);
});
}
}

return row;
}

Expand Down Expand Up @@ -376,18 +389,20 @@ 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) {
const corruptionIdx = i + 1;
if (corruptionIdx in data.usedCorruptions && data.usedCorruptions[corruptionIdx] !== 0) {
corruptions += ` <img alt="${resetHistoryCorruptionTitles[i]}" src="${resetHistoryCorruptionImages[i]}" title="${resetHistoryCorruptionTitles[i]}">${data.usedCorruptions[corruptionIdx]}`;
}

}
if (data.currentChallenge !== undefined) {
score += ` / C${data.currentChallenge}`;
}
const exportCorruptionButton = `<button data-loadout="${data.usedCorruptions.join(',')}">Copy layout</button>`;

return [score, corruptions];
return [score, corruptions, exportCorruptionButton];
}
23 changes: 3 additions & 20 deletions src/ImportExport.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -75,25 +75,8 @@ export const exportSynergism = async () => {

const toClipboard = getElementById<HTMLInputElement>('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);
Expand Down
24 changes: 24 additions & 0 deletions src/Utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,27 @@ export const btoa = (s: string) => {
return null;
}
}

export const copyToClipboard = async (text: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: Promise

if ('clipboard' in navigator) {
await navigator.clipboard.writeText(text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return void navigator.clipboard...

.catch(e => console.error(e));

return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axe this 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);
};