mtgcsv/app.js

274 lines
9.2 KiB
JavaScript
Raw Normal View History

// Define setTheme globally to handle theme changes
window.setTheme = function setTheme(theme) {
document.body.className = ''; // Clear existing themes
document.body.classList.add(theme);
// Save the selected theme to localStorage
localStorage.setItem('selectedTheme', theme);
};
2024-08-21 03:23:06 +02:00
let cardList = [];
2024-08-21 04:27:26 +02:00
let soundThreshold = 1.00; // Default sound threshold
2024-08-21 03:23:06 +02:00
// Make toggleMenu globally accessible
window.toggleMenu = function toggleMenu() {
const menuContent = document.getElementById('menu-content');
menuContent.classList.toggle('show');
};
2024-08-21 03:23:06 +02:00
document.addEventListener('DOMContentLoaded', () => {
const savedTheme = localStorage.getItem('selectedTheme');
if (savedTheme) {
setTheme(savedTheme);
}
const savedThreshold = localStorage.getItem('soundThreshold');
if (savedThreshold) {
soundThreshold = parseFloat(savedThreshold);
document.getElementById('sound-threshold').value = savedThreshold;
}
document.getElementById('add-card-button').addEventListener('click', addCard);
document.getElementById('download-csv-button').addEventListener('click', downloadCSV);
document.getElementById('card-name').addEventListener('input', async function() {
const query = this.value;
const suggestions = await fetchCardSuggestions(query);
const suggestionsDiv = document.getElementById('suggestions');
suggestionsDiv.innerHTML = '';
suggestions.forEach(suggestion => {
const div = document.createElement('div');
div.textContent = suggestion;
div.onclick = () => {
document.getElementById('card-name').value = suggestion;
suggestionsDiv.innerHTML = '';
};
suggestionsDiv.appendChild(div);
});
2024-08-21 03:23:06 +02:00
});
2024-08-21 04:27:26 +02:00
// Update sound threshold based on user input
document.getElementById('sound-threshold').addEventListener('input', (event) => {
soundThreshold = parseFloat(event.target.value);
localStorage.setItem('soundThreshold', soundThreshold); // Save the threshold to localStorage
2024-08-21 04:27:26 +02:00
});
// Check and apply dark mode
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
if (isDarkMode) {
document.body.classList.add('dark-mode');
}
2024-08-21 03:23:06 +02:00
});
async function fetchCardSuggestions(query) {
2024-08-21 14:46:39 +02:00
try {
const response = await fetch(`https://api.scryfall.com/cards/autocomplete?q=${encodeURIComponent(query)}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return data.data;
} catch (error) {
console.error('Fetch error:', error);
return [];
}
2024-08-21 03:23:06 +02:00
}
2024-08-21 14:46:39 +02:00
document.getElementById('card-name').addEventListener('input', async function() {
const query = this.value.trim();
if (query.length < 2) { // Only search for queries longer than 1 character
document.getElementById('suggestions').innerHTML = '';
return;
}
const suggestions = await fetchCardSuggestions(query);
const suggestionsDiv = document.getElementById('suggestions');
suggestionsDiv.innerHTML = ''; // Clear previous suggestions
suggestions.forEach(suggestion => {
const div = document.createElement('div');
div.textContent = suggestion;
div.onclick = () => {
document.getElementById('card-name').value = suggestion;
suggestionsDiv.innerHTML = ''; // Clear suggestions after selection
};
suggestionsDiv.appendChild(div);
});
});
2024-08-21 03:23:06 +02:00
async function fetchCardDetails(cardName, setCode) {
2024-08-21 05:18:42 +02:00
// Use the Scryfall 'named' endpoint to fetch the exact card by name and set code
2024-08-21 03:23:06 +02:00
const response = await fetch(`https://api.scryfall.com/cards/named?exact=${encodeURIComponent(cardName)}&set=${encodeURIComponent(setCode)}`);
const data = await response.json();
2024-08-21 05:18:42 +02:00
2024-08-21 18:07:20 +02:00
console.log('Fetched Data from Scryfall:', data); // Debugging: Log the fetched data
2024-08-21 05:18:42 +02:00
// Return the relevant card details
2024-08-21 03:23:06 +02:00
return {
2024-08-21 05:18:42 +02:00
set: data.set.toUpperCase(),
card_id: data.collector_number,
2024-08-21 03:23:06 +02:00
mana_cost: data.mana_cost || 'N/A',
power: data.power || 'N/A',
toughness: data.toughness || 'N/A',
2024-08-21 05:18:42 +02:00
type: data.type_line.split('—')[0].trim(),
2024-08-21 04:27:26 +02:00
rarity: data.rarity || 'N/A',
2024-08-21 05:18:42 +02:00
price: data.prices.usd || '0.00',
imageUrl: data.image_uris ? data.image_uris.small : null,
name: data.name
2024-08-21 03:23:06 +02:00
};
}
async function addCard() {
const cardName = document.getElementById('card-name').value;
const setCodeInput = document.getElementById('set-code');
const cardIdInput = document.getElementById('card-id');
2024-08-21 03:23:06 +02:00
2024-08-21 05:18:42 +02:00
// Fetch card details using the card name and set code
let setCode = setCodeInput.value;
let cardId = cardIdInput.value;
// Always fetch card details
2024-08-21 03:23:06 +02:00
const cardDetails = await fetchCardDetails(cardName, setCode);
2024-08-21 05:18:42 +02:00
if (cardDetails) {
// If set code or card ID is not filled, auto-fill them
if (!setCode) {
setCode = cardDetails.set;
setCodeInput.value = setCode;
}
if (!cardId) {
cardId = cardDetails.card_id;
cardIdInput.value = cardId;
}
// Update card details with either user input or fetched data
updateCardDetails({
name: cardName,
set: setCode,
card_id: cardId,
mana_cost: cardDetails.mana_cost,
power: cardDetails.power,
toughness: cardDetails.toughness,
type: cardDetails.type,
rarity: cardDetails.rarity,
price: cardDetails.price,
imageUrl: cardDetails.imageUrl
});
2024-08-21 05:18:42 +02:00
} else {
alert("Card not found with the given name.");
2024-08-21 05:18:42 +02:00
}
}
2024-08-21 05:18:42 +02:00
function updateCardDetails(printing) {
document.getElementById('set-code').value = printing.set;
document.getElementById('card-id').value = printing.card_id;
const quantity = document.getElementById('quantity').value;
const foil = document.getElementById('foil').value;
2024-08-21 03:23:06 +02:00
const cardData = {
2024-08-21 05:18:42 +02:00
name: document.getElementById('card-name').value,
set: printing.set,
card_id: printing.card_id,
2024-08-21 03:23:06 +02:00
quantity: quantity,
foil: foil,
2024-08-21 05:18:42 +02:00
mana_type: printing.mana_cost,
power: printing.power,
toughness: printing.toughness,
type: printing.type,
rarity: printing.rarity
2024-08-21 03:23:06 +02:00
};
cardList.push(cardData);
// Update the textarea with the new card information
let csvContent = generateCSVContent();
document.getElementById('csv-output').value = csvContent;
2024-08-21 04:27:26 +02:00
// Show the card image, name, and price in the dissolving popup
2024-08-21 05:18:42 +02:00
showCardPopup(cardData.name, printing.imageUrl, printing.price);
2024-08-21 03:23:06 +02:00
}
function generateCSVContent() {
let csvContent = "Card Name,Set,Card ID,Quantity,Foil,Mana Type,Power,Toughness,Type,Rarity\n";
cardList.forEach(card => {
const escapedValues = [
card.name,
card.set,
card.card_id,
card.quantity,
card.foil,
card.mana_type,
card.power,
card.toughness,
card.type,
card.rarity
].map(value => `"${value.replace(/"/g, '""')}"`); // Escape any existing quotes by doubling them
csvContent += escapedValues.join(",") + "\n";
});
return csvContent;
}
2024-08-21 04:27:26 +02:00
async function showCardPopup(cardName, imageUrl, price) {
2024-08-21 03:23:06 +02:00
const cardPopup = document.getElementById('card-popup');
2024-08-21 04:27:26 +02:00
cardPopup.innerHTML = `
<div style="font-family: 'Sorts Mill Goudy', serif; text-align: center;">
<strong>${cardName}</strong>
</div>
<img src="${imageUrl}" alt="${cardName}">
<div style="font-family: 'Sorts Mill Goudy', serif; text-align: center; margin-top: 10px;">
Price: $${price}
</div>
`;
cardPopup.style.display = 'block';
cardPopup.style.opacity = 1;
if (parseFloat(price) >= soundThreshold) {
playSound();
}
2024-08-21 03:23:06 +02:00
2024-08-21 04:27:26 +02:00
// Dissolve the popup after a few seconds
setTimeout(() => {
cardPopup.style.opacity = 0;
2024-08-21 03:23:06 +02:00
setTimeout(() => {
2024-08-21 04:27:26 +02:00
cardPopup.style.display = 'none';
}, 500);
}, 3000);
}
function playSound() {
const audio = new Audio('cash-money.mp3');
audio.play();
2024-08-21 03:23:06 +02:00
}
function getRandomWordsFromFile() {
let randomWordsSelected = [];
for (let i = 0; i < 3; i++) { // Get three random words
const randomIndex = Math.floor(Math.random() * randomWords.length);
randomWordsSelected.push(randomWords[randomIndex]);
}
2024-08-21 04:27:26 +02:00
return randomWordsSelected.join('-');
}
async function downloadCSV() {
2024-08-21 03:23:06 +02:00
const csvContent = generateCSVContent();
const randomFileName = getRandomWordsFromFile();
2024-08-21 03:23:06 +02:00
// Create a blob with the CSV content
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
// Create a temporary link element
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", `${randomFileName}.csv`);
2024-08-21 03:23:06 +02:00
document.body.appendChild(link);
// Trigger the download
link.click();
// Clean up and remove the link
document.body.removeChild(link);
}