tarot/tarot.js

119 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

import CardDB from './carddb.js';
2020-11-25 04:23:06 +00:00
import CardPile from './cardpile.js';
2020-11-25 03:54:38 +00:00
2020-11-25 07:36:44 +00:00
/* TODO
*
* - drag and drop the cards wherever
* - card front art
* - card back art for deck
* - fill in interpretive descriptions
* - fill in rest of keywords
*/
2020-11-25 03:54:38 +00:00
class Tarot {
constructor() {
this.cardDB = new CardDB();
2020-11-25 03:54:38 +00:00
this.deck = new CardPile();
for (const cardName in this.cardDB.cards) {
2020-11-25 03:54:38 +00:00
this.deck.add(cardName);
}
this.table = new CardPile();
const deckElem = document.querySelector("#deck");
const tableElem = document.querySelector("#table");
const infoElem = document.querySelector("#info");
const resetElem = document.querySelector("#reset");
this.renderers = [
2020-11-25 06:50:24 +00:00
this.deckRender(deckElem, this.deck),
this.tableRender(tableElem, this.table),
this.infoRender(infoElem, tableElem)
2020-11-25 03:54:38 +00:00
];
tableElem.addEventListener("mouseenter", this.render.bind(this));
tableElem.addEventListener("mouseleave", this.render.bind(this));
deckElem.addEventListener("click", this.drawCard.bind(this));
resetElem.addEventListener("click", this.reset.bind(this));
2020-11-25 06:50:24 +00:00
this.render();
2020-11-25 03:54:38 +00:00
}
reset() {
2020-11-25 06:50:24 +00:00
let card = this.table.random();
2020-11-25 03:54:38 +00:00
while (card != null) {
this.deck.add(card);
2020-11-25 06:50:24 +00:00
card = this.table.random();
2020-11-25 03:54:38 +00:00
}
this.render();
}
drawCard() {
2020-11-25 06:52:31 +00:00
const cardName = this.deck.random();
2020-11-25 03:54:38 +00:00
if (cardName === null) {
this.render();
return;
}
this.table.add(cardName);
this.render();
}
render() {
2020-11-25 06:50:24 +00:00
for (const r of this.renderers) {
r();
2020-11-25 03:54:38 +00:00
}
}
2020-11-25 06:50:24 +00:00
deckRender(elem, deck) {
return () => {
const width = deck.size / 5;
elem.setAttribute("style", `float:left; height: 250px; width: 150px; border-left: 1px solid white; border-top: 1px solid white; border-bottom: ${width}px solid white; border-right: ${width}px solid white`);
2020-11-25 03:54:38 +00:00
}
}
2020-11-25 06:50:24 +00:00
infoRender(elem, tableElem) {
return () => {
const cardElem = tableElem.querySelector(".card:hover");
if (cardElem == null) {
elem.innerHTML = "";
return
2020-11-25 03:54:38 +00:00
}
const cardName = cardElem.getAttribute('data-cardName');
const keywords = this.cardDB.keywords(cardName);
elem.innerHTML = "";
elem.innerHTML += `<h2>${cardName}</h2>`;
elem.innerHTML += keywords.join(", ");
2020-11-25 03:54:38 +00:00
}
2020-11-25 06:50:24 +00:00
}
2020-11-25 03:54:38 +00:00
2020-11-25 06:50:24 +00:00
tableRender(elem, table) {
return () => {
const cardsOut = elem.querySelectorAll('.card');
for (let cardElem of cardsOut) {
if (!table.has(cardElem.getAttribute('data-cardName'))) {
cardElem.remove();
}
2020-11-25 03:54:38 +00:00
}
2020-11-25 06:50:24 +00:00
for (let cardName of table.cards) {
const found = elem.querySelector(`[data-cardName="${cardName}"]`);
if (found != null) {
continue;
}
2020-11-25 06:52:31 +00:00
const bp = document.querySelector(".card.blueprint");
const cardElem = bp.cloneNode(true);
2020-11-25 06:50:24 +00:00
cardElem.classList.remove("blueprint");
cardElem.setAttribute("data-cardName", cardName)
2020-11-25 07:36:44 +00:00
cardElem.setAttribute("style", "float:left; margin-right: 20px; width: 150px; height: 250px; border: 1px solid grey; box-shadow: 2px 2px 2px pink; text-align:center;");
2020-11-25 06:50:24 +00:00
cardElem.querySelector("span").innerHTML = cardName;
cardElem.addEventListener("mouseenter", this.render.bind(this));
elem.append(cardElem);
}
2020-11-25 03:54:38 +00:00
}
}
}
export default Tarot;