Skip to content

Commit

Permalink
Merge pull request #58 from Ajordat/collections
Browse files Browse the repository at this point in the history
Collections
  • Loading branch information
Ajordat authored Jan 11, 2025
2 parents 1de3e74 + 0e39bc2 commit 45f63cb
Show file tree
Hide file tree
Showing 12 changed files with 765 additions and 24 deletions.
2 changes: 1 addition & 1 deletion alteredbuilder/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.14.1"
__version__ = "1.15.0"
11 changes: 11 additions & 0 deletions alteredbuilder/decks/static/css/card-list.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,15 @@ html[data-bs-theme="dark"] .card-name {
.max-width-xl {
max-width: 160px;
}
}

.card-badge {
position: absolute;
bottom: .75rem;
right: .75rem;
background-color: rgba(0, 0, 0, 0.7);
color: white;
border-radius: 12px;
z-index: 10;
pointer-events: none;
}
102 changes: 102 additions & 0 deletions alteredbuilder/decks/static/js/card-list-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

document.addEventListener("DOMContentLoaded", () => {
startCollection();
displayTextCollection();
});

document.getElementById("save-collection").addEventListener("click", () => {

const collectionListEl = document.getElementById("collection-list");

let cardEntries = textCollectionToEntries(collectionListEl.value);
let collection = parseCardEntries(cardEntries);

saveCollection(collection);

startCollection();
});

document.getElementById("update-collection-settings").addEventListener("click", () => {
const mergeSetsEl = document.getElementById("merge-sets-check");

let settings = {
mergeSets: mergeSetsEl.checked
};
saveSettings(settings);

startCollection();
});

function displayTextCollection() {
let collection = fetchCollection();
if (!collection) {
return;
}
printCollection(collection);
}

function printCollection(collection) {
document.getElementById("collection-list").value = collectionToText(collection);
}

function startCollection() {

resetCollectedCards();

let collection = fetchCollection();

console.log(collection);

if (!collection) {
return;
}

let settings = fetchSettings();
printSettings(settings);

markCollectedCards(collection, settings);
}

function resetCollectedCards() {
const badges = document.querySelectorAll('.card-badge');
badges.forEach(badge => badge.remove());
}

function markCollectedCards(collection, settings) {

const cards = document.getElementsByClassName('card-display');
var finalCollection = {};

if (settings.mergeSets) {
for (let [reference, quantity] of Object.entries(collection)) {
finalCollection[reference] = quantity + (finalCollection[reference] || 0);
if (reference.includes("_CORE_")) {
let altRef = reference.replace("_CORE_", "_COREKS_");
finalCollection[altRef] = quantity + (finalCollection[altRef] || 0);
} else if (reference.includes("_COREKS_")) {
let altRef = reference.replace("_COREKS_", "_CORE_");
finalCollection[altRef] = quantity + (finalCollection[altRef] || 0);
}
}
} else {
finalCollection = { ...collection };
}

for (let card of cards) {
const cardReference = card.getAttribute('data-card-reference');

if (finalCollection[cardReference]) {

const badge = document.createElement('div');
badge.textContent = finalCollection[cardReference];
badge.className = 'card-badge px-2 py-1 border border-white';

card.style.position = 'relative';
card.appendChild(badge);
}
}
}

function printSettings(settings) {
document.getElementById("merge-sets-check").checked = settings.mergeSets;
}
52 changes: 52 additions & 0 deletions alteredbuilder/decks/static/js/collection-API.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

const LS_COLLECTION_KEY = "collectionContent";
const LS_SETTINGS_KEY = "collectionSettings";


function saveCollection(collection) {
localStorage.setItem(LS_COLLECTION_KEY, JSON.stringify(collection));
}

function fetchCollection() {
try {
return JSON.parse(localStorage.getItem(LS_COLLECTION_KEY));
} catch (e) {
return {};
}
}

function saveSettings(settings) {
localStorage.setItem(LS_SETTINGS_KEY, JSON.stringify(settings));
}

function fetchSettings() {
let settings = JSON.parse(localStorage.getItem(LS_SETTINGS_KEY));

if (!settings) {
settings = {
mergeSets: false
}
saveSettings(settings);
}

return settings;
}

function textCollectionToEntries(textCollection) {
return textCollection.trim().split("\n");
}

function parseCardEntries(cardEntries) {
let collection = {};
for (let cardEntry of cardEntries) {
if (!cardEntry)
continue;
let card = cardEntry.split(" ");
collection[card[1]] = parseInt(card[0]);
}
return collection;
}

function collectionToText(collection) {
return Object.entries(collection).map(([reference, quantity]) => `${quantity} ${reference}`).join("\n");
}
Loading

0 comments on commit 45f63cb

Please sign in to comment.