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

1.8 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1,031 changes: 611 additions & 420 deletions Palladium Rifts by Grinning Gecko/dist/rifts.html

Large diffs are not rendered by default.

1,031 changes: 611 additions & 420 deletions Palladium Rifts by Grinning Gecko/rifts.html

Large diffs are not rendered by default.

727 changes: 0 additions & 727 deletions Palladium Rifts by Grinning Gecko/src/js/character_attributes.js

This file was deleted.

402 changes: 0 additions & 402 deletions Palladium Rifts by Grinning Gecko/src/js/definitions.js

Large diffs are not rendered by default.

135 changes: 116 additions & 19 deletions Palladium Rifts by Grinning Gecko/src/js/import_export.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
(function () {
async function getRepeatingSectionArrayAsync(section, rowIds, attrNames) {
async function getRepeatingSectionArrayAsync(
keynamesDefaultsArray,
section,
rowIds,
attrNames
) {
let sectionArray = [];
const attrs = await getAttrsAsync(attrNames);
rowIds.forEach((rowId) => {
const wpObj = Object.keys(attrs).reduce((acc, attr) => {
if (attr.includes(rowId)) {
acc[attr.replace(`repeating_${section}_${rowId}_`, "")] = attrs[attr];
const newAttrName = attr.replace(`repeating_${section}_${rowId}_`, "");
const defaultValue = keynamesDefaultsArray[newAttrName];
const rawImportValue = attrs[attr];
const attributeType = typeof defaultValue;
let importValue;
switch (attributeType) {
case "number":
importValue = +rawImportValue;
break;
case "string":
importValue = rawImportValue.toString();
break;
}
if (attr.includes(rowId) && defaultValue !== importValue) {
acc[newAttrName] = importValue;
}
return acc;
}, {});
Expand All @@ -14,7 +32,7 @@
return sectionArray;
}

async function getRepeatingRowsAsync(section) {
async function getRepeatingRowsAsync(keysDefaultsArray, section) {
const ids = await getSectionIDsOrderedAsync(section);
const attrNames = ids.reduce((acc, id) => {
SECTIONS[section].forEach((key) => {
Expand All @@ -23,6 +41,7 @@
return acc;
}, []);
const repeatingSectionArray = await getRepeatingSectionArrayAsync(
keysDefaultsToKeynamesDefaults(keysDefaultsArray),
section,
ids,
attrNames
Expand Down Expand Up @@ -90,26 +109,104 @@
});
}

/**
* Imports an object, ignoring values that haven't changed from their defaults.
*
* @param {string} keysDefaultsProp The KEYS_DEFAULTS property to compare to.
* @param {object} objectToImport An object of key/value pairs to import.
* @param {string} prefix The row ID prefix.
*/
function getSmartImportObject(
keysDefaultsArray,
objectToImport,
prefix = ""
) {
console.log("getSmartImportObject", keysDefaultsArray, objectToImport);
if (!objectToImport) return {};
const reducedImportObject = Object.entries(objectToImport).reduce(
(acc, [importKey, rawImportValue]) => {
const defaultValue = keysDefaultsArray.find(
(obj) => obj.key === importKey
).default;
const attributeType = typeof defaultValue;
let importValue;
switch (attributeType) {
case "number":
importValue = +rawImportValue;
break;
case "string":
importValue = rawImportValue.toString();
break;
}
if (defaultValue !== importValue) {
console.log(importKey, defaultValue, importValue);
acc[prefix + importKey] = importValue;
}
return acc;
},
{}
);
console.log("getSmartImportObject", reducedImportObject);
return reducedImportObject;
}

async function smartImportArray(keysDefaultsArray, section, arrayOfObjects) {
await setAttrsAsync({ importexportstatus: `Importing ${section}...` });
console.log("smartImportArray", keysDefaultsArray, section, arrayOfObjects);
if (!arrayOfObjects) return;
const attrs = arrayOfObjects.reduce((acc, row) => {
const rowId = generateRowID();
const prefix = `repeating_${section}_${rowId}_`;
const reducedRowImportObject = getSmartImportObject(
keysDefaultsArray,
row,
prefix
);
acc = Object.assign(acc, reducedRowImportObject);
return acc;
}, {});
attrs.importexportstatus = `Done importing ${section}...`;
await setAttrsAsync(attrs);
}

on("clicked:import", async (e) => {
console.log("import", e);
await setAttrsAsync({ importexportstatus: "Importing core..." });
const a = await getAttrsAsync(["importexport"]);
const data = JSON.parse(a.importexport);
console.log(data);
// importAll(data);
// return;
await setAttrsAsync(data.core);
await setRepeatingRowsAsync("h2h", data.h2h);
await setRepeatingRowsAsync("wp", data.wp);
await setRepeatingRowsAsync("wpmodern", data.wpmodern);
await setRepeatingRowsAsync("skills", data.skills);
await setRepeatingRowsAsync("magic", data.magic);
await setRepeatingRowsAsync("psionics", data.psionics);
await setRepeatingRowsAsync("movement", data.movement);
await setRepeatingRowsAsync("powersabilities", data.powersabilities);
await setRepeatingRowsAsync("modifiers", data.modifiers);
await setRepeatingRowsAsync("armor", data.armor);
await setRepeatingRowsAsync("equipment", data.equipment);
console.log(data, KEYS_DEFAULTS.MODIFIERS);

await smartImportArray(
KEYS_DEFAULTS.MODIFIERS,
"modifiers",
data.modifiers
);

await setAttrsAsync(getSmartImportObject(KEYS_DEFAULTS.CORE, data.core));

await smartImportArray(KEYS_DEFAULTS.H2H, "h2h", data.h2h);
await smartImportArray(KEYS_DEFAULTS.SKILL, "skills", data.skills);
await smartImportArray(KEYS_DEFAULTS.WP.wp, "wp", data.wp);
await smartImportArray(
KEYS_DEFAULTS.WP.wpmodern,
"wpmodern",
data.wpmodern
);
await smartImportArray(KEYS_DEFAULTS.MAGIC, "magic", data.magic);
await smartImportArray(KEYS_DEFAULTS.PSIONICS, "psionics", data.psionics);
await smartImportArray(
KEYS_DEFAULTS.ABILITIES,
"powersabilities",
data.powersabilities
);
await smartImportArray(KEYS_DEFAULTS.MOVEMENT, "movement", data.movement);
await smartImportArray(KEYS_DEFAULTS.ARMOR, "armor", data.armor);
await smartImportArray(
KEYS_DEFAULTS.EQUIPMENT,
"equipment",
data.equipment
);

await setAttrsAsync({
importexportstatus:
"Done importing, but triggered events are probably still running. To be sure open your browser console and when the logging stops, the import is really done.",
Expand Down
Loading