-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Ajordat/collections
Collections
- Loading branch information
Showing
12 changed files
with
765 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.14.1" | ||
__version__ = "1.15.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
Oops, something went wrong.